SqliteTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 http://www.opensource.org/licenses/mit-license.php MIT License
  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. $data = array(
  234. 'name' => 'id',
  235. 'type' => 'biginteger',
  236. 'length' => 20,
  237. 'null' => false,
  238. 'key' => 'primary',
  239. );
  240. $result = $this->Dbo->buildColumn($data);
  241. $expected = '"id" bigint(20) NOT NULL PRIMARY KEY';
  242. $this->assertEquals($expected, $result);
  243. }
  244. /**
  245. * test describe() and normal results.
  246. *
  247. * @return void
  248. */
  249. public function testDescribe() {
  250. $this->loadFixtures('User');
  251. $Model = new Model(array(
  252. 'name' => 'User',
  253. 'ds' => 'test',
  254. 'table' => 'users'
  255. ));
  256. $this->Dbo->cacheSources = true;
  257. Configure::write('Cache.disable', false);
  258. $result = $this->Dbo->describe($Model);
  259. $expected = array(
  260. 'id' => array(
  261. 'type' => 'integer',
  262. 'key' => 'primary',
  263. 'null' => false,
  264. 'default' => null,
  265. 'length' => 11
  266. ),
  267. 'user' => array(
  268. 'type' => 'string',
  269. 'length' => 255,
  270. 'null' => true,
  271. 'default' => null
  272. ),
  273. 'password' => array(
  274. 'type' => 'string',
  275. 'length' => 255,
  276. 'null' => true,
  277. 'default' => null
  278. ),
  279. 'created' => array(
  280. 'type' => 'datetime',
  281. 'null' => true,
  282. 'default' => null,
  283. 'length' => null,
  284. ),
  285. 'updated' => array(
  286. 'type' => 'datetime',
  287. 'null' => true,
  288. 'default' => null,
  289. 'length' => null,
  290. )
  291. );
  292. $this->assertEquals($expected, $result);
  293. $result = $this->Dbo->describe($Model->useTable);
  294. $this->assertEquals($expected, $result);
  295. $result = Cache::read('test_users', '_cake_model_');
  296. $this->assertEquals($expected, $result);
  297. }
  298. /**
  299. * Test that datatypes are reflected
  300. *
  301. * @return void
  302. */
  303. public function testDatatypes() {
  304. $this->loadFixtures('Datatype');
  305. $Model = new Model(array(
  306. 'name' => 'Datatype',
  307. 'ds' => 'test',
  308. 'table' => 'datatypes'
  309. ));
  310. $result = $this->Dbo->describe($Model);
  311. $expected = array(
  312. 'id' => array(
  313. 'type' => 'integer',
  314. 'null' => false,
  315. 'default' => '',
  316. 'length' => 11,
  317. 'key' => 'primary',
  318. ),
  319. 'float_field' => array(
  320. 'type' => 'float',
  321. 'null' => false,
  322. 'default' => '',
  323. 'length' => '5,2',
  324. ),
  325. 'huge_int' => array(
  326. 'type' => 'biginteger',
  327. 'null' => true,
  328. 'default' => null,
  329. 'length' => 20,
  330. ),
  331. 'bool' => array(
  332. 'type' => 'boolean',
  333. 'null' => false,
  334. 'default' => '0',
  335. 'length' => null
  336. ),
  337. );
  338. $this->assertSame($expected, $result);
  339. }
  340. /**
  341. * test that describe does not corrupt UUID primary keys
  342. *
  343. * @return void
  344. */
  345. public function testDescribeWithUuidPrimaryKey() {
  346. $tableName = 'uuid_tests';
  347. $this->Dbo->query("CREATE TABLE {$tableName} (id VARCHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
  348. $Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
  349. $result = $this->Dbo->describe($Model);
  350. $expected = array(
  351. 'type' => 'string',
  352. 'length' => 36,
  353. 'null' => false,
  354. 'default' => null,
  355. 'key' => 'primary',
  356. );
  357. $this->assertEquals($expected, $result['id']);
  358. $this->Dbo->query('DROP TABLE ' . $tableName);
  359. $tableName = 'uuid_tests';
  360. $this->Dbo->query("CREATE TABLE {$tableName} (id CHAR(36) PRIMARY KEY, name VARCHAR, created DATETIME, modified DATETIME)");
  361. $Model = new Model(array('name' => 'UuidTest', 'ds' => 'test', 'table' => 'uuid_tests'));
  362. $result = $this->Dbo->describe($Model);
  363. $expected = array(
  364. 'type' => 'string',
  365. 'length' => 36,
  366. 'null' => false,
  367. 'default' => null,
  368. 'key' => 'primary',
  369. );
  370. $this->assertEquals($expected, $result['id']);
  371. $this->Dbo->query('DROP TABLE ' . $tableName);
  372. }
  373. /**
  374. * Test virtualFields with functions.
  375. *
  376. * @return void
  377. */
  378. public function testVirtualFieldWithFunction() {
  379. $this->loadFixtures('User');
  380. $User = ClassRegistry::init('User');
  381. $User->virtualFields = array('name' => 'SUBSTR(User.user, 5, LENGTH(User.user) - 4)');
  382. $result = $User->find('first', array(
  383. 'conditions' => array('User.user' => 'garrett')
  384. ));
  385. $this->assertEquals('ett', $result['User']['name']);
  386. }
  387. /**
  388. * Test that records can be inserted with uuid primary keys, and
  389. * that the primary key is not blank
  390. *
  391. * @return void
  392. */
  393. public function testUuidPrimaryKeyInsertion() {
  394. $this->loadFixtures('Uuid');
  395. $Model = ClassRegistry::init('Uuid');
  396. $data = array(
  397. 'title' => 'A uuid should work',
  398. 'count' => 10
  399. );
  400. $Model->create($data);
  401. $this->assertTrue((bool)$Model->save());
  402. $result = $Model->read();
  403. $this->assertEquals($data['title'], $result['Uuid']['title']);
  404. $this->assertTrue(Validation::uuid($result['Uuid']['id']), 'Not a uuid');
  405. }
  406. /**
  407. * Test nested transaction
  408. *
  409. * @return void
  410. */
  411. public function testNestedTransaction() {
  412. $this->skipIf($this->Dbo->nestedTransactionSupported() === false, 'The Sqlite version do not support nested transaction');
  413. $this->loadFixtures('User');
  414. $model = new User();
  415. $model->hasOne = $model->hasMany = $model->belongsTo = $model->hasAndBelongsToMany = array();
  416. $model->cacheQueries = false;
  417. $this->Dbo->cacheMethods = false;
  418. $this->assertTrue($this->Dbo->begin());
  419. $this->assertNotEmpty($model->read(null, 1));
  420. $this->assertTrue($this->Dbo->begin());
  421. $this->assertTrue($model->delete(1));
  422. $this->assertEmpty($model->read(null, 1));
  423. $this->assertTrue($this->Dbo->rollback());
  424. $this->assertNotEmpty($model->read(null, 1));
  425. $this->assertTrue($this->Dbo->begin());
  426. $this->assertTrue($model->delete(1));
  427. $this->assertEmpty($model->read(null, 1));
  428. $this->assertTrue($this->Dbo->commit());
  429. $this->assertEmpty($model->read(null, 1));
  430. $this->assertTrue($this->Dbo->rollback());
  431. $this->assertNotEmpty($model->read(null, 1));
  432. }
  433. /**
  434. * Test the limit function.
  435. *
  436. * @return void
  437. */
  438. public function testLimit() {
  439. $db = $this->Dbo;
  440. $result = $db->limit('0');
  441. $this->assertNull($result);
  442. $result = $db->limit('10');
  443. $this->assertEquals(' LIMIT 10', $result);
  444. $result = $db->limit('FARTS', 'BOOGERS');
  445. $this->assertEquals(' LIMIT 0 OFFSET 0', $result);
  446. $result = $db->limit(20, 10);
  447. $this->assertEquals(' LIMIT 20 OFFSET 10', $result);
  448. $result = $db->limit(10, 300000000000000000000000000000);
  449. $scientificNotation = sprintf('%.1E', 300000000000000000000000000000);
  450. $this->assertNotContains($scientificNotation, $result);
  451. }
  452. }