Query/tests/databases/sqlite/SqliteTest.php

274 lines
6.5 KiB
PHP
Raw Normal View History

2012-03-15 09:25:18 -04:00
<?php
/**
2012-04-10 14:06:34 -04:00
* OpenSQLManager
2012-03-15 09:25:18 -04:00
*
2012-04-10 14:06:34 -04:00
* Free Database manager for Open Source Databases
2012-03-15 09:25:18 -04:00
*
* @author Timothy J. Warren
2013-01-02 14:26:42 -05:00
* @copyright Copyright (c) 2012 - 2013
2012-04-10 14:06:34 -04:00
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
2012-03-15 09:25:18 -04:00
*/
// --------------------------------------------------------------------------
/**
* SQLiteTest class.
2012-04-10 14:06:34 -04:00
*
2014-02-14 22:08:19 -05:00
* @extends DBTest
* @requires extension pdo_sqlite
2012-03-15 09:25:18 -04:00
*/
2014-02-14 22:08:19 -05:00
class SQLiteTest extends DBTest {
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function setUp()
2012-03-15 09:25:18 -04:00
{
2012-04-30 16:06:06 -04:00
$path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db';
2012-03-15 09:25:18 -04:00
$this->db = new SQLite($path);
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function tearDown()
2012-03-15 09:25:18 -04:00
{
unset($this->db);
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
// ! Util Method tests
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testCreateTable()
{
2012-03-15 09:25:18 -04:00
//Attempt to create the table
$sql = $this->db->util->create_table('create_test',
2012-03-15 09:25:18 -04:00
array(
'id' => 'INTEGER',
'key' => 'TEXT',
'val' => 'TEXT',
2012-04-10 14:06:34 -04:00
),
2012-03-15 09:25:18 -04:00
array(
'id' => 'PRIMARY KEY'
)
);
$this->db->query($sql);
2012-04-10 14:06:34 -04:00
2012-03-15 09:25:18 -04:00
//Attempt to create the table
$sql = $this->db->util->create_table('create_join',
2012-03-15 09:25:18 -04:00
array(
'id' => 'INTEGER',
'key' => 'TEXT',
'val' => 'TEXT',
2012-04-10 14:06:34 -04:00
),
2012-03-15 09:25:18 -04:00
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);
2012-03-15 09:25:18 -04:00
//Check
$dbs = $this->db->get_tables();
2012-04-10 14:06:34 -04:00
$this->assertTrue(in_array('create_test', $dbs));
2012-03-15 09:25:18 -04:00
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
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);
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
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);
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
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));
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
// ! General tests
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testConnection()
{
$this->assertIsA($this->db, 'SQLite');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testGetTables()
{
$tables = $this->db->get_tables();
$this->assertTrue(is_array($tables));
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testGetSystemTables()
{
$tables = $this->db->get_system_tables();
$this->assertTrue(is_array($tables));
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testCreateTransaction()
{
$res = $this->db->beginTransaction();
$this->assertTrue($res);
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testTruncate()
{
2012-03-15 09:25:18 -04:00
$this->db->truncate('create_test');
2012-04-10 14:06:34 -04:00
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testPreparedStatements()
2012-04-10 14:06:34 -04:00
{
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$statement = $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
$statement->execute();
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testPrepareExecute()
2012-04-10 14:06:34 -04:00
{
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->db->prepare_execute($sql, array(
2, "works", 'also?'
));
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testCommitTransaction()
2012-04-10 14:06:34 -04:00
{
$res = $this->db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)';
$this->db->query($sql);
$res = $this->db->commit();
$this->assertTrue($res);
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testRollbackTransaction()
2012-04-10 14:06:34 -04:00
{
2014-02-14 22:08:19 -05:00
$this->markTestSkipped();
2012-04-10 14:06:34 -04:00
$res = $this->db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
$this->db->query($sql);
$res = $this->db->rollback();
$this->assertTrue($res);
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testGetDBs()
2012-04-10 14:06:34 -04:00
{
2014-02-14 22:08:19 -05:00
$this->assertNull($this->db->get_dbs());
2012-04-10 14:06:34 -04:00
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testGetSchemas()
2012-04-10 14:06:34 -04:00
{
2014-02-14 22:08:19 -05:00
$this->assertNull($this->db->get_schemas());
2012-04-10 14:06:34 -04:00
}
2013-12-09 20:11:49 -05:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
// ! SQL tests
2013-12-09 20:11:49 -05:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testNullMethods()
2013-12-09 20:11:49 -05:00
{
$sql = $this->db->sql->system_table_list();
$this->assertEqual(NULL, $sql);
$sql = $this->db->sql->trigger_list();
$this->assertEqual(NULL, $sql);
$sql = $this->db->sql->function_list();
$this->assertEqual(NULL, $sql);
$sql = $this->db->sql->procedure_list();
$this->assertEqual(NULL, $sql);
$sql = $this->db->sql->sequence_list();
$this->assertEqual(NULL, $sql);
}
2014-02-14 22:08:19 -05:00
public function testGetTypes()
{
$this->markTestIncomplete();
}
2012-03-15 09:25:18 -04:00
}