SqliteTest.php 8.2 KB

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