PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/phpunit/includes/db/DatabaseSqliteTest.php

https://github.com/daevid/MWFork
PHP | 312 lines | 277 code | 22 blank | 13 comment | 10 complexity | 060725ca4985e319df84a3bb862d0a57 MD5 | raw file
  1. <?php
  2. class MockDatabaseSqlite extends DatabaseSqliteStandalone {
  3. var $lastQuery;
  4. function __construct( ) {
  5. parent::__construct( ':memory:' );
  6. }
  7. function query( $sql, $fname = '', $tempIgnore = false ) {
  8. $this->lastQuery = $sql;
  9. return true;
  10. }
  11. function replaceVars( $s ) {
  12. return parent::replaceVars( $s );
  13. }
  14. }
  15. /**
  16. * @group sqlite
  17. */
  18. class DatabaseSqliteTest extends MediaWikiTestCase {
  19. var $db;
  20. public function setUp() {
  21. if ( !Sqlite::isPresent() ) {
  22. $this->markTestSkipped( 'No SQLite support detected' );
  23. }
  24. $this->db = new MockDatabaseSqlite();
  25. if ( version_compare( $this->db->getServerVersion(), '3.6.0', '<' ) ) {
  26. $this->markTestSkipped( "SQLite at least 3.6 required, {$this->db->getServerVersion()} found" );
  27. }
  28. }
  29. private function replaceVars( $sql ) {
  30. // normalize spacing to hide implementation details
  31. return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
  32. }
  33. private function assertResultIs( $expected, $res ) {
  34. $this->assertNotNull( $res );
  35. $i = 0;
  36. foreach( $res as $row ) {
  37. foreach( $expected[$i] as $key => $value ) {
  38. $this->assertTrue( isset( $row->$key ) );
  39. $this->assertEquals( $value, $row->$key );
  40. }
  41. $i++;
  42. }
  43. $this->assertEquals( count( $expected ), $i, 'Unexpected number of rows' );
  44. }
  45. public function testReplaceVars() {
  46. $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
  47. $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
  48. . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
  49. $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
  50. foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
  51. );
  52. $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
  53. $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
  54. );
  55. $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
  56. $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
  57. );
  58. $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
  59. $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
  60. 'Table name changed'
  61. );
  62. $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
  63. $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
  64. );
  65. $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
  66. $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
  67. );
  68. $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
  69. $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
  70. );
  71. $this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
  72. $this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
  73. );
  74. }
  75. public function testTableName() {
  76. // @todo Moar!
  77. $db = new DatabaseSqliteStandalone( ':memory:' );
  78. $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
  79. $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
  80. $db->tablePrefix( 'foo' );
  81. $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
  82. $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
  83. }
  84. public function testDuplicateTableStructure() {
  85. $db = new DatabaseSqliteStandalone( ':memory:' );
  86. $db->query( 'CREATE TABLE foo(foo, barfoo)' );
  87. $db->duplicateTableStructure( 'foo', 'bar' );
  88. $this->assertEquals( 'CREATE TABLE "bar"(foo, barfoo)',
  89. $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
  90. 'Normal table duplication'
  91. );
  92. $db->duplicateTableStructure( 'foo', 'baz', true );
  93. $this->assertEquals( 'CREATE TABLE "baz"(foo, barfoo)',
  94. $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
  95. 'Creation of temporary duplicate'
  96. );
  97. $this->assertEquals( 0,
  98. $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
  99. 'Create a temporary duplicate only'
  100. );
  101. }
  102. public function testDuplicateTableStructureVirtual() {
  103. $db = new DatabaseSqliteStandalone( ':memory:' );
  104. if ( $db->getFulltextSearchModule() != 'FTS3' ) {
  105. $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
  106. }
  107. $db->query( 'CREATE VIRTUAL TABLE "foo" USING FTS3(foobar)' );
  108. $db->duplicateTableStructure( 'foo', 'bar' );
  109. $this->assertEquals( 'CREATE VIRTUAL TABLE "bar" USING FTS3(foobar)',
  110. $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
  111. 'Duplication of virtual tables'
  112. );
  113. $db->duplicateTableStructure( 'foo', 'baz', true );
  114. $this->assertEquals( 'CREATE VIRTUAL TABLE "baz" USING FTS3(foobar)',
  115. $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
  116. "Can't create temporary virtual tables, should fall back to non-temporary duplication"
  117. );
  118. }
  119. public function testDeleteJoin() {
  120. $db = new DatabaseSqliteStandalone( ':memory:' );
  121. $db->query( 'CREATE TABLE a (a_1)', __METHOD__ );
  122. $db->query( 'CREATE TABLE b (b_1, b_2)', __METHOD__ );
  123. $db->insert( 'a', array(
  124. array( 'a_1' => 1 ),
  125. array( 'a_1' => 2 ),
  126. array( 'a_1' => 3 ),
  127. ),
  128. __METHOD__
  129. );
  130. $db->insert( 'b', array(
  131. array( 'b_1' => 2, 'b_2' => 'a' ),
  132. array( 'b_1' => 3, 'b_2' => 'b' ),
  133. ),
  134. __METHOD__
  135. );
  136. $db->deleteJoin( 'a', 'b', 'a_1', 'b_1', array( 'b_2' => 'a' ), __METHOD__ );
  137. $res = $db->query( "SELECT * FROM a", __METHOD__ );
  138. $this->assertResultIs( array(
  139. array( 'a_1' => 1 ),
  140. array( 'a_1' => 3 ),
  141. ),
  142. $res
  143. );
  144. }
  145. public function testEntireSchema() {
  146. global $IP;
  147. $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
  148. if ( $result !== true ) {
  149. $this->fail( $result );
  150. }
  151. $this->assertTrue( true ); // avoid test being marked as incomplete due to lack of assertions
  152. }
  153. /**
  154. * Runs upgrades of older databases and compares results with current schema
  155. * @todo: currently only checks list of tables
  156. */
  157. public function testUpgrades() {
  158. global $IP, $wgVersion;
  159. // Versions tested
  160. $versions = array(
  161. //'1.13', disabled for now, was totally screwed up
  162. // SQLite wasn't included in 1.14
  163. '1.15',
  164. '1.16',
  165. '1.17',
  166. );
  167. // Mismatches for these columns we can safely ignore
  168. $ignoredColumns = array(
  169. 'user_newtalk.user_last_timestamp', // r84185
  170. );
  171. $currentDB = new DatabaseSqliteStandalone( ':memory:' );
  172. $currentDB->sourceFile( "$IP/maintenance/tables.sql" );
  173. $currentTables = $this->getTables( $currentDB );
  174. sort( $currentTables );
  175. foreach ( $versions as $version ) {
  176. $versions = "upgrading from $version to $wgVersion";
  177. $db = $this->prepareDB( $version );
  178. $tables = $this->getTables( $db );
  179. $this->assertEquals( $currentTables, $tables, "Different tables $versions" );
  180. foreach ( $tables as $table ) {
  181. $currentCols = $this->getColumns( $currentDB, $table );
  182. $cols = $this->getColumns( $db, $table );
  183. $this->assertEquals(
  184. array_keys( $currentCols ),
  185. array_keys( $cols ),
  186. "Mismatching columns for table \"$table\" $versions"
  187. );
  188. foreach ( $currentCols as $name => $column ) {
  189. $fullName = "$table.$name";
  190. $this->assertEquals(
  191. (bool)$column->pk,
  192. (bool)$cols[$name]->pk,
  193. "PRIMARY KEY status does not match for column $fullName $versions"
  194. );
  195. if ( !in_array( $fullName, $ignoredColumns ) ) {
  196. $this->assertEquals(
  197. (bool)$column->notnull,
  198. (bool)$cols[$name]->notnull,
  199. "NOT NULL status does not match for column $fullName $versions"
  200. );
  201. $this->assertEquals(
  202. $column->dflt_value,
  203. $cols[$name]->dflt_value,
  204. "Default values does not match for column $fullName $versions"
  205. );
  206. }
  207. }
  208. $currentIndexes = $this->getIndexes( $currentDB, $table );
  209. $indexes = $this->getIndexes( $db, $table );
  210. $this->assertEquals(
  211. array_keys( $currentIndexes ),
  212. array_keys( $indexes ),
  213. "mismatching indexes for table \"$table\" $versions"
  214. );
  215. }
  216. $db->close();
  217. }
  218. }
  219. private function prepareDB( $version ) {
  220. static $maint = null;
  221. if ( $maint === null ) {
  222. $maint = new FakeMaintenance();
  223. $maint->loadParamsAndArgs( null, array( 'quiet' => 1 ) );
  224. }
  225. $db = new DatabaseSqliteStandalone( ':memory:' );
  226. $db->sourceFile( dirname( __FILE__ ) . "/sqlite/tables-$version.sql" );
  227. $updater = DatabaseUpdater::newForDB( $db, false, $maint );
  228. $updater->doUpdates( array( 'core' ) );
  229. return $db;
  230. }
  231. private function getTables( $db ) {
  232. $list = array_flip( $db->listTables() );
  233. $excluded = array(
  234. 'math', // moved out of core in 1.18
  235. 'searchindex',
  236. 'searchindex_content',
  237. 'searchindex_segments',
  238. 'searchindex_segdir',
  239. // FTS4 ready!!1
  240. 'searchindex_docsize',
  241. 'searchindex_stat',
  242. );
  243. foreach ( $excluded as $t ) {
  244. unset( $list[$t] );
  245. }
  246. $list = array_flip( $list );
  247. sort( $list );
  248. return $list;
  249. }
  250. private function getColumns( $db, $table ) {
  251. $cols = array();
  252. $res = $db->query( "PRAGMA table_info($table)" );
  253. $this->assertNotNull( $res );
  254. foreach ( $res as $col ) {
  255. $cols[$col->name] = $col;
  256. }
  257. ksort( $cols );
  258. return $cols;
  259. }
  260. private function getIndexes( $db, $table ) {
  261. $indexes = array();
  262. $res = $db->query( "PRAGMA index_list($table)" );
  263. $this->assertNotNull( $res );
  264. foreach ( $res as $index ) {
  265. $res2 = $db->query( "PRAGMA index_info({$index->name})" );
  266. $this->assertNotNull( $res2 );
  267. $index->columns = array();
  268. foreach ( $res2 as $col ) {
  269. $index->columns[] = $col;
  270. }
  271. $indexes[$index->name] = $index;
  272. }
  273. ksort( $indexes );
  274. return $indexes;
  275. }
  276. }