Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/model/datasources/dbo/dbo_sqlite.test.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /** | |
3 * DboSqliteTest file | |
4 * | |
5 * PHP versions 4 and 5 | |
6 * | |
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The MIT License | |
11 * Redistributions of files must retain the above copyright notice. | |
12 * | |
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
14 * @link http://cakephp.org CakePHP(tm) Project | |
15 * @package cake | |
16 * @subpackage cake.cake.libs | |
17 * @since CakePHP(tm) v 1.2.0 | |
18 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) | |
19 */ | |
20 App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboSqlite')); | |
21 | |
22 /** | |
23 * DboSqliteTestDb class | |
24 * | |
25 * @package cake | |
26 * @subpackage cake.tests.cases.libs.model.datasources | |
27 */ | |
28 class DboSqliteTestDb extends DboSqlite { | |
29 | |
30 /** | |
31 * simulated property | |
32 * | |
33 * @var array | |
34 * @access public | |
35 */ | |
36 var $simulated = array(); | |
37 | |
38 /** | |
39 * execute method | |
40 * | |
41 * @param mixed $sql | |
42 * @access protected | |
43 * @return void | |
44 */ | |
45 function _execute($sql) { | |
46 $this->simulated[] = $sql; | |
47 return null; | |
48 } | |
49 | |
50 /** | |
51 * getLastQuery method | |
52 * | |
53 * @access public | |
54 * @return void | |
55 */ | |
56 function getLastQuery() { | |
57 return $this->simulated[count($this->simulated) - 1]; | |
58 } | |
59 } | |
60 | |
61 /** | |
62 * DboSqliteTest class | |
63 * | |
64 * @package cake | |
65 * @subpackage cake.tests.cases.libs.model.datasources.dbo | |
66 */ | |
67 class DboSqliteTest extends CakeTestCase { | |
68 | |
69 /** | |
70 * Do not automatically load fixtures for each test, they will be loaded manually using CakeTestCase::loadFixtures | |
71 * | |
72 * @var boolean | |
73 * @access public | |
74 */ | |
75 var $autoFixtures = false; | |
76 | |
77 /** | |
78 * Fixtures | |
79 * | |
80 * @var object | |
81 * @access public | |
82 */ | |
83 var $fixtures = array('core.user'); | |
84 | |
85 /** | |
86 * Actual DB connection used in testing | |
87 * | |
88 * @var DboSource | |
89 * @access public | |
90 */ | |
91 var $db = null; | |
92 | |
93 /** | |
94 * Simulated DB connection used in testing | |
95 * | |
96 * @var DboSource | |
97 * @access public | |
98 */ | |
99 var $db2 = null; | |
100 | |
101 /** | |
102 * Skip if cannot connect to SQLite | |
103 * | |
104 * @access public | |
105 */ | |
106 function skip() { | |
107 $this->_initDb(); | |
108 $this->skipUnless($this->db->config['driver'] == 'sqlite', '%s SQLite connection not available'); | |
109 } | |
110 | |
111 /** | |
112 * Set up test suite database connection | |
113 * | |
114 * @access public | |
115 */ | |
116 function startTest() { | |
117 $this->_initDb(); | |
118 } | |
119 | |
120 /** | |
121 * Sets up a Dbo class instance for testing | |
122 * | |
123 * @access public | |
124 */ | |
125 function setUp() { | |
126 Configure::write('Cache.disable', true); | |
127 $this->startTest(); | |
128 $this->db =& ConnectionManager::getDataSource('test_suite'); | |
129 $this->db2 = new DboSqliteTestDb($this->db->config, false); | |
130 } | |
131 | |
132 /** | |
133 * Sets up a Dbo class instance for testing | |
134 * | |
135 * @access public | |
136 */ | |
137 function tearDown() { | |
138 Configure::write('Cache.disable', false); | |
139 unset($this->db2); | |
140 } | |
141 | |
142 /** | |
143 * Tests that SELECT queries from DboSqlite::listSources() are not cached | |
144 * | |
145 * @access public | |
146 */ | |
147 function testTableListCacheDisabling() { | |
148 $this->assertFalse(in_array('foo_test', $this->db->listSources())); | |
149 | |
150 $this->db->query('CREATE TABLE foo_test (test VARCHAR(255));'); | |
151 $this->assertTrue(in_array('foo_test', $this->db->listSources())); | |
152 | |
153 $this->db->query('DROP TABLE foo_test;'); | |
154 $this->assertFalse(in_array('foo_test', $this->db->listSources())); | |
155 } | |
156 | |
157 /** | |
158 * test Index introspection. | |
159 * | |
160 * @access public | |
161 * @return void | |
162 */ | |
163 function testIndex() { | |
164 $name = $this->db->fullTableName('with_a_key'); | |
165 $this->db->query('CREATE TABLE ' . $name . ' ("id" int(11) PRIMARY KEY, "bool" int(1), "small_char" varchar(50), "description" varchar(40) );'); | |
166 $this->db->query('CREATE INDEX pointless_bool ON ' . $name . '("bool")'); | |
167 $this->db->query('CREATE UNIQUE INDEX char_index ON ' . $name . '("small_char")'); | |
168 $expected = array( | |
169 'PRIMARY' => array('column' => 'id', 'unique' => 1), | |
170 'pointless_bool' => array('column' => 'bool', 'unique' => 0), | |
171 'char_index' => array('column' => 'small_char', 'unique' => 1), | |
172 | |
173 ); | |
174 $result = $this->db->index($name); | |
175 $this->assertEqual($expected, $result); | |
176 $this->db->query('DROP TABLE ' . $name); | |
177 | |
178 $this->db->query('CREATE TABLE ' . $name . ' ("id" int(11) PRIMARY KEY, "bool" int(1), "small_char" varchar(50), "description" varchar(40) );'); | |
179 $this->db->query('CREATE UNIQUE INDEX multi_col ON ' . $name . '("small_char", "bool")'); | |
180 $expected = array( | |
181 'PRIMARY' => array('column' => 'id', 'unique' => 1), | |
182 'multi_col' => array('column' => array('small_char', 'bool'), 'unique' => 1), | |
183 ); | |
184 $result = $this->db->index($name); | |
185 $this->assertEqual($expected, $result); | |
186 $this->db->query('DROP TABLE ' . $name); | |
187 } | |
188 | |
189 /** | |
190 * Tests that cached table descriptions are saved under the sanitized key name | |
191 * | |
192 * @access public | |
193 */ | |
194 function testCacheKeyName() { | |
195 Configure::write('Cache.disable', false); | |
196 | |
197 $dbName = 'db' . rand() . '$(*%&).db'; | |
198 $this->assertFalse(file_exists(TMP . $dbName)); | |
199 | |
200 $config = $this->db->config; | |
201 $db = new DboSqlite(array_merge($this->db->config, array('database' => TMP . $dbName))); | |
202 $this->assertTrue(file_exists(TMP . $dbName)); | |
203 | |
204 $db->execute("CREATE TABLE test_list (id VARCHAR(255));"); | |
205 | |
206 $db->cacheSources = true; | |
207 $this->assertEqual($db->listSources(), array('test_list')); | |
208 $db->cacheSources = false; | |
209 | |
210 $fileName = '_' . preg_replace('/[^A-Za-z0-9_\-+]/', '_', TMP . $dbName) . '_list'; | |
211 | |
212 $result = Cache::read($fileName, '_cake_model_'); | |
213 $this->assertEqual($result, array('test_list')); | |
214 | |
215 Cache::delete($fileName, '_cake_model_'); | |
216 Configure::write('Cache.disable', true); | |
217 } | |
218 | |
219 /** | |
220 * test building columns with SQLite | |
221 * | |
222 * @return void | |
223 */ | |
224 function testBuildColumn() { | |
225 $data = array( | |
226 'name' => 'int_field', | |
227 'type' => 'integer', | |
228 'null' => false, | |
229 ); | |
230 $result = $this->db->buildColumn($data); | |
231 $expected = '"int_field" integer(11) NOT NULL'; | |
232 $this->assertEqual($result, $expected); | |
233 | |
234 $data = array( | |
235 'name' => 'name', | |
236 'type' => 'string', | |
237 'length' => 20, | |
238 'null' => false, | |
239 ); | |
240 $result = $this->db->buildColumn($data); | |
241 $expected = '"name" varchar(20) NOT NULL'; | |
242 $this->assertEqual($result, $expected); | |
243 | |
244 $data = array( | |
245 'name' => 'testName', | |
246 'type' => 'string', | |
247 'length' => 20, | |
248 'default' => null, | |
249 'null' => true, | |
250 'collate' => 'NOCASE' | |
251 ); | |
252 $result = $this->db->buildColumn($data); | |
253 $expected = '"testName" varchar(20) DEFAULT NULL COLLATE NOCASE'; | |
254 $this->assertEqual($result, $expected); | |
255 | |
256 $data = array( | |
257 'name' => 'testName', | |
258 'type' => 'string', | |
259 'length' => 20, | |
260 'default' => 'test-value', | |
261 'null' => false, | |
262 ); | |
263 $result = $this->db->buildColumn($data); | |
264 $expected = '"testName" varchar(20) DEFAULT \'test-value\' NOT NULL'; | |
265 $this->assertEqual($result, $expected); | |
266 | |
267 $data = array( | |
268 'name' => 'testName', | |
269 'type' => 'integer', | |
270 'length' => 10, | |
271 'default' => 10, | |
272 'null' => false, | |
273 ); | |
274 $result = $this->db->buildColumn($data); | |
275 $expected = '"testName" integer(10) DEFAULT \'10\' NOT NULL'; | |
276 $this->assertEqual($result, $expected); | |
277 | |
278 $data = array( | |
279 'name' => 'testName', | |
280 'type' => 'integer', | |
281 'length' => 10, | |
282 'default' => 10, | |
283 'null' => false, | |
284 'collate' => 'BADVALUE' | |
285 ); | |
286 $result = $this->db->buildColumn($data); | |
287 $expected = '"testName" integer(10) DEFAULT \'10\' NOT NULL'; | |
288 $this->assertEqual($result, $expected); | |
289 } | |
290 | |
291 /** | |
292 * test describe() and normal results. | |
293 * | |
294 * @return void | |
295 */ | |
296 function testDescribe() { | |
297 $Model =& new Model(array('name' => 'User', 'ds' => 'test_suite', 'table' => 'users')); | |
298 $result = $this->db->describe($Model); | |
299 $expected = array( | |
300 'id' => array( | |
301 'type' => 'integer', | |
302 'key' => 'primary', | |
303 'null' => false, | |
304 'default' => null, | |
305 'length' => 11 | |
306 ), | |
307 'user' => array( | |
308 'type' => 'string', | |
309 'length' => 255, | |
310 'null' => false, | |
311 'default' => null | |
312 ), | |
313 'password' => array( | |
314 'type' => 'string', | |
315 'length' => 255, | |
316 'null' => false, | |
317 'default' => null | |
318 ), | |
319 'created' => array( | |
320 'type' => 'datetime', | |
321 'null' => true, | |
322 'default' => null, | |
323 'length' => null, | |
324 ), | |
325 'updated' => array( | |
326 'type' => 'datetime', | |
327 'null' => true, | |
328 'default' => null, | |
329 'length' => null, | |
330 ) | |
331 ); | |
332 $this->assertEqual($result, $expected); | |
333 } | |
334 | |
335 /** | |
336 * test that describe does not corrupt UUID primary keys | |
337 * | |
338 * @return void | |
339 */ | |
340 function testDescribeWithUuidPrimaryKey() { | |
341 $tableName = 'uuid_tests'; | |
342 $this->db->query("CREATE TABLE {$tableName} (id VARCHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)"); | |
343 $Model =& new Model(array('name' => 'UuidTest', 'ds' => 'test_suite', 'table' => 'uuid_tests')); | |
344 $result = $this->db->describe($Model); | |
345 $expected = array( | |
346 'type' => 'string', | |
347 'length' => 36, | |
348 'null' => false, | |
349 'default' => null, | |
350 'key' => 'primary', | |
351 ); | |
352 $this->assertEqual($result['id'], $expected); | |
353 $this->db->query('DROP TABLE ' . $tableName); | |
354 } | |
355 } |