2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class MockDatabaseSqlite extends DatabaseSqliteStandalone {
|
|
|
|
|
var $lastQuery;
|
|
|
|
|
|
|
|
|
|
function __construct( ) {
|
|
|
|
|
parent::__construct( ':memory:' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function query( $sql, $fname = '', $tempIgnore = false ) {
|
|
|
|
|
$this->lastQuery = $sql;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function replaceVars( $s ) {
|
|
|
|
|
return parent::replaceVars( $s );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group sqlite
|
|
|
|
|
*/
|
2010-12-28 18:17:16 +00:00
|
|
|
class DatabaseSqliteTest extends MediaWikiTestCase {
|
2010-12-14 16:26:35 +00:00
|
|
|
var $db;
|
|
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
|
if ( !Sqlite::isPresent() ) {
|
|
|
|
|
$this->markTestSkipped( 'No SQLite support detected' );
|
|
|
|
|
}
|
|
|
|
|
$this->db = new MockDatabaseSqlite();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function replaceVars( $sql ) {
|
|
|
|
|
// normalize spacing to hide implementation details
|
|
|
|
|
return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testReplaceVars() {
|
|
|
|
|
$this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
|
|
|
|
|
. "foo_bar TEXT, foo_name TEXT NOT NULL DEFAULT '', foo_int INTEGER, foo_int2 INTEGER );",
|
|
|
|
|
$this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
|
|
|
|
foo_bar char(13), foo_name varchar(255) binary NOT NULL DEFAULT '', foo_int tinyint ( 8 ), foo_int2 int(16) ) ENGINE=MyISAM;" )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( "CREATE TABLE foo ( foo1 REAL, foo2 REAL, foo3 REAL );",
|
|
|
|
|
$this->replaceVars( "CREATE TABLE foo ( foo1 FLOAT, foo2 DOUBLE( 1,10), foo3 DOUBLE PRECISION );" )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
|
|
|
|
|
$this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
|
|
|
|
|
$this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
|
|
|
|
|
'Table name changed'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( "CREATE TABLE enums( enum1 TEXT, myenum TEXT)",
|
|
|
|
|
$this->replaceVars( "CREATE TABLE enums( enum1 ENUM('A', 'B'), myenum ENUM ('X', 'Y'))" )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( "ALTER TABLE foo ADD COLUMN foo_bar INTEGER DEFAULT 42",
|
|
|
|
|
$this->replaceVars( "ALTER TABLE foo\nADD COLUMN foo_bar int(10) unsigned DEFAULT 42" )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTableName() {
|
|
|
|
|
// @todo Moar!
|
|
|
|
|
$db = new DatabaseSqliteStandalone( ':memory:' );
|
|
|
|
|
$this->assertEquals( 'foo', $db->tableName( 'foo' ) );
|
|
|
|
|
$this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
|
|
|
|
|
$db->tablePrefix( 'foo' );
|
|
|
|
|
$this->assertEquals( 'sqlite_master', $db->tableName( 'sqlite_master' ) );
|
|
|
|
|
$this->assertEquals( 'foobar', $db->tableName( 'bar' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testEntireSchema() {
|
|
|
|
|
global $IP;
|
|
|
|
|
|
|
|
|
|
$result = Sqlite::checkSqlSyntax( "$IP/maintenance/tables.sql" );
|
|
|
|
|
if ( $result !== true ) {
|
|
|
|
|
$this->fail( $result );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|