Skip to content

Commit

Permalink
fix: test for gql query 300 limit
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwarajanand committed Nov 9, 2022
1 parent 0fc89a1 commit f902098
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
13 changes: 13 additions & 0 deletions Datastore/src/EntityPageIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ public function current()
$this->moreResultsType = isset($this->page['batch']['moreResults'])
? $this->page['batch']['moreResults']
: null;
if ($this->config['resultLimit'] === 0 &&
isset($this->page['query']['limit'])
) {
// limit is not set properly, so set the limit from parsed query
// which is returned by the server together with batch response
$limit = $this->page['query']['limit'];
if (is_array($limit) &&
isset($this->page['query']['limit']['value'])
) {
$limit = $this->page['query']['limit']['value'];
}
$this->config['resultLimit'] = $limit;
}

return $this->get($this->itemsPath, $this->page);
}
Expand Down
13 changes: 13 additions & 0 deletions Datastore/tests/System/QueryResultPaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ public function testGqlQueryPaginationByPage(DatastoreClient $client)
$this->assertQueryPageCount(self::$expectedTotal, $client, $q);
}

/**
* @dataProvider clientProvider
*/
public function testGqlQueryPaginationWithLimit(DatastoreClient $client)
{
$testLimit = 310;
$q = $client->gqlQuery(
sprintf('SELECT * FROM `%s` LIMIT %s', self::$testKind, $testLimit),
['allowLiterals' => true]
);
$this->assertQueryCount($testLimit, $client, $q);
}

/**
* @dataProvider clientProvider
*/
Expand Down
66 changes: 62 additions & 4 deletions Datastore/tests/Unit/EntityPageIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
namespace Google\Cloud\Datastore\Tests\Unit;

use Google\Cloud\Datastore\EntityPageIterator;
use PHPUnit\Framework\TestCase;
use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

/**
* @group datastore
*/
class EntityPageIteratorTest extends TestCase
{
use AssertIsType;

private static $moreResultsType = 'NOT_FINISHED';
private static $items = ['a', 'b', 'c'];

Expand All @@ -40,6 +43,61 @@ function ($item) {

$pagesArray = iterator_to_array($pages);

$this->assertOverPages($pages);
}

public function testCurrentSetsQueryIntegerLimits()
{
$pages = new EntityPageIterator(
function ($item) {
return $item;
},
[$this, 'theCall'],
[
'test' => 'call',
'query' => ['limit' => 1000],
]
);

$pagesArray = iterator_to_array($pages);

$this->assertOverPages($pages, 1000);
}

public function testCurrentSetsQueryArrayLimits()
{

$pages = new EntityPageIterator(
function ($item) {
return $item;
},
[$this, 'theCall'],
[
'test' => 'call',
'query' => ['limit' => ['value' => 1100]],
]
);

$this->assertOverPages($pages, 1100);
}

private function assertOverPages($pages, $expectedLimit = 0)
{
$pagesArray = iterator_to_array($pages);

$reflection = new \ReflectionClass($pages);
$configProp = $reflection->getProperty('config');
$configProp->setAccessible(true);
$configs = $configProp->getValue($pages);

$this->assertIsArray($configs);
$this->assertArrayHasKey('resultLimit', $configs);
$this->assertEquals(
$expectedLimit,
$configs['resultLimit'],
"resultLimit assertion failed."
);

$this->assertEquals(self::$moreResultsType, $pages->moreResultsType());
$this->assertEquals(self::$items, $pagesArray[0]);
}
Expand All @@ -48,9 +106,9 @@ public function theCall(array $options)
{
return [
'batch' => [
'moreResults' => self::$moreResultsType
'moreResults' => self::$moreResultsType,
],
'items' => self::$items
];
'items' => self::$items,
] + $options;
}
}

0 comments on commit f902098

Please sign in to comment.