Skip to content

Commit

Permalink
feat(Datastore): Resolve PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ajupazhamayil committed Dec 30, 2022
1 parent 8252565 commit e3e79db
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 34 deletions.
36 changes: 19 additions & 17 deletions Datastore/src/Connection/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public function allocateIds(array $args)
*/
public function beginTransaction(array $args)
{
$this->setReadTime($args);
return $this->send('projects', 'beginTransaction', $args);
$argsWithReadTime = $this->setReadTime($args);
return $this->send('projects', 'beginTransaction', $argsWithReadTime);
}

/**
Expand All @@ -94,8 +94,8 @@ public function commit(array $args)
*/
public function lookup(array $args)
{
$this->setReadTime($args);
return $this->send('projects', 'lookup', $args);
$argsWithReadTime = $this->setReadTime($args);
return $this->send('projects', 'lookup', $argsWithReadTime);
}

/**
Expand All @@ -111,27 +111,29 @@ public function rollback(array $args)
*/
public function runQuery(array $args)
{
$this->setReadTime($args);
return $this->send('projects', 'runQuery', $args);
$argsWithReadTime = $this->setReadTime($args);
return $this->send('projects', 'runQuery', $argsWithReadTime);
}

/**
* @param array $args
*/
private function setReadTime(array &$args)
private function setReadTime(array $args)
{
if (isset($args['readOptions']['readTime'])) {
$time = $args['readOptions']['readTime']->getSeconds();
$date = date('Y/m/d H:i:s.u\Z', $time);
$args['readOptions']['readTime'] = new Timestamp(
new \DateTime($date)
);
$this->setReadTimeOption($args['readOptions']['readTime']);
} elseif (isset($args['transactionOptions']['readOnly']['readTime'])) {
$time = $args['transactionOptions']['readOnly']['readTime']->getSeconds();
$date = date('Y/m/d H:i:s.u\Z', $time);
$args['transactionOptions']['readOnly']['readTime'] = new Timestamp(
new \DateTime($date)
);
$this->setReadTimeOption($args['transactionOptions']['readOnly']['readTime']);
}
return $args;
}

/**
* @param timestamp $readTime
*/
private function setReadTimeOption(&$readTime)
{
$date = date('Y/m/d H:i:s.u\Z', $readTime->getSeconds());
$readTime = new Timestamp(new \DateTime($date));
}
}
10 changes: 2 additions & 8 deletions Datastore/tests/System/RunQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,20 +363,14 @@ public function testRunQueryWithReadTime(DatastoreClient $client)
->kind($kind)
->filter('__key__', '=', $key);
$result = $client->runQuery($query);
$personListEntities = [];
foreach ($result as $person) {
$personListEntities[] = $person;
}
$personListEntities = iterator_to_array($result);

// Person lastName should be the lastName AFTER update
$this->assertEquals($personListEntities[0]['lastName'], $newLastName);

// Person lastName should be the lastName BEFORE update
$result = $client->runQuery($query, ['readTime' => $time]);
$personListEntities = [];
foreach ($result as $person) {
$personListEntities[] = $person;
}
$personListEntities = iterator_to_array($result);
$this->assertEquals($personListEntities[0]['lastName'], $lastName);
}

Expand Down
11 changes: 2 additions & 9 deletions Datastore/tests/System/RunTransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ public function testTransactionWithReadTime(DatastoreClient $client)
->filter('__key__', '=', $key)
->hasAncestor($ancKey);
$result = $client->runQuery($query);
$personListEntities = [];
foreach ($result as $person) {
$personListEntities[] = $person;
}

$personListEntities = iterator_to_array($result);
// Person lastName should be the lastName AFTER update
$this->assertEquals($personListEntities[0]['lastName'], $newLastName);

Expand All @@ -130,10 +126,7 @@ public function testTransactionWithReadTime(DatastoreClient $client)
);
// runQuery function: Person lastName should be the lastName BEFORE update
$persons = $transaction2->runQuery($query);
$personListEntities = [];
foreach ($persons as $person) {
$personListEntities[] = $person;
}
$personListEntities = iterator_to_array($persons);
$this->assertEquals($personListEntities[0]['lastName'], $lastName);

// lookUp function: Person lastName should be the lastName BEFORE update
Expand Down

0 comments on commit e3e79db

Please sign in to comment.