Skip to content

Commit

Permalink
Update lookup rpc + unit + snippet tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yash30201 committed Apr 1, 2024
1 parent d06abf7 commit cee1539
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 238 deletions.
2 changes: 0 additions & 2 deletions Core/src/Testing/DatastoreOperationRefreshTrait.php
Expand Up @@ -141,9 +141,7 @@ private function mockSendRequest($methodName, $params, $returnValue, $shouldBeCa
return false;
}
$data = $serializer->encodeMessage($arg);
// $z = array_replace_recursive($data, $params) == $data;
return array_replace_recursive($data, $params) == $data;
// return array_merge($params, $data) == $data;
}),
Argument::cetera()
);
Expand Down
61 changes: 58 additions & 3 deletions Datastore/src/Operation.php
Expand Up @@ -31,6 +31,9 @@
use Google\Cloud\Datastore\V1\AllocateIdsRequest;
use Google\Cloud\Datastore\V1\BeginTransactionRequest;
use Google\Cloud\Datastore\V1\Client\DatastoreClient;
use Google\Cloud\Datastore\V1\Key\PathElement;
use Google\Cloud\Datastore\V1\LookupRequest;
use Google\Cloud\Datastore\V1\PartitionId;
use Google\Cloud\Datastore\V1\QueryResultBatch\MoreResultsType;
use Google\Cloud\Datastore\V1\TransactionOptions;

Expand Down Expand Up @@ -453,11 +456,22 @@ public function lookup(array $keys, array $options = [])
$serviceKeys[] = $key->keyObject();
});

$res = $this->connection->lookup($options + $this->readOptions($options) + [
list($data, $optionalArgs) = $this->splitOptionalArgs($options, ['transaction', 'className', 'sort', 'readTime', 'readConsistency']);
$data += $this->readOptions($options) + [
'projectId' => $this->projectId,
'databaseId' => $this->databaseId,
'keys' => $serviceKeys,
]);
'keys' => $this->keysList($serviceKeys),
];

$request = $this->serializer->decodeMessage(new LookupRequest(), $data);
$x = $this->serializer->encodeMessage($request);

$res = $this->requestHandler->sendRequest(
DatastoreClient::class,
'lookup',
$request,
$optionalArgs
);

$result = [];
if (isset($res['found'])) {
Expand Down Expand Up @@ -929,4 +943,45 @@ private function sortEntities(array $entities, array $keys)

return $ret;
}

/**
* Convert a list of keys to a list of {@see Google\Cloud\Datastore\V1\Key}.
*
* @param array[] $keys
* @return Key[]
*/
private function keysList(array $keys)
{
$out = [];
foreach ($keys as $key) {
$local = [];

if (isset($key['partitionId'])) {
$p = $this->arrayFilterRemoveNull([
'project_id' => isset($key['partitionId']['projectId'])
? $key['partitionId']['projectId']
: null,
'namespace_id' => isset($key['partitionId']['namespaceId'])
? $key['partitionId']['namespaceId']
: null,
'database_id' => isset($key['partitionId']['databaseId'])
? $key['partitionId']['databaseId']
: null,
]);

$local['partition_id'] = new PartitionId($p);
}

$local['path'] = [];
if (isset($key['path'])) {
foreach ($key['path'] as $element) {
$local['path'][] = new PathElement($element);
}
}

$out[] = $local;
}

return $out;
}
}
20 changes: 12 additions & 8 deletions Datastore/tests/Snippet/DatastoreClientTest.php
Expand Up @@ -562,9 +562,10 @@ public function testLookup()
$snippet = $this->snippetFromMethod(DatastoreClient::class, 'lookup');
$snippet->addLocal('datastore', $this->client);

$this->connection->lookup(Argument::any())
->shouldBeCalled()
->willReturn([
$this->mockSendRequest(
'lookup',
[],
[
'found' => [
[
'entity' => [
Expand All @@ -581,7 +582,8 @@ public function testLookup()
]
]
]
]);
]
);

$this->refreshOperation($this->client, $this->connection->reveal(), $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand All @@ -602,9 +604,10 @@ public function testLookupBatch()
$snippet = $this->snippetFromMethod(DatastoreClient::class, 'lookupBatch');
$snippet->addLocal('datastore', $this->client);

$this->connection->lookup(Argument::any())
->shouldBeCalled()
->willReturn([
$this->mockSendRequest(
'lookup',
[],
[
'found' => [
[
'entity' => [
Expand Down Expand Up @@ -635,7 +638,8 @@ public function testLookupBatch()
]
]
]
]);
]
);

$this->refreshOperation($this->client, $this->connection->reveal(), $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand Down
48 changes: 32 additions & 16 deletions Datastore/tests/Snippet/DatastoreSessionHandlerTest.php
Expand Up @@ -72,14 +72,22 @@ public function testClass()
$snippet = $this->snippetFromClass(DatastoreSessionHandler::class);
$snippet->replace('$datastore = new DatastoreClient();', '');

$this->connection->lookup(Argument::allOf(
Argument::withEntry('transaction', self::TRANSACTION),
Argument::that(function ($args) {
return $args['keys'][0]['partitionId']['namespaceId'] === 'sessions'
&& $args['keys'][0]['path'][0]['kind'] === 'PHPSESSID'
&& isset($args['keys'][0]['path'][0]['name']);
})
))->shouldBeCalled()->willReturn([]);
$this->mockSendRequest(
'lookup',
[
'keys' => [
[
'partitionId' => ['namespaceId' => 'sessions'],
'path' => [
['kind' => 'PHPSESSID']
]
]
],
'readOptions' => ['transaction' => self::TRANSACTION]
],
[],
0
);

$this->mockSendRequest(
'beginTransaction',
Expand Down Expand Up @@ -119,14 +127,22 @@ public function testClassErrorHandler()
$snippet = $this->snippetFromClass(DatastoreSessionHandler::class, 1);
$snippet->replace('$datastore = new DatastoreClient();', '');

$this->connection->lookup(Argument::allOf(
Argument::withEntry('transaction', self::TRANSACTION),
Argument::that(function ($args) {
return $args['keys'][0]['partitionId']['namespaceId'] === 'sessions'
&& $args['keys'][0]['path'][0]['kind'] === 'PHPSESSID'
&& isset($args['keys'][0]['path'][0]['name']);
})
))->shouldBeCalled()->willReturn([]);
$this->mockSendRequest(
'lookup',
[
'keys' => [
[
'partitionId' => ['namespaceId' => 'sessions'],
'path' => [
['kind' => 'PHPSESSID']
]
]
],
'readOptions' => ['transaction' => self::TRANSACTION]
],
[],
0
);

$this->mockSendRequest(
'beginTransaction',
Expand Down
29 changes: 18 additions & 11 deletions Datastore/tests/Snippet/ReadOnlyTransactionTest.php
Expand Up @@ -131,9 +131,12 @@ public function testClassRollback()
['transaction' => 'foo'],
0
);
$this->connection->lookup(Argument::any())
->shouldBeCalled()
->willReturn([]);
$this->mockSendRequest(
'lookup',
[],
[],
0
);
$this->connection->rollback(Argument::any())
->shouldBeCalled();

Expand All @@ -157,9 +160,10 @@ public function testLookup()
$snippet->addLocal('datastore', $this->client);
$snippet->addLocal('transaction', $this->transaction);

$this->connection->lookup(Argument::withEntry('transaction', self::TRANSACTION))
->shouldBeCalled()
->willReturn([
$this->mockSendRequest(
'lookup',
['readOptions' => ['transaction' => self::TRANSACTION]],
[
'found' => [
[
'entity' => [
Expand All @@ -176,7 +180,8 @@ public function testLookup()
]
]
]
]);
]
);

$this->refreshOperation($this->transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand All @@ -192,9 +197,10 @@ public function testLookupBatch()
$snippet->addLocal('datastore', $this->client);
$snippet->addLocal('transaction', $this->transaction);

$this->connection->lookup(Argument::withEntry('transaction', self::TRANSACTION))
->shouldBeCalled()
->willReturn([
$this->mockSendRequest(
'lookup',
['readOptions' => ['transaction' => self::TRANSACTION]],
[
'found' => [
[
'entity' => [
Expand Down Expand Up @@ -225,7 +231,8 @@ public function testLookupBatch()
]
]
]
]);
]
);

$this->refreshOperation($this->transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand Down
20 changes: 12 additions & 8 deletions Datastore/tests/Snippet/TransactionTest.php
Expand Up @@ -320,9 +320,10 @@ public function testLookup()
$snippet->addLocal('datastore', $this->client);
$snippet->addLocal('transaction', $this->transaction);

$this->connection->lookup(Argument::withEntry('transaction', self::TRANSACTION))
->shouldBeCalled()
->willReturn([
$this->mockSendRequest(
'lookup',
['readOptions' => ['transaction' => self::TRANSACTION]],
[
'found' => [
[
'entity' => [
Expand All @@ -339,7 +340,8 @@ public function testLookup()
]
]
]
]);
]
);

$this->refreshOperation($this->transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand All @@ -355,9 +357,10 @@ public function testLookupBatch()
$snippet->addLocal('datastore', $this->client);
$snippet->addLocal('transaction', $this->transaction);

$this->connection->lookup(Argument::withEntry('transaction', self::TRANSACTION))
->shouldBeCalled()
->willReturn([
$this->mockSendRequest(
'lookup',
['readOptions' => ['transaction' => self::TRANSACTION]],
[
'found' => [
[
'entity' => [
Expand Down Expand Up @@ -388,7 +391,8 @@ public function testLookupBatch()
]
]
]
]);
]
);

$this->refreshOperation($this->transaction, $this->connection->reveal(), $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand Down

0 comments on commit cee1539

Please sign in to comment.