Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14098 anikendra 1
<?php
2
/**
3
 * Tests specific to the sql compatible behavior
4
 *
5
 * PHP version 5
6
 *
7
 * Copyright (c) 2010, Andy Dawson
8
 *
9
 * Licensed under The MIT License
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @filesource
13
 * @copyright     Copyright (c) 2010, Andy Dawson
14
 * @link          www.ad7six.com
15
 * @package       mongodb
16
 * @subpackage    mongodb.tests.cases.behaviors
17
 * @since         v 1.0 (14-Dec-2010)
18
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
19
 */
20
 
21
 
22
App::uses('Model', 'Model');
23
App::uses('AppModel', 'Model');
24
 
25
 
26
/**
27
 * SqlCompatiblePost class
28
 *
29
 * @uses          Post
30
 * @package       mongodb
31
 * @subpackage    mongodb.tests.cases.behaviors
32
 */
33
class SqlCompatiblePost extends AppModel {
34
 
35
/**
36
 * useDbConfig property
37
 *
38
 * @var string 'test_mongo'
39
 * @access public
40
 */
41
	public $useDbConfig = 'test_mongo';
42
 
43
/**
44
 * actsAs property
45
 *
46
 * @var array
47
 * @access public
48
 */
49
	public $actsAs = array(
50
		'Mongodb.SqlCompatible'
51
	);
52
 
53
	public $lastQuery = array();
54
 
55
	public function beforeFind($query) {
56
		$this->lastQuery = $query;
57
		return $query;
58
	}
59
}
60
 
61
/**
62
 * SqlCompatibleTest class
63
 *
64
 * @uses          CakeTestCase
65
 * @package       mongodb
66
 * @subpackage    mongodb.tests.cases.behaviors
67
 */
68
class SqlCompatibleTest extends CakeTestCase {
69
 
70
/**
71
 * Default db config. overriden by test db connection if present
72
 *
73
 * @var array
74
 * @access protected
75
 */
76
	protected $_config = array(
77
		'datasource' => 'Mongodb.MongodbSource',
78
		'host' => 'localhost',
79
		'login' => '',
80
		'password' => '',
81
		'database' => 'test_mongo',
82
		'port' => 27017,
83
		'prefix' => '',
84
		'persistent' => false,
85
	);
86
 
87
	public function setUp() {
88
		$connections = ConnectionManager::enumConnectionObjects();
89
 
90
		if (!empty($connections['test']['classname']) && $connections['test']['classname'] === 'mongodbSource') {
91
			$config = new DATABASE_CONFIG();
92
			$this->_config = $config->test;
93
		}
94
 
95
		if(!isset($connections['test_mongo'])) {
96
			ConnectionManager::create('test_mongo', $this->_config);
97
			$this->Mongo = new MongodbSource($this->_config);
98
		}
99
 
100
		$this->Post = ClassRegistry::init(array('class' => 'SqlCompatiblePost', 'alias' => 'Post', 'ds' => 'test_mongo'), true);
101
	}
102
 
103
/**
104
 * Sets up the environment for each test method
105
 *
106
 * @return void
107
 * @access public
108
 */
109
	public function startTest($method) {
110
		$this->_setupData();
111
	}
112
 
113
/**
114
 * Destroys the environment after each test method is run
115
 *
116
 * @return void
117
 * @access public
118
 */
119
	public function endTest($method) {
120
		$this->Post->deleteAll(true);
121
	}
122
 
123
	public function tearDown() {
124
		unset($this->Post);
125
		ClassRegistry::flush();
126
	}
127
 
128
/**
129
 * testNOT method
130
 *
131
 * @return void
132
 * @access public
133
 */
134
	public function testNOT() {
135
		$expected = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
136
		$result = $this->Post->find('all', array(
137
			'conditions' => array(
138
				'title !=' => 10,
139
			),
140
			'fields' => array('_id', 'title', 'number'),
141
			'order' => array('number' => 'ASC')
142
		));
143
 
144
		$result = Hash::extract($result, '{n}.Post.title');
145
		$this->assertEqual($expected, $result);
146
 
147
		$conditions = array(
148
			'title' => array('$ne' => 10)
149
		);
150
		$this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
151
 
152
		$result = $this->Post->find('all', array(
153
			'conditions' => array(
154
				'NOT' => array(
155
					'title' => 10
156
				),
157
			),
158
			'fields' => array('_id', 'title', 'number'),
159
			'order' => array('number' => 'ASC')
160
		));
161
		$result = Hash::extract($result, '{n}.Post.title');
162
		$this->assertEqual($expected, $result);
163
 
164
		$conditions = array(
165
			'title' => array('$nin' => array(10))
166
		);
167
		$this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
168
	}
169
 
170
/**
171
 * testNOTIN method
172
 *
173
 * @return void
174
 * @access public
175
 */
176
	public function testNOTIN() {
177
		$expected = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
178
		$result = $this->Post->find('all', array(
179
			'conditions' => array(
180
				'title NOT IN' => array(10),
181
			),
182
			'fields' => array('_id', 'title', 'number'),
183
			'order' => array('number' => 'ASC')
184
		));
185
		$result = Hash::extract($result, '{n}.Post.title');
186
		$this->assertEqual($expected, $result);
187
 
188
		$conditions = array(
189
			'title' => array('$nin' => array(10))
190
		);
191
		$this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
192
	}
193
 
194
/**
195
 * testGTLT method
196
 *
197
 * @return void
198
 * @access public
199
 */
200
	public function testGTLT() {
201
		$expected = array(8, 9, 10, 11, 12, 13);
202
		$result = $this->Post->find('all', array(
203
			'conditions' => array(
204
				'title >' => 7,
205
				'title <' => 14,
206
			),
207
			'fields' => array('_id', 'title', 'number'),
208
			'order' => array('number' => 'ASC')
209
		));
210
		$result = Hash::extract($result, '{n}.Post.title');
211
		$this->assertEqual($expected, $result);
212
 
213
		$conditions = array(
214
			'title' => array(
215
				'$gt' => 7,
216
				'$lt' => 14
217
			)
218
		);
219
		$this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
220
	}
221
 
222
/**
223
 * testGTE method
224
 *
225
 * @return void
226
 * @access public
227
 */
228
	public function testGTE() {
229
		$expected = array(19, 20);
230
		$result = $this->Post->find('all', array(
231
			'conditions' => array(
232
				'title >=' => 19,
233
			),
234
			'fields' => array('_id', 'title', 'number'),
235
			'order' => array('number' => 'ASC')
236
		));
237
		$result = Hash::extract($result, '{n}.Post.title');
238
		$this->assertEqual($expected, $result);
239
 
240
		$conditions = array(
241
			'title' => array('$gte' => 19)
242
		);
243
		$this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
244
	}
245
 
246
/**
247
 * testOR method
248
 *
249
 * @return void
250
 * @access public
251
 */
252
	public function testOR() {
253
		$expected = array(1, 2, 19, 20);
254
		$result = $this->Post->find('all', array(
255
			'conditions' => array(
256
				'OR' => array(
257
					'title <=' => 2,
258
					'title >=' => 19,
259
				)
260
			),
261
			'fields' => array('_id', 'title', 'number'),
262
			'order' => array('number' => 'ASC')
263
		));
264
		$result = Hash::extract($result, '{n}.Post.title');
265
		$this->assertEqual($expected, $result);
266
 
267
		$conditions = array(
268
			'$or' => array(
269
				array('title' => array('$lte' => 2)),
270
				array('title' => array('$gte' => 19))
271
			)
272
		);
273
		$this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
274
	}
275
 
276
 
277
	/**
278
	 * Tests find method with conditions _id=>array()
279
	 *
280
	 * @return void
281
	 * @access public
282
	 */
283
	public function testFindConditionIn() {
284
 
285
		for ($i = 1; $i <= 5; $i++) {
286
			$data = array(
287
					'_id' => 'A1' . $i,
288
					'title' => $i,
289
					);
290
			$saveData['Post'] = $data;
291
			$this->Post->create();
292
			$this->Post->save($saveData);
293
		}
294
 
295
		$params = array('conditions' => array('_id' => array('A11', 'A12')));
296
		$result = $this->Post->find('all', $params);
297
 
298
		$expected = array('A11','A12');
299
		$result = Hash::extract($result, '{n}.Post._id');
300
		$this->assertEqual($expected, $result);
301
		$this->assertEqual(2, count($result));
302
 
303
		$conditions = array(
304
			'_id' => array(
305
				'$in' => array('A11', 'A12')
306
			)
307
		);
308
		$this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
309
 
310
 
311
		$params = array('conditions' => array('_id' => array('$nin' => array('A11', 'A12'))));
312
		$result = $this->Post->find('all', $params);
313
		//$expected = array('A13','A14');
314
		$result = Hash::extract($result, '{n}.Post._id');
315
		$this->assertTrue(in_array('A13', $result));
316
		$this->assertFalse(in_array('A11', $result));
317
		$this->assertFalse(in_array('A12', $result));
318
		$this->assertEqual(23, count($result));
319
 
320
 
321
		$conditions = array(
322
			'_id' => array(
323
				'$nin' => array('A11', 'A12')
324
			)
325
		);
326
		$this->assertEqual($conditions, $this->Post->lastQuery['conditions']);
327
	}
328
 
329
 
330
/**
331
 * Order method
332
 *
333
 * @return void
334
 * @access public
335
 */
336
	public function testOrderDESC() {
337
		$expected = array(20, 19);
338
		$result = $this->Post->find('all', array(
339
			'conditions' => array('title >' => 18),
340
			'fields' => array('_id', 'title'),
341
			'order' => array('title DESC')
342
		));
343
		$result = Hash::extract($result, '{n}.Post.title');
344
		$this->assertEqual($expected, $result);
345
 
346
		$order = array(array('title' => 'DESC'));
347
		$this->assertEqual($order, $this->Post->lastQuery['order']);
348
	}
349
 
350
/**
351
 * Order method
352
 *
353
 * @return void
354
 * @access public
355
 */
356
	public function testOrderASC() {
357
		$expected = array(19, 20);
358
		$result = $this->Post->find('all', array(
359
			'conditions' => array('title >' => 18),
360
			'fields' => array('_id', 'title'),
361
			'order' => array('title ASC')
362
		));
363
		$result = Hash::extract($result, '{n}.Post.title');
364
		$this->assertEqual($expected, $result);
365
 
366
		$order = array(array('title' => 'ASC'));
367
		$this->assertEqual($order, $this->Post->lastQuery['order']);
368
	}
369
 
370
 
371
/**
372
 * Order method with model alias
373
 *
374
 * @return void
375
 * @access public
376
 */
377
	public function testOrderWithModelAlias() {
378
		$expected = array(20, 19);
379
		$result = $this->Post->find('all', array(
380
			'conditions' => array('title >' => 18),
381
			'fields' => array('_id', 'title'),
382
			'order' => array('Post.title DESC')
383
		));
384
		$result = Hash::extract($result, '{n}.Post.title');
385
		$this->assertEqual($expected, $result);
386
	}
387
 
388
/**
389
 * Convert MongoDate objects to strings
390
 *
391
 * @return void
392
 * @access public
393
 */
394
	public function testConvertDates() {
395
		$expected = '2011-Nov-22 00:00:00';
396
		$data = array('title' => 'date', 'created_at' => new MongoDate(strtotime('2011-11-22 00:00:00')));
397
		$this->Post->save($data);
398
		$result = $this->Post->read();
399
		$this->assertEqual($expected, $result['Post']['created_at']);
400
	}
401
 
402
/**
403
 * Convert MongoDate objects to another format strings
404
 *
405
 * @return void
406
 * @access public
407
 */
408
	public function testConvertDatesAnotherFormat() {
409
		$this->Post->Behaviors->detach('SqlCompatible');
410
		$this->Post->Behaviors->attach('Mongodb.SqlCompatible', array('dateFormat' => 'Y-m-d H:i:s'));
411
 
412
		$expected = '2011-11-22 00:00:00';
413
		$data = array('title' => 'date', 'created_at' => new MongoDate(strtotime('2011-11-22 00:00:00')));
414
		$this->Post->save($data);
415
		$result = $this->Post->read();
416
		$this->assertEqual($expected, $result['Post']['created_at']);
417
	}
418
 
419
/**
420
 * setupData method
421
 *
422
 * @return void
423
 * @access protected
424
 */
425
	protected function _setupData() {
426
		$this->Post->deleteAll(true, false);
427
		$this->Post->primaryKey = '_id';
428
 
429
		for ($i = 1; $i <= 20; $i++) {
430
			$data = array(
431
				'title' => $i,
432
			);
433
			$saveData['Post'] = $data;
434
			$this->Post->create();
435
			$this->Post->save($saveData);
436
		}
437
	}
438
}