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);
// If the row is empty, continue;
if (empty($obj_res)) continue;
// 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();
@ -190,7 +193,7 @@ class SQLite_Util extends DB_Util {
$sql_array[] = $r['sql'];
}
$sql_structure = implode("\n\n", $sql_array);
$sql_structure = implode(";\n", $sql_array) . ";";
return $sql_structure;
}

View File

@ -39,6 +39,109 @@ class SQLiteTest extends UnitTestCase {
unset($this->db);
}
// --------------------------------------------------------------------------
// ! Util Method tests
// --------------------------------------------------------------------------
public function TestCreateTable()
{
//Attempt to create the table
$sql = $this->db->util->create_table('create_test',
array(
'id' => 'INTEGER',
'key' => 'TEXT',
'val' => 'TEXT',
),
array(
'id' => 'PRIMARY KEY'
)
);
$this->db->query($sql);
//Attempt to create the table
$sql = $this->db->util->create_table('create_join',
array(
'id' => 'INTEGER',
'key' => 'TEXT',
'val' => 'TEXT',
),
array(
'id' => 'PRIMARY KEY'
)
);
$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
$dbs = $this->db->get_tables();
$this->assertTrue(in_array('create_test', $dbs));
}
// --------------------------------------------------------------------------
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()
@ -73,42 +176,6 @@ class SQLiteTest extends UnitTestCase {
// --------------------------------------------------------------------------
public function TestCreateTable()
{
//Attempt to create the table
$sql = $this->db->util->create_table('create_test',
array(
'id' => 'INTEGER',
'key' => 'TEXT',
'val' => 'TEXT',
),
array(
'id' => 'PRIMARY KEY'
)
);
$this->db->query($sql);
//Attempt to create the table
$sql = $this->db->util->create_table('create_join',
array(
'id' => 'INTEGER',
'key' => 'TEXT',
'val' => 'TEXT',
),
array(
'id' => 'PRIMARY KEY'
)
);
$this->db->query($sql);
//Check
$dbs = $this->db->get_tables();
$this->assertTrue(in_array('create_test', $dbs));
}
// --------------------------------------------------------------------------
public function TestTruncate()
{
$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()
{
$this->assertFalse($this->db->get_dbs());
@ -201,6 +250,8 @@ SQL;
$this->assertFalse($this->db->get_schemas());
}
// --------------------------------------------------------------------------
// ! SQL Tests
// --------------------------------------------------------------------------
public function TestNullMethods()