SqliteTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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', 'core.datatype');
  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. $db = new Sqlite(array_merge($this->Dbo->config, array('database' => TMP . $dbName)));
  147. $this->assertTrue(file_exists(TMP . $dbName));
  148. $db->execute("CREATE TABLE test_list (id VARCHAR(255));");
  149. $db->cacheSources = true;
  150. $this->assertEquals(array('test_list'), $db->listSources());
  151. $db->cacheSources = false;
  152. $fileName = '_' . preg_replace('/[^A-Za-z0-9_\-+]/', '_', TMP . $dbName) . '_list';
  153. $result = Cache::read($fileName, '_cake_model_');
  154. $this->assertEquals(array('test_list'), $result);
  155. Cache::delete($fileName, '_cake_model_');
  156. Configure::write('Cache.disable', true);
  157. }
  158. /**
  159. * test building columns with SQLite
  160. *
  161. * @return void
  162. */
  163. public function testBuildColumn() {
  164. $data = array(
  165. 'name' => 'int_field',
  166. 'type' => 'integer',
  167. 'null' => false,
  168. );
  169. $result = $this->Dbo->buildColumn($data);
  170. $expected = '"int_field" integer NOT NULL';
  171. $this->assertEquals($expected, $result);
  172. $data = array(
  173. 'name' => 'name',
  174. 'type' => 'string',
  175. 'length' => 20,
  176. 'null' => false,
  177. );
  178. $result = $this->Dbo->buildColumn($data);
  179. $expected = '"name" varchar(20) NOT NULL';
  180. $this->assertEquals($expected, $result);
  181. $data = array(
  182. 'name' => 'testName',
  183. 'type' => 'string',
  184. 'length' => 20,
  185. 'default' => null,
  186. 'null' => true,
  187. 'collate' => 'NOCASE'
  188. );
  189. $result = $this->Dbo->buildColumn($data);
  190. $expected = '"testName" varchar(20) DEFAULT NULL COLLATE NOCASE';
  191. $this->assertEquals($expected, $result);
  192. $data = array(
  193. 'name' => 'testName',
  194. 'type' => 'string',
  195. 'length' => 20,
  196. 'default' => 'test-value',
  197. 'null' => false,
  198. );
  199. $result = $this->Dbo->buildColumn($data);
  200. $expected = '"testName" varchar(20) DEFAULT \'test-value\' NOT NULL';
  201. $this->assertEquals($expected, $result);
  202. $data = array(
  203. 'name' => 'testName',
  204. 'type' => 'integer',
  205. 'length' => 10,
  206. 'default' => 10,
  207. 'null' => false,
  208. );
  209. $result = $this->Dbo->buildColumn($data);
  210. $expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
  211. $this->assertEquals($expected, $result);
  212. $data = array(
  213. 'name' => 'testName',
  214. 'type' => 'integer',
  215. 'length' => 10,
  216. 'default' => 10,
  217. 'null' => false,
  218. 'collate' => 'BADVALUE'
  219. );
  220. $result = $this->Dbo->buildColumn($data);
  221. $expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
  222. $this->assertEquals($expected, $result);
  223. $data = array(
  224. 'name' => 'huge',
  225. 'type' => 'biginteger',
  226. 'length' => 20,
  227. 'null' => false,
  228. );
  229. $result = $this->Dbo->buildColumn($data);
  230. $expected = '"huge" bigint(20) NOT NULL';
  231. $this->assertEquals($expected, $result);
  232. }
  233. /**
  234. * test describe() and normal results.
  235. *
  236. * @return void
  237. */
  238. public function testDescribe() {
  239. $this->loadFixtures('User');
  240. $Model = new Model(array(
  241. 'name' => 'User',
  242. 'ds' => 'test',
  243. 'table' => 'users'
  244. ));
  245. $this->Dbo->cacheSources = true;
  246. Configure::write('Cache.disable', false);
  247. $result = $this->Dbo->describe($Model);
  248. $expected = array(
  249. 'id' => array(
  250. 'type' => 'integer',
  251. 'key' => 'primary',
  252. 'null' => false,
  253. 'default' => null,
  254. 'length' => 11
  255. ),
  256. 'user' => array(
  257. 'type' => 'string',
  258. 'length' => 255,
  259. 'null' => true,
  260. 'default' => null
  261. ),
  262. 'password' => array(
  263. 'type' => 'string',
  264. 'length' => 255,
  265. 'null' => true,
  266. 'default' => null
  267. ),
  268. 'created' => array(
  269. 'type' => 'datetime',
  270. 'null' => true,
  271. 'default' => null,
  272. 'length' => null,
  273. ),
  274. 'updated' => array(
  275. 'type' => 'datetime',
  276. 'null' => true,
  277. 'default' => null,
  278. 'length' => null,
  279. )
  280. );
  281. $this->assertEquals($expected, $result);
  282. $result = $this->Dbo->describe($Model->useTable);
  283. $this->assertEquals($expected, $result);
  284. $result = Cache::read('test_users', '_cake_model_');
  285. $this->assertEquals($expected, $result);
  286. }
  287. /**
  288. * Test that datatypes are reflected
  289. *
  290. * @return void
  291. */
  292. public function testDatatypes() {
  293. $this->loadFixtures('Datatype');
  294. $Model = new Model(array(
  295. 'name' => 'Datatype',
  296. 'ds' => 'test',
  297. 'table' => 'datatypes'
  298. ));
  299. $result = $this->Dbo->describe($Model);
  300. $expected = array(
  301. 'id' => array(
  302. 'type' => 'integer',
  303. 'null' => false,
  304. 'default' => '',
  305. 'length' => 11,
  306. 'key' => 'primary',
  307. ),
  308. 'float_field' => array(
  309. 'type' => 'float',
  310. 'null' => false,
  311. 'default' => '',
  312. 'length' => '5,2',
  313. ),
  314. 'huge_int' => array(
  315. 'type' => 'biginteger',
  316. 'null' => true,
  317. 'default' => null,
  318. 'length' => 20,
  319. ),
  320. 'bool' => array(
  321. 'type' => 'boolean',
  322. 'null' => false,
  323. 'default' => '0',
  324. 'length' => null
  325. ),
  326. );
  327. $this->assertSame($expected, $result);
  328. }
  329. /**
  330. * test that describe does not corrupt UUID primary keys
  331. *
  332. * @return void
  333. */
  334. public function testDescribeWithUuidPrimaryKey() {
  335. $tableName = 'uuid_tests';
  336. $this->Dbo->query("CREATE TABLE {$tableName} (id VARCHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
  337. $Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
  338. $result = $this->Dbo->describe($Model);
  339. $expected = array(
  340. 'type' => 'string',
  341. 'length' => 36,
  342. 'null' => false,
  343. 'default' => null,
  344. 'key' => 'primary',
  345. );
  346. $this->assertEquals($expected, $result['id']);
  347. $this->Dbo->query('DROP TABLE ' . $tableName);
  348. $tableName = 'uuid_tests';
  349. $this->Dbo->query("CREATE TABLE {$tableName} (id CHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
  350. $Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
  351. $result = $this->Dbo->describe($Model);
  352. $expected = array(
  353. 'type' => 'string',
  354. 'length' => 36,
  355. 'null' => false,
  356. 'default' => null,
  357. 'key' => 'primary',
  358. );
  359. $this->assertEquals($expected, $result['id']);
  360. $this->Dbo->query('DROP TABLE ' . $tableName);
  361. }
  362. /**
  363. * Test virtualFields with functions.
  364. *
  365. * @return void
  366. */
  367. public function testVirtualFieldWithFunction() {
  368. $this->loadFixtures('User');
  369. $User = ClassRegistry::init('User');
  370. $User->virtualFields = array('name' => 'SUBSTR(User.user, 5, LENGTH(User.user) - 4)');
  371. $result = $User->find('first', array(
  372. 'conditions' => array('User.user' => 'garrett')
  373. ));
  374. $this->assertEquals('ett', $result['User']['name']);
  375. }
  376. /**
  377. * Test that records can be inserted with uuid primary keys, and
  378. * that the primary key is not blank
  379. *
  380. * @return void
  381. */
  382. public function testUuidPrimaryKeyInsertion() {
  383. $this->loadFixtures('Uuid');
  384. $Model = ClassRegistry::init('Uuid');
  385. $data = array(
  386. 'title' => 'A uuid should work',
  387. 'count' => 10
  388. );
  389. $Model->create($data);
  390. $this->assertTrue((bool)$Model->save());
  391. $result = $Model->read();
  392. $this->assertEquals($data['title'], $result['Uuid']['title']);
  393. $this->assertTrue(Validation::uuid($result['Uuid']['id']), 'Not a uuid');
  394. }
  395. /**
  396. * Test nested transaction
  397. *
  398. * @return void
  399. */
  400. public function testNestedTransaction() {
  401. $this->skipIf($this->Dbo->nestedTransactionSupported() === false, 'The Sqlite version do not support nested transaction');
  402. $this->loadFixtures('User');
  403. $model = new User();
  404. $model->hasOne = $model->hasMany = $model->belongsTo = $model->hasAndBelongsToMany = array();
  405. $model->cacheQueries = false;
  406. $this->Dbo->cacheMethods = false;
  407. $this->assertTrue($this->Dbo->begin());
  408. $this->assertNotEmpty($model->read(null, 1));
  409. $this->assertTrue($this->Dbo->begin());
  410. $this->assertTrue($model->delete(1));
  411. $this->assertEmpty($model->read(null, 1));
  412. $this->assertTrue($this->Dbo->rollback());
  413. $this->assertNotEmpty($model->read(null, 1));
  414. $this->assertTrue($this->Dbo->begin());
  415. $this->assertTrue($model->delete(1));
  416. $this->assertEmpty($model->read(null, 1));
  417. $this->assertTrue($this->Dbo->commit());
  418. $this->assertEmpty($model->read(null, 1));
  419. $this->assertTrue($this->Dbo->rollback());
  420. $this->assertNotEmpty($model->read(null, 1));
  421. }
  422. }