Skip to content

Commit

Permalink
Fix readOptions on runQuery (#184)
Browse files Browse the repository at this point in the history
* Fix readOptions on runQuery

* address code review concerns
  • Loading branch information
jdpedrie authored and dwsupplee committed Oct 4, 2016
1 parent 80ea81b commit b0aa9c7
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/Datastore/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,9 @@ public function runQuery(QueryInterface $query, array $options = [])

$moreResults = true;
do {
$request = $options + [
$request = $options + $this->readOptions($options) + [
'projectId' => $this->projectId,
'partitionId' => $this->partitionId($this->projectId, $this->namespaceId),
'readOptions' => $this->readOptions($options),
$query->queryKey() => $query->queryObject()
];

Expand Down
89 changes: 89 additions & 0 deletions tests/Datastore/OperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,49 @@ public function testLookupDeferred()
$this->assertInstanceOf(Key::class, $res['deferred'][0]);
}

public function testLookupWithReadOptionsFromTransaction()
{
$this->connection->lookup(Argument::withKey('readOptions'))->shouldBeCalled()->willReturn([]);

$this->operation->setConnection($this->connection->reveal());

$k = new Key('test-project', [
'path' => [['kind' => 'kind', 'id' => '123']]
]);

$this->operation->lookup([$k], ['transaction' => 'foo']);
}

public function testLookupWithReadOptionsFromReadConsistency()
{
$this->connection->lookup(Argument::withKey('readOptions'))->shouldBeCalled()->willReturn([]);

$this->operation->setConnection($this->connection->reveal());

$k = new Key('test-project', [
'path' => [['kind' => 'kind', 'id' => '123']]
]);

$this->operation->lookup([$k], ['readConsistency' => 'foo']);
}

public function testLookupWithoutReadOptions()
{
$this->connection->lookup(Argument::that(function ($args) {
if (isset($args['readOptions'])) return false;

return true;
}))->shouldBeCalled()->willReturn([]);

$this->operation->setConnection($this->connection->reveal());

$k = new Key('test-project', [
'path' => [['kind' => 'kind', 'id' => '123']]
]);

$this->operation->lookup([$k]);
}

/**
* @expectedException InvalidArgumentException
*/
Expand Down Expand Up @@ -340,6 +383,52 @@ public function testRunQueryNoResults()
$this->assertEquals(count($arr), 0);
}

public function testRunQueryWithReadOptionsFromTransaction()
{
$this->connection->runQuery(Argument::withKey('readOptions'))->willReturn([]);

$this->operation->setConnection($this->connection->reveal());

$q = $this->prophesize(QueryInterface::class);
$q->queryKey()->willReturn('query');
$q->queryObject()->willReturn([]);

$res = $this->operation->runQuery($q->reveal(), ['transaction' => 'foo']);
iterator_to_array($res);
}

public function testRunQueryWithReadOptionsFromReadConsistency()
{
$this->connection->runQuery(Argument::withKey('readOptions'))->willReturn([]);

$this->operation->setConnection($this->connection->reveal());

$q = $this->prophesize(QueryInterface::class);
$q->queryKey()->willReturn('query');
$q->queryObject()->willReturn([]);

$res = $this->operation->runQuery($q->reveal(), ['readConsistency' => 'foo']);
iterator_to_array($res);
}

public function testRunQueryWithoutReadOptions()
{
$this->connection->runQuery(Argument::that(function ($args) {
if (isset($args['readOptions'])) return false;

return true;
}))->willReturn([]);

$this->operation->setConnection($this->connection->reveal());

$q = $this->prophesize(QueryInterface::class);
$q->queryKey()->willReturn('query');
$q->queryObject()->willReturn([]);

$res = $this->operation->runQuery($q->reveal());
iterator_to_array($res);
}

public function testCommit()
{
$this->connection->commit(Argument::that(function($arg) {
Expand Down

0 comments on commit b0aa9c7

Please sign in to comment.