-
-
Notifications
You must be signed in to change notification settings - Fork 342
Support for Aggregate Queries #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
c778e01
fd93d09
50f31bb
2254cf6
39a45f3
0da75ea
c8daf82
36abc00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -519,6 +519,65 @@ public function count($useMasterKey = false) | |
return $result['count']; | ||
} | ||
|
||
/** | ||
* Execute a distinct query and return unique values. | ||
* | ||
* @param string $key field to find distinct values | ||
* | ||
* @return array | ||
*/ | ||
public function distinct($key) | ||
{ | ||
$sessionToken = null; | ||
if ($user = ParseUser::getCurrentUser()) { | ||
$sessionToken = $user->getSessionToken(); | ||
} | ||
$opts = []; | ||
if (!empty($this->where)) { | ||
$opts['where'] = $this->where; | ||
} | ||
$opts['distinct'] = $key; | ||
$queryString = $this->buildQueryString($opts); | ||
$result = ParseClient::_request( | ||
'GET', | ||
'aggregate/'.$this->className.'?'.$queryString, | ||
$sessionToken, | ||
null, | ||
true | ||
); | ||
|
||
return $result['results']; | ||
} | ||
|
||
/** | ||
* Execute an aggregate query and returns aggregate results. | ||
* | ||
* @param array $pipeline stages to process query | ||
* | ||
* @return array | ||
*/ | ||
public function aggregate($pipeline) | ||
{ | ||
$sessionToken = null; | ||
if ($user = ParseUser::getCurrentUser()) { | ||
$sessionToken = $user->getSessionToken(); | ||
} | ||
$stages = []; | ||
foreach ($pipeline as $stage => $value) { | ||
$stages[$stage] = json_encode($value); | ||
} | ||
$queryString = $this->buildQueryString($stages); | ||
$result = ParseClient::_request( | ||
'GET', | ||
'aggregate/'.$this->className.'?'.$queryString, | ||
$sessionToken, | ||
null, | ||
true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here regarding the use of the master key. If it's not required to work it should be a method parameter with a default of |
||
); | ||
|
||
return $result['results']; | ||
} | ||
|
||
/** | ||
* Execute a find query and return the results. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,276 @@ | ||
<?php | ||
|
||
namespace Parse\Test; | ||
|
||
use Parse\ParseACL; | ||
use Parse\ParseException; | ||
use Parse\ParseObject; | ||
use Parse\ParseQuery; | ||
use Parse\ParseUser; | ||
|
||
class ParseQueryAggregateTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public static function setUpBeforeClass() | ||
{ | ||
Helper::setUp(); | ||
} | ||
|
||
public function setUp() | ||
{ | ||
Helper::clearClass('TestObject'); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
Helper::tearDown(); | ||
} | ||
|
||
/** | ||
* This function used as a helper function in test functions | ||
*/ | ||
public function loadObjects() | ||
{ | ||
$obj1 = new ParseObject('TestObject'); | ||
$obj2 = new ParseObject('TestObject'); | ||
$obj3 = new ParseObject('TestObject'); | ||
$obj4 = new ParseObject('TestObject'); | ||
|
||
$obj1->set('score', 10); | ||
$obj2->set('score', 10); | ||
$obj3->set('score', 10); | ||
$obj4->set('score', 20); | ||
|
||
$obj1->set('name', 'foo'); | ||
$obj2->set('name', 'foo'); | ||
$obj3->set('name', 'bar'); | ||
$obj4->set('name', 'dpl'); | ||
|
||
$objects = [$obj1, $obj2, $obj3, $obj4]; | ||
ParseObject::saveAll($objects); | ||
} | ||
|
||
public function testDistinctQuery() | ||
{ | ||
$this->loadObjects(); | ||
$query = new ParseQuery('TestObject'); | ||
$results = $query->distinct('score'); | ||
|
||
$this->assertEquals(2, count($results)); | ||
$this->assertEquals(in_array(10, $results), true); | ||
$this->assertEquals(in_array(20, $results), true); | ||
} | ||
|
||
public function testDistinctWhereQuery() | ||
{ | ||
$this->loadObjects(); | ||
$query = new ParseQuery('TestObject'); | ||
$query->equalTo('name', 'foo'); | ||
$results = $query->distinct('score'); | ||
|
||
$this->assertEquals(1, count($results)); | ||
$this->assertEquals($results[0], 10); | ||
} | ||
|
||
public function testDistinctClassNotExistQuery() | ||
{ | ||
$this->loadObjects(); | ||
$query = new ParseQuery('UnknownClass'); | ||
$results = $query->distinct('score'); | ||
|
||
$this->assertEquals(0, count($results)); | ||
} | ||
|
||
public function testDistinctFieldNotExistQuery() | ||
{ | ||
$this->loadObjects(); | ||
$query = new ParseQuery('TestObject'); | ||
$results = $query->distinct('unknown'); | ||
|
||
$this->assertEquals(0, count($results)); | ||
} | ||
|
||
public function testDisinctOnUsers() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoop almost missed this, typo here on testDisinctOnUsers |
||
{ | ||
Helper::clearClass(ParseUser::$parseClassName); | ||
$user1 = new ParseUser(); | ||
$user1->setUsername('foo'); | ||
$user1->setPassword('password'); | ||
$user1->set('score', 10); | ||
$user1->signUp(); | ||
|
||
$user2 = new ParseUser(); | ||
$user2->setUsername('bar'); | ||
$user2->setPassword('password'); | ||
$user2->set('score', 10); | ||
$user2->signUp(); | ||
|
||
$user3 = new ParseUser(); | ||
$user3->setUsername('hello'); | ||
$user3->setPassword('password'); | ||
$user3->set('score', 20); | ||
$user3->signUp(); | ||
|
||
$query = ParseUser::query(); | ||
$results = $query->distinct('score'); | ||
|
||
$this->assertEquals(2, count($results)); | ||
$this->assertEquals($results[0], 10); | ||
$this->assertEquals($results[1], 20); | ||
} | ||
|
||
public function testAggregateGroupQuery() | ||
{ | ||
$pipeline = [ | ||
'group' => [ | ||
'objectId' => '$name' | ||
] | ||
]; | ||
$this->loadObjects(); | ||
$query = new ParseQuery('TestObject'); | ||
$results = $query->aggregate($pipeline); | ||
|
||
$this->assertEquals(3, count($results)); | ||
} | ||
|
||
public function testAggregateGroupClassNotExistQuery() | ||
{ | ||
$pipeline = [ | ||
'group' => [ | ||
'objectId' => '$name' | ||
] | ||
]; | ||
$this->loadObjects(); | ||
$query = new ParseQuery('UnknownClass'); | ||
$results = $query->aggregate($pipeline); | ||
|
||
$this->assertEquals(0, count($results)); | ||
} | ||
|
||
public function testAggregateGroupFieldNotExistQuery() | ||
{ | ||
$pipeline = [ | ||
'group' => [ | ||
'objectId' => '$unknown' | ||
] | ||
]; | ||
$this->loadObjects(); | ||
$query = new ParseQuery('UnknownClass'); | ||
$results = $query->aggregate($pipeline); | ||
|
||
$this->assertEquals(0, count($results)); | ||
} | ||
|
||
public function testAggregateMatchQuery() | ||
{ | ||
$pipeline = [ | ||
'match' => [ | ||
'score' => [ '$gt' => 15 ] | ||
] | ||
]; | ||
$this->loadObjects(); | ||
$query = new ParseQuery('TestObject'); | ||
$results = $query->aggregate($pipeline); | ||
|
||
$this->assertEquals(1, count($results)); | ||
$this->assertEquals(20, $results[0]['score']); | ||
} | ||
|
||
public function testAggregateProjectQuery() | ||
{ | ||
$pipeline = [ | ||
'project' => [ | ||
'name' => 1 | ||
] | ||
]; | ||
$this->loadObjects(); | ||
$query = new ParseQuery('TestObject'); | ||
$results = $query->aggregate($pipeline); | ||
|
||
foreach ($results as $result) { | ||
$this->assertEquals(array_key_exists('name', $result), true); | ||
$this->assertEquals(array_key_exists('objectId', $result), true); | ||
$this->assertEquals(array_key_exists('score', $result), false); | ||
} | ||
} | ||
|
||
public function testAggregatePipelineInvalid() | ||
{ | ||
$pipeline = [ | ||
'unknown' => [] | ||
]; | ||
$this->loadObjects(); | ||
$query = new ParseQuery('TestObject'); | ||
$this->setExpectedException( | ||
'Parse\ParseException', | ||
'Invalid parameter for query: unknown', | ||
102 | ||
); | ||
$results = $query->aggregate($pipeline); | ||
} | ||
|
||
public function testAggregateGroupInvalid() | ||
{ | ||
$pipeline = [ | ||
'group' => [ | ||
'_id' => '$name' | ||
] | ||
]; | ||
$this->loadObjects(); | ||
$query = new ParseQuery('TestObject'); | ||
$this->setExpectedException( | ||
'Parse\ParseException', | ||
'Invalid parameter for query: group. Please use objectId instead of _id', | ||
102 | ||
); | ||
$results = $query->aggregate($pipeline); | ||
} | ||
|
||
public function testAggregateGroupObjectIdRequired() | ||
{ | ||
$pipeline = [ | ||
'group' => [] | ||
]; | ||
$this->loadObjects(); | ||
$query = new ParseQuery('TestObject'); | ||
$this->setExpectedException( | ||
'Parse\ParseException', | ||
'Invalid parameter for query: group. objectId is required', | ||
102 | ||
); | ||
$results = $query->aggregate($pipeline); | ||
} | ||
|
||
public function testAggregateOnUsers() | ||
{ | ||
Helper::clearClass(ParseUser::$parseClassName); | ||
$user1 = new ParseUser(); | ||
$user1->setUsername('foo'); | ||
$user1->setPassword('password'); | ||
$user1->set('score', 10); | ||
$user1->signUp(); | ||
|
||
$user2 = new ParseUser(); | ||
$user2->setUsername('bar'); | ||
$user2->setPassword('password'); | ||
$user2->set('score', 10); | ||
$user2->signUp(); | ||
|
||
$user3 = new ParseUser(); | ||
$user3->setUsername('hello'); | ||
$user3->setPassword('password'); | ||
$user3->set('score', 20); | ||
$user3->signUp(); | ||
|
||
$pipeline = [ | ||
'match' => [ | ||
'score' => [ '$gt' => 15 ] | ||
] | ||
]; | ||
|
||
$query = ParseUser::query(); | ||
$results = $query->aggregate($pipeline); | ||
|
||
$this->assertEquals(1, count($results)); | ||
$this->assertEquals($results[0]['score'], 20); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't looked over the PR in detail yet over on the server, but does this require the master key to work? If not we should be adding a method parameter to optionally use the master key, same as how the other query methods work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The server requires a masterKey for security purposes