Example #1
0
 public function run()
 {
     $this->requireDataset('Control');
     $this->requireDataset('Heroes');
     $this->checkQueryResult(r\rDo(array(1, 2, 3), function ($x, $y, $z) {
         return $x->mul($y->add($z));
     }), 5.0);
     $this->checkQueryResult(r\branch(r\expr(true), r\expr('true'), r\expr('false')), 'true');
     $this->checkQueryResult(r\branch(r\expr(false), r\expr('true'), r\expr('false')), 'false');
     $this->checkQueryResult(r\expr(array(1, 2, 3))->rForeach(function ($x) {
         return r\db('Control')->table('t1')->insert(array('x' => $x));
     })->attr('inserted'), 3.0);
     $this->checkQueryResult(r\db('Control')->table('t1')->map(r\row('x')), array(1, 2, 3));
     $errorCaught = false;
     try {
         r\error('ERRRRRR')->run($this->conn);
     } catch (r\RqlUserError $e) {
         $errorCaught = true;
     }
     if (!$errorCaught) {
         echo "rrror() did not throw an error.\n";
     }
     // Js is also tested separately in JsTest
     $this->checkQueryResult(r\js("'str1' + 'str2'"), 'str1str2');
     $this->checkQueryResult(r\expr('5.0')->coerceTo('number'), 5.0);
     $this->checkQueryResult(r\expr(5.0)->coerceTo('string'), '5');
     $this->checkQueryResult(r\expr(5.0)->typeOf(), 'NUMBER');
     $this->checkQueryResult(r\expr('foo')->typeOf(), 'STRING');
     $this->checkQueryResult(r\expr(null)->typeOf(), 'NULL');
     $this->checkQueryResult(r\expr(array(1, 2, 3))->typeOf(), 'ARRAY');
     $this->checkQueryResult(r\expr(array('x' => 1))->typeOf(), 'OBJECT');
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->info(), array('type' => "TABLE", 'primary_key' => 'superhero', 'name' => 'marvel', 'indexes' => array(), 'db' => array('type' => 'DB', 'name' => 'Heroes')));
     $this->datasets['Control']->reset();
 }
Example #2
0
 public function run()
 {
     $this->requireDataset('Heroes');
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->filter(r\row('superhero')->eq('Iron Man'))->count(), 1.0);
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->filter(function ($x) {
         return $x('superhero')->ne('Iron Man');
     })->count(), 2.0);
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->filter(r\row('foo')->eq('naaa'))->count(), 0.0);
     $caught = false;
     try {
         $this->checkQueryResult(r\db('Heroes')->table('marvel')->filter(r\row('foo')->eq('naaa'), r\error())->count(), 0.0);
     } catch (r\RqlServerError $e) {
         $caught = true;
     }
     if (!$caught) {
         echo "Filter with default rrror() did not throw.";
     }
     $caught = false;
     try {
         $this->checkQueryResult(r\db('Heroes')->table('marvel')->filter(r\row('foo')->eq('naaa'), r\error('msg'))->count(), 0.0);
     } catch (r\RqlServerError $e) {
         $caught = true;
     }
     if (!$caught) {
         echo "Filter with default rrror() did not throw.";
     }
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->filter(r\row('foo')->eq('naaa'), true)->count(), 3.0);
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->filter(r\row('foo')->eq('naaa'), r\expr('true'))->count(), 3.0);
 }
Example #3
0
 public function run()
 {
     // Test management operations
     r\dbCreate('tableTest')->run($this->conn);
     $this->checkQueryResult(r\db('tableTest')->tableCreate('t1', array('durability' => 'soft', 'primary_key' => 'p'))->pluck('tables_created'), array('tables_created' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->insert(array('p' => 'foo')), array('unchanged' => 0, 'skipped' => 0, 'replaced' => 0, 'inserted' => 1, 'errors' => 0, 'deleted' => 0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->rebalance()->pluck('rebalanced'), array('rebalanced' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->reconfigure(array('shards' => 1, 'replicas' => 1))->pluck('reconfigured'), array('reconfigured' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->wait()->pluck('ready'), array('ready' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->wait(array('wait_for' => "all_replicas_ready"))->pluck('ready'), array('ready' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->config()->pluck('name'), array('name' => "t1"));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->status()->getField('status')->pluck('all_replicas_ready'), array('all_replicas_ready' => true));
     $this->checkQueryResult(r\db('tableTest')->tableList(), array('t1'));
     // TODO: These index tests are kind of duplicates of IndexTest...
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexCreate('akey'), array('created' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexCreate('bfun', r\row('p')), array('created' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexCreate('cfun', function ($r) {
         return r\expr(5);
     }), array('created' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexList(), array('akey', 'bfun', 'cfun'));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexDrop('akey'), array('dropped' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexDrop('bfun'), array('dropped' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexDrop('cfun'), array('dropped' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->sync(), array('synced' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1', false)->count(), 1.0);
     $this->checkQueryResult(r\db('tableTest')->table('t1', true)->count(), 1.0);
     $this->checkQueryResult(r\db('tableTest')->table('t1', array("use_outdated" => true))->count(), 1.0);
     $this->checkQueryResult(r\db('tableTest')->tableDrop('t1')->pluck('tables_dropped'), array('tables_dropped' => 1.0));
     r\dbDrop('tableTest')->run($this->conn);
     // Test general whole-table queries
     $this->requireDataset('Heroes');
     $testResult = r\db('Heroes')->table('marvel')->orderBy('superhero')->run($this->conn);
     $this->checkQueryResult(r\expr($testResult)->count(), 3.0);
     $this->datasets['Heroes']->reset();
 }
Example #4
0
 public function testMap()
 {
     $this->db()->table('t1')->insert(array(array('x' => 1), array('x' => 2), array('x' => 3)))->run($this->conn);
     $res = $this->db()->table('t1')->map(\r\row('x'))->run($this->conn)->toArray();
     sort($res);
     $this->assertEquals(array(1.0, 2.0, 3.0), $res);
 }
Example #5
0
 public function run()
 {
     $this->requireDataset('Heroes');
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->map(function ($hero) {
         return $hero('combatPower')->add($hero('compassionPower')->mul(2));
     }), array(7.0, 9.0, 5.0));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->map(r\row('combatPower')->add(r\row('compassionPower')->mul(2))), array(7.0, 9.0, 5.0));
     $this->checkQueryResult(r\expr(array(r\db('Heroes')->table('marvel')->coerceTo('array'), r\db('Heroes')->table('marvel')->coerceTo('array')))->concatMap(function ($hero) {
         return $hero->pluck('superhero');
     })->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman', 'Iron Man', 'Wolverine', 'Spiderman'));
     $this->checkQueryResult(r\expr(array(r\db('Heroes')->table('marvel')->coerceTo('array'), r\db('Heroes')->table('marvel')->coerceTo('array')))->concatMap(r\row()->pluck('superhero'))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman', 'Iron Man', 'Wolverine', 'Spiderman'));
     // Regression test for #62
     $this->checkQueryResult(r\expr(array(1, 2, 3))->map(r\branch(r\expr(true), function ($x) {
         return $x;
     }, function ($x) {
         return $x;
     })), array(1.0, 2.0, 3.0));
     $this->checkQueryResult(r\mapMultiple(array(r\range(1, 4), r\range(2, 5)), function ($x, $y) {
         return $x->add($y);
     }), array(3, 5, 7));
     $this->checkQueryResult(r\range(1, 4)->mapMultiple(array(r\range(2, 5)), function ($x, $y) {
         return $x->add($y);
     }), array(3, 5, 7));
     $this->checkQueryResult(r\range(1, 4)->mapMultiple(r\range(2, 5), function ($x, $y) {
         return $x->add($y);
     }), array(3, 5, 7));
     $this->checkQueryResult(r\range(1, 4)->mapMultiple(array(r\range(2, 5), r\range(1, 4)), function ($x, $y, $z) {
         return $x->add($y)->sub($z);
     }), array(2, 3, 4));
 }
Example #6
0
 public function run()
 {
     // Test management operations
     r\dbCreate('tableTest')->run($this->conn);
     $this->checkQueryResult(r\db('tableTest')->tableCreate('t1', array('hard_durability' => false, 'cache_size' => 8, 'primary_key' => 'p')), array('created' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->insert(array('p' => 'foo')), array('unchanged' => 0, 'skipped' => 0, 'replaced' => 0, 'inserted' => 1, 'errors' => 0, 'deleted' => 0));
     $this->checkQueryResult(r\db('tableTest')->tableList(), array('t1'));
     // TODO: These index tests are kind of duplicates of IndexTest...
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexCreate('akey'), array('created' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexCreate('bfun', r\row('p')), array('created' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexCreate('cfun', function ($r) {
         return r\expr(5);
     }), array('created' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexList(), array('akey', 'bfun', 'cfun'));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexDrop('akey'), array('dropped' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexDrop('bfun'), array('dropped' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->table('t1')->indexDrop('cfun'), array('dropped' => 1.0));
     $this->checkQueryResult(r\db('tableTest')->tableDrop('t1'), array('dropped' => 1.0));
     r\dbDrop('tableTest')->run($this->conn);
     // Test general whole-table queries
     $this->requireDataset('Heroes');
     $testResult = r\db('Heroes')->table('marvel')->orderBy('superhero')->run($this->conn)->toNative();
     $this->checkQueryResult(r\expr($testResult)->count(), 3.0);
     $this->datasets['Heroes']->reset();
 }
Example #7
0
 public function run()
 {
     $this->requireDataset('Heroes');
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->filter(r\row('superhero')->eq('Iron Man'))->count(), 1.0);
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->filter(function ($x) {
         return $x('superhero')->ne('Iron Man');
     })->count(), 2.0);
 }
Example #8
0
 public function run()
 {
     $this->requireDataset('Heroes');
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->get('Wolverine')->update(array('age' => 30)), array('unchanged' => 0, 'skipped' => 0, 'replaced' => 1, 'inserted' => 0, 'errors' => 0, 'deleted' => 0));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->update(function ($r) {
         return $r->merge(array('age' => 5));
     }), array('unchanged' => 0, 'skipped' => 0, 'replaced' => 3, 'inserted' => 0, 'errors' => 0, 'deleted' => 0));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->update(array('age' => r\row('age')->add(1))), array('unchanged' => 0, 'skipped' => 0, 'replaced' => 3, 'inserted' => 0, 'errors' => 0, 'deleted' => 0));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->update(array('age' => r\row('age')->add(r\js('1'))), array('durability' => 'soft', 'non_atomic' => true)), array('unchanged' => 0, 'skipped' => 0, 'replaced' => 3, 'inserted' => 0, 'errors' => 0, 'deleted' => 0));
     $this->datasets['Heroes']->reset();
 }
Example #9
0
 public function run()
 {
     $this->requireDataset('Heroes');
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->map(function ($hero) {
         return $hero('combatPower')->add($hero('compassionPower')->mul(2));
     }), array(7.0, 9.0, 5.0));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->map(r\row('combatPower')->add(r\row('compassionPower')->mul(2))), array(7.0, 9.0, 5.0));
     $this->checkQueryResult(r\expr(array(r\db('Heroes')->table('marvel')->coerceTo('array'), r\db('Heroes')->table('marvel')->coerceTo('array')))->concatMap(function ($hero) {
         return $hero->pluck('superhero');
     })->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman', 'Iron Man', 'Wolverine', 'Spiderman'));
     $this->checkQueryResult(r\expr(array(r\db('Heroes')->table('marvel')->coerceTo('array'), r\db('Heroes')->table('marvel')->coerceTo('array')))->concatMap(r\row()->pluck('superhero'))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman', 'Iron Man', 'Wolverine', 'Spiderman'));
 }
Example #10
0
 public function run()
 {
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => 2)))->filter(r\row()->getField('y')->eq(2))->pluck('x'), array(array('x' => 1)));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => 2)))->filter(r\row('y')->eq(2))->pluck('x'), array(array('x' => 1)));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => 2)))->pluck('x'), array(array('x' => 1)));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => 2)))->pluck(array('x', 'y')), array(array('x' => 1, 'y' => 2)));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => array('a' => 2.1, 'b' => 2.2))))->pluck(array('x' => true)), array(array('x' => 1)));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => array('a' => 2.1, 'b' => 2.2))))->pluck(array('y' => array('a', 'b'))), array(array('y' => array('a' => 2.1, 'b' => 2.2))));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => array('a' => 2.1, 'b' => 2.2))))->pluck(array('y' => array('b' => true))), array(array('y' => array('b' => 2.2))));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => 2)))->without('x'), array(array('y' => 2)));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => 2)))->without(array('x', 'y')), array(array()));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => array('a' => 2.1, 'b' => 2.2))))->without(array('x' => true)), array(array('y' => array('a' => 2.1, 'b' => 2.2))));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => array('a' => 2.1, 'b' => 2.2))))->without(array('y' => array('a', 'b'))), array(array('x' => 1, 'y' => array())));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => array('a' => 2.1, 'b' => 2.2))))->without(array('y' => array('b' => true))), array(array('x' => 1, 'y' => array('a' => 2.1))));
     $this->checkQueryResult(r\expr(array('x' => 1))->merge(array('y' => 2)), array('x' => 1, 'y' => 2));
     $this->checkQueryResult(r\expr(array('x' => 1))->merge(r\expr(array('y' => 2))), array('x' => 1, 'y' => 2));
     $this->checkQueryResult(r\expr(array('x' => 1, 'y' => array('a' => 1, 'b' => 2)))->merge(array('y' => array('c' => 3))), array('x' => 1, 'y' => array('a' => 1, 'b' => 2, 'c' => 3)));
     $this->checkQueryResult(r\expr(array('x' => 1, 'y' => array('a' => 1, 'b' => 2)))->merge(array('y' => r\literal(array('c' => 3)))), array('x' => 1, 'y' => array('c' => 3)));
     $this->checkQueryResult(r\expr(array('x' => 1, 'y' => array('a' => 1, 'b' => 2)))->merge(array('y' => r\literal())), array('x' => 1));
     $this->checkQueryResult(r\expr(array(array('a' => 1), array('a' => 2)))->merge(function ($doc) {
         return array('b' => $doc('a')->add(1));
     }), array(array('a' => 1, 'b' => 2), array('a' => 2, 'b' => 3)));
     $this->checkQueryResult(r\expr(array(1, 2, 3))->append(4), array(1, 2, 3, 4));
     $this->checkQueryResult(r\expr(array(1, 2, 3))->append(r\expr(4)), array(1, 2, 3, 4));
     $this->checkQueryResult(r\expr(array(1, 2, 3))->prepend(4), array(4, 1, 2, 3));
     $this->checkQueryResult(r\expr(array(1, 2, 3))->prepend(r\expr(4)), array(4, 1, 2, 3));
     $this->checkQueryResult(r\expr(array(1, 2, 3))->difference(array(1, 2)), array(3));
     $this->checkQueryResult(r\expr(array(1, 2, 3))->difference(r\expr(array(1, 2))), array(3));
     $this->checkQueryResult(r\expr(array('x' => 1, 'y' => 2))->hasFields('x'), true);
     $this->checkQueryResult(r\expr(array('x' => 1, 'y' => 2))->hasFields('foo'), false);
     $this->checkQueryResult(r\expr(array('x' => 1, 'y' => 2))->hasFields(array('x', 'y')), true);
     $this->checkQueryResult(r\expr(array('x' => 1, 'y' => 2))->hasFields(array('x', 'foo')), false);
     $this->checkQueryResult(r\expr(array('x' => 1, 'y' => 2))->hasFields(array('x' => true)), true);
     $this->checkQueryResult(r\expr(array('x' => 1, 'y' => 2))->hasFields(array('foo' => true)), false);
     $this->checkQueryResult(r\expr(array(1, 2, 3))->setInsert(4), array(1, 2, 3, 4));
     $this->checkQueryResult(r\expr(array(1, 2, 3))->setInsert(1), array(1, 2, 3));
     $this->checkQueryResult(r\expr(array(1, 2, 3))->setUnion(array(1, 4)), array(1, 2, 3, 4));
     $this->checkQueryResult(r\expr(array(1, 2, 3))->setIntersection(array(1, 4)), array(1));
     $this->checkQueryResult(r\expr(array(1, 2, 3))->setDifference(array(1, 4)), array(2, 3));
     $this->checkQueryResult(r\expr(array('a' => 1, 'b' => 2, 'c' => 3))->keys(), array('a', 'b', 'c'));
     $this->checkQueryResult(r\expr(array("Iron Man", "Spider-Man"))->insertAt(1, "Hulk"), array("Iron Man", "Hulk", "Spider-Man"));
     $this->checkQueryResult(r\expr(array("Iron Man", "Spider-Man"))->spliceAt(1, array("Hulk", "Thor")), array("Iron Man", "Hulk", "Thor", "Spider-Man"));
     $this->checkQueryResult(r\expr(array("Iron Man", "Hulk", "Spider-Man"))->deleteAt(1), array("Iron Man", "Spider-Man"));
     $this->checkQueryResult(r\expr(array("Iron Man", "Hulk", "Thor", "Spider-Man"))->deleteAt(1, 2), array("Iron Man", "Thor", "Spider-Man"));
     // TODO: This is disabled due to a potential bug in the server as of RethinkDB 1.9.0: https://github.com/rethinkdb/rethinkdb/issues/1456
     /*$this->checkQueryResult(r\expr(array("Iron Man", "Hulk", "Thor", "Spider-Man"))->deleteAt(1,2, array('right_bound' => 'closed')),
       array("Iron Man", "Spider-Man"));*/
     $this->checkQueryResult(r\expr(array("Iron Man", "Bruce", "Spider-Man"))->changeAt(1, "Hulk"), array("Iron Man", "Hulk", "Spider-Man"));
 }
Example #11
0
 public function run()
 {
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => 2)))->filter(r\row()->attr('y')->eq(2))->pluck('x'), array(array('x' => 1)));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => 2)))->filter(r\row('y')->eq(2))->pluck('x'), array(array('x' => 1)));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => 2)))->pluck('x'), array(array('x' => 1)));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => 2)))->pluck(array('x', 'y')), array(array('x' => 1, 'y' => 2)));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => 2)))->without('x'), array(array('y' => 2)));
     $this->checkQueryResult(r\expr(array(array('x' => 1, 'y' => 2)))->without(array('x', 'y')), array(array()));
     $this->checkQueryResult(r\expr(array('x' => 1))->merge(array('y' => 2)), array('x' => 1, 'y' => 2));
     $this->checkQueryResult(r\expr(array('x' => 1))->merge(r\expr(array('y' => 2))), array('x' => 1, 'y' => 2));
     $this->checkQueryResult(r\expr(array(1, 2, 3))->append(4), array(1, 2, 3, 4));
     $this->checkQueryResult(r\expr(array(1, 2, 3))->append(r\expr(4)), array(1, 2, 3, 4));
     $this->checkQueryResult(r\expr(array('x' => 1, 'y' => 2))->contains('x'), true);
     $this->checkQueryResult(r\expr(array('x' => 1, 'y' => 2))->contains('foo'), false);
 }
Example #12
0
 public function run()
 {
     $this->requireDataset('Heroes');
     // FIXME: These checks don't actually verify the ordering (and the ones we specify here as reference are wrong)...
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy(array('combatPower', 'compassionPower'))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy(array(r\Desc('combatPower'), r\Desc('compassionPower')))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy(array(r\Asc('combatPower'), r\Asc('compassionPower')))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->skip(1)->map(r\row('superhero')), array('Spiderman', 'Wolverine'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->limit(1)->map(r\row('superhero')), array('Iron Man'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->nth(1)->attr('superhero'), 'Spiderman');
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->nth(-1)->attr('superhero'), 'Wolverine');
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->slice(1)->map(r\row('superhero')), array('Spiderman', 'Wolverine'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->slice(1, 1)->map(r\row('superhero')), array('Spiderman'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->pluck('superhero')->union(r\expr(array(array('superhero' => 'foo'))))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman', 'foo'));
 }
Example #13
0
 public function run()
 {
     $this->checkQueryResult(r\expr(array(1, 2, 3, 4))->reduce(function ($a, $b) {
         return $a->add($b);
     }), 10.0);
     $this->checkQueryResult(r\expr(array(1, 2, 3, 4))->count(), 4.0);
     $this->checkQueryResult(r\expr(array(1, 2, 3, 4))->count(2), 1.0);
     $this->checkQueryResult(r\expr(array(1, 2, 3, 4))->count(r\row()->lt(3)), 2.0);
     $this->checkQueryResult(r\expr(array(1, 2, 2, 4))->distinct(), array(1, 2, 4));
     $this->checkQueryResult(r\expr(array(1, 2, 2, 4))->group(function ($r) {
         return $r;
     })->map(function ($r) {
         return $r;
     })->reduce(function ($a, $b) {
         return $a->add($b);
     })->ungroup(), array(array('reduction' => 1, 'group' => 1), array('reduction' => 4, 'group' => 2), array('reduction' => 4, 'group' => 4)));
     $this->checkQueryResult(r\expr(array(array('v' => 1), array('v' => 2), array('v' => 2), array('v' => 4)))->group('v')->count()->ungroup(), array(array('reduction' => 1, 'group' => 1), array('reduction' => 2, 'group' => 2), array('reduction' => 1, 'group' => 4)));
     $this->checkQueryResult(r\expr(array(array('v' => 1), array('v' => 2), array('v' => 2), array('v' => 4)))->group('v')->sum('v')->ungroup(), array(array('reduction' => 1, 'group' => 1), array('reduction' => 4, 'group' => 2), array('reduction' => 4, 'group' => 4)));
     $this->checkQueryResult(r\expr(array(array('v' => 1), array('v' => 2), array('v' => 2), array('v' => 4)))->group('v')->avg('v')->ungroup(), array(array('reduction' => 1, 'group' => 1), array('reduction' => 2, 'group' => 2), array('reduction' => 4, 'group' => 4)));
     $this->checkQueryResult(r\expr(array(array('v' => 1, 'x' => 1), array('v' => 2, 'x' => 2), array('v' => 2, 'x' => 3), array('v' => 4, 'x' => 4)))->group(array('v', 'x'))->count()->ungroup(), array(array('reduction' => 1, 'group' => array('v' => 1, 'x' => 1)), array('reduction' => 1, 'group' => array('v' => 2, 'x' => 2)), array('reduction' => 1, 'group' => array('v' => 2, 'x' => 3)), array('reduction' => 1, 'group' => array('v' => 4, 'x' => 4))));
     $this->checkQueryResult(r\expr(array(1, 2, 3))->count(), 3.0);
     $this->checkQueryResult(r\expr(array(1, 2, 3))->sum(), 6.0);
     $this->checkQueryResult(r\expr(array(1, 2, 3))->avg(), 2.0);
     $this->checkQueryResult(r\expr(array(1, 2, 3))->max(), 3.0);
     $this->checkQueryResult(r\expr(array(1, 2, 3))->min(), 1.0);
     $this->checkQueryResult(r\expr(array(array('v' => 1), array('v' => 2), array('v' => 3)))->sum('v'), 6.0);
     $this->checkQueryResult(r\expr(array(array('v' => 1), array('v' => 2), array('v' => 3)))->avg('v'), 2.0);
     $this->checkQueryResult(r\expr(array(array('v' => 1), array('v' => 2), array('v' => 3)))->max('v'), array('v' => 3.0));
     $this->checkQueryResult(r\expr(array(array('v' => 1), array('v' => 2), array('v' => 3)))->min('v'), array('v' => 1.0));
     $this->checkQueryResult(r\expr(array('a', 'b', 'c'))->contains('a'), true);
     $this->checkQueryResult(r\expr(array('a', 'b', 'c'))->contains('z'), false);
     $this->checkQueryResult(r\expr(array('a', 'b', 'c'))->contains(r\row()->eq('a')), true);
     $this->checkQueryResult(r\expr(array('a', 'b', 'c'))->contains(r\row()->eq('z')), false);
     $this->checkQueryResult(r\expr(array('a', 'b', 'c'))->contains(function ($x) {
         return $x->eq('a');
     }), true);
     $this->checkQueryResult(r\expr(array('a', 'b', 'c'))->contains(function ($x) {
         return $x->eq('z');
     }), false);
     $this->requireDataset('Heroes');
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->indexCreate('combatPower'), array('created' => 1.0));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->indexWait('combatPower')->pluck(array('index', 'ready')), array(array('index' => 'combatPower', 'ready' => true)));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->distinct(array('index' => 'combatPower')), array(2.0, 5.0));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->max(array('index' => 'combatPower'))->getField("combatPower"), 5.0);
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->min(array('index' => 'combatPower'))->getField("combatPower"), 2.0);
     $this->datasets['Heroes']->reset();
 }
Example #14
0
 public function run()
 {
     $this->requireDataset('Control');
     $this->requireDataset('Heroes');
     $this->checkQueryResult(r\rDo(array(1, 2, 3), function ($x, $y, $z) {
         return $x->mul($y->add($z));
     }), 5.0);
     $this->checkQueryResult(r\branch(r\expr(true), r\expr('true'), r\expr('false')), 'true');
     $this->checkQueryResult(r\branch(r\expr(false), r\expr('true'), r\expr('false')), 'false');
     $this->checkQueryResult(r\expr(array(1, 2, 3))->rForeach(function ($x) {
         return r\db('Control')->table('t1')->insert(array('x' => $x));
     })->getField('inserted'), 3.0);
     $this->checkQueryResult(r\db('Control')->table('t1')->map(r\row('x')), array(1, 2, 3));
     $errorCaught = false;
     try {
         r\error('ERRRRRR')->run($this->conn);
     } catch (r\RqlServerError $e) {
         $errorCaught = true;
     }
     if (!$errorCaught) {
         echo "rrror() did not throw an error.\n";
     }
     // Js is also tested separately in JsTest
     $this->checkQueryResult(r\js("'str1' + 'str2'"), 'str1str2');
     $this->checkQueryResult(r\expr('5.0')->coerceTo('number'), 5.0);
     $this->checkQueryResult(r\expr(5.0)->coerceTo('string'), '5');
     $this->checkQueryResult(r\expr(5.0)->typeOf(), 'NUMBER');
     $this->checkQueryResult(r\expr('foo')->typeOf(), 'STRING');
     $this->checkQueryResult(r\expr(null)->typeOf(), 'NULL');
     $this->checkQueryResult(r\expr(array(1, 2, 3))->typeOf(), 'ARRAY');
     $this->checkQueryResult(r\expr(array('x' => 1))->typeOf(), 'OBJECT');
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->info()->pluck(array('type', 'name')), array('type' => "TABLE", 'name' => 'marvel'));
     $this->checkQueryResult(r\expr(array('a' => 4))->getField('a')->rDefault(5), 4.0);
     $this->checkQueryResult(r\expr(array('a' => 4))->getField('b')->rDefault(5), 5.0);
     $this->checkQueryResult(r\expr(array('a' => 4))->getField('b')->rDefault(function ($e) {
         return r\expr(5);
     }), 5.0);
     $this->checkQueryResult(r\range()->limit(3), array(0, 1, 2));
     $this->checkQueryResult(r\range(3), array(0, 1, 2));
     $this->checkQueryResult(r\range(1, 3), array(1, 2));
     $this->checkQueryResult(r\expr("123")->toJsonString(), "\"123\"");
     $this->datasets['Control']->reset();
 }
Example #15
0
 public function run()
 {
     $this->checkQueryResult(r\geojson(array('type' => 'Point', 'coordinates' => array(0.0, 1.0))), array('$reql_type$' => 'GEOMETRY', 'type' => 'Point', 'coordinates' => array(0.0, 1.0)));
     $this->checkQueryResult(r\expr(array('$reql_type$' => 'GEOMETRY', 'type' => 'Point', 'coordinates' => array(0.0, 1.0)))->toGeojson(), array('type' => 'Point', 'coordinates' => array(0.0, 1.0)));
     $this->checkQueryResult(r\point(0.0, 1.0), array('$reql_type$' => 'GEOMETRY', 'type' => 'Point', 'coordinates' => array(0.0, 1.0)));
     $this->checkQueryResult(r\line(array(array(0.0, 0.0), array(0.0, 1.0), array(1.0, 1.0))), array('$reql_type$' => 'GEOMETRY', 'type' => 'LineString', 'coordinates' => array(array(0.0, 0.0), array(0.0, 1.0), array(1.0, 1.0))));
     $this->checkQueryResult(r\line(array(array(0.0, 0.0), array(0.0, 1.0), array(1.0, 1.0)))->fill(), array('$reql_type$' => 'GEOMETRY', 'type' => 'Polygon', 'coordinates' => array(array(array(0.0, 0.0), array(0.0, 1.0), array(1.0, 1.0), array(0.0, 0.0)))));
     $this->checkQueryResult(r\polygon(array(array(0.0, 0.0), array(0.0, 1.0), array(1.0, 1.0))), array('$reql_type$' => 'GEOMETRY', 'type' => 'Polygon', 'coordinates' => array(array(array(0.0, 0.0), array(0.0, 1.0), array(1.0, 1.0), array(0.0, 0.0)))));
     $this->checkQueryResult(r\polygon(array(array(0.0, 0.0), array(0.0, 2.0), array(2.0, 2.0), array(2.0, 0.0)))->polygonSub(r\polygon(array(array(0.5, 0.5), array(0.5, 0.8), array(0.8, 0.8)))), array('$reql_type$' => 'GEOMETRY', 'type' => 'Polygon', 'coordinates' => array(array(array(0.0, 0.0), array(0.0, 2.0), array(2.0, 2.0), array(2.0, 0.0), array(0.0, 0.0)), array(array(0.5, 0.5), array(0.5, 0.8), array(0.8, 0.8), array(0.5, 0.5)))));
     // These might fail due to rounding issues, depending on the server's architecture
     $this->checkQueryResult(r\point(0.0, 1.0)->distance(r\point(1.0, 1.0))->coerceTo("STRING"), "111302.64933943081996");
     $this->checkQueryResult(r\point(0.0, 1.0)->distance(r\point(1.0, 1.0), array("unit" => "km"))->coerceTo("STRING"), "111.30264933943082895");
     $this->checkQueryResult(r\polygon(array(array(0.0, 0.0), array(0.0, 1.0), array(1.0, 1.0)))->intersects(r\line(array(array(0.0, 0.0), array(2.0, 2.0)))), true);
     $this->checkQueryResult(r\polygon(array(array(0.0, 0.0), array(0.0, 1.0), array(1.0, 1.0)))->includes(r\line(array(array(0.0, 0.0), array(2.0, 2.0)))), false);
     $this->checkQueryResult(r\circle(r\point(0.0, 0.0), 10.0)->intersects(r\line(array(array(0.1, 0.0), array(2.0, 2.0)))), false);
     $this->checkQueryResult(r\circle(r\point(0.0, 0.0), 10.0, array("unit" => "mi"))->intersects(r\line(array(array(0.1, 0.0), array(2.0, 2.0)))), true);
     $this->requireDataset('Geo');
     $this->checkQueryResult(r\db('Geo')->table('geo')->getIntersecting(r\circle(r\point(0.0, 0.0), 150.0, array('unit' => "km")), array('index' => 'geo'))->count(), 1.0);
     $this->checkQueryResult(r\db('Geo')->table('geo')->getIntersecting(r\circle(r\point(0.0, 0.0), 150.0, array('unit' => "km")), array('index' => 'mgeo'))->count(), 1.0);
     // Again, there might be rounding issues on some servers
     $this->checkQueryResult(r\db('Geo')->table('geo')->getNearest(r\point(0.0, 0.0), array('max_dist' => 200.0, 'unit' => 'km', 'index' => 'geo'))->map(r\row('dist')->coerceTo('STRING')), array('111.3194907932735731', '156.89956829134027316'));
 }
Example #16
0
 public function run()
 {
     $this->requireDataset('Heroes');
     // FIXME: These checks don't actually verify the ordering (and the ones we specify here as reference are wrong)...
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy(array('combatPower', 'compassionPower'))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy(array(r\Desc('combatPower'), r\Desc('compassionPower')))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy(array(r\Asc('combatPower'), r\Asc('compassionPower')))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy(array(r\row('combatPower'), r\row('compassionPower')))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy(array(r\Asc(r\row('combatPower')), r\Desc(r\row('compassionPower'))))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy(array(function ($x) {
         return $x('combatPower');
     }, function ($x) {
         return $x('compassionPower');
     }))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy(array(r\Asc(function ($x) {
         return $x('combatPower');
     }), r\Desc(function ($x) {
         return $x('compassionPower');
     })))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->skip(1)->map(r\row('superhero')), array('Spiderman', 'Wolverine'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->limit(1)->map(r\row('superhero')), array('Iron Man'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->nth(1)->getField('superhero'), 'Spiderman');
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->nth(-1)->getField('superhero'), 'Wolverine');
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->slice(1)->map(r\row('superhero')), array('Spiderman', 'Wolverine'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->slice(1, 1)->map(r\row('superhero')), array());
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->slice(1, 2)->map(r\row('superhero')), array('Spiderman'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->orderBy('superhero')->slice(1, 1, array('right_bound' => 'closed'))->map(r\row('superhero')), array('Spiderman'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->pluck('superhero')->union(r\expr(array(array('superhero' => 'foo'))))->map(r\row('superhero')), array('Iron Man', 'Wolverine', 'Spiderman', 'foo'));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->withFields(array('superhero', 'nemesis'))->count(), 0.0);
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->withFields('superhero')->count(), 3.0);
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->withFields(array('superhero' => true))->count(), 3.0);
     $this->checkQueryResult(r\expr(array('a', 'b', 'c'))->offsetsOf('c'), array(2));
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->isEmpty(), false);
     $this->checkQueryResult(r\expr(new r\ArrayDatum(array()))->isEmpty(), true);
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->sample(1)->count(), 1.0);
     $this->checkQueryResult(r\db('Heroes')->table('marvel')->sample(3)->count(), 3.0);
 }
Example #17
0
 public function testFilterMissingViaExpr()
 {
     $res = $this->db()->table('marvel')->filter(\r\row('foo')->eq('naaa'), \r\expr('true'))->count()->run($this->conn);
     $this->assertEquals(3.0, $res);
 }
Example #18
0
 public function testFilterPluck()
 {
     $res = \r\expr(array(array('x' => 1, 'y' => 2)))->filter(\r\row('y')->eq(2))->pluck('x')->run($this->conn);
     $this->assertEquals(array(array('x' => 1)), $this->toArray($res));
 }
Example #19
0
 public function testTablegetNearest()
 {
     $res = $this->db()->table('geo')->getNearest(\r\point(0.0, 0.0), array('max_dist' => 200.0, 'unit' => 'km', 'index' => 'geo'))->map(\r\row('dist')->coerceTo('STRING'))->run($this->conn);
     $this->assertContains('111319490793273', str_replace('.', '', $res[0]));
     $this->assertContains('156899568291340', str_replace('.', '', $res[1]));
 }
Example #20
0
 public function testContainsZRow()
 {
     $this->assertFalse(\r\expr(array('a', 'b', 'c'))->contains(\r\row()->eq('z'))->run($this->conn));
 }
Example #21
0
 public function testCoerceToMap()
 {
     $res = \r\expr(array($this->db()->table('marvel')->coerceTo('array'), $this->db()->table('marvel')->coerceTo('array')))->concatMap(\r\row()->pluck('superhero'))->map(\r\row('superhero'))->run($this->conn);
     $this->assertEquals(array('Spiderman', 'Wolverine', 'Iron Man', 'Spiderman', 'Wolverine', 'Iron Man'), (array) $res);
 }
Example #22
0
 private function db_filter($key, $value, $operator)
 {
     $this->db_connect();
     $start = microtime(true);
     if ($operator == '=') {
         $result = r\db($this->db)->table($this->table)->filter(r\row($key)->eq($value))->run($this->conn);
     } elseif ($operator == '<') {
         $result = r\db($this->db)->table($this->table)->filter(r\row($key)->lt($value))->run($this->conn);
     } elseif ($operator == '>') {
         $result = r\db($this->db)->table($this->table)->filter(r\row($key)->gt($value))->run($this->conn);
     }
     $delta = microtime(true) - $start;
     $this->output(sprintf("\\Filter ({$key} {$operator} {$value}) finished after %0.2f secs:\r\n", $delta));
     print_r($result, true);
     $this->db_close();
 }
 public function testPluckUnion()
 {
     $res = $this->db()->table('marvel')->pluck('superhero')->union(\r\expr(array(array('superhero' => 'foo'))))->map(\r\row('superhero'))->run($this->conn);
     $this->assertInstanceOf('\\r\\Cursor', $res);
     $this->assertEquals(array('foo', 'Spiderman', 'Wolverine', 'Iron Man'), $res->toArray());
 }
Example #24
0
 public function testUpdateRowAdd()
 {
     $this->db()->table('marvel')->update(array('age' => 30))->run($this->conn, $this->opts);
     $res = $this->db()->table('marvel')->update(array('age' => \r\row('age')->add(\r\js('1'))))->run($this->conn, array('durability' => 'soft', 'non_atomic' => true));
     $this->assertObStatus(array('replaced' => 3), $res);
 }
Example #25
0
 public function testIndexRow()
 {
     $res = $this->db()->table('t1')->indexCreate('bfun', \r\row('p'))->run($this->conn);
     $this->assertEquals(array('created' => 1.0), (array) $res);
 }