Skip to content

Commit

Permalink
Update snippet tests part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
yash30201 committed May 9, 2024
1 parent 2e9a749 commit c0e2644
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 82 deletions.
3 changes: 2 additions & 1 deletion Datastore/tests/Snippet/DatastoreSessionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Google\Cloud\Datastore\DatastoreClient;
use Google\Cloud\Datastore\DatastoreSessionHandler;
use Google\Cloud\Datastore\V1\Client\DatastoreClient as V1DatastoreClient;
use Google\Cloud\Datastore\V1\CommitRequest;
use Google\Cloud\Datastore\V1\CommitRequest\Mode;
use Google\Cloud\Datastore\V1\TransactionOptions;
use Prophecy\Argument;
Expand Down Expand Up @@ -67,7 +68,6 @@ public function setUp(): void
$this->requestHandler = $this->prophesize(RequestHandler::class);
}

/** @group current */
public function testClass()
{
$snippet = $this->snippetFromClass(DatastoreSessionHandler::class);
Expand Down Expand Up @@ -161,6 +161,7 @@ public function testClassErrorHandler()
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'commit',
Argument::type(CommitRequest::class),
Argument::cetera()
)->shouldBeCalled()->will(fn () => trigger_error('oops!', E_USER_WARNING));

Expand Down
56 changes: 30 additions & 26 deletions Datastore/tests/Snippet/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
use Google\Cloud\Datastore\EntityMapper;
use Google\Cloud\Datastore\Key;
use Google\Cloud\Datastore\Operation;
use Google\Cloud\Datastore\V1\Client\DatastoreClient as V1DatastoreClient;
use Google\Cloud\Datastore\V1\CommitRequest;
use Google\Cloud\Datastore\V1\LookupRequest;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

Expand Down Expand Up @@ -98,42 +101,43 @@ public function testClassEntityType()
'operation'
]);

$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'commit',
[],
['mutationResults' => [['version' => 1]]],
0
);
$this->mockSendRequest(
Argument::type(CommitRequest::class),
Argument::cetera()
)->shouldBeCalled()->willReturn(['mutationResults' => [['version' => 1]]]);

$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'lookup',
[],
[
'found' => [
[
'entity' => [
'key' => [
'path' => [['kind' => 'Business', 'name' => 'Google']]
Argument::type(LookupRequest::class),
Argument::cetera()
)->shouldBeCalled()->willReturn([
'found' => [
[
'entity' => [
'key' => [
'path' => [['kind' => 'Business', 'name' => 'Google']]
],
'properties' => [
'name' => [
'stringValue' => 'Google'
],
'properties' => [
'name' => [
'stringValue' => 'Google'
],
'parent' => [
'entityValue' => [
'properties' => [
'name' => [
'stringValue' => 'Alphabet'
]
'parent' => [
'entityValue' => [
'properties' => [
'name' => [
'stringValue' => 'Alphabet'
]
]
]
]
]
]
]
],
0
);
]
]);

$operation = new Operation(
$this->requestHandler->reveal(),
Expand Down
13 changes: 9 additions & 4 deletions Datastore/tests/Snippet/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Google\Cloud\Datastore\EntityMapper;
use Google\Cloud\Datastore\Query\Filter;
use Google\Cloud\Datastore\Query\Query;
use Google\Cloud\Datastore\V1\Client\DatastoreClient as V1DatastoreClient;
use Google\Cloud\Datastore\V1\RunQueryRequest;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

class FilterTest extends SnippetTestCase
Expand Down Expand Up @@ -84,9 +87,12 @@ public function getCompositeFilterTypes()

private function createRequestHandlerProphecy()
{
$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'runQuery',
[],
Argument::type(RunQueryRequest::class),
Argument::cetera()
)->shouldBeCalled()->willReturn(
[
'batch' => [
'entityResults' => [
Expand All @@ -103,8 +109,7 @@ private function createRequestHandlerProphecy()
],
'moreResults' => 'no'
]
],
0
]
);

$this->refreshOperation($this->datastore, $this->requestHandler->reveal(), [
Expand Down
94 changes: 66 additions & 28 deletions Datastore/tests/Snippet/ReadOnlyTransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
use Google\Cloud\Datastore\Operation;
use Google\Cloud\Datastore\Query\QueryInterface;
use Google\Cloud\Datastore\ReadOnlyTransaction;
use Google\Cloud\Datastore\V1\BeginTransactionRequest;
use Google\Cloud\Datastore\V1\Client\DatastoreClient as V1DatastoreClient;
use Google\Cloud\Datastore\V1\LookupRequest;
use Google\Cloud\Datastore\V1\RollbackRequest;
use Google\Cloud\Datastore\V1\RunQueryRequest;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

/**
Expand Down Expand Up @@ -97,12 +103,12 @@ public function setUp(): void

public function testClass()
{
$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'beginTransaction',
[],
['transaction' => 'foo'],
0
);
Argument::type(BeginTransactionRequest::class),
Argument::cetera()
)->willReturn(['transaction' => 'foo'])->shouldBeCalled();

$this->refreshOperation($this->client, $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand All @@ -118,19 +124,26 @@ public function testClass()

public function testClassRollback()
{
$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'beginTransaction',
[],
['transaction' => 'foo'],
0
);
$this->mockSendRequest(
Argument::type(BeginTransactionRequest::class),
Argument::cetera()
)->willReturn(['transaction' => 'foo'])->shouldBeCalled();

$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'lookup',
[],
[],
0
);
$this->mockSendRequest('rollback', [], [], 0);
Argument::type(LookupRequest::class),
Argument::cetera()
)->shouldBeCalled();

$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'rollback',
Argument::type(RollbackRequest::class),
Argument::cetera()
)->shouldBeCalled();

$snippet = $this->snippetFromClass(ReadOnlyTransaction::class, 1);

Expand All @@ -152,9 +165,16 @@ public function testLookup()
$snippet->addLocal('datastore', $this->client);
$snippet->addLocal('transaction', $this->transaction);

$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'lookup',
['readOptions' => ['transaction' => self::TRANSACTION]],
Argument::that(function ($req) {
$data = $this->getSerializer()->encodeMessage($req);
return isset($data['readOptions']['transaction'])
&& $data['readOptions']['transaction'] === self::TRANSACTION;
}),
Argument::cetera()
)->willReturn(
[
'found' => [
[
Expand All @@ -173,7 +193,7 @@ public function testLookup()
]
]
]
);
)->shouldBeCalled();

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

$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'lookup',
['readOptions' => ['transaction' => self::TRANSACTION]],
Argument::that(function ($req) {
$data = $this->getSerializer()->encodeMessage($req);
return isset($data['readOptions']['transaction'])
&& $data['readOptions']['transaction'] === self::TRANSACTION;
}),
Argument::cetera()
)->willReturn(
[
'found' => [
[
Expand Down Expand Up @@ -224,7 +251,7 @@ public function testLookupBatch()
]
]
]
);
)->shouldBeCalled();

$this->refreshOperation($this->transaction, $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand All @@ -246,9 +273,16 @@ public function testRunQuery()
$query->queryKey()->willReturn('query');
$snippet->addLocal('query', $query->reveal());

$this->mockSendRequest(
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'runQuery',
['readOptions' => ['transaction' => self::TRANSACTION]],
Argument::that(function ($req) {
$data = $this->getSerializer()->encodeMessage($req);
return isset($data['readOptions']['transaction'])
&& $data['readOptions']['transaction'] === self::TRANSACTION;
}),
Argument::cetera()
)->willReturn(
[
'batch' => [
'entityResults' => [
Expand All @@ -268,9 +302,8 @@ public function testRunQuery()
]
]
]
],
0
);
]
)->shouldBeCalled();

$this->refreshOperation($this->transaction, $this->requestHandler->reveal(), [
'projectId' => self::PROJECT
Expand All @@ -285,7 +318,12 @@ public function testRollback()
$snippet = $this->snippetFromMethod(ReadOnlyTransaction::class, 'rollback');
$snippet->addLocal('transaction', $this->transaction);

$this->mockSendRequest('rollback', [], [], 0);
$this->requestHandler->sendRequest(
V1DatastoreClient::class,
'rollback',
Argument::type(RollbackRequest::class),
Argument::cetera()
)->shouldBeCalled();

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

0 comments on commit c0e2644

Please sign in to comment.