From c9d2186af045c6fcc361d88f0bbbfc7698876361 Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Wed, 3 Apr 2024 11:13:14 +0000 Subject: [PATCH] Update RunAggregationQuery rpc + unit + snippet tests --- Datastore/src/Operation.php | 36 +++++++++- .../tests/Snippet/DatastoreClientTest.php | 11 ++-- .../Snippet/Query/AggregationQueryTest.php | 28 +++++--- Datastore/tests/Snippet/TransactionTest.php | 11 ++-- Datastore/tests/Unit/DatastoreClientTest.php | 66 ++++++++++--------- Datastore/tests/Unit/TransactionTest.php | 33 +++++----- 6 files changed, 120 insertions(+), 65 deletions(-) diff --git a/Datastore/src/Operation.php b/Datastore/src/Operation.php index 25746d1d7c11..d00637922b8a 100644 --- a/Datastore/src/Operation.php +++ b/Datastore/src/Operation.php @@ -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; @@ -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; @@ -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, @@ -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); } diff --git a/Datastore/tests/Snippet/DatastoreClientTest.php b/Datastore/tests/Snippet/DatastoreClientTest.php index 831da9751ddb..84ce8aba7c79 100644 --- a/Datastore/tests/Snippet/DatastoreClientTest.php +++ b/Datastore/tests/Snippet/DatastoreClientTest.php @@ -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' => [ [ @@ -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 diff --git a/Datastore/tests/Snippet/Query/AggregationQueryTest.php b/Datastore/tests/Snippet/Query/AggregationQueryTest.php index 78568ba149fc..542cba94df86 100644 --- a/Datastore/tests/Snippet/Query/AggregationQueryTest.php +++ b/Datastore/tests/Snippet/Query/AggregationQueryTest.php @@ -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; @@ -38,6 +39,7 @@ class AggregationQueryTest extends SnippetTestCase { use ProphecyTrait; use ApiHelperTrait; + use DatastoreOperationRefreshTrait; private $datastore; private $connection; @@ -80,9 +82,10 @@ public function setUp(): void public function testClass() { - $this->connection->runAggregationQuery(Argument::any()) - ->shouldBeCalled() - ->willReturn([ + $this->mockSendRequest( + 'runAggregationQuery', + [], + [ 'batch' => [ 'aggregationResults' => [ [ @@ -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); @@ -110,9 +115,10 @@ public function testClass() public function testClassWithOverAggregation() { - $this->connection->runAggregationQuery(Argument::any()) - ->shouldBeCalled() - ->willReturn([ + $this->mockSendRequest( + 'runAggregationQuery', + [], + [ 'batch' => [ 'aggregationResults' => [ [ @@ -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); diff --git a/Datastore/tests/Snippet/TransactionTest.php b/Datastore/tests/Snippet/TransactionTest.php index c2e9877aaf94..c988c0d9318a 100644 --- a/Datastore/tests/Snippet/TransactionTest.php +++ b/Datastore/tests/Snippet/TransactionTest.php @@ -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' => [ [ @@ -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 diff --git a/Datastore/tests/Unit/DatastoreClientTest.php b/Datastore/tests/Unit/DatastoreClientTest.php index e145d4c2eb03..0e676d35a4fe 100644 --- a/Datastore/tests/Unit/DatastoreClientTest.php +++ b/Datastore/tests/Unit/DatastoreClientTest.php @@ -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 @@ -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 diff --git a/Datastore/tests/Unit/TransactionTest.php b/Datastore/tests/Unit/TransactionTest.php index b550c432a7af..e232dc81086e 100644 --- a/Datastore/tests/Unit/TransactionTest.php +++ b/Datastore/tests/Unit/TransactionTest.php @@ -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(), [