From b0aa9c7480a72b255d70a53a838ec1811d397508 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Tue, 4 Oct 2016 18:24:06 -0400 Subject: [PATCH] Fix readOptions on runQuery (#184) * Fix readOptions on runQuery * address code review concerns --- src/Datastore/Operation.php | 3 +- tests/Datastore/OperationTest.php | 89 +++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/src/Datastore/Operation.php b/src/Datastore/Operation.php index 15360fafbd9e..adfa8d2ecad5 100644 --- a/src/Datastore/Operation.php +++ b/src/Datastore/Operation.php @@ -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() ]; diff --git a/tests/Datastore/OperationTest.php b/tests/Datastore/OperationTest.php index 83af4a49c96d..70734fd87d82 100644 --- a/tests/Datastore/OperationTest.php +++ b/tests/Datastore/OperationTest.php @@ -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 */ @@ -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) {