/
PostgreSqlTest.php
368 lines (302 loc) · 11.7 KB
/
PostgreSqlTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2014, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace lithium\tests\integration\data\source\database\adapter;
use lithium\data\Schema;
use lithium\data\model\Query;
use lithium\data\source\database\adapter\PostgreSql;
use lithium\tests\mocks\data\source\database\adapter\MockPostgreSql;
use lithium\tests\fixture\model\gallery\Galleries;
class PostgreSqlTest extends \lithium\tests\integration\data\Base {
protected $_schema = array('fields' => array(
'id' => array('type' => 'id'),
'name' => array('type' => 'string', 'length' => 255),
'active' => array('type' => 'boolean'),
'created' => array('type' => 'datetime', 'null' => true),
'modified' => array('type' => 'datetime', 'null' => true)
));
/**
* Skip the test if a PostgreSQL adapter configuration is unavailable.
*/
public function skip() {
parent::connect($this->_connection);
$this->skipIf(!$this->with(array('PostgreSql')));
}
public function setUp() {
$this->_db->dropSchema('galleries');
$schema = new Schema($this->_schema);
$this->_db->createSchema('galleries', $schema);
Galleries::config(array('meta' => array('connection' => $this->_connection)));
}
public function tearDown() {
$this->_db->dropSchema('galleries');
Galleries::reset();
}
public function testEnabledFeatures() {
$supported = array('booleans', 'schema', 'relationships', 'sources', 'transactions');
$notSupported = array('arrays');
foreach ($supported as $feature) {
$this->assertTrue(PostgreSql::enabled($feature));
}
foreach ($notSupported as $feature) {
$this->assertFalse(PostgreSql::enabled($feature));
}
$this->assertNull(PostgreSql::enabled('unexisting'));
}
/**
* Tests that the object is initialized with the correct default values.
*/
public function testConstructorDefaults() {
$db = new MockPostgreSql(array('autoConnect' => false));
$result = $db->get('_config');
$expected = array(
'autoConnect' => false, 'encoding' => null,'persistent' => true,
'host' => 'localhost:5432', 'login' => 'root', 'password' => '',
'database' => null, 'dsn' => null, 'options' => array(),
'init' => true, 'schema' => 'public', 'timezone' => null
);
$this->assertEqual($expected, $result);
}
/**
* Tests that this adapter can connect to the database, and that the status is properly
* persisted.
*/
public function testDatabaseConnection() {
$db = new PostgreSql(array('autoConnect' => false) + $this->_dbConfig);
$this->assertTrue($db->connect());
$this->assertTrue($db->isConnected());
$this->assertTrue($db->disconnect());
$this->assertFalse($db->isConnected());
$db = new PostgreSql(array(
'autoConnect' => false, 'encoding' => null,'persistent' => false,
'host' => 'localhost:5432', 'login' => 'garbage', 'password' => '',
'database' => 'garbage', 'init' => true, 'schema' => 'garbage'
) + $this->_dbConfig);
$this->expectException();
$this->assertFalse($db->connect());
$this->assertFalse($db->isConnected());
$this->assertTrue($db->disconnect());
$this->assertFalse($db->isConnected());
}
public function testDatabaseEncoding() {
$this->assertTrue($this->_db->isConnected());
$this->assertTrue($this->_db->encoding('utf8'));
$this->assertEqual('UTF-8', $this->_db->encoding());
$this->assertTrue($this->_db->encoding('UTF-8'));
$this->assertEqual('UTF-8', $this->_db->encoding());
}
public function testValueByIntrospect() {
$expected = "'string'";
$result = $this->_db->value("string");
$this->assertInternalType('string', $result);
$this->assertEqual($expected, $result);
$expected = "'''this string is escaped'''";
$result = $this->_db->value("'this string is escaped'");
$this->assertInternalType('string', $result);
$this->assertEqual($expected, $result);
$this->assertIdentical("'t'", $this->_db->value(true));
$this->assertIdentical(1, $this->_db->value('1'));
$this->assertIdentical(1.1, $this->_db->value('1.1'));
}
public function testValueWithSchema() {
$result = $this->_db->value('2013-01-07 13:57:03.621684', array('type' => 'timestamp'));
$this->assertIdentical("'2013-01-07 13:57:03.621684'", $result);
$result = $this->_db->value('2012-05-25 22:44:00', array('type' => 'timestamp'));
$this->assertIdentical("'2012-05-25 22:44:00'", $result);
$result = $this->_db->value('2012-00-00', array('type' => 'date'));
$this->assertIdentical("'2011-11-30'", $result);
$result = $this->_db->value((object) "'2012-00-00'", array('type' => 'date'));
$this->assertIdentical("'2012-00-00'", $result);
}
public function testNameQuoting() {
$result = $this->_db->name('title');
$expected = '"title"';
$this->assertEqual($expected, $result);
}
public function testColumnAbstraction() {
$result = $this->_db->invokeMethod('_column', array('varchar'));
$this->assertIdentical(array('type' => 'string'), $result);
$result = $this->_db->invokeMethod('_column', array('tinyint(1)'));
$this->assertIdentical(array('type' => 'boolean'), $result);
$result = $this->_db->invokeMethod('_column', array('varchar(255)'));
$this->assertIdentical(array('type' => 'string', 'length' => 255), $result);
$result = $this->_db->invokeMethod('_column', array('text'));
$this->assertIdentical(array('type' => 'text'), $result);
$result = $this->_db->invokeMethod('_column', array('text'));
$this->assertIdentical(array('type' => 'text'), $result);
$result = $this->_db->invokeMethod('_column', array('decimal(12,2)'));
$this->assertIdentical(array('type' => 'float', 'length' => 12, 'precision' => 2), $result);
$result = $this->_db->invokeMethod('_column', array('int(11)'));
$this->assertIdentical(array('type' => 'integer', 'length' => 11), $result);
}
public function testRawSqlQuerying() {
$this->assertTrue($this->_db->create(
'INSERT INTO galleries (name, active) VALUES (?, ?)',
array('Test', "t")
));
$result = $this->_db->read('SELECT * FROM galleries AS Company WHERE name = {:name}', array(
'name' => 'Test',
'return' => 'array'
));
$this->assertCount(1, $result);
$expected = array('id', 'name', 'active', 'created', 'modified');
$this->assertEqual($expected, array_keys($result[0]));
$this->assertInternalType('numeric', $result[0]['id']);
unset($result[0]['id']);
$expected = array(
'name' => 'Test',
'active' => true,
'created' => null,
'modified' => null
);
$this->assertIdentical($expected, $result[0]);
$this->assertTrue($this->_db->delete('DELETE FROM galleries WHERE name = {:name}', array(
'name' => 'Test'
)));
$result = $this->_db->read('SELECT * FROM galleries AS Company WHERE name = {:name}', array(
'name' => 'Test',
'return' => 'array'
));
$this->assertEmpty($result);
}
public function testExecuteException() {
$this->expectException();
$this->_db->read('SELECT deliberate syntax error');
}
public function testEntityQuerying() {
$sources = $this->_db->sources();
$this->assertInternalType('array', $sources);
$this->assertNotEmpty($sources);
}
public function testQueryOrdering() {
$insert = new Query(array(
'type' => 'create',
'source' => 'galleries',
'data' => array(
'name' => 'Foo',
'active' => true,
'created' => date('Y-m-d H:i:s')
)
));
$this->assertTrue($this->_db->create($insert));
$insert->data(array(
'name' => 'Bar',
'created' => date('Y-m-d H:i:s', strtotime('-5 minutes'))
));
$this->assertTrue($this->_db->create($insert));
$insert->data(array(
'name' => 'Baz',
'created' => date('Y-m-d H:i:s', strtotime('-10 minutes'))
));
$this->assertTrue($this->_db->create($insert));
$read = new Query(array(
'type' => 'read',
'source' => 'galleries',
'fields' => array('name'),
'order' => array('created' => 'asc')
));
$result = $this->_db->read($read, array('return' => 'array'));
$expected = array(
array('name' => 'Baz'),
array('name' => 'Bar'),
array('name' => 'Foo')
);
$this->assertEqual($expected, $result);
$read->order(array('created' => 'desc'));
$result = $this->_db->read($read, array('return' => 'array'));
$expected = array(
array('name' => 'Foo'),
array('name' => 'Bar'),
array('name' => 'Baz')
);
$this->assertEqual($expected, $result);
$delete = new Query(array('type' => 'delete', 'source' => 'galleries'));
$this->assertTrue($this->_db->delete($delete));
}
/**
* Ensures that DELETE queries are not generated with table aliases, as PostgreSQL does not
* support this.
*/
public function testDeletesWithoutAliases() {
$delete = new Query(array('type' => 'delete', 'source' => 'galleries'));
$this->assertTrue($this->_db->delete($delete));
}
/**
* Tests that describing a table's schema returns the correct column meta-information.
*/
public function testDescribe() {
$result = $this->_db->describe('galleries');
$expected = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null),
'name' => array(
'type' => 'string', 'length' => 255, 'null' => true, 'default' => null
),
'active' => array('type' => 'boolean', 'null' => true, 'default' => null),
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
'modified' => array('type' => 'datetime', 'null' => true, 'default' => null)
);
$this->assertEqual($expected, $result->fields());
unset($expected['name']);
unset($expected['modified']);
$result = $this->_db->describe('galleries', $expected)->fields();
$this->assertEqual($expected, $result);
}
public function testDatabaseTimezone() {
$this->assertTrue($this->_db->isConnected());
$this->assertTrue($this->_db->timezone('UTC'));
$this->assertEqual('UTC', $this->_db->timezone());
$this->assertTrue($this->_db->timezone('US/Eastern'));
$this->assertEqual('US/Eastern', $this->_db->timezone());
}
public function testResultSet() {
for ($i = 1; $i < 9; $i++) {
Galleries::create(array('id' => $i, 'name' => "Title {$i}"))->save();
}
$galleries = Galleries::all();
$cpt = 0;
foreach ($galleries as $gallery) {
$this->assertEqual(++$cpt, $gallery->id);
}
$this->assertIdentical(8, $cpt);
$this->assertCount(8, $galleries);
}
public function testDefaultValues() {
$this->_db->dropSchema('galleries');
$schema = new Schema(array('fields' => array(
'id' => array('type' => 'id'),
'name' => array('type' => 'string', 'length' => 255, 'default' => 'image'),
'active' => array('type' => 'boolean', 'default' => false),
'show' => array('type' => 'boolean', 'default' => true),
'empty' => array('type' => 'text', 'null' => true),
'created' => array(
'type' => 'timestamp', 'null' => true, 'default' => (object) 'CURRENT_TIMESTAMP'
),
'modified' => array(
'type' => 'timestamp', 'null' => false, 'default' => (object) 'CURRENT_TIMESTAMP'
)
)));
$this->_db->createSchema('galleries', $schema);
$gallery = Galleries::create();
$this->assertEqual('image', $gallery->name);
$this->assertEqual(false, $gallery->active);
$this->assertEqual(true, $gallery->show);
$this->assertEqual(null, $gallery->empty);
$gallery->save();
$result = Galleries::find('first')->data();
$this->assertEqual(1, $result['id']);
$this->assertEqual('image', $result['name']);
$this->assertEqual(false, $result['active']);
$this->assertEqual(true, $result['show']);
$this->assertEqual(null, $result['empty']);
$this->assertPattern('$\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\.\d+$', $result['created']);
$this->assertPattern('$\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\.\d+$', $result['modified']);
$time = time();
$this->assertTrue($time - strtotime($result['created']) < 24 * 3600);
$this->assertTrue($time - strtotime($result['modified']) < 24 * 3600);
}
}
?>