SqliteTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * DboSqliteTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, 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-2012, 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. protected function _execute($sql, $params = array(), $prepareOptions = 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', 'core.uuid');
  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', false, false);
  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->assertEquals($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->assertEquals($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->assertEquals(array('test_list'), $db->listSources());
  152. $db->cacheSources = false;
  153. $fileName = '_' . preg_replace('/[^A-Za-z0-9_\-+]/', '_', TMP . $dbName) . '_list';
  154. $result = Cache::read($fileName, '_cake_model_');
  155. $this->assertEquals(array('test_list'), $result);
  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->assertEquals($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->assertEquals($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->assertEquals($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->assertEquals($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->assertEquals($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->assertEquals($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. $this->Dbo->cacheSources = true;
  234. Configure::write('Cache.disable', false);
  235. $result = $this->Dbo->describe($Model);
  236. $expected = array(
  237. 'id' => array(
  238. 'type' => 'integer',
  239. 'key' => 'primary',
  240. 'null' => false,
  241. 'default' => null,
  242. 'length' => 11
  243. ),
  244. 'user' => array(
  245. 'type' => 'string',
  246. 'length' => 255,
  247. 'null' => true,
  248. 'default' => null
  249. ),
  250. 'password' => array(
  251. 'type' => 'string',
  252. 'length' => 255,
  253. 'null' => true,
  254. 'default' => null
  255. ),
  256. 'created' => array(
  257. 'type' => 'datetime',
  258. 'null' => true,
  259. 'default' => null,
  260. 'length' => null,
  261. ),
  262. 'updated' => array(
  263. 'type' => 'datetime',
  264. 'null' => true,
  265. 'default' => null,
  266. 'length' => null,
  267. )
  268. );
  269. $this->assertEquals($expected, $result);
  270. $result = $this->Dbo->describe($Model->useTable);
  271. $this->assertEquals($expected, $result);
  272. $result = Cache::read('test_users', '_cake_model_');
  273. $this->assertEquals($expected, $result);
  274. }
  275. /**
  276. * test that describe does not corrupt UUID primary keys
  277. *
  278. * @return void
  279. */
  280. public function testDescribeWithUuidPrimaryKey() {
  281. $tableName = 'uuid_tests';
  282. $this->Dbo->query("CREATE TABLE {$tableName} (id VARCHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
  283. $Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
  284. $result = $this->Dbo->describe($Model);
  285. $expected = array(
  286. 'type' => 'string',
  287. 'length' => 36,
  288. 'null' => false,
  289. 'default' => null,
  290. 'key' => 'primary',
  291. );
  292. $this->assertEquals($expected, $result['id']);
  293. $this->Dbo->query('DROP TABLE ' . $tableName);
  294. $tableName = 'uuid_tests';
  295. $this->Dbo->query("CREATE TABLE {$tableName} (id CHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
  296. $Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
  297. $result = $this->Dbo->describe($Model);
  298. $expected = array(
  299. 'type' => 'string',
  300. 'length' => 36,
  301. 'null' => false,
  302. 'default' => null,
  303. 'key' => 'primary',
  304. );
  305. $this->assertEquals($expected, $result['id']);
  306. $this->Dbo->query('DROP TABLE ' . $tableName);
  307. }
  308. /**
  309. * Test virtualFields with functions.
  310. *
  311. * @return void
  312. */
  313. public function testVirtualFieldWithFunction() {
  314. $this->loadFixtures('User');
  315. $User = ClassRegistry::init('User');
  316. $User->virtualFields = array('name' => 'SUBSTR(User.user, 5, LENGTH(User.user) - 4)');
  317. $result = $User->find('first', array(
  318. 'conditions' => array('User.user' => 'garrett')
  319. ));
  320. $this->assertEquals('ett', $result['User']['name']);
  321. }
  322. /**
  323. * Test that records can be inserted with uuid primary keys, and
  324. * that the primary key is not blank
  325. *
  326. * @return void
  327. */
  328. public function testUuidPrimaryKeyInsertion() {
  329. $this->loadFixtures('Uuid');
  330. $Model = ClassRegistry::init('Uuid');
  331. $data = array(
  332. 'title' => 'A uuid should work',
  333. 'count' => 10
  334. );
  335. $Model->create($data);
  336. $this->assertTrue((bool)$Model->save());
  337. $result = $Model->read();
  338. $this->assertEquals($data['title'], $result['Uuid']['title']);
  339. $this->assertTrue(Validation::uuid($result['Uuid']['id']), 'Not a uuid');
  340. }
  341. }