SqliteTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. /**
  3. * DboSqliteTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Test.Case.Model.Datasource.Database
  15. * @since CakePHP(tm) v 1.2.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Model', 'Model');
  19. App::uses('AppModel', 'Model');
  20. App::uses('Sqlite', 'Model/Datasource/Database');
  21. require_once dirname(dirname(dirname(__FILE__))) . DS . 'models.php';
  22. /**
  23. * DboSqliteTestDb class
  24. *
  25. * @package Cake.Test.Case.Model.Datasource.Database
  26. */
  27. class DboSqliteTestDb extends Sqlite {
  28. /**
  29. * simulated property
  30. *
  31. * @var array
  32. */
  33. public $simulated = array();
  34. /**
  35. * execute method
  36. *
  37. * @param mixed $sql
  38. * @return void
  39. */
  40. protected function _execute($sql, $params = array(), $prepareOptions = array()) {
  41. $this->simulated[] = $sql;
  42. return null;
  43. }
  44. /**
  45. * getLastQuery method
  46. *
  47. * @return void
  48. */
  49. public function getLastQuery() {
  50. return $this->simulated[count($this->simulated) - 1];
  51. }
  52. }
  53. /**
  54. * DboSqliteTest class
  55. *
  56. * @package Cake.Test.Case.Model.Datasource.Database
  57. */
  58. class SqliteTest extends CakeTestCase {
  59. /**
  60. * Do not automatically load fixtures for each test, they will be loaded manually using CakeTestCase::loadFixtures
  61. *
  62. * @var bool
  63. */
  64. public $autoFixtures = false;
  65. /**
  66. * Fixtures
  67. *
  68. * @var object
  69. */
  70. public $fixtures = array('core.user', 'core.uuid', 'core.datatype');
  71. /**
  72. * Actual DB connection used in testing
  73. *
  74. * @var DboSource
  75. */
  76. public $Dbo = null;
  77. /**
  78. * Sets up a Dbo class instance for testing
  79. *
  80. * @return void
  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. * @return void
  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. * @return void
  103. */
  104. public function testTableListCacheDisabling() {
  105. $this->assertFalse(in_array('foo_test', $this->Dbo->listSources()));
  106. $this->Dbo->query('CREATE TABLE foo_test (test VARCHAR(255))');
  107. $this->assertTrue(in_array('foo_test', $this->Dbo->listSources()));
  108. $this->Dbo->cacheSources = false;
  109. $this->Dbo->query('DROP TABLE foo_test');
  110. $this->assertFalse(in_array('foo_test', $this->Dbo->listSources()));
  111. }
  112. /**
  113. * test Index introspection.
  114. *
  115. * @return void
  116. */
  117. public function testIndex() {
  118. $name = $this->Dbo->fullTableName('with_a_key', false, false);
  119. $this->Dbo->query('CREATE TABLE ' . $name . ' ("id" int(11) PRIMARY KEY, "bool" int(1), "small_char" varchar(50), "description" varchar(40) );');
  120. $this->Dbo->query('CREATE INDEX pointless_bool ON ' . $name . '("bool")');
  121. $this->Dbo->query('CREATE UNIQUE INDEX char_index ON ' . $name . '("small_char")');
  122. $expected = array(
  123. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  124. 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
  125. 'char_index' => array('column' => 'small_char', 'unique' => 1),
  126. );
  127. $result = $this->Dbo->index($name);
  128. $this->assertEquals($expected, $result);
  129. $this->Dbo->query('DROP TABLE ' . $name);
  130. $this->Dbo->query('CREATE TABLE ' . $name . ' ("id" int(11) PRIMARY KEY, "bool" int(1), "small_char" varchar(50), "description" varchar(40) );');
  131. $this->Dbo->query('CREATE UNIQUE INDEX multi_col ON ' . $name . '("small_char", "bool")');
  132. $expected = array(
  133. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  134. 'multi_col' => array('column' => array('small_char', 'bool'), 'unique' => 1),
  135. );
  136. $result = $this->Dbo->index($name);
  137. $this->assertEquals($expected, $result);
  138. $this->Dbo->query('DROP TABLE ' . $name);
  139. }
  140. /**
  141. * Tests that cached table descriptions are saved under the sanitized key name
  142. *
  143. * @return void
  144. */
  145. public function testCacheKeyName() {
  146. Configure::write('Cache.disable', false);
  147. $dbName = 'db' . rand() . '$(*%&).db';
  148. $this->assertFalse(file_exists(TMP . $dbName));
  149. $db = new Sqlite(array_merge($this->Dbo->config, array('database' => TMP . $dbName)));
  150. $this->assertTrue(file_exists(TMP . $dbName));
  151. $db->execute("CREATE TABLE test_list (id VARCHAR(255));");
  152. $db->cacheSources = true;
  153. $this->assertEquals(array('test_list'), $db->listSources());
  154. $db->cacheSources = false;
  155. $fileName = '_' . preg_replace('/[^A-Za-z0-9_\-+]/', '_', TMP . $dbName) . '_list';
  156. $result = Cache::read($fileName, '_cake_model_');
  157. $this->assertEquals(array('test_list'), $result);
  158. Cache::delete($fileName, '_cake_model_');
  159. Configure::write('Cache.disable', true);
  160. }
  161. /**
  162. * test building columns with SQLite
  163. *
  164. * @return void
  165. */
  166. public function testBuildColumn() {
  167. $data = array(
  168. 'name' => 'int_field',
  169. 'type' => 'integer',
  170. 'null' => false,
  171. );
  172. $result = $this->Dbo->buildColumn($data);
  173. $expected = '"int_field" integer NOT NULL';
  174. $this->assertEquals($expected, $result);
  175. $data = array(
  176. 'name' => 'name',
  177. 'type' => 'string',
  178. 'length' => 20,
  179. 'null' => false,
  180. );
  181. $result = $this->Dbo->buildColumn($data);
  182. $expected = '"name" varchar(20) NOT NULL';
  183. $this->assertEquals($expected, $result);
  184. $data = array(
  185. 'name' => 'testName',
  186. 'type' => 'string',
  187. 'length' => 20,
  188. 'default' => null,
  189. 'null' => true,
  190. 'collate' => 'NOCASE'
  191. );
  192. $result = $this->Dbo->buildColumn($data);
  193. $expected = '"testName" varchar(20) DEFAULT NULL COLLATE NOCASE';
  194. $this->assertEquals($expected, $result);
  195. $data = array(
  196. 'name' => 'testName',
  197. 'type' => 'string',
  198. 'length' => 20,
  199. 'default' => 'test-value',
  200. 'null' => false,
  201. );
  202. $result = $this->Dbo->buildColumn($data);
  203. $expected = '"testName" varchar(20) DEFAULT \'test-value\' NOT NULL';
  204. $this->assertEquals($expected, $result);
  205. $data = array(
  206. 'name' => 'testName',
  207. 'type' => 'integer',
  208. 'length' => 10,
  209. 'default' => 10,
  210. 'null' => false,
  211. );
  212. $result = $this->Dbo->buildColumn($data);
  213. $expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
  214. $this->assertEquals($expected, $result);
  215. $data = array(
  216. 'name' => 'testName',
  217. 'type' => 'integer',
  218. 'length' => 10,
  219. 'default' => 10,
  220. 'null' => false,
  221. 'collate' => 'BADVALUE'
  222. );
  223. $result = $this->Dbo->buildColumn($data);
  224. $expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
  225. $this->assertEquals($expected, $result);
  226. $data = array(
  227. 'name' => 'huge',
  228. 'type' => 'biginteger',
  229. 'length' => 20,
  230. 'null' => false,
  231. );
  232. $result = $this->Dbo->buildColumn($data);
  233. $expected = '"huge" bigint(20) NOT NULL';
  234. $this->assertEquals($expected, $result);
  235. $data = array(
  236. 'name' => 'id',
  237. 'type' => 'biginteger',
  238. 'length' => 20,
  239. 'null' => false,
  240. 'key' => 'primary',
  241. );
  242. $result = $this->Dbo->buildColumn($data);
  243. $expected = '"id" bigint(20) NOT NULL PRIMARY KEY';
  244. $this->assertEquals($expected, $result);
  245. }
  246. /**
  247. * test describe() and normal results.
  248. *
  249. * @return void
  250. */
  251. public function testDescribe() {
  252. $this->loadFixtures('User');
  253. $Model = new Model(array(
  254. 'name' => 'User',
  255. 'ds' => 'test',
  256. 'table' => 'users'
  257. ));
  258. $this->Dbo->cacheSources = true;
  259. Configure::write('Cache.disable', false);
  260. $result = $this->Dbo->describe($Model);
  261. $expected = array(
  262. 'id' => array(
  263. 'type' => 'integer',
  264. 'key' => 'primary',
  265. 'null' => false,
  266. 'default' => null,
  267. 'length' => 11
  268. ),
  269. 'user' => array(
  270. 'type' => 'string',
  271. 'length' => 255,
  272. 'null' => true,
  273. 'default' => null
  274. ),
  275. 'password' => array(
  276. 'type' => 'string',
  277. 'length' => 255,
  278. 'null' => true,
  279. 'default' => null
  280. ),
  281. 'created' => array(
  282. 'type' => 'datetime',
  283. 'null' => true,
  284. 'default' => null,
  285. 'length' => null,
  286. ),
  287. 'updated' => array(
  288. 'type' => 'datetime',
  289. 'null' => true,
  290. 'default' => null,
  291. 'length' => null,
  292. )
  293. );
  294. $this->assertEquals($expected, $result);
  295. $result = $this->Dbo->describe($Model->useTable);
  296. $this->assertEquals($expected, $result);
  297. $result = Cache::read('test_users', '_cake_model_');
  298. $this->assertEquals($expected, $result);
  299. }
  300. /**
  301. * Test that datatypes are reflected
  302. *
  303. * @return void
  304. */
  305. public function testDatatypes() {
  306. $this->loadFixtures('Datatype');
  307. $Model = new Model(array(
  308. 'name' => 'Datatype',
  309. 'ds' => 'test',
  310. 'table' => 'datatypes'
  311. ));
  312. $result = $this->Dbo->describe($Model);
  313. $expected = array(
  314. 'id' => array(
  315. 'type' => 'integer',
  316. 'null' => false,
  317. 'default' => '',
  318. 'length' => 11,
  319. 'key' => 'primary',
  320. ),
  321. 'float_field' => array(
  322. 'type' => 'float',
  323. 'null' => false,
  324. 'default' => '',
  325. 'length' => '5,2',
  326. ),
  327. 'decimal_field' => array(
  328. 'type' => 'decimal',
  329. 'null' => true,
  330. 'default' => '0.000',
  331. 'length' => '6,3',
  332. ),
  333. 'huge_int' => array(
  334. 'type' => 'biginteger',
  335. 'null' => true,
  336. 'default' => null,
  337. 'length' => 20,
  338. ),
  339. 'bool' => array(
  340. 'type' => 'boolean',
  341. 'null' => false,
  342. 'default' => '0',
  343. 'length' => null
  344. ),
  345. );
  346. $this->assertSame($expected, $result);
  347. }
  348. /**
  349. * test that describe does not corrupt UUID primary keys
  350. *
  351. * @return void
  352. */
  353. public function testDescribeWithUuidPrimaryKey() {
  354. $tableName = 'uuid_tests';
  355. $this->Dbo->query("CREATE TABLE {$tableName} (id VARCHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
  356. $Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
  357. $result = $this->Dbo->describe($Model);
  358. $expected = array(
  359. 'type' => 'string',
  360. 'length' => 36,
  361. 'null' => false,
  362. 'default' => null,
  363. 'key' => 'primary',
  364. );
  365. $this->assertEquals($expected, $result['id']);
  366. $this->Dbo->query('DROP TABLE ' . $tableName);
  367. $tableName = 'uuid_tests';
  368. $this->Dbo->query("CREATE TABLE {$tableName} (id CHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
  369. $Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
  370. $result = $this->Dbo->describe($Model);
  371. $expected = array(
  372. 'type' => 'string',
  373. 'length' => 36,
  374. 'null' => false,
  375. 'default' => null,
  376. 'key' => 'primary',
  377. );
  378. $this->assertEquals($expected, $result['id']);
  379. $this->Dbo->query('DROP TABLE ' . $tableName);
  380. }
  381. /**
  382. * Test virtualFields with functions.
  383. *
  384. * @return void
  385. */
  386. public function testVirtualFieldWithFunction() {
  387. $this->loadFixtures('User');
  388. $User = ClassRegistry::init('User');
  389. $User->virtualFields = array('name' => 'SUBSTR(User.user, 5, LENGTH(User.user) - 4)');
  390. $result = $User->find('first', array(
  391. 'conditions' => array('User.user' => 'garrett')
  392. ));
  393. $this->assertEquals('ett', $result['User']['name']);
  394. }
  395. /**
  396. * Test that records can be inserted with UUID primary keys, and
  397. * that the primary key is not blank
  398. *
  399. * @return void
  400. */
  401. public function testUuidPrimaryKeyInsertion() {
  402. $this->loadFixtures('Uuid');
  403. $Model = ClassRegistry::init('Uuid');
  404. $data = array(
  405. 'title' => 'A UUID should work',
  406. 'count' => 10
  407. );
  408. $Model->create($data);
  409. $this->assertTrue((bool)$Model->save());
  410. $result = $Model->read();
  411. $this->assertEquals($data['title'], $result['Uuid']['title']);
  412. $this->assertTrue(Validation::uuid($result['Uuid']['id']), 'Not a UUID');
  413. }
  414. /**
  415. * Test nested transaction
  416. *
  417. * @return void
  418. */
  419. public function testNestedTransaction() {
  420. $this->skipIf($this->Dbo->nestedTransactionSupported() === false, 'The Sqlite version do not support nested transaction');
  421. $this->loadFixtures('User');
  422. $model = new User();
  423. $model->hasOne = $model->hasMany = $model->belongsTo = $model->hasAndBelongsToMany = array();
  424. $model->cacheQueries = false;
  425. $this->Dbo->cacheMethods = false;
  426. $this->assertTrue($this->Dbo->begin());
  427. $this->assertNotEmpty($model->read(null, 1));
  428. $this->assertTrue($this->Dbo->begin());
  429. $this->assertTrue($model->delete(1));
  430. $this->assertEmpty($model->read(null, 1));
  431. $this->assertTrue($this->Dbo->rollback());
  432. $this->assertNotEmpty($model->read(null, 1));
  433. $this->assertTrue($this->Dbo->begin());
  434. $this->assertTrue($model->delete(1));
  435. $this->assertEmpty($model->read(null, 1));
  436. $this->assertTrue($this->Dbo->commit());
  437. $this->assertEmpty($model->read(null, 1));
  438. $this->assertTrue($this->Dbo->rollback());
  439. $this->assertNotEmpty($model->read(null, 1));
  440. }
  441. /**
  442. * Test the limit function.
  443. *
  444. * @return void
  445. */
  446. public function testLimit() {
  447. $db = $this->Dbo;
  448. $result = $db->limit('0');
  449. $this->assertNull($result);
  450. $result = $db->limit('10');
  451. $this->assertEquals(' LIMIT 10', $result);
  452. $result = $db->limit('FARTS', 'BOOGERS');
  453. $this->assertEquals(' LIMIT 0 OFFSET 0', $result);
  454. $result = $db->limit(20, 10);
  455. $this->assertEquals(' LIMIT 20 OFFSET 10', $result);
  456. $result = $db->limit(10, 300000000000000000000000000000);
  457. $scientificNotation = sprintf('%.1E', 300000000000000000000000000000);
  458. $this->assertNotContains($scientificNotation, $result);
  459. }
  460. /**
  461. * Test that fields are parsed out in a reasonable fashion.
  462. *
  463. * @return void
  464. */
  465. public function testFetchRowColumnParsing() {
  466. $this->loadFixtures('User');
  467. $sql = 'SELECT "User"."id", "User"."user", "User"."password", "User"."created", (1 + 1) AS "two" ' .
  468. 'FROM "users" AS "User" WHERE ' .
  469. '"User"."id" IN (SELECT MAX("id") FROM "users") ' .
  470. 'OR "User.id" IN (5, 6, 7, 8)';
  471. $result = $this->Dbo->fetchRow($sql);
  472. $expected = array(
  473. 'User' => array(
  474. 'id' => 4,
  475. 'user' => 'garrett',
  476. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  477. 'created' => '2007-03-17 01:22:23'
  478. ),
  479. 0 => array(
  480. 'two' => 2
  481. )
  482. );
  483. $this->assertEquals($expected, $result);
  484. $sql = 'SELECT "User"."id", "User"."user" ' .
  485. 'FROM "users" AS "User" WHERE "User"."id" = 4 ' .
  486. 'UNION ' .
  487. 'SELECT "User"."id", "User"."user" ' .
  488. 'FROM "users" AS "User" WHERE "User"."id" = 3';
  489. $result = $this->Dbo->fetchRow($sql);
  490. $expected = array(
  491. 'User' => array(
  492. 'id' => 3,
  493. 'user' => 'larry',
  494. ),
  495. );
  496. $this->assertEquals($expected, $result);
  497. }
  498. }