7
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The MIT License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://cakephp.org CakePHP(tm) Project
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)
20
App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboSqlite'));
23
* DboSqliteTestDb class
26
* @subpackage cake.tests.cases.libs.model.datasources
28
class DboSqliteTestDb extends DboSqlite {
36
var $simulated = array();
45
function _execute($sql) {
46
$this->simulated[] = $sql;
56
function getLastQuery() {
57
return $this->simulated[count($this->simulated) - 1];
65
* @subpackage cake.tests.cases.libs.model.datasources.dbo
67
class DboSqliteTest extends CakeTestCase {
70
* Do not automatically load fixtures for each test, they will be loaded manually using CakeTestCase::loadFixtures
75
var $autoFixtures = false;
83
var $fixtures = array('core.user');
86
* Actual DB connection used in testing
94
* Simulated DB connection used in testing
102
* Skip if cannot connect to SQLite
108
$this->skipUnless($this->db->config['driver'] == 'sqlite', '%s SQLite connection not available');
112
* Set up test suite database connection
116
function startTest() {
121
* Sets up a Dbo class instance for testing
126
Configure::write('Cache.disable', true);
128
$this->db =& ConnectionManager::getDataSource('test_suite');
129
$this->db2 = new DboSqliteTestDb($this->db->config, false);
133
* Sets up a Dbo class instance for testing
137
function tearDown() {
138
Configure::write('Cache.disable', false);
143
* Tests that SELECT queries from DboSqlite::listSources() are not cached
147
function testTableListCacheDisabling() {
148
$this->assertFalse(in_array('foo_test', $this->db->listSources()));
150
$this->db->query('CREATE TABLE foo_test (test VARCHAR(255));');
151
$this->assertTrue(in_array('foo_test', $this->db->listSources()));
153
$this->db->query('DROP TABLE foo_test;');
154
$this->assertFalse(in_array('foo_test', $this->db->listSources()));
158
* test Index introspection.
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")');
169
'PRIMARY' => array('column' => 'id', 'unique' => 1),
170
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
171
'char_index' => array('column' => 'small_char', 'unique' => 1),
174
$result = $this->db->index($name);
175
$this->assertEqual($expected, $result);
176
$this->db->query('DROP TABLE ' . $name);
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")');
181
'PRIMARY' => array('column' => 'id', 'unique' => 1),
182
'multi_col' => array('column' => array('small_char', 'bool'), 'unique' => 1),
184
$result = $this->db->index($name);
185
$this->assertEqual($expected, $result);
186
$this->db->query('DROP TABLE ' . $name);
190
* Tests that cached table descriptions are saved under the sanitized key name
194
function testCacheKeyName() {
195
Configure::write('Cache.disable', false);
197
$dbName = 'db' . rand() . '$(*%&).db';
198
$this->assertFalse(file_exists(TMP . $dbName));
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));
204
$db->execute("CREATE TABLE test_list (id VARCHAR(255));");
206
$db->cacheSources = true;
207
$this->assertEqual($db->listSources(), array('test_list'));
208
$db->cacheSources = false;
210
$fileName = '_' . preg_replace('/[^A-Za-z0-9_\-+]/', '_', TMP . $dbName) . '_list';
212
$result = Cache::read($fileName, '_cake_model_');
213
$this->assertEqual($result, array('test_list'));
215
Cache::delete($fileName, '_cake_model_');
216
Configure::write('Cache.disable', true);
220
* test building columns with SQLite
224
function testBuildColumn() {
226
'name' => 'int_field',
230
$result = $this->db->buildColumn($data);
231
$expected = '"int_field" integer(11) NOT NULL';
232
$this->assertEqual($result, $expected);
240
$result = $this->db->buildColumn($data);
241
$expected = '"name" varchar(20) NOT NULL';
242
$this->assertEqual($result, $expected);
245
'name' => 'testName',
250
'collate' => 'NOCASE'
252
$result = $this->db->buildColumn($data);
253
$expected = '"testName" varchar(20) DEFAULT NULL COLLATE NOCASE';
254
$this->assertEqual($result, $expected);
257
'name' => 'testName',
260
'default' => 'test-value',
263
$result = $this->db->buildColumn($data);
264
$expected = '"testName" varchar(20) DEFAULT \'test-value\' NOT NULL';
265
$this->assertEqual($result, $expected);
268
'name' => 'testName',
274
$result = $this->db->buildColumn($data);
275
$expected = '"testName" integer(10) DEFAULT \'10\' NOT NULL';
276
$this->assertEqual($result, $expected);
279
'name' => 'testName',
284
'collate' => 'BADVALUE'
286
$result = $this->db->buildColumn($data);
287
$expected = '"testName" integer(10) DEFAULT \'10\' NOT NULL';
288
$this->assertEqual($result, $expected);
292
* test describe() and normal results.
296
function testDescribe() {
297
$Model =& new Model(array('name' => 'User', 'ds' => 'test_suite', 'table' => 'users'));
298
$result = $this->db->describe($Model);
320
'type' => 'datetime',
326
'type' => 'datetime',
332
$this->assertEqual($result, $expected);
336
* test that describe does not corrupt UUID primary keys
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);
352
$this->assertEqual($result['id'], $expected);
353
$this->db->query('DROP TABLE ' . $tableName);