Skip to content

Commit

Permalink
Update RunAggregationQuery rpc + unit + snippet tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yash30201 committed Apr 3, 2024
1 parent 927d510 commit c9d2186
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 65 deletions.
36 changes: 34 additions & 2 deletions Datastore/src/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Google\Cloud\Datastore\Query\AggregationQueryResult;
use Google\Cloud\Datastore\Query\Query;
use Google\Cloud\Datastore\Query\QueryInterface;
use Google\Cloud\Datastore\V1\AggregationQuery as V1AggregationQuery;
use Google\Cloud\Datastore\V1\AllocateIdsRequest;
use Google\Cloud\Datastore\V1\BeginTransactionRequest;
use Google\Cloud\Datastore\V1\Client\DatastoreClient;
Expand All @@ -42,6 +43,7 @@
use Google\Cloud\Datastore\V1\Query as V1Query;
use Google\Cloud\Datastore\V1\QueryResultBatch\MoreResultsType;
use Google\Cloud\Datastore\V1\ReadOptions;
use Google\Cloud\Datastore\V1\RunAggregationQueryRequest;
use Google\Cloud\Datastore\V1\RunQueryRequest;
use Google\Cloud\Datastore\V1\TransactionOptions;
use Google\Protobuf\NullValue;
Expand Down Expand Up @@ -681,7 +683,7 @@ public function runAggregationQuery(AggregationQuery $runQueryObj, array $option
'query' => [],
];
$requestQueryArr = $args['query'] + $runQueryObj->queryObject();
$request = [
$req = [
'projectId' => $this->projectId,
'partitionId' => $this->partitionId(
$this->projectId,
Expand All @@ -690,7 +692,37 @@ public function runAggregationQuery(AggregationQuery $runQueryObj, array $option
),
] + $requestQueryArr + $this->readOptions($options) + $options;

$res = $this->connection->runAggregationQuery($request);
list($data, $optionalArgs) = $this->splitOptionalArgs($req, [
'namespaceId',
'readTime',
'readConsistency',
'transaction'
]);

if (isset($data['aggregationQuery'])) {
if (isset($data['aggregationQuery']['nestedQuery'])) {
$data['aggregationQuery']['nestedQuery'] = $this->parseQuery(
$data['aggregationQuery']['nestedQuery']
);
}

$data['aggregationQuery'] = $this->serializer->decodeMessage(
new V1AggregationQuery(),
$data['aggregationQuery']
);
}

if (isset($data['gqlQuery'])) {
$data['gqlQuery'] = $this->parseGqlQuery($data['gqlQuery']);
}

$request = $this->serializer->decodeMessage(new RunAggregationQueryRequest(), $data);
$res = $this->requestHandler->sendRequest(
DatastoreClient::class,
'runAggregationQuery',
$request,
$optionalArgs
);
return new AggregationQueryResult($res, $this->entityMapper);
}

Expand Down
11 changes: 7 additions & 4 deletions Datastore/tests/Snippet/DatastoreClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -747,9 +747,10 @@ public function testRunAggregationQuery()
$query->queryObject()->willReturn([]);
$snippet->addLocal('query', $query->reveal());

$this->connection->runAggregationQuery(Argument::any())
->shouldBeCalled()
->willReturn([
$this->mockSendRequest(
'runAggregationQuery',
[],
[
'batch' => [
'aggregationResults' => [
[
Expand All @@ -760,7 +761,9 @@ public function testRunAggregationQuery()
],
'readTime' => (new \DateTime)->format('Y-m-d\TH:i:s') .'.000001Z'
]
]);
],
0
);

$this->refreshOperation($this->client, $this->connection->reveal(), $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand Down
28 changes: 18 additions & 10 deletions Datastore/tests/Snippet/Query/AggregationQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Google\ApiCore\Serializer;
use Google\Cloud\Core\ApiHelperTrait;
use Google\Cloud\Core\RequestHandler;
use Google\Cloud\Core\Testing\DatastoreOperationRefreshTrait;
use Google\Cloud\Core\Testing\Snippet\SnippetTestCase;
use Google\Cloud\Core\Testing\TestHelpers;
use Google\Cloud\Datastore\Connection\ConnectionInterface;
Expand All @@ -38,6 +39,7 @@ class AggregationQueryTest extends SnippetTestCase
{
use ProphecyTrait;
use ApiHelperTrait;
use DatastoreOperationRefreshTrait;

private $datastore;
private $connection;
Expand Down Expand Up @@ -80,9 +82,10 @@ public function setUp(): void

public function testClass()
{
$this->connection->runAggregationQuery(Argument::any())
->shouldBeCalled()
->willReturn([
$this->mockSendRequest(
'runAggregationQuery',
[],
[
'batch' => [
'aggregationResults' => [
[
Expand All @@ -93,9 +96,11 @@ public function testClass()
],
'readTime' => (new \DateTime())->format('Y-m-d\TH:i:s') .'.000001Z'
]
]);
],
0
);

$this->operation->___setProperty('connection', $this->connection->reveal());
$this->operation->___setProperty('requestHandler', $this->requestHandler->reveal());

$this->datastore->___setProperty('operation', $this->operation);

Expand All @@ -110,9 +115,10 @@ public function testClass()

public function testClassWithOverAggregation()
{
$this->connection->runAggregationQuery(Argument::any())
->shouldBeCalled()
->willReturn([
$this->mockSendRequest(
'runAggregationQuery',
[],
[
'batch' => [
'aggregationResults' => [
[
Expand All @@ -123,9 +129,11 @@ public function testClassWithOverAggregation()
],
'readTime' => (new \DateTime())->format('Y-m-d\TH:i:s') .'.000001Z'
]
]);
],
0
);

$this->operation->___setProperty('connection', $this->connection->reveal());
$this->operation->___setProperty('requestHandler', $this->requestHandler->reveal());

$this->datastore->___setProperty('operation', $this->operation);

Expand Down
11 changes: 7 additions & 4 deletions Datastore/tests/Snippet/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,10 @@ public function testRunAggregationQuery()
$query->queryObject()->willReturn([]);
$snippet->addLocal('query', $query->reveal());

$this->connection->runAggregationQuery(Argument::withEntry('transaction', self::TRANSACTION))
->shouldBeCalled()
->willReturn([
$this->mockSendRequest(
'runAggregationQuery',
['readOptions' => ['transaction' => self::TRANSACTION]],
[
'batch' => [
'aggregationResults' => [
[
Expand All @@ -471,7 +472,9 @@ public function testRunAggregationQuery()
],
'readTime' => (new \DateTime)->format('Y-m-d\TH:i:s') .'.000001Z'
]
]);
],
0
);

$this->refreshOperation($this->transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand Down
66 changes: 36 additions & 30 deletions Datastore/tests/Unit/DatastoreClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,21 +680,24 @@ public function testRunQuery()

public function testRunAggregationQuery()
{
$this->connection->runAggregationQuery(Argument::allOf(
Argument::withEntry('partitionId', ['projectId' => self::PROJECT]),
Argument::withEntry('gqlQuery', [
'queryString' => 'AGGREGATE (COUNT(*)) over (SELECT 1=1)'
])
))->shouldBeCalled()->willReturn([
'batch' => [
'aggregationResults' => [
[
'aggregateProperties' => ['property_1' => 1]
]
],
'readTime' => (new \DateTime())->format('Y-m-d\TH:i:s') .'.000001Z'
]
]);
$this->mockSendRequest(
'runAggregationQuery',
[
'partitionId' => ['projectId' => self::PROJECT],
'gqlQuery' => ['queryString' => 'AGGREGATE (COUNT(*)) over (SELECT 1=1)'],
],
[
'batch' => [
'aggregationResults' => [
[
'aggregateProperties' => ['property_1' => 1]
]
],
'readTime' => (new \DateTime())->format('Y-m-d\TH:i:s') .'.000001Z'
]
],
0
);

$this->refreshOperation($this->client, $this->connection->reveal(), $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand All @@ -716,21 +719,24 @@ public function testRunAggregationQuery()
*/
public function testAggregationQueryWithDifferentReturnTypes($response, $expected)
{
$this->connection->runAggregationQuery(Argument::allOf(
Argument::withEntry('partitionId', ['projectId' => self::PROJECT]),
Argument::withEntry('gqlQuery', [
'queryString' => 'foo bar'
])
))->shouldBeCalled()->willReturn([
'batch' => [
'aggregationResults' => [
[
'aggregateProperties' => ['property_1' => $response]
]
],
'readTime' => (new \DateTime())->format('Y-m-d\TH:i:s') .'.000001Z'
]
]);
$this->mockSendRequest(
'runAggregationQuery',
[
'partitionId' => ['projectId' => self::PROJECT],
'gqlQuery' => ['queryString' => 'foo bar'],
],
[
'batch' => [
'aggregationResults' => [
[
'aggregateProperties' => ['property_1' => $response]
]
],
'readTime' => (new \DateTime())->format('Y-m-d\TH:i:s') .'.000001Z'
]
],
0
);

$this->refreshOperation($this->client, $this->connection->reveal(), $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand Down
33 changes: 18 additions & 15 deletions Datastore/tests/Unit/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,21 +300,24 @@ public function testRunQuery(callable $transaction)
*/
public function testRunAggregationQuery(callable $transaction)
{
$this->connection->runAggregationQuery(Argument::allOf(
Argument::withEntry('partitionId', ['projectId' => self::PROJECT]),
Argument::withEntry('gqlQuery', [
'queryString' => 'AGGREGATE (COUNT(*)) over (SELECT 1=1)'
])
))->shouldBeCalled()->willReturn([
'batch' => [
'aggregationResults' => [
[
'aggregateProperties' => ['property_1' => 1]
]
],
'readTime' => (new \DateTime)->format('Y-m-d\TH:i:s') .'.000001Z'
]
]);
$this->mockSendRequest(
'runAggregationQuery',
[
'partitionId' => ['projectId' => self::PROJECT],
'gqlQuery' => ['queryString' => 'AGGREGATE (COUNT(*)) over (SELECT 1=1)'],
],
[
'batch' => [
'aggregationResults' => [
[
'aggregateProperties' => ['property_1' => 1]
]
],
'readTime' => (new \DateTime())->format('Y-m-d\TH:i:s') .'.000001Z'
]
],
0
);

$transaction = $transaction();
$this->refreshOperation($transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [
Expand Down

0 comments on commit c9d2186

Please sign in to comment.