SqliteTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. /**
  3. * DboSqliteTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.Test.Case.Model.Datasource.Database
  17. * @since CakePHP(tm) v 1.2.0
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('Model', 'Model');
  21. App::uses('AppModel', 'Model');
  22. App::uses('Sqlite', 'Model/Datasource/Database');
  23. require_once dirname(dirname(dirname(__FILE__))) . DS . 'models.php';
  24. /**
  25. * DboSqliteTestDb class
  26. *
  27. * @package Cake.Test.Case.Model.Datasource.Database
  28. */
  29. class DboSqliteTestDb extends Sqlite {
  30. /**
  31. * simulated property
  32. *
  33. * @var array
  34. */
  35. public $simulated = array();
  36. /**
  37. * execute method
  38. *
  39. * @param mixed $sql
  40. * @return void
  41. */
  42. protected function _execute($sql, $params = array(), $prepareOptions = array()) {
  43. $this->simulated[] = $sql;
  44. return null;
  45. }
  46. /**
  47. * getLastQuery method
  48. *
  49. * @return void
  50. */
  51. public function getLastQuery() {
  52. return $this->simulated[count($this->simulated) - 1];
  53. }
  54. }
  55. /**
  56. * DboSqliteTest class
  57. *
  58. * @package Cake.Test.Case.Model.Datasource.Database
  59. */
  60. class SqliteTest extends CakeTestCase {
  61. /**
  62. * Do not automatically load fixtures for each test, they will be loaded manually using CakeTestCase::loadFixtures
  63. *
  64. * @var boolean
  65. */
  66. public $autoFixtures = false;
  67. /**
  68. * Fixtures
  69. *
  70. * @var object
  71. */
  72. public $fixtures = array('core.user', 'core.uuid', 'core.datatype');
  73. /**
  74. * Actual DB connection used in testing
  75. *
  76. * @var DboSource
  77. */
  78. public $Dbo = null;
  79. /**
  80. * Sets up a Dbo class instance for testing
  81. *
  82. */
  83. public function setUp() {
  84. parent::setUp();
  85. Configure::write('Cache.disable', true);
  86. $this->Dbo = ConnectionManager::getDataSource('test');
  87. if (!$this->Dbo instanceof Sqlite) {
  88. $this->markTestSkipped('The Sqlite extension is not available.');
  89. }
  90. }
  91. /**
  92. * Sets up a Dbo class instance for testing
  93. *
  94. */
  95. public function tearDown() {
  96. parent::tearDown();
  97. Configure::write('Cache.disable', false);
  98. }
  99. /**
  100. * Tests that SELECT queries from DboSqlite::listSources() are not cached
  101. *
  102. */
  103. public function testTableListCacheDisabling() {
  104. $this->assertFalse(in_array('foo_test', $this->Dbo->listSources()));
  105. $this->Dbo->query('CREATE TABLE foo_test (test VARCHAR(255))');
  106. $this->assertTrue(in_array('foo_test', $this->Dbo->listSources()));
  107. $this->Dbo->cacheSources = false;
  108. $this->Dbo->query('DROP TABLE foo_test');
  109. $this->assertFalse(in_array('foo_test', $this->Dbo->listSources()));
  110. }
  111. /**
  112. * test Index introspection.
  113. *
  114. * @return void
  115. */
  116. public function testIndex() {
  117. $name = $this->Dbo->fullTableName('with_a_key', false, false);
  118. $this->Dbo->query('CREATE TABLE ' . $name . ' ("id" int(11) PRIMARY KEY, "bool" int(1), "small_char" varchar(50), "description" varchar(40) );');
  119. $this->Dbo->query('CREATE INDEX pointless_bool ON ' . $name . '("bool")');
  120. $this->Dbo->query('CREATE UNIQUE INDEX char_index ON ' . $name . '("small_char")');
  121. $expected = array(
  122. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  123. 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
  124. 'char_index' => array('column' => 'small_char', 'unique' => 1),
  125. );
  126. $result = $this->Dbo->index($name);
  127. $this->assertEquals($expected, $result);
  128. $this->Dbo->query('DROP TABLE ' . $name);
  129. $this->Dbo->query('CREATE TABLE ' . $name . ' ("id" int(11) PRIMARY KEY, "bool" int(1), "small_char" varchar(50), "description" varchar(40) );');
  130. $this->Dbo->query('CREATE UNIQUE INDEX multi_col ON ' . $name . '("small_char", "bool")');
  131. $expected = array(
  132. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  133. 'multi_col' => array('column' => array('small_char', 'bool'), 'unique' => 1),
  134. );
  135. $result = $this->Dbo->index($name);
  136. $this->assertEquals($expected, $result);
  137. $this->Dbo->query('DROP TABLE ' . $name);
  138. }
  139. /**
  140. * Tests that cached table descriptions are saved under the sanitized key name
  141. *
  142. */
  143. public function testCacheKeyName() {
  144. Configure::write('Cache.disable', false);
  145. $dbName = 'db' . rand() . '$(*%&).db';
  146. $this->assertFalse(file_exists(TMP . $dbName));
  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. $data = array(
  225. 'name' => 'huge',
  226. 'type' => 'biginteger',
  227. 'length' => 20,
  228. 'null' => false,
  229. );
  230. $result = $this->Dbo->buildColumn($data);
  231. $expected = '"huge" bigint(20) NOT NULL';
  232. $this->assertEquals($expected, $result);
  233. }
  234. /**
  235. * test describe() and normal results.
  236. *
  237. * @return void
  238. */
  239. public function testDescribe() {
  240. $this->loadFixtures('User');
  241. $Model = new Model(array(
  242. 'name' => 'User',
  243. 'ds' => 'test',
  244. 'table' => 'users'
  245. ));
  246. $this->Dbo->cacheSources = true;
  247. Configure::write('Cache.disable', false);
  248. $result = $this->Dbo->describe($Model);
  249. $expected = array(
  250. 'id' => array(
  251. 'type' => 'integer',
  252. 'key' => 'primary',
  253. 'null' => false,
  254. 'default' => null,
  255. 'length' => 11
  256. ),
  257. 'user' => array(
  258. 'type' => 'string',
  259. 'length' => 255,
  260. 'null' => true,
  261. 'default' => null
  262. ),
  263. 'password' => array(
  264. 'type' => 'string',
  265. 'length' => 255,
  266. 'null' => true,
  267. 'default' => null
  268. ),
  269. 'created' => array(
  270. 'type' => 'datetime',
  271. 'null' => true,
  272. 'default' => null,
  273. 'length' => null,
  274. ),
  275. 'updated' => array(
  276. 'type' => 'datetime',
  277. 'null' => true,
  278. 'default' => null,
  279. 'length' => null,
  280. )
  281. );
  282. $this->assertEquals($expected, $result);
  283. $result = $this->Dbo->describe($Model->useTable);
  284. $this->assertEquals($expected, $result);
  285. $result = Cache::read('test_users', '_cake_model_');
  286. $this->assertEquals($expected, $result);
  287. }
  288. /**
  289. * Test that datatypes are reflected
  290. *
  291. * @return void
  292. */
  293. public function testDatatypes() {
  294. $this->loadFixtures('Datatype');
  295. $Model = new Model(array(
  296. 'name' => 'Datatype',
  297. 'ds' => 'test',
  298. 'table' => 'datatypes'
  299. ));
  300. $result = $this->Dbo->describe($Model);
  301. $expected = array(
  302. 'id' => array(
  303. 'type' => 'integer',
  304. 'null' => false,
  305. 'default' => '',
  306. 'length' => 11,
  307. 'key' => 'primary',
  308. ),
  309. 'float_field' => array(
  310. 'type' => 'float',
  311. 'null' => false,
  312. 'default' => '',
  313. 'length' => '5,2',
  314. ),
  315. 'huge_int' => array(
  316. 'type' => 'biginteger',
  317. 'null' => true,
  318. 'default' => null,
  319. 'length' => 20,
  320. ),
  321. 'bool' => array(
  322. 'type' => 'boolean',
  323. 'null' => false,
  324. 'default' => '0',
  325. 'length' => null
  326. ),
  327. );
  328. $this->assertSame($expected, $result);
  329. }
  330. /**
  331. * test that describe does not corrupt UUID primary keys
  332. *
  333. * @return void
  334. */
  335. public function testDescribeWithUuidPrimaryKey() {
  336. $tableName = 'uuid_tests';
  337. $this->Dbo->query("CREATE TABLE {$tableName} (id VARCHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
  338. $Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
  339. $result = $this->Dbo->describe($Model);
  340. $expected = array(
  341. 'type' => 'string',
  342. 'length' => 36,
  343. 'null' => false,
  344. 'default' => null,
  345. 'key' => 'primary',
  346. );
  347. $this->assertEquals($expected, $result['id']);
  348. $this->Dbo->query('DROP TABLE ' . $tableName);
  349. $tableName = 'uuid_tests';
  350. $this->Dbo->query("CREATE TABLE {$tableName} (id CHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
  351. $Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
  352. $result = $this->Dbo->describe($Model);
  353. $expected = array(
  354. 'type' => 'string',
  355. 'length' => 36,
  356. 'null' => false,
  357. 'default' => null,
  358. 'key' => 'primary',
  359. );
  360. $this->assertEquals($expected, $result['id']);
  361. $this->Dbo->query('DROP TABLE ' . $tableName);
  362. }
  363. /**
  364. * Test virtualFields with functions.
  365. *
  366. * @return void
  367. */
  368. public function testVirtualFieldWithFunction() {
  369. $this->loadFixtures('User');
  370. $User = ClassRegistry::init('User');
  371. $User->virtualFields = array('name' => 'SUBSTR(User.user, 5, LENGTH(User.user) - 4)');
  372. $result = $User->find('first', array(
  373. 'conditions' => array('User.user' => 'garrett')
  374. ));
  375. $this->assertEquals('ett', $result['User']['name']);
  376. }
  377. /**
  378. * Test that records can be inserted with uuid primary keys, and
  379. * that the primary key is not blank
  380. *
  381. * @return void
  382. */
  383. public function testUuidPrimaryKeyInsertion() {
  384. $this->loadFixtures('Uuid');
  385. $Model = ClassRegistry::init('Uuid');
  386. $data = array(
  387. 'title' => 'A uuid should work',
  388. 'count' => 10
  389. );
  390. $Model->create($data);
  391. $this->assertTrue((bool)$Model->save());
  392. $result = $Model->read();
  393. $this->assertEquals($data['title'], $result['Uuid']['title']);
  394. $this->assertTrue(Validation::uuid($result['Uuid']['id']), 'Not a uuid');
  395. }
  396. /**
  397. * Test nested transaction
  398. *
  399. * @return void
  400. */
  401. public function testNestedTransaction() {
  402. $this->skipIf($this->Dbo->nestedTransactionSupported() === false, 'The Sqlite version do not support nested transaction');
  403. $this->loadFixtures('User');
  404. $model = new User();
  405. $model->hasOne = $model->hasMany = $model->belongsTo = $model->hasAndBelongsToMany = array();
  406. $model->cacheQueries = false;
  407. $this->Dbo->cacheMethods = false;
  408. $this->assertTrue($this->Dbo->begin());
  409. $this->assertNotEmpty($model->read(null, 1));
  410. $this->assertTrue($this->Dbo->begin());
  411. $this->assertTrue($model->delete(1));
  412. $this->assertEmpty($model->read(null, 1));
  413. $this->assertTrue($this->Dbo->rollback());
  414. $this->assertNotEmpty($model->read(null, 1));
  415. $this->assertTrue($this->Dbo->begin());
  416. $this->assertTrue($model->delete(1));
  417. $this->assertEmpty($model->read(null, 1));
  418. $this->assertTrue($this->Dbo->commit());
  419. $this->assertEmpty($model->read(null, 1));
  420. $this->assertTrue($this->Dbo->rollback());
  421. $this->assertNotEmpty($model->read(null, 1));
  422. }
  423. /**
  424. * Test the limit function.
  425. *
  426. * @return void
  427. */
  428. public function testLimit() {
  429. $db = $this->Dbo;
  430. $result = $db->limit('0');
  431. $this->assertNull($result);
  432. $result = $db->limit('10');
  433. $this->assertEquals(' LIMIT 10', $result);
  434. $result = $db->limit('FARTS', 'BOOGERS');
  435. $this->assertEquals(' LIMIT 0 OFFSET 0', $result);
  436. $result = $db->limit(20, 10);
  437. $this->assertEquals(' LIMIT 20 OFFSET 10', $result);
  438. $result = $db->limit(10, 300000000000000000000000000000);
  439. $this->assertEquals(' LIMIT 10 OFFSET 0', $result);
  440. }
  441. }