SqliteTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /**
  3. * DboSqliteTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Model.Datasource.Database
  16. * @since CakePHP(tm) v 1.2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Model', 'Model');
  20. App::uses('AppModel', 'Model');
  21. App::uses('Sqlite', 'Model/Datasource/Database');
  22. require_once dirname(dirname(dirname(__FILE__))) . DS . 'models.php';
  23. /**
  24. * DboSqliteTestDb class
  25. *
  26. * @package Cake.Test.Case.Model.Datasource.Database
  27. */
  28. class DboSqliteTestDb extends Sqlite {
  29. /**
  30. * simulated property
  31. *
  32. * @var array
  33. */
  34. public $simulated = array();
  35. /**
  36. * execute method
  37. *
  38. * @param mixed $sql
  39. * @return void
  40. */
  41. function _execute($sql, $params = array()) {
  42. $this->simulated[] = $sql;
  43. return null;
  44. }
  45. /**
  46. * getLastQuery method
  47. *
  48. * @return void
  49. */
  50. public function getLastQuery() {
  51. return $this->simulated[count($this->simulated) - 1];
  52. }
  53. }
  54. /**
  55. * DboSqliteTest class
  56. *
  57. * @package Cake.Test.Case.Model.Datasource.Database
  58. */
  59. class SqliteTest extends CakeTestCase {
  60. /**
  61. * Do not automatically load fixtures for each test, they will be loaded manually using CakeTestCase::loadFixtures
  62. *
  63. * @var boolean
  64. */
  65. public $autoFixtures = false;
  66. /**
  67. * Fixtures
  68. *
  69. * @var object
  70. */
  71. public $fixtures = array('core.user');
  72. /**
  73. * Actual DB connection used in testing
  74. *
  75. * @var DboSource
  76. */
  77. public $Dbo = null;
  78. /**
  79. * Sets up a Dbo class instance for testing
  80. *
  81. */
  82. public function setUp() {
  83. parent::setUp();
  84. Configure::write('Cache.disable', true);
  85. $this->Dbo = ConnectionManager::getDataSource('test');
  86. if (!$this->Dbo instanceof Sqlite) {
  87. $this->markTestSkipped('The Sqlite extension is not available.');
  88. }
  89. }
  90. /**
  91. * Sets up a Dbo class instance for testing
  92. *
  93. */
  94. public function tearDown() {
  95. parent::tearDown();
  96. Configure::write('Cache.disable', false);
  97. }
  98. /**
  99. * Tests that SELECT queries from DboSqlite::listSources() are not cached
  100. *
  101. */
  102. public function testTableListCacheDisabling() {
  103. $this->assertFalse(in_array('foo_test', $this->Dbo->listSources()));
  104. $this->Dbo->query('CREATE TABLE foo_test (test VARCHAR(255))');
  105. $this->assertTrue(in_array('foo_test', $this->Dbo->listSources()));
  106. $this->Dbo->cacheSources = false;
  107. $this->Dbo->query('DROP TABLE foo_test');
  108. $this->assertFalse(in_array('foo_test', $this->Dbo->listSources()));
  109. }
  110. /**
  111. * test Index introspection.
  112. *
  113. * @return void
  114. */
  115. public function testIndex() {
  116. $name = $this->Dbo->fullTableName('with_a_key');
  117. $this->Dbo->query('CREATE TABLE ' . $name . ' ("id" int(11) PRIMARY KEY, "bool" int(1), "small_char" varchar(50), "description" varchar(40) );');
  118. $this->Dbo->query('CREATE INDEX pointless_bool ON ' . $name . '("bool")');
  119. $this->Dbo->query('CREATE UNIQUE INDEX char_index ON ' . $name . '("small_char")');
  120. $expected = array(
  121. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  122. 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
  123. 'char_index' => array('column' => 'small_char', 'unique' => 1),
  124. );
  125. $result = $this->Dbo->index($name);
  126. $this->assertEqual($expected, $result);
  127. $this->Dbo->query('DROP TABLE ' . $name);
  128. $this->Dbo->query('CREATE TABLE ' . $name . ' ("id" int(11) PRIMARY KEY, "bool" int(1), "small_char" varchar(50), "description" varchar(40) );');
  129. $this->Dbo->query('CREATE UNIQUE INDEX multi_col ON ' . $name . '("small_char", "bool")');
  130. $expected = array(
  131. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  132. 'multi_col' => array('column' => array('small_char', 'bool'), 'unique' => 1),
  133. );
  134. $result = $this->Dbo->index($name);
  135. $this->assertEqual($expected, $result);
  136. $this->Dbo->query('DROP TABLE ' . $name);
  137. }
  138. /**
  139. * Tests that cached table descriptions are saved under the sanitized key name
  140. *
  141. */
  142. public function testCacheKeyName() {
  143. Configure::write('Cache.disable', false);
  144. $dbName = 'db' . rand() . '$(*%&).db';
  145. $this->assertFalse(file_exists(TMP . $dbName));
  146. $config = $this->Dbo->config;
  147. $db = new Sqlite(array_merge($this->Dbo->config, array('database' => TMP . $dbName)));
  148. $this->assertTrue(file_exists(TMP . $dbName));
  149. $db->execute("CREATE TABLE test_list (id VARCHAR(255));");
  150. $db->cacheSources = true;
  151. $this->assertEqual($db->listSources(), array('test_list'));
  152. $db->cacheSources = false;
  153. $fileName = '_' . preg_replace('/[^A-Za-z0-9_\-+]/', '_', TMP . $dbName) . '_list';
  154. $result = Cache::read($fileName, '_cake_model_');
  155. $this->assertEqual($result, array('test_list'));
  156. Cache::delete($fileName, '_cake_model_');
  157. Configure::write('Cache.disable', true);
  158. }
  159. /**
  160. * test building columns with SQLite
  161. *
  162. * @return void
  163. */
  164. public function testBuildColumn() {
  165. $data = array(
  166. 'name' => 'int_field',
  167. 'type' => 'integer',
  168. 'null' => false,
  169. );
  170. $result = $this->Dbo->buildColumn($data);
  171. $expected = '"int_field" integer NOT NULL';
  172. $this->assertEqual($expected, $result);
  173. $data = array(
  174. 'name' => 'name',
  175. 'type' => 'string',
  176. 'length' => 20,
  177. 'null' => false,
  178. );
  179. $result = $this->Dbo->buildColumn($data);
  180. $expected = '"name" varchar(20) NOT NULL';
  181. $this->assertEqual($expected, $result);
  182. $data = array(
  183. 'name' => 'testName',
  184. 'type' => 'string',
  185. 'length' => 20,
  186. 'default' => null,
  187. 'null' => true,
  188. 'collate' => 'NOCASE'
  189. );
  190. $result = $this->Dbo->buildColumn($data);
  191. $expected = '"testName" varchar(20) DEFAULT NULL COLLATE NOCASE';
  192. $this->assertEqual($expected, $result);
  193. $data = array(
  194. 'name' => 'testName',
  195. 'type' => 'string',
  196. 'length' => 20,
  197. 'default' => 'test-value',
  198. 'null' => false,
  199. );
  200. $result = $this->Dbo->buildColumn($data);
  201. $expected = '"testName" varchar(20) DEFAULT \'test-value\' NOT NULL';
  202. $this->assertEqual($expected, $result);
  203. $data = array(
  204. 'name' => 'testName',
  205. 'type' => 'integer',
  206. 'length' => 10,
  207. 'default' => 10,
  208. 'null' => false,
  209. );
  210. $result = $this->Dbo->buildColumn($data);
  211. $expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
  212. $this->assertEqual($expected, $result);
  213. $data = array(
  214. 'name' => 'testName',
  215. 'type' => 'integer',
  216. 'length' => 10,
  217. 'default' => 10,
  218. 'null' => false,
  219. 'collate' => 'BADVALUE'
  220. );
  221. $result = $this->Dbo->buildColumn($data);
  222. $expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
  223. $this->assertEqual($expected, $result);
  224. }
  225. /**
  226. * test describe() and normal results.
  227. *
  228. * @return void
  229. */
  230. public function testDescribe() {
  231. $this->loadFixtures('User');
  232. $Model = new Model(array('name' => 'User', 'ds' => 'test', 'table' => 'users'));
  233. $result = $this->Dbo->describe($Model);
  234. $expected = array(
  235. 'id' => array(
  236. 'type' => 'integer',
  237. 'key' => 'primary',
  238. 'null' => false,
  239. 'default' => null,
  240. 'length' => 11
  241. ),
  242. 'user' => array(
  243. 'type' => 'string',
  244. 'length' => 255,
  245. 'null' => false,
  246. 'default' => null
  247. ),
  248. 'password' => array(
  249. 'type' => 'string',
  250. 'length' => 255,
  251. 'null' => false,
  252. 'default' => null
  253. ),
  254. 'created' => array(
  255. 'type' => 'datetime',
  256. 'null' => true,
  257. 'default' => null,
  258. 'length' => null,
  259. ),
  260. 'updated' => array(
  261. 'type' => 'datetime',
  262. 'null' => true,
  263. 'default' => null,
  264. 'length' => null,
  265. )
  266. );
  267. $this->assertEquals($expected, $result);
  268. $result = $this->Dbo->describe($Model->useTable);
  269. $this->assertEquals($expected, $result);
  270. }
  271. /**
  272. * test that describe does not corrupt UUID primary keys
  273. *
  274. * @return void
  275. */
  276. public function testDescribeWithUuidPrimaryKey() {
  277. $tableName = 'uuid_tests';
  278. $this->Dbo->query("CREATE TABLE {$tableName} (id VARCHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
  279. $Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
  280. $result = $this->Dbo->describe($Model);
  281. $expected = array(
  282. 'type' => 'string',
  283. 'length' => 36,
  284. 'null' => false,
  285. 'default' => null,
  286. 'key' => 'primary',
  287. );
  288. $this->assertEqual($result['id'], $expected);
  289. $this->Dbo->query('DROP TABLE ' . $tableName);
  290. }
  291. /**
  292. * Test virtualFields with functions.
  293. *
  294. * @return void
  295. */
  296. public function testVirtualFieldWithFunction() {
  297. $this->loadFixtures('User');
  298. $User = ClassRegistry::init('User');
  299. $User->virtualFields = array('name' => 'SUBSTR(User.user, 5)');
  300. $result = $User->find('first', array(
  301. 'conditions' => array('User.user' => 'garrett')
  302. ));
  303. $this->assertEquals('ett', $result['User']['name']);
  304. }
  305. }