- Added ability to delete multiple rows with a given criteria (with where()); and...
[activemongo.git] / tests / QueryTest.php
blob9ce916d21a9d8a5b2fbcfbbe7c2522acdf08d265
1 <?php
3 class QueryTest extends PHPUnit_Framework_TestCase
6 function __construct()
8 Model3::drop();
9 $c = new Model3;
10 for ($i=0; $i < 5000; $i++) {
11 $c->reset();
12 $c->int = $i;
13 $c->str = sha1(uniqid());
14 $c->save(false);
16 $c->reset();
17 $this->assertEquals($c->count(), 5000);
20 function testQuery()
22 $c = new Model1;
24 /* rand values */
25 $val1 = rand(1, 50);
26 $val2 = rand(1, 50);
27 $val3 = rand(1, 50);
28 $val4 = rand(1, 50);
29 $val5 = rand(1, 50);
32 /* prepare the query */
33 $c->properties('a,b')->where('a >', $val1)->where('b <', $val2)->where('c !=', $val3);
34 $c->where('h regexp', '/[a-f0-9]+/');
35 $c->where('x in', array(1, 2));
36 $c->where('x nin', array(4));
37 $c->where('y ==', array(4));
38 $c->sort('c DESC, a ASC')->limit($val4, $val5);
40 /* perform it */
41 $c->doQuery();
43 /* Get cursor info */
44 $sQuery = $c->getReference(true);
46 /* expected cursor info */
47 $eQuery = array(
48 'ns' => DB.'.model1',
49 'limit' => $val4,
50 'skip' => $val5,
51 'query' => array(
52 '$query' => array(
53 'a' => array('$gt' => $val1),
54 'b' => array('$lt' => $val2),
55 'c' => array('$ne' => $val3),
56 'h' => new MongoRegex('/[a-f0-9]+/'),
57 'x' => array('$in' => array(1,2), '$nin' => array(4)),
58 'y' => array('$all' => array(4)),
60 '$orderby' => array(
61 'c' => -1,
62 'a' => 1,
65 'fields' => array(
66 'a' => 1,
67 'b' => 1,
71 $this->assertEquals($sQuery['dynamic'], $eQuery);
75 function testQueryArray()
77 $c = new Model1;
79 /* rand values */
80 $val1 = rand(1, 50);
81 $val2 = rand(1, 50);
82 $val3 = rand(1, 50);
83 $val4 = rand(1, 50);
84 $val5 = rand(1, 50);
87 /* prepare the query */
88 $filter = array(
89 'a > ' => $val1,
90 'b < ' => $val2,
91 'c != ' => $val3,
92 'h regexp' => '/[a-f0-9]+/',
93 'x in ' => array(1,2),
94 'x nin ' => array(4),
95 'y == ' => array(4)
97 $c->properties('a,b')->sort('c DESC, a ASC')->limit($val4, $val5);;
98 $c->where($filter);
100 /* perform it */
101 $c->doQuery();
103 /* Get cursor info */
104 $sQuery = $c->getReference(true);
106 /* expected cursor info */
107 $eQuery = array(
108 'ns' => DB.'.model1',
109 'limit' => $val4,
110 'skip' => $val5,
111 'query' => array(
112 '$query' => array(
113 'a' => array('$gt' => $val1),
114 'b' => array('$lt' => $val2),
115 'c' => array('$ne' => $val3),
116 'h' => new MongoRegex('/[a-f0-9]+/'),
117 'x' => array('$in' => array(1,2), '$nin' => array(4)),
118 'y' => array('$all' => array(4)),
120 '$orderby' => array(
121 'c' => -1,
122 'a' => 1,
125 'fields' => array(
126 'a' => 1,
127 'b' => 1,
131 $this->assertEquals($sQuery['dynamic'], $eQuery);
134 function testQueryRequireArray()
136 $c = new Model1;
137 try {
138 $c->where('c near', 'string');
139 $this->assertTrue(false);
140 } catch (ActiveMongo_Exception $e) {
141 $this->assertTrue(true);
143 try {
144 $c->where('c in', 55);
145 $this->assertTrue(false);
146 } catch (ActiveMongo_Exception $e) {
147 $this->assertTrue(true);
149 try {
150 $c->where('c nin', 559);
151 $this->assertTrue(false);
152 } catch (ActiveMongo_Exception $e) {
153 $this->assertTrue(true);
157 function testMultipleOperationsPerProperty()
159 list($min, $max) = array(50, 100);
161 $c = new Model3;
162 foreach ($c->where('int >', $min)->where('int <', $max) as $item) {
163 $this->assertGreaterThan($min, $item['int']);
164 $this->assertLessThan($max, $item['int']);
167 /* this could be done with a single regexp but
168 * this test should cover the multiple ALL amoung
169 * properties
171 * str regexp '//' AND str regexp '//' AND str regexp '//'
173 $c = new Model3;
174 $c->where('str regex', '/^4/')->where('str regexp', '/a$/');
175 foreach ($c->where('str regex', '/[a-z0-9]+/') as $item) {
176 $this->assertEquals($item['str'][0], 4);
177 $this->assertEquals($item['str'][strlen($item['str'])-1], 'a');
181 $c = new Model3;
182 $c->where('int >', $min)->where('int <', $max);
183 foreach ($c->where('int nin', array($min+1, $min+2, $min+3)) as $item) {
184 $this->assertNotEquals($min+1, $item['int']);
185 $this->assertNotEquals($min+2, $item['int']);
186 $this->assertNotEquals($min+3, $item['int']);
190 function testMultipleUpdate()
192 $str = sha1(uniqid());
193 $query = array(
194 'int >' => 5,
195 'int <' => 30,
196 'int !=' => 6,
198 $c = new Model3;
199 $c->where($query);
200 $c->update(array('newproperty' => $str));
202 $c->where(array('int >' => 5, 'int <' => 30));
203 foreach ($c as $item) {
204 if ($item['int'] == 6) {
205 /* 6 is not included */
206 $this->assertFalse(isset($item['newproperty']));
207 } else {
208 $this->assertEquals($str, $item['newproperty']);
213 function testOnQueryModifyError()
215 try {
216 $c = new Model1;
217 $c->where('a', 1);
218 $c->doQuery();
219 $c->where('b', 4);
220 $this->assertTrue(false);
221 } catch (ActiveMongo_Exception $e) {
222 $this->assertTrue(true);
226 function testClone()
228 $c = new Model1;
229 $c->a = 1;
230 $c->save();
232 $c->reset();
233 $this->assertLessThan($c->count(), 1);
234 foreach ($c as $item) {
235 $item_cloned = clone $item;
236 $item_cloned->c = 1;
237 $item_cloned->save();
238 try {
239 /* iterations are forbidden in cloned objects */
240 foreach ($item_cloned as $nitem) {
241 $this->assertTrue(false);
243 } catch (Exception $e) {
244 $this->assertTrue(true);
249 function testDelete()
251 $c = new Model3;
252 $c->where('int < ', 100);
253 $c->delete();
255 $this->assertEquals($c->count(), 4900);