Skip to content

Commit

Permalink
Updates for Key parameters and other parameter types
Browse files Browse the repository at this point in the history
  • Loading branch information
twalder-docnet committed Jun 12, 2015
1 parent 5ca7422 commit 0a999e3
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/GDS/Gateway/ProtoBuf.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
* limitations under the License.
*/
namespace GDS\Gateway;
use GDS\Entity;
use google\appengine\datastore\v4\BeginTransactionRequest;
use google\appengine\datastore\v4\BeginTransactionResponse;
use google\appengine\datastore\v4\CommitRequest;
use google\appengine\datastore\v4\CommitRequest\Mode;
use google\appengine\datastore\v4\CommitResponse;
use google\appengine\datastore\v4\Key;
use google\appengine\datastore\v4\LookupRequest;
use google\appengine\datastore\v4\LookupResponse;
use google\appengine\datastore\v4\QueryResultBatch;
Expand Down Expand Up @@ -259,8 +261,12 @@ public function deleteMulti(array $arr_entities)
/**
* Fetch some Entities, based on the supplied GQL and, optionally, parameters
*
* @todo Handle parameters
* @todo break out for local dev GQL interpretation
*
* @param string $str_gql
* @param array|null $arr_params
* @return \GDS\Entity[]|null
* @throws \Exception
*/
public function gql($str_gql, $arr_params = NULL)
{
Expand All @@ -280,8 +286,6 @@ public function gql($str_gql, $arr_params = NULL)
/**
* Add Parameters to a GQL Query object
*
* @todo Add support for non-cursor parameters (see API Client version)
*
* @param \google\appengine\datastore\v4\GqlQuery $obj_query
* @param array $arr_params
* @throws \Exception
Expand All @@ -295,7 +299,18 @@ private function addParamsToQuery(\google\appengine\datastore\v4\GqlQuery $obj_q
if ('startCursor' == $str_name) {
$obj_arg->setCursor($mix_value);
} else {
throw new \Exception(__METHOD__ . '() unimplemented for non-start-cursor args');
$obj_val = $obj_arg->mutableValue();
if($mix_value instanceof Entity) {
// @todo review re-use of Mapper
$obj_key = $obj_val->getKeyValue();
$this->createMapper()->configureGoogleKey($obj_key, $mix_value);
} elseif($mix_value instanceof \DateTime) {
$obj_val->setTimestampMicrosecondsValue($mix_value->format('Uu'));
} elseif (is_int($mix_value)) {
$obj_val->setIntegerValue($mix_value);
} else {
$obj_val->setStringValue($mix_value);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/GDS/Mapper/GoogleAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function createKeys(array $arr_gds_entities)
* @param Entity $obj_gds_entity
* @return \Google_Service_Datastore_Key
*/
protected function createKey(Entity $obj_gds_entity)
public function createKey(Entity $obj_gds_entity)
{
$obj_key = new \Google_Service_Datastore_Key();
$obj_key->setPath($this->buildKeyPath($obj_gds_entity));
Expand Down
119 changes: 119 additions & 0 deletions tests/ProtoBufGqlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Copyright 2015 Tom Walder
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Tests for Protocol Buffer GQL queries
*
* @author Tom Walder <tom@docnet.nu>
*/
class ProtoBufGqlTest extends GDSTest {

/**
* Fetch one test
*/
public function testFetchOneNoParams()
{
$str_gql = "SELECT * FROM Kind";
$obj_request = new \google\appengine\datastore\v4\RunQueryRequest();
$obj_request->mutableReadOptions();
$obj_partition = $obj_request->mutablePartitionId();
$obj_partition->setDatasetId('Dataset');
$obj_gql_query = $obj_request->mutableGqlQuery();
$obj_gql_query->setAllowLiteral(TRUE);
$obj_gql_query->setQueryString($str_gql . " LIMIT 1");

$this->apiProxyMock->expectCall('datastore_v4', 'RunQuery', $obj_request, new \google\appengine\datastore\v4\RunQueryResponse());

$obj_store = $this->createBasicStore();
$obj_result = $obj_store->fetchOne($str_gql);

$this->assertEquals($obj_result, NULL);
$this->apiProxyMock->verify();
}

/**
* Fetch page test
*/
public function testFetchPageNoParams(){
$str_gql = "SELECT * FROM Kind";
$obj_request = new \google\appengine\datastore\v4\RunQueryRequest();
$obj_request->mutableReadOptions();
$obj_partition = $obj_request->mutablePartitionId();
$obj_partition->setDatasetId('Dataset');
$obj_gql_query = $obj_request->mutableGqlQuery();
$obj_gql_query->setAllowLiteral(TRUE);
$obj_gql_query->setQueryString($str_gql . " LIMIT 11 ");

$this->apiProxyMock->expectCall('datastore_v4', 'RunQuery', $obj_request, new \google\appengine\datastore\v4\RunQueryResponse());

$obj_store = $this->createBasicStore();
$obj_result = $obj_store->query($str_gql)->fetchPage(11);

$this->assertEquals($obj_result, []);
$this->apiProxyMock->verify();
}

/**
* GQL Fetch ONE with one string parameter
*/
public function testFetchOneStringParam()
{
$str_gql = "SELECT * FROM Kind WHERE property = @param";
$obj_request = new \google\appengine\datastore\v4\RunQueryRequest();
$obj_request->mutableReadOptions();
$obj_partition = $obj_request->mutablePartitionId();
$obj_partition->setDatasetId('Dataset');

$obj_gql_query = $obj_request->mutableGqlQuery();
$obj_gql_query->setAllowLiteral(TRUE);
$obj_gql_query->setQueryString($str_gql . " LIMIT 1");
$obj_arg = $obj_gql_query->addNameArg();
$obj_arg->setName('param');
$obj_arg->mutableValue()->setStringValue('test');

$this->apiProxyMock->expectCall('datastore_v4', 'RunQuery', $obj_request, new \google\appengine\datastore\v4\RunQueryResponse());

$obj_store = $this->createBasicStore();
$obj_result = $obj_store->fetchOne($str_gql, ['param' => 'test']);

$this->assertEquals($obj_result, NULL);
$this->apiProxyMock->verify();
}

/**
* @todo Fetch with an Integer parameter
*/

/**
* @todo Fetch with a DateTime parameter
*/

/**
* @todo Fetch with a root GDS\Entity parameter
*/

/**
* @todo Fetch with an GDS\Entity parameter (with ancestors)
*/

/**
* @todo Fetch with a multiple mixed type parameters
*/



}

0 comments on commit 0a999e3

Please sign in to comment.