SQLite is picky about the position of AUTOINCREMENT in field definition, handle it...
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseSqliteTest.php
1 <?php
2
3 class MockDatabaseSqlite extends DatabaseSqliteStandalone {
4 var $lastQuery;
5
6 function __construct( ) {
7 parent::__construct( ':memory:' );
8 }
9
10 function query( $sql, $fname = '', $tempIgnore = false ) {
11 $this->lastQuery = $sql;
12 return true;
13 }
14
15 function replaceVars( $s ) {
16 return parent::replaceVars( $s );
17 }
18 }
19
20 /**
21 * @group sqlite
22 */
23 class DatabaseSqliteTest extends MediaWikiTestCase {
24 var $db;
25
26 public function setUp() {
27 if ( !Sqlite::isPresent() ) {
28 $this->markTestSkipped( 'No SQLite support detected' );
29 }
30 $this->db = new MockDatabaseSqlite();
31 }
32
33 private function replaceVars( $sql ) {
34 // normalize spacing to hide implementation details
35 return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
36 }
37
38 public function testReplaceVars() {
39 $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
40
41 $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
42 . "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
43 $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
44 foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
45 );
46
47 $this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
48 $this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
49 );
50
51 $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
52 $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
53 );
54
55 $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
56 $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
57 'Table name changed'
58 );
59
60 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
61 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY NOT NULL AUTO_INCREMENT );" )
62 );
63 $this->assertEquals( "CREATE TABLE foo ( foobar INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL );",
64 $this->replaceVars("CREATE TABLE foo ( foobar INT PRIMARY KEY AUTO_INCREMENT NOT NULL );" )
65 );
66
67 $this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
68 $this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
69 );
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
76 public function testTableName() {
77 // @todo Moar!
78 $db = new DatabaseSqliteStandalone( ':memory:' );
79 $this->assertEquals( 'foo', $db->tableName( 'foo' ) );
80 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
81 $db->tablePrefix( 'foo' );
82 $this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
83 $this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
84 }
85
86 public function testDuplicateTableStructure() {
87 $db = new DatabaseSqliteStandalone( ':memory:' );
88 $db->query( 'CREATE TABLE foo(foo, barfoo)' );
89
90 $db->duplicateTableStructure( 'foo', 'bar' );
91 $this->assertEquals( 'CREATE TABLE bar(foo, barfoo)',
92 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
93 'Normal table duplication'
94 );
95
96 $db->duplicateTableStructure( 'foo', 'baz', true );
97 $this->assertEquals( 'CREATE TABLE baz(foo, barfoo)',
98 $db->selectField( 'sqlite_temp_master', 'sql', array( 'name' => 'baz' ) ),
99 'Creation of temporary duplicate'
100 );
101 $this->assertEquals( 0,
102 $db->selectField( 'sqlite_master', 'COUNT(*)', array( 'name' => 'baz' ) ),
103 'Create a temporary duplicate only'
104 );
105 }
106
107 public function testDuplicateTableStructureVirtual() {
108 $db = new DatabaseSqliteStandalone( ':memory:' );
109 if ( $db->getFulltextSearchModule() != 'FTS3' ) {
110 $this->markTestSkipped( 'FTS3 not supported, cannot create virtual tables' );
111 }
112 $db->query( 'CREATE VIRTUAL TABLE foo USING FTS3(foobar)' );
113
114 $db->duplicateTableStructure( 'foo', 'bar' );
115 $this->assertEquals( 'CREATE VIRTUAL TABLE bar USING FTS3(foobar)',
116 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'bar' ) ),
117 'Duplication of virtual tables'
118 );
119
120 $db->duplicateTableStructure( 'foo', 'baz', true );
121 $this->assertEquals( 'CREATE VIRTUAL TABLE baz USING FTS3(foobar)',
122 $db->selectField( 'sqlite_master', 'sql', array( 'name' => 'baz' ) ),
123 "Can't create temporary virtual tables, should fall back to non-temporary duplication"
124 );
125 }
126
127 function testEntireSchema() {
128 global $IP;
129
130 $result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
131 if ( $result !== true ) {
132 $this->fail( $result );
133 }
134 }
135 }