Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * DboPostgresTest file
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
12
 * @link          http://cakephp.org CakePHP(tm) Project
13
 * @package       Cake.Test.Case.Model.Datasource.Database
14
 * @since         CakePHP(tm) v 1.2.0
15
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
16
 */
17
 
18
App::uses('Model', 'Model');
19
App::uses('AppModel', 'Model');
20
App::uses('Postgres', 'Model/Datasource/Database');
21
 
22
require_once dirname(dirname(dirname(__FILE__))) . DS . 'models.php';
23
 
24
/**
25
 * DboPostgresTestDb class
26
 *
27
 * @package       Cake.Test.Case.Model.Datasource.Database
28
 */
29
class DboPostgresTestDb extends Postgres {
30
 
31
/**
32
 * simulated property
33
 *
34
 * @var array
35
 */
36
	public $simulated = array();
37
 
38
/**
39
 * execute method
40
 *
41
 * @param mixed $sql
42
 * @return void
43
 */
44
	protected function _execute($sql, $params = array(), $prepareOptions = array()) {
45
		$this->simulated[] = $sql;
46
		return null;
47
	}
48
 
49
/**
50
 * getLastQuery method
51
 *
52
 * @return void
53
 */
54
	public function getLastQuery() {
55
		return $this->simulated[count($this->simulated) - 1];
56
	}
57
 
58
}
59
 
60
/**
61
 * PostgresTestModel class
62
 *
63
 * @package       Cake.Test.Case.Model.Datasource.Database
64
 */
65
class PostgresTestModel extends Model {
66
 
67
/**
68
 * useTable property
69
 *
70
 * @var bool
71
 */
72
	public $useTable = false;
73
 
74
/**
75
 * belongsTo property
76
 *
77
 * @var array
78
 */
79
	public $belongsTo = array(
80
		'PostgresClientTestModel' => array(
81
			'foreignKey' => 'client_id'
82
		)
83
	);
84
 
85
/**
86
 * find method
87
 *
88
 * @param mixed $conditions
89
 * @param mixed $fields
90
 * @param mixed $order
91
 * @param mixed $recursive
92
 * @return void
93
 */
94
	public function find($conditions = null, $fields = null, $order = null, $recursive = null) {
95
		return $conditions;
96
	}
97
 
98
/**
99
 * findAll method
100
 *
101
 * @param mixed $conditions
102
 * @param mixed $fields
103
 * @param mixed $order
104
 * @param mixed $recursive
105
 * @return void
106
 */
107
	public function findAll($conditions = null, $fields = null, $order = null, $recursive = null) {
108
		return $conditions;
109
	}
110
 
111
/**
112
 * schema method
113
 *
114
 * @return void
115
 */
116
	public function schema($field = false) {
117
		return array(
118
			'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
119
			'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'),
120
			'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
121
			'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
122
			'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
123
			'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
124
			'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
125
			'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
126
			'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
127
			'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
128
			'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
129
			'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
130
			'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
131
			'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
132
			'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''),
133
			'last_login' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
134
			'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
135
			'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
136
		);
137
	}
138
 
139
}
140
 
141
/**
142
 * PostgresClientTestModel class
143
 *
144
 * @package       Cake.Test.Case.Model.Datasource.Database
145
 */
146
class PostgresClientTestModel extends Model {
147
 
148
/**
149
 * useTable property
150
 *
151
 * @var bool
152
 */
153
	public $useTable = false;
154
 
155
/**
156
 * schema method
157
 *
158
 * @return void
159
 */
160
	public function schema($field = false) {
161
		return array(
162
			'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary'),
163
			'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
164
			'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
165
			'created' => array('type' => 'datetime', 'null' => true, 'default' => null, 'length' => ''),
166
			'updated' => array('type' => 'datetime', 'null' => true, 'default' => null, 'length' => null)
167
		);
168
	}
169
 
170
}
171
 
172
/**
173
 * PostgresTest class
174
 *
175
 * @package       Cake.Test.Case.Model.Datasource.Database
176
 */
177
class PostgresTest extends CakeTestCase {
178
 
179
/**
180
 * Do not automatically load fixtures for each test, they will be loaded manually
181
 * using CakeTestCase::loadFixtures
182
 *
183
 * @var bool
184
 */
185
	public $autoFixtures = false;
186
 
187
/**
188
 * Fixtures
189
 *
190
 * @var object
191
 */
192
	public $fixtures = array('core.user', 'core.binary_test', 'core.comment', 'core.article',
193
		'core.tag', 'core.articles_tag', 'core.attachment', 'core.person', 'core.post', 'core.author',
194
		'core.datatype',
195
	);
196
 
197
/**
198
 * Actual DB connection used in testing
199
 *
200
 * @var DboSource
201
 */
202
	public $Dbo = null;
203
 
204
/**
205
 * Simulated DB connection used in testing
206
 *
207
 * @var DboSource
208
 */
209
	public $Dbo2 = null;
210
 
211
/**
212
 * Sets up a Dbo class instance for testing
213
 *
214
 * @return void
215
 */
216
	public function setUp() {
217
		parent::setUp();
218
		Configure::write('Cache.disable', true);
219
		$this->Dbo = ConnectionManager::getDataSource('test');
220
		$this->skipIf(!($this->Dbo instanceof Postgres));
221
		$this->Dbo2 = new DboPostgresTestDb($this->Dbo->config, false);
222
		$this->model = new PostgresTestModel();
223
	}
224
 
225
/**
226
 * Sets up a Dbo class instance for testing
227
 *
228
 * @return void
229
 */
230
	public function tearDown() {
231
		parent::tearDown();
232
		Configure::write('Cache.disable', false);
233
		unset($this->Dbo2);
234
	}
235
 
236
/**
237
 * Test field quoting method
238
 *
239
 * @return void
240
 */
241
	public function testFieldQuoting() {
242
		$fields = array(
243
			'"PostgresTestModel"."id" AS "PostgresTestModel__id"',
244
			'"PostgresTestModel"."client_id" AS "PostgresTestModel__client_id"',
245
			'"PostgresTestModel"."name" AS "PostgresTestModel__name"',
246
			'"PostgresTestModel"."login" AS "PostgresTestModel__login"',
247
			'"PostgresTestModel"."passwd" AS "PostgresTestModel__passwd"',
248
			'"PostgresTestModel"."addr_1" AS "PostgresTestModel__addr_1"',
249
			'"PostgresTestModel"."addr_2" AS "PostgresTestModel__addr_2"',
250
			'"PostgresTestModel"."zip_code" AS "PostgresTestModel__zip_code"',
251
			'"PostgresTestModel"."city" AS "PostgresTestModel__city"',
252
			'"PostgresTestModel"."country" AS "PostgresTestModel__country"',
253
			'"PostgresTestModel"."phone" AS "PostgresTestModel__phone"',
254
			'"PostgresTestModel"."fax" AS "PostgresTestModel__fax"',
255
			'"PostgresTestModel"."url" AS "PostgresTestModel__url"',
256
			'"PostgresTestModel"."email" AS "PostgresTestModel__email"',
257
			'"PostgresTestModel"."comments" AS "PostgresTestModel__comments"',
258
			'"PostgresTestModel"."last_login" AS "PostgresTestModel__last_login"',
259
			'"PostgresTestModel"."created" AS "PostgresTestModel__created"',
260
			'"PostgresTestModel"."updated" AS "PostgresTestModel__updated"'
261
		);
262
 
263
		$result = $this->Dbo->fields($this->model);
264
		$expected = $fields;
265
		$this->assertEquals($expected, $result);
266
 
267
		$result = $this->Dbo->fields($this->model, null, 'PostgresTestModel.*');
268
		$expected = $fields;
269
		$this->assertEquals($expected, $result);
270
 
271
		$result = $this->Dbo->fields($this->model, null, array('*', 'AnotherModel.id', 'AnotherModel.name'));
272
		$expected = array_merge($fields, array(
273
			'"AnotherModel"."id" AS "AnotherModel__id"',
274
			'"AnotherModel"."name" AS "AnotherModel__name"'));
275
		$this->assertEquals($expected, $result);
276
 
277
		$result = $this->Dbo->fields($this->model, null, array('*', 'PostgresClientTestModel.*'));
278
		$expected = array_merge($fields, array(
279
			'"PostgresClientTestModel"."id" AS "PostgresClientTestModel__id"',
280
			'"PostgresClientTestModel"."name" AS "PostgresClientTestModel__name"',
281
			'"PostgresClientTestModel"."email" AS "PostgresClientTestModel__email"',
282
			'"PostgresClientTestModel"."created" AS "PostgresClientTestModel__created"',
283
			'"PostgresClientTestModel"."updated" AS "PostgresClientTestModel__updated"'));
284
		$this->assertEquals($expected, $result);
285
	}
286
 
287
/**
288
 * testColumnParsing method
289
 *
290
 * @return void
291
 */
292
	public function testColumnParsing() {
293
		$this->assertEquals('text', $this->Dbo2->column('text'));
294
		$this->assertEquals('date', $this->Dbo2->column('date'));
295
		$this->assertEquals('boolean', $this->Dbo2->column('boolean'));
296
		$this->assertEquals('string', $this->Dbo2->column('character varying'));
297
		$this->assertEquals('time', $this->Dbo2->column('time without time zone'));
298
		$this->assertEquals('datetime', $this->Dbo2->column('timestamp without time zone'));
299
		$this->assertEquals('decimal', $this->Dbo2->column('decimal'));
300
		$this->assertEquals('decimal', $this->Dbo2->column('numeric'));
301
		$this->assertEquals('float', $this->Dbo2->column('float'));
302
		$this->assertEquals('float', $this->Dbo2->column('double precision'));
303
 
304
		$result = $this->Dbo2->column('bigint');
305
		$expected = 'biginteger';
306
		$this->assertEquals($expected, $result);
307
	}
308
 
309
/**
310
 * testValueQuoting method
311
 *
312
 * @return void
313
 */
314
	public function testValueQuoting() {
315
		$this->assertEquals("1.200000", $this->Dbo->value(1.2, 'float'));
316
		$this->assertEquals("'1,2'", $this->Dbo->value('1,2', 'float'));
317
 
318
		$this->assertEquals("0", $this->Dbo->value('0', 'integer'));
319
		$this->assertEquals('NULL', $this->Dbo->value('', 'integer'));
320
		$this->assertEquals('NULL', $this->Dbo->value('', 'float'));
321
		$this->assertEquals("NULL", $this->Dbo->value('', 'integer', false));
322
		$this->assertEquals("NULL", $this->Dbo->value('', 'float', false));
323
		$this->assertEquals("'0.0'", $this->Dbo->value('0.0', 'float'));
324
 
325
		$this->assertEquals("'TRUE'", $this->Dbo->value('t', 'boolean'));
326
		$this->assertEquals("'FALSE'", $this->Dbo->value('f', 'boolean'));
327
		$this->assertEquals("'TRUE'", $this->Dbo->value(true));
328
		$this->assertEquals("'FALSE'", $this->Dbo->value(false));
329
		$this->assertEquals("'t'", $this->Dbo->value('t'));
330
		$this->assertEquals("'f'", $this->Dbo->value('f'));
331
		$this->assertEquals("'TRUE'", $this->Dbo->value('true', 'boolean'));
332
		$this->assertEquals("'FALSE'", $this->Dbo->value('false', 'boolean'));
333
		$this->assertEquals("'FALSE'", $this->Dbo->value('', 'boolean'));
334
		$this->assertEquals("'FALSE'", $this->Dbo->value(0, 'boolean'));
335
		$this->assertEquals("'TRUE'", $this->Dbo->value(1, 'boolean'));
336
		$this->assertEquals("'TRUE'", $this->Dbo->value('1', 'boolean'));
337
		$this->assertEquals("NULL", $this->Dbo->value(null, 'boolean'));
338
		$this->assertEquals("NULL", $this->Dbo->value(array()));
339
	}
340
 
341
/**
342
 * test that localized floats don't cause trouble.
343
 *
344
 * @return void
345
 */
346
	public function testLocalizedFloats() {
347
		$restore = setlocale(LC_NUMERIC, 0);
348
 
349
		$this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available.");
350
 
351
		$result = $this->db->value(3.141593, 'float');
352
		$this->assertEquals("3.141593", $result);
353
 
354
		$result = $this->db->value(3.14);
355
		$this->assertEquals("3.140000", $result);
356
 
357
		setlocale(LC_NUMERIC, $restore);
358
	}
359
 
360
/**
361
 * test that date and time columns do not generate errors with null and nullish values.
362
 *
363
 * @return void
364
 */
365
	public function testDateAndTimeAsNull() {
366
		$this->assertEquals('NULL', $this->Dbo->value(null, 'date'));
367
		$this->assertEquals('NULL', $this->Dbo->value('', 'date'));
368
 
369
		$this->assertEquals('NULL', $this->Dbo->value('', 'datetime'));
370
		$this->assertEquals('NULL', $this->Dbo->value(null, 'datetime'));
371
 
372
		$this->assertEquals('NULL', $this->Dbo->value('', 'timestamp'));
373
		$this->assertEquals('NULL', $this->Dbo->value(null, 'timestamp'));
374
 
375
		$this->assertEquals('NULL', $this->Dbo->value('', 'time'));
376
		$this->assertEquals('NULL', $this->Dbo->value(null, 'time'));
377
	}
378
 
379
/**
380
 * Tests that different Postgres boolean 'flavors' are properly returned as native PHP booleans
381
 *
382
 * @return void
383
 */
384
	public function testBooleanNormalization() {
385
		$this->assertEquals(true, $this->Dbo2->boolean('t', false));
386
		$this->assertEquals(true, $this->Dbo2->boolean('true', false));
387
		$this->assertEquals(true, $this->Dbo2->boolean('TRUE', false));
388
		$this->assertEquals(true, $this->Dbo2->boolean(true, false));
389
		$this->assertEquals(true, $this->Dbo2->boolean(1, false));
390
		$this->assertEquals(true, $this->Dbo2->boolean(" ", false));
391
 
392
		$this->assertEquals(false, $this->Dbo2->boolean('f', false));
393
		$this->assertEquals(false, $this->Dbo2->boolean('false', false));
394
		$this->assertEquals(false, $this->Dbo2->boolean('FALSE', false));
395
		$this->assertEquals(false, $this->Dbo2->boolean(false, false));
396
		$this->assertEquals(false, $this->Dbo2->boolean(0, false));
397
		$this->assertEquals(false, $this->Dbo2->boolean('', false));
398
	}
399
 
400
/**
401
 * test that default -> false in schemas works correctly.
402
 *
403
 * @return void
404
 */
405
	public function testBooleanDefaultFalseInSchema() {
406
		$this->loadFixtures('Datatype');
407
 
408
		$model = new Model(array('name' => 'Datatype', 'table' => 'datatypes', 'ds' => 'test'));
409
		$model->create();
410
		$this->assertSame(false, $model->data['Datatype']['bool']);
411
	}
412
 
413
/**
414
 * testLastInsertIdMultipleInsert method
415
 *
416
 * @return void
417
 */
418
	public function testLastInsertIdMultipleInsert() {
419
		$this->loadFixtures('User');
420
		$db1 = ConnectionManager::getDataSource('test');
421
 
422
		$table = $db1->fullTableName('users', false);
423
		$password = '5f4dcc3b5aa765d61d8327deb882cf99';
424
		$db1->execute(
425
			"INSERT INTO {$table} (\"user\", password) VALUES ('mariano', '{$password}')"
426
		);
427
 
428
		$this->assertEquals(5, $db1->lastInsertId($table));
429
 
430
		$db1->execute("INSERT INTO {$table} (\"user\", password) VALUES ('hoge', '{$password}')");
431
		$this->assertEquals(6, $db1->lastInsertId($table));
432
	}
433
 
434
/**
435
 * Tests that column types without default lengths in $columns do not have length values
436
 * applied when generating schemas.
437
 *
438
 * @return void
439
 */
440
	public function testColumnUseLength() {
441
		$result = array('name' => 'foo', 'type' => 'string', 'length' => 100, 'default' => 'FOO');
442
		$expected = '"foo" varchar(100) DEFAULT \'FOO\'';
443
		$this->assertEquals($expected, $this->Dbo->buildColumn($result));
444
 
445
		$result = array('name' => 'foo', 'type' => 'text', 'length' => 100, 'default' => 'FOO');
446
		$expected = '"foo" text DEFAULT \'FOO\'';
447
		$this->assertEquals($expected, $this->Dbo->buildColumn($result));
448
	}
449
 
450
/**
451
 * Tests that binary data is escaped/unescaped properly on reads and writes
452
 *
453
 * @return void
454
 */
455
	public function testBinaryDataIntegrity() {
456
		$this->loadFixtures('BinaryTest');
457
		$data = '%PDF-1.3
458
		%ƒÂÚÂÎßÛ†–ƒ∆
459
		4 0 obj
460
		<< /Length 5 0 R /Filter /FlateDecode >>
461
		stream
462
		xµYMì€∆Ω„WÃ%)nï0¯îâ-«é]Q"πXµáÿ•Ip	-	P V,]Ú#c˚ˇ‰ut¥†∏Ti9 Ü=”›Ø_˜4>à∑‚Épcé¢Pxæ®2q\'
463
		1UªbUᡒ+ö«√[ıµ⁄ão"R∑"HiGæä€(å≠≈^Ãøsm?YlƒÃõªfi‹âEÚB&‚Î◊7bÒ^¸m°÷˛?2±Øs“fiu#®U√ˇú÷g¥C;ä")n})JºIÔ3ËSnÑÎ¥≤ıD∆¢∂Msx1üèG˚±Œ™⁄>¶ySïufØ ˝¸?UπÃã√6flÌÚC=øK?˝…s
464
		˛§¯ˇ:-˜ò7€ÓFæ∂∑Õ˛∆“V’>ılflëÅd«ÜQdI›ÎB%W¿ΩıÉn~hvêCS>«é˛(ØôK!€¡zB!√
465
		[œÜ"ûß ·iH¸[Àºæ∑¯¡L,ÀÚAlS∫ˆ=∫Œ≤cÄr&ˆÈ:√ÿ£˚È«4fl•À]vc›bÅôÿî=siXe4/¡p]ã]ôÆIœ™ Ωflà_ƒ‚G?«7	ùÿ ı¯K4ïIpV◊÷·\'éµóªÚæ>î
466
		;›sú!2fl¬F•/f∑j£
467
		dw"IÊÜπ<ôÿˆ%IG1ytÛDflXg|Éòa§˜}C˛¿ÿe°G´Ú±jÍm~¿/∂hã<#-¥•ıùe87€t˜õ6w}´{æ
468
		m‹ê–	∆¡ 6⁄\
469
		rAÀBùZ3aË‚r$G·$ó0ÑüâUY4È™¡%C∑Ÿ2rc<Iõ-cï.
470
		[ŒöâFA†É‡+QglMÉîÉÄúÌ|¸»#x7¥«MgVÎ-GGÚ• I?Á‘”Lzw∞pHů◊nefqCî.nÕeè∆ÿÛy¡˙fb≤üŒHÜAëÕNq=´@	’cQdÖúAÉIqñŸ˘+2&∏  Àù.gÅ‚ƒœ3EPƒOi—‰:>ÍCäı
471
		=Õec=ëR˝”eñ=<V$ì˙+x+¢ïÒÕ<àeWå»–˚∫Õd§&£àf ]fPA´âtënöå∏◊ó„Ë@∆≠K´÷˘}a_CI˚©yòHg,ôSSVìBƒl4 L.ÈY…á,2∂íäÙ.$ó¸CäŸ*€óy
472
		π?G,_√·ÆÎç=^Vkvo±ó{§ƒ2»±¨Ïüo»ëD-ãé fió¥cVÙ\'™G~\'p¢%* ã˚÷
473
		ªºnh˚ºO^∏…®[Ó“‚ÅfıÌ≥∫F!Eœ(π∑T6`¬tΩÆ0ì»rTÎ`»Ñ«
474
		]≈åp˝)=¿Ô0∆öVÂmˇˆ„ø~¯ÁÔ∏b*fc»‡Îı„Ú}∆tœs∂Y∫ÜaÆ˙X∏~<ÿ·Ùvé1‹p¿TD∆ÔîÄ“úhˆ*Ú€îe)K–p¨ÚJ3Ÿ∞ã>ÊuNê°“√Ü ‹Ê9iÙ0˙AAEÍ ˙`∂£\'ûce•åƒX›ŸÁ´1SK{qdá"tÏ[wQ#SµBe∞∑µó…ÌV`B"Ñ≥„!è_Óφ-º*ºú¿Ë0ˆeê∂´ë+HFj…‡zvHÓN|ÔL÷ûñ3õÜ$z%sá…pÎóV38âs	Çoµ•ß3†<9B·¨û~¢3)ÂxóÿÁCÕòÆ∫Í=»ÿSπS;∆~±êÆTEp∑óÈ÷ÀuìDHÈ$ÉõæÜjû§"≤ÃONM®RËíRr{õS	∏Ê™op±W;ÂUÔ P∫kÔˇflTæ∑óflË”ÆC©Ô[≥◊HÁ˚¨hê"ÆbF?ú%h˙ˇ4xèÕ(ó2ÙáíM])Ñd|=fë-cI0ñL¢kÖêk‰Rƒ«ıÄWñ8mO3∏&√æËX¯Hó—ì]yF2»–˜ádàà‡‹Çο„≥7mªHAS∑¶.;Œx(1} _kd©.fidç48M\'àáªCp^Krí<ɉXÓıïl!Ì$N<ı∞B»G]…∂Ó¯>˛ÔbõÒπÀ•:ôO<j∂™œ%âÏ—>@È$pÖu‹Ê´-QqV ?V≥JÆÍqÛX8(lπï@zgÖ}Fe<ˇ‡Sñ“ÿ˜ê?6‡L∫Oß~µ –?ËeäÚ®YîÕ=Ü=¢DÁu*GvBk;)L¬N«î:flö∂≠ÇΩq„Ñm하Ë∂‚"û≥§:±≤i^ΩÑ!)Wıyŧô á„RÄ÷Òôc’≠—s™rı‚Pdêãh˘ßHVç5fifiÈF€çÌÛuçÖ/M=gëµ±ÿGû1coÔuñæ‘z®. õ∑7ÉÏÜÆ,°’H†ÍÉÌ∂7e	º® íˆ⁄◊øNWK”ÂYµ‚ñé;µ¶gV-fl>µtË¥áßN2 ¯¶BaP-)eW.àôt^∏1›C∑Ö?L„&”5’4jvã–ªZ	÷+4% ´0l…»ú^°´© ûiπ∑é®óܱÒÿ‰ïˆÌ–dˆ◊Æ19rQ=Í|ı•rMæ¬;ò‰Y‰é9.”‹˝V«ã¯∏,+ë®j*¡·/';
475
 
476
		$model = new AppModel(array('name' => 'BinaryTest', 'ds' => 'test'));
477
		$model->save(compact('data'));
478
 
479
		$result = $model->find('first');
480
		$this->assertEquals($data, $result['BinaryTest']['data']);
481
	}
482
 
483
/**
484
 * Tests passing PostgreSQL regular expression operators when building queries
485
 *
486
 * @return void
487
 */
488
	public function testRegexpOperatorConditionsParsing() {
489
		$this->assertSame(' WHERE "name" ~ \'[a-z_]+\'', $this->Dbo->conditions(array('name ~' => '[a-z_]+')));
490
		$this->assertSame(' WHERE "name" ~* \'[a-z_]+\'', $this->Dbo->conditions(array('name ~*' => '[a-z_]+')));
491
		$this->assertSame(' WHERE "name" !~ \'[a-z_]+\'', $this->Dbo->conditions(array('name !~' => '[a-z_]+')));
492
		$this->assertSame(' WHERE "name" !~* \'[a-z_]+\'', $this->Dbo->conditions(array('name !~*' => '[a-z_]+')));
493
	}
494
 
495
/**
496
 * Tests the syntax of generated schema indexes
497
 *
498
 * @return void
499
 */
500
	public function testSchemaIndexSyntax() {
501
		$schema = new CakeSchema();
502
		$schema->tables = array('i18n' => array(
503
			'id' => array(
504
				'type' => 'integer', 'null' => false, 'default' => null,
505
				'length' => 10, 'key' => 'primary'
506
			),
507
			'locale' => array('type' => 'string', 'null' => false, 'length' => 6, 'key' => 'index'),
508
			'model' => array('type' => 'string', 'null' => false, 'key' => 'index'),
509
			'foreign_key' => array(
510
				'type' => 'integer', 'null' => false, 'length' => 10, 'key' => 'index'
511
			),
512
			'field' => array('type' => 'string', 'null' => false, 'key' => 'index'),
513
			'content' => array('type' => 'text', 'null' => true, 'default' => null),
514
			'indexes' => array(
515
				'PRIMARY' => array('column' => 'id', 'unique' => 1),
516
				'locale' => array('column' => 'locale', 'unique' => 0),
517
				'model' => array('column' => 'model', 'unique' => 0),
518
				'row_id' => array('column' => 'foreign_key', 'unique' => 0),
519
				'field' => array('column' => 'field', 'unique' => 0)
520
			)
521
		));
522
 
523
		$result = $this->Dbo->createSchema($schema);
524
		$this->assertNotRegExp('/^CREATE INDEX(.+);,$/', $result);
525
	}
526
 
527
/**
528
 * testCakeSchema method
529
 *
530
 * Test that schema generated postgresql queries are valid. ref #5696
531
 * Check that the create statement for a schema generated table is the same as the original sql
532
 *
533
 * @return void
534
 */
535
	public function testCakeSchema() {
536
		$db1 = ConnectionManager::getDataSource('test');
537
		$db1->cacheSources = false;
538
 
539
		$db1->rawQuery('CREATE TABLE ' . $db1->fullTableName('datatype_tests') . ' (
540
			id serial NOT NULL,
541
			"varchar" character varying(40) NOT NULL,
542
			"full_length" character varying NOT NULL,
543
			"huge_int" bigint NOT NULL,
544
			"timestamp" timestamp without time zone,
545
			"date" date,
546
			CONSTRAINT test_data_types_pkey PRIMARY KEY (id)
547
		)');
548
 
549
		$schema = new CakeSchema(array('connection' => 'test'));
550
		$result = $schema->read(array(
551
			'connection' => 'test',
552
			'models' => array('DatatypeTest')
553
		));
554
 
555
		$schema->tables = array(
556
			'datatype_tests' => $result['tables']['missing']['datatype_tests']
557
		);
558
		$result = $db1->createSchema($schema, 'datatype_tests');
559
 
560
		$this->assertNotRegExp('/timestamp DEFAULT/', $result);
561
		$this->assertRegExp('/\"full_length\"\s*text\s.*,/', $result);
562
		$this->assertContains('timestamp ,', $result);
563
		$this->assertContains('"huge_int" bigint NOT NULL,', $result);
564
 
565
		$db1->query('DROP TABLE ' . $db1->fullTableName('datatype_tests'));
566
 
567
		$db1->query($result);
568
		$result2 = $schema->read(array(
569
			'connection' => 'test',
570
			'models' => array('DatatypeTest')
571
		));
572
		$schema->tables = array('datatype_tests' => $result2['tables']['missing']['datatype_tests']);
573
		$result2 = $db1->createSchema($schema, 'datatype_tests');
574
		$this->assertEquals($result, $result2);
575
 
576
		$db1->query('DROP TABLE ' . $db1->fullTableName('datatype_tests'));
577
	}
578
 
579
/**
580
 * testCakeSchemaBegserial method
581
 *
582
 * Test that schema generated postgresql queries are valid.
583
 *
584
 * @return void
585
 */
586
	public function testCakeSchemaBigserial() {
587
		$db1 = ConnectionManager::getDataSource('test');
588
		$db1->cacheSources = false;
589
 
590
		$db1->rawQuery('CREATE TABLE ' . $db1->fullTableName('bigserial_tests') . ' (
591
			"id" bigserial NOT NULL,
592
			"varchar" character varying(40) NOT NULL,
593
			PRIMARY KEY ("id")
594
		)');
595
 
596
		$schema = new CakeSchema(array('connection' => 'test'));
597
		$result = $schema->read(array(
598
			'connection' => 'test',
599
			'models' => array('BigserialTest')
600
		));
601
		$schema->tables = array(
602
			'bigserial_tests' => $result['tables']['missing']['bigserial_tests']
603
		);
604
		$result = $db1->createSchema($schema, 'bigserial_tests');
605
 
606
		$this->assertContains('"id" bigserial NOT NULL,', $result);
607
 
608
		$db1->query('DROP TABLE ' . $db1->fullTableName('bigserial_tests'));
609
	}
610
 
611
/**
612
 * Test index generation from table info.
613
 *
614
 * @return void
615
 */
616
	public function testIndexGeneration() {
617
		$name = $this->Dbo->fullTableName('index_test', false, false);
618
		$this->Dbo->query('CREATE TABLE ' . $name . ' ("id" serial NOT NULL PRIMARY KEY, "bool" integer, "small_char" varchar(50), "description" varchar(40) )');
619
		$this->Dbo->query('CREATE INDEX pointless_bool ON ' . $name . '("bool")');
620
		$this->Dbo->query('CREATE UNIQUE INDEX char_index ON ' . $name . '("small_char")');
621
		$expected = array(
622
			'PRIMARY' => array('unique' => true, 'column' => 'id'),
623
			'pointless_bool' => array('unique' => false, 'column' => 'bool'),
624
			'char_index' => array('unique' => true, 'column' => 'small_char'),
625
		);
626
		$result = $this->Dbo->index($name);
627
		$this->Dbo->query('DROP TABLE ' . $name);
628
		$this->assertEquals($expected, $result);
629
 
630
		$name = $this->Dbo->fullTableName('index_test_2', false, false);
631
		$this->Dbo->query('CREATE TABLE ' . $name . ' ("id" serial NOT NULL PRIMARY KEY, "bool" integer, "small_char" varchar(50), "description" varchar(40) )');
632
		$this->Dbo->query('CREATE UNIQUE INDEX multi_col ON ' . $name . '("small_char", "bool")');
633
		$expected = array(
634
			'PRIMARY' => array('unique' => true, 'column' => 'id'),
635
			'multi_col' => array('unique' => true, 'column' => array('small_char', 'bool')),
636
		);
637
		$result = $this->Dbo->index($name);
638
		$this->Dbo->query('DROP TABLE ' . $name);
639
		$this->assertEquals($expected, $result);
640
	}
641
 
642
/**
643
 * Test the alterSchema capabilities of postgres
644
 *
645
 * @return void
646
 */
647
	public function testAlterSchema() {
648
		$Old = new CakeSchema(array(
649
			'connection' => 'test',
650
			'name' => 'AlterPosts',
651
			'alter_posts' => array(
652
				'id' => array('type' => 'integer', 'key' => 'primary'),
653
				'author_id' => array('type' => 'integer', 'null' => false),
654
				'title' => array('type' => 'string', 'null' => true),
655
				'body' => array('type' => 'text'),
656
				'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'),
657
				'created' => array('type' => 'datetime'),
658
				'updated' => array('type' => 'datetime'),
659
			)
660
		));
661
		$this->Dbo->query($this->Dbo->createSchema($Old));
662
 
663
		$New = new CakeSchema(array(
664
			'connection' => 'test',
665
			'name' => 'AlterPosts',
666
			'alter_posts' => array(
667
				'id' => array('type' => 'integer', 'key' => 'primary'),
668
				'author_id' => array('type' => 'integer', 'null' => true),
669
				'title' => array('type' => 'string', 'null' => false, 'default' => 'my title'),
670
				'body' => array('type' => 'string', 'length' => 500),
671
				'status' => array('type' => 'integer', 'length' => 3, 'default' => 1),
672
				'created' => array('type' => 'datetime'),
673
				'updated' => array('type' => 'datetime'),
674
			)
675
		));
676
		$this->Dbo->query($this->Dbo->alterSchema($New->compare($Old), 'alter_posts'));
677
 
678
		$model = new CakeTestModel(array('table' => 'alter_posts', 'ds' => 'test'));
679
		$result = $model->schema();
680
		$this->assertTrue(isset($result['status']));
681
		$this->assertFalse(isset($result['published']));
682
		$this->assertEquals('string', $result['body']['type']);
683
		$this->assertEquals(1, $result['status']['default']);
684
		$this->assertEquals(true, $result['author_id']['null']);
685
		$this->assertEquals(false, $result['title']['null']);
686
 
687
		$this->Dbo->query($this->Dbo->dropSchema($New));
688
 
689
		$New = new CakeSchema(array(
690
			'connection' => 'test_suite',
691
			'name' => 'AlterPosts',
692
			'alter_posts' => array(
693
				'id' => array('type' => 'string', 'length' => 36, 'key' => 'primary'),
694
				'author_id' => array('type' => 'integer', 'null' => false),
695
				'title' => array('type' => 'string', 'null' => true),
696
				'body' => array('type' => 'text'),
697
				'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'),
698
				'created' => array('type' => 'datetime'),
699
				'updated' => array('type' => 'datetime'),
700
			)
701
		));
702
		$result = $this->Dbo->alterSchema($New->compare($Old), 'alter_posts');
703
		$this->assertNotRegExp('/varchar\(36\) NOT NULL/i', $result);
704
	}
705
 
706
/**
707
 * Test the alterSchema changing boolean to integer
708
 *
709
 * @return void
710
 */
711
	public function testAlterSchemaBooleanToIntegerField() {
712
		$default = array(
713
			'connection' => 'test',
714
			'name' => 'BoolField',
715
			'bool_fields' => array(
716
				'id' => array('type' => 'integer', 'key' => 'primary'),
717
				'name' => array('type' => 'string', 'length' => 50),
718
				'active' => array('type' => 'boolean', 'null' => false),
719
			)
720
		);
721
		$Old = new CakeSchema($default);
722
		$result = $this->Dbo->query($this->Dbo->createSchema($Old));
723
		$this->assertTrue($result);
724
 
725
		$modified = $default;
726
		$modified['bool_fields']['active'] = array('type' => 'integer', 'null' => true);
727
 
728
		$New = new CakeSchema($modified);
729
		$query = $this->Dbo->alterSchema($New->compare($Old));
730
		$result = $this->Dbo->query($query);
731
		$this->Dbo->query($this->Dbo->dropSchema($Old));
732
	}
733
 
734
/**
735
 * Test the alter index capabilities of postgres
736
 *
737
 * @return void
738
 */
739
	public function testAlterIndexes() {
740
		$this->Dbo->cacheSources = false;
741
 
742
		$schema1 = new CakeSchema(array(
743
			'name' => 'AlterTest1',
744
			'connection' => 'test',
745
			'altertest' => array(
746
				'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
747
				'name' => array('type' => 'string', 'null' => false, 'length' => 50),
748
				'group1' => array('type' => 'integer', 'null' => true),
749
				'group2' => array('type' => 'integer', 'null' => true)
750
			)
751
		));
752
 
753
		$this->Dbo->rawQuery($this->Dbo->createSchema($schema1));
754
 
755
		$schema2 = new CakeSchema(array(
756
			'name' => 'AlterTest2',
757
			'connection' => 'test',
758
			'altertest' => array(
759
				'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
760
				'name' => array('type' => 'string', 'null' => false, 'length' => 50),
761
				'group1' => array('type' => 'integer', 'null' => true),
762
				'group2' => array('type' => 'integer', 'null' => true),
763
				'indexes' => array(
764
					'name_idx' => array('unique' => false, 'column' => 'name'),
765
					'group_idx' => array('unique' => false, 'column' => 'group1'),
766
					'compound_idx' => array('unique' => false, 'column' => array('group1', 'group2')),
767
					'PRIMARY' => array('unique' => true, 'column' => 'id')
768
				)
769
			)
770
		));
771
		$this->Dbo->query($this->Dbo->alterSchema($schema2->compare($schema1)));
772
 
773
		$indexes = $this->Dbo->index('altertest');
774
		$this->assertEquals($schema2->tables['altertest']['indexes'], $indexes);
775
 
776
		// Change three indexes, delete one and add another one
777
		$schema3 = new CakeSchema(array(
778
			'name' => 'AlterTest3',
779
			'connection' => 'test',
780
			'altertest' => array(
781
				'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
782
				'name' => array('type' => 'string', 'null' => false, 'length' => 50),
783
				'group1' => array('type' => 'integer', 'null' => true),
784
				'group2' => array('type' => 'integer', 'null' => true),
785
				'indexes' => array(
786
					'name_idx' => array('unique' => true, 'column' => 'name'),
787
					'group_idx' => array('unique' => false, 'column' => 'group2'),
788
					'compound_idx' => array('unique' => false, 'column' => array('group2', 'group1')),
789
					'another_idx' => array('unique' => false, 'column' => array('group1', 'name')))
790
		)));
791
 
792
		$this->Dbo->query($this->Dbo->alterSchema($schema3->compare($schema2)));
793
 
794
		$indexes = $this->Dbo->index('altertest');
795
		$this->assertEquals($schema3->tables['altertest']['indexes'], $indexes);
796
 
797
		// Compare us to ourself.
798
		$this->assertEquals(array(), $schema3->compare($schema3));
799
 
800
		// Drop the indexes
801
		$this->Dbo->query($this->Dbo->alterSchema($schema1->compare($schema3)));
802
 
803
		$indexes = $this->Dbo->index('altertest');
804
		$this->assertEquals(array(), $indexes);
805
 
806
		$this->Dbo->query($this->Dbo->dropSchema($schema1));
807
	}
808
 
809
/**
810
 * Test the alterSchema RENAME statements
811
 *
812
 * @return void
813
 */
814
	public function testAlterSchemaRenameTo() {
815
		$query = $this->Dbo->alterSchema(array(
816
			'posts' => array(
817
				'change' => array(
818
					'title' => array('name' => 'subject', 'type' => 'string', 'null' => false)
819
				)
820
			)
821
		));
822
		$this->assertContains('RENAME "title" TO "subject";', $query);
823
		$this->assertContains('ALTER COLUMN "subject" TYPE', $query);
824
		$this->assertNotContains(";\n\tALTER COLUMN \"subject\" TYPE", $query);
825
		$this->assertNotContains('ALTER COLUMN "title" TYPE "subject"', $query);
826
	}
827
 
828
/**
829
 * Test it is possible to use virtual field with postgresql
830
 *
831
 * @return void
832
 */
833
	public function testVirtualFields() {
834
		$this->loadFixtures('Article', 'Comment', 'User', 'Attachment', 'Tag', 'ArticlesTag');
835
		$Article = new Article;
836
		$Article->virtualFields = array(
837
			'next_id' => 'Article.id + 1',
838
			'complex' => 'Article.title || Article.body',
839
			'functional' => 'COALESCE(User.user, Article.title)',
840
			'subquery' => 'SELECT count(*) FROM ' . $Article->Comment->table
841
		);
842
		$result = $Article->find('first');
843
		$this->assertEquals(2, $result['Article']['next_id']);
844
		$this->assertEquals($result['Article']['complex'], $result['Article']['title'] . $result['Article']['body']);
845
		$this->assertEquals($result['Article']['functional'], $result['User']['user']);
846
		$this->assertEquals(6, $result['Article']['subquery']);
847
	}
848
 
849
/**
850
 * Test that virtual fields work with SQL constants
851
 *
852
 * @return void
853
 */
854
	public function testVirtualFieldAsAConstant() {
855
		$this->loadFixtures('Article', 'Comment');
856
		$Article = ClassRegistry::init('Article');
857
		$Article->virtualFields = array(
858
			'empty' => "NULL",
859
			'number' => 43,
860
			'truth' => 'TRUE'
861
		);
862
		$result = $Article->find('first');
863
		$this->assertNull($result['Article']['empty']);
864
		$this->assertTrue($result['Article']['truth']);
865
		$this->assertEquals(43, $result['Article']['number']);
866
	}
867
 
868
/**
869
 * Tests additional order options for postgres
870
 *
871
 * @return void
872
 */
873
	public function testOrderAdditionalParams() {
874
		$result = $this->Dbo->order(array('title' => 'DESC NULLS FIRST', 'body' => 'DESC'));
875
		$expected = ' ORDER BY "title" DESC NULLS FIRST, "body" DESC';
876
		$this->assertEquals($expected, $result);
877
	}
878
 
879
/**
880
 * Test it is possible to do a SELECT COUNT(DISTINCT Model.field)
881
 * query in postgres and it gets correctly quoted
882
 *
883
 * @return void
884
 */
885
	public function testQuoteDistinctInFunction() {
886
		$this->loadFixtures('Article');
887
		$Article = new Article;
888
		$result = $this->Dbo->fields($Article, null, array('COUNT(DISTINCT Article.id)'));
889
		$expected = array('COUNT(DISTINCT "Article"."id")');
890
		$this->assertEquals($expected, $result);
891
 
892
		$result = $this->Dbo->fields($Article, null, array('COUNT(DISTINCT id)'));
893
		$expected = array('COUNT(DISTINCT "id")');
894
		$this->assertEquals($expected, $result);
895
 
896
		$result = $this->Dbo->fields($Article, null, array('COUNT(DISTINCT FUNC(id))'));
897
		$expected = array('COUNT(DISTINCT FUNC("id"))');
898
		$this->assertEquals($expected, $result);
899
	}
900
 
901
/**
902
 * test that saveAll works even with conditions that lack a model name.
903
 *
904
 * @return void
905
 */
906
	public function testUpdateAllWithNonQualifiedConditions() {
907
		$this->loadFixtures('Article');
908
		$Article = new Article();
909
		$result = $Article->updateAll(array('title' => "'Awesome'"), array('title' => 'Third Article'));
910
		$this->assertTrue($result);
911
 
912
		$result = $Article->find('count', array(
913
			'conditions' => array('Article.title' => 'Awesome')
914
		));
915
		$this->assertEquals(1, $result, 'Article count is wrong or fixture has changed.');
916
	}
917
 
918
/**
919
 * test alterSchema on two tables.
920
 *
921
 * @return void
922
 */
923
	public function testAlteringTwoTables() {
924
		$schema1 = new CakeSchema(array(
925
			'name' => 'AlterTest1',
926
			'connection' => 'test',
927
			'altertest' => array(
928
				'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
929
				'name' => array('type' => 'string', 'null' => false, 'length' => 50),
930
			),
931
			'other_table' => array(
932
				'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
933
				'name' => array('type' => 'string', 'null' => false, 'length' => 50),
934
			)
935
		));
936
		$schema2 = new CakeSchema(array(
937
			'name' => 'AlterTest1',
938
			'connection' => 'test',
939
			'altertest' => array(
940
				'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
941
				'field_two' => array('type' => 'string', 'null' => false, 'length' => 50),
942
			),
943
			'other_table' => array(
944
				'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
945
				'field_two' => array('type' => 'string', 'null' => false, 'length' => 50),
946
			)
947
		));
948
		$result = $this->db->alterSchema($schema2->compare($schema1));
949
		$this->assertEquals(2, substr_count($result, 'field_two'), 'Too many fields');
950
		$this->assertFalse(strpos(';ALTER', $result), 'Too many semi colons');
951
	}
952
 
953
/**
954
 * test encoding setting.
955
 *
956
 * @return void
957
 */
958
	public function testEncoding() {
959
		$result = $this->Dbo->setEncoding('UTF8');
960
		$this->assertTrue($result);
961
 
962
		$result = $this->Dbo->getEncoding();
963
		$this->assertEquals('UTF8', $result);
964
 
965
		$result = $this->Dbo->setEncoding('EUC_JP'); /* 'EUC_JP' is right character code name in PostgreSQL */
966
		$this->assertTrue($result);
967
 
968
		$result = $this->Dbo->getEncoding();
969
		$this->assertEquals('EUC_JP', $result);
970
	}
971
 
972
/**
973
 * Test truncate with a mock.
974
 *
975
 * @return void
976
 */
977
	public function testTruncateStatements() {
978
		$this->loadFixtures('Article', 'User');
979
		$db = ConnectionManager::getDatasource('test');
980
		$schema = $db->config['schema'];
981
		$Article = new Article();
982
 
983
		$this->Dbo = $this->getMock('Postgres', array('execute'), array($db->config));
984
 
985
		$this->Dbo->expects($this->at(0))->method('execute')
986
			->with("DELETE FROM \"$schema\".\"articles\"");
987
		$this->Dbo->truncate($Article);
988
 
989
		$this->Dbo->expects($this->at(0))->method('execute')
990
			->with("DELETE FROM \"$schema\".\"articles\"");
991
		$this->Dbo->truncate('articles');
992
 
993
		// #2355: prevent duplicate prefix
994
		$this->Dbo->config['prefix'] = 'tbl_';
995
		$Article->tablePrefix = 'tbl_';
996
		$this->Dbo->expects($this->at(0))->method('execute')
997
			->with("DELETE FROM \"$schema\".\"tbl_articles\"");
998
		$this->Dbo->truncate($Article);
999
 
1000
		$this->Dbo->expects($this->at(0))->method('execute')
1001
			->with("DELETE FROM \"$schema\".\"tbl_articles\"");
1002
		$this->Dbo->truncate('articles');
1003
	}
1004
 
1005
/**
1006
 * Test nested transaction
1007
 *
1008
 * @return void
1009
 */
1010
	public function testNestedTransaction() {
1011
		$this->Dbo->useNestedTransactions = true;
1012
		$this->skipIf($this->Dbo->nestedTransactionSupported() === false, 'The Postgres server do not support nested transaction');
1013
 
1014
		$this->loadFixtures('Article');
1015
		$model = new Article();
1016
		$model->hasOne = $model->hasMany = $model->belongsTo = $model->hasAndBelongsToMany = array();
1017
		$model->cacheQueries = false;
1018
		$this->Dbo->cacheMethods = false;
1019
 
1020
		$this->assertTrue($this->Dbo->begin());
1021
		$this->assertNotEmpty($model->read(null, 1));
1022
 
1023
		$this->assertTrue($this->Dbo->begin());
1024
		$this->assertTrue($model->delete(1));
1025
		$this->assertEmpty($model->read(null, 1));
1026
		$this->assertTrue($this->Dbo->rollback());
1027
		$this->assertNotEmpty($model->read(null, 1));
1028
 
1029
		$this->assertTrue($this->Dbo->begin());
1030
		$this->assertTrue($model->delete(1));
1031
		$this->assertEmpty($model->read(null, 1));
1032
		$this->assertTrue($this->Dbo->commit());
1033
		$this->assertEmpty($model->read(null, 1));
1034
 
1035
		$this->assertTrue($this->Dbo->rollback());
1036
		$this->assertNotEmpty($model->read(null, 1));
1037
	}
1038
 
1039
	public function testResetSequence() {
1040
		$model = new Article();
1041
 
1042
		$table = $this->Dbo->fullTableName($model, false);
1043
		$fields = array(
1044
			'id', 'user_id', 'title', 'body', 'published',
1045
		);
1046
		$values = array(
1047
			array(1, 1, 'test', 'first post', false),
1048
			array(2, 1, 'test 2', 'second post post', false),
1049
		);
1050
		$this->Dbo->insertMulti($table, $fields, $values);
1051
		$sequence = $this->Dbo->getSequence($table);
1052
		$result = $this->Dbo->rawQuery("SELECT nextval('$sequence')");
1053
		$original = $result->fetch(PDO::FETCH_ASSOC);
1054
 
1055
		$this->assertTrue($this->Dbo->resetSequence($table, 'id'));
1056
		$result = $this->Dbo->rawQuery("SELECT currval('$sequence')");
1057
		$new = $result->fetch(PDO::FETCH_ASSOC);
1058
		$this->assertTrue($new['currval'] > $original['nextval'], 'Sequence did not update');
1059
	}
1060
 
1061
	public function testSettings() {
1062
		Configure::write('Cache.disable', true);
1063
		$this->Dbo = ConnectionManager::getDataSource('test');
1064
		$this->skipIf(!($this->Dbo instanceof Postgres));
1065
 
1066
		$config2 = $this->Dbo->config;
1067
		$config2['settings']['datestyle'] = 'sql, dmy';
1068
		ConnectionManager::create('test2', $config2);
1069
		$dbo2 = new Postgres($config2, true);
1070
		$expected = array(array('r' => date('d/m/Y')));
1071
		$r = $dbo2->fetchRow('SELECT now()::date AS "r"');
1072
		$this->assertEquals($expected, $r);
1073
		$dbo2->execute('SET DATESTYLE TO ISO');
1074
		$dbo2->disconnect();
1075
	}
1076
 
1077
/**
1078
 * Test the limit function.
1079
 *
1080
 * @return void
1081
 */
1082
	public function testLimit() {
1083
		$db = $this->Dbo;
1084
 
1085
		$result = $db->limit('0');
1086
		$this->assertNull($result);
1087
 
1088
		$result = $db->limit('10');
1089
		$this->assertEquals(' LIMIT 10', $result);
1090
 
1091
		$result = $db->limit('FARTS', 'BOOGERS');
1092
		$this->assertEquals(' LIMIT 0 OFFSET 0', $result);
1093
 
1094
		$result = $db->limit(20, 10);
1095
		$this->assertEquals(' LIMIT 20 OFFSET 10', $result);
1096
 
1097
		$result = $db->limit(10, 300000000000000000000000000000);
1098
		$scientificNotation = sprintf('%.1E', 300000000000000000000000000000);
1099
		$this->assertNotContains($scientificNotation, $result);
1100
	}
1101
 
1102
/**
1103
 * Test describe() behavior for timestamp columns.
1104
 *
1105
 * @return void
1106
 */
1107
	public function testDescribeTimestamp() {
1108
		$this->loadFixtures('User');
1109
		$model = ClassRegistry::init('User');
1110
		$result = $this->Dbo->describe($model);
1111
		$expected = array(
1112
			'id' => array(
1113
				'type' => 'integer',
1114
				'null' => false,
1115
				'default' => null,
1116
				'length' => 11,
1117
				'key' => 'primary'
1118
			),
1119
			'user' => array(
1120
				'type' => 'string',
1121
				'null' => true,
1122
				'default' => null,
1123
				'length' => 255
1124
			),
1125
			'password' => array(
1126
				'type' => 'string',
1127
				'null' => true,
1128
				'default' => null,
1129
				'length' => 255
1130
			),
1131
			'created' => array(
1132
				'type' => 'datetime',
1133
				'null' => true,
1134
				'default' => null,
1135
				'length' => null
1136
			),
1137
			'updated' => array(
1138
				'type' => 'datetime',
1139
				'null' => true,
1140
				'default' => null,
1141
				'length' => null
1142
			)
1143
		);
1144
		$this->assertEquals($expected, $result);
1145
	}
1146
 
1147
}