Reorganize sqlite tests and improve coverage

This commit is contained in:
Timothy Warren 2014-02-07 14:18:08 -05:00
parent f640de4e81
commit 29f824b562
2 changed files with 111 additions and 57 deletions

View File

@ -138,8 +138,11 @@ class SQLite_Util extends DB_Util {
unset($res); unset($res);
// If the row is empty, continue;
if (empty($obj_res)) continue;
// Nab the column names by getting the keys of the first row // Nab the column names by getting the keys of the first row
$columns = array_keys($obj_res[0]); $columns = array_keys(current($obj_res));
$insert_rows = array(); $insert_rows = array();
@ -190,7 +193,7 @@ class SQLite_Util extends DB_Util {
$sql_array[] = $r['sql']; $sql_array[] = $r['sql'];
} }
$sql_structure = implode("\n\n", $sql_array); $sql_structure = implode(";\n", $sql_array) . ";";
return $sql_structure; return $sql_structure;
} }

View File

@ -40,37 +40,7 @@ class SQLiteTest extends UnitTestCase {
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Util Method tests
public function TestConnection()
{
$this->assertIsA($this->db, 'SQLite');
}
// --------------------------------------------------------------------------
public function TestGetTables()
{
$tables = $this->db->get_tables();
$this->assertTrue(is_array($tables));
}
// --------------------------------------------------------------------------
public function TestGetSystemTables()
{
$tables = $this->db->get_system_tables();
$this->assertTrue(is_array($tables));
}
// --------------------------------------------------------------------------
public function TestCreateTransaction()
{
$res = $this->db->beginTransaction();
$this->assertTrue($res);
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestCreateTable() public function TestCreateTable()
@ -101,6 +71,19 @@ class SQLiteTest extends UnitTestCase {
); );
$this->db->query($sql); $this->db->query($sql);
// A table to delete
$sql = $this->db->util->create_table('create_delete',
array(
'id' => 'INTEGER',
'key' => 'TEXT',
'val' => 'TEXT',
),
array(
'id' => 'PRIMARY KEY'
)
);
$this->db->query($sql);
//Check //Check
$dbs = $this->db->get_tables(); $dbs = $this->db->get_tables();
@ -109,6 +92,90 @@ class SQLiteTest extends UnitTestCase {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestBackupData()
{
$sql = mb_trim($this->db->util->backup_data());
$sql_array = explode("\n", $sql);
$expected = <<<SQL
INSERT INTO "create_test" ("id","key","val") VALUES (1,'boogers','Gross');
INSERT INTO "create_test" ("id","key","val") VALUES (2,'works','also?');
INSERT INTO "create_test" ("id","key","val") VALUES (10,12,14);
INSERT INTO "create_test" ("id","key","val") VALUES (587,1,2);
INSERT INTO "create_test" ("id","key","val") VALUES (999,'''ring''','''sale''');
SQL;
$expected_array = explode("\n", $sql);
$this->assertEqual($expected_array, $sql_array);
}
// --------------------------------------------------------------------------
public function TestBackupStructure()
{
$sql = mb_trim($this->db->util->backup_structure());
$expected = <<<SQL
CREATE TABLE "create_test" (id INTEGER PRIMARY KEY, key TEXT , val TEXT );
CREATE TABLE "create_join" (id INTEGER PRIMARY KEY, key TEXT , val TEXT );
CREATE TABLE "create_delete" (id INTEGER PRIMARY KEY, key TEXT , val TEXT );
SQL;
$expected_array = explode("\n", $expected);
$result_array = explode("\n", $sql);
$this->assertEqual($expected_array, $result_array);
}
// --------------------------------------------------------------------------
public function TestDeleteTable()
{
$sql = $this->db->util->delete_table('create_delete');
$this->db->query($sql);
//Check
$dbs = $this->db->get_tables();
$this->assertFalse(in_array('create_delete', $dbs));
}
// --------------------------------------------------------------------------
// ! General Tests
// --------------------------------------------------------------------------
public function TestConnection()
{
$this->assertIsA($this->db, 'SQLite');
}
// --------------------------------------------------------------------------
public function TestGetTables()
{
$tables = $this->db->get_tables();
$this->assertTrue(is_array($tables));
}
// --------------------------------------------------------------------------
public function TestGetSystemTables()
{
$tables = $this->db->get_system_tables();
$this->assertTrue(is_array($tables));
}
// --------------------------------------------------------------------------
public function TestCreateTransaction()
{
$res = $this->db->beginTransaction();
$this->assertTrue($res);
}
// --------------------------------------------------------------------------
public function TestTruncate() public function TestTruncate()
{ {
$this->db->truncate('create_test'); $this->db->truncate('create_test');
@ -171,24 +238,6 @@ SQL;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// This is really time intensive ! Run only when needed
/*public function TestDeleteTable()
{
//Make sure the table exists to delete
$dbs = $this->db->get_tables();
$this->assertTrue(isset($dbs['create_test']));
//Attempt to delete the table
$sql = $this->db->sql->delete_table('create_test');
$this->db->query($sql);
//Check
$dbs = $this->db->get_tables();
$this->assertFalse(in_array('create_test', $dbs));
}*/
// --------------------------------------------------------------------------
public function TestGetDBs() public function TestGetDBs()
{ {
$this->assertFalse($this->db->get_dbs()); $this->assertFalse($this->db->get_dbs());
@ -201,6 +250,8 @@ SQL;
$this->assertFalse($this->db->get_schemas()); $this->assertFalse($this->db->get_schemas());
} }
// --------------------------------------------------------------------------
// ! SQL Tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestNullMethods() public function TestNullMethods()