Added mysql tests

This commit is contained in:
Timothy Warren 2012-03-15 15:46:04 -04:00
parent a9995ecd6c
commit 2f4dfd9d9e
6 changed files with 371 additions and 5 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
test_config.json

View File

@ -82,7 +82,7 @@ class Query_Builder {
switch($dbtype) switch($dbtype)
{ {
default: default:
$this->db = new $dbtype("host={$params->host};port={$params->port};dbname={$this->database}", $params->user, $params->pass); $this->db = new $dbtype("host={$params->host};port={$params->port};dbname={$params->database}", $params->user, $params->pass);
break; break;
case "sqlite": case "sqlite":

View File

@ -15,12 +15,230 @@
class MySQLQBTest extends UnitTestCase { class MySQLQBTest extends UnitTestCase {
function __construct() function __construct()
{ {
parent::__construct();
}
// Attempt to connect, if there is a test config file
if (is_file("../test_config.json"))
{
$params = json_decode(file_get_contents("../test_config.json"));
$params = $params->mysql;
$params->type = "mysql";
$this->db = new Query_Builder($params);
echo '<hr /> MySQL Queries <hr />';
}
}
function TestExists() function TestExists()
{ {
$this->assertTrue(in_array('mysql', pdo_drivers())); $this->assertTrue(in_array('mysql', pdo_drivers()));
} }
function TestGet()
{
if (empty($this->db)) return;
$query = $this->db->get('create_test');
$this->assertIsA($query, 'PDOStatement');
}
function TestGetLimit()
{
if (empty($this->db)) return;
$query = $this->db->get('create_test', 2);
$this->assertIsA($query, 'PDOStatement');
}
function TestGetLimitSkip()
{
if (empty($this->db)) return;
$query = $this->db->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
function TestSelectWhereGet()
{
if (empty($this->db)) return;
$query = $this->db->select('id, key as k, val')
->where('id >', 1)
->where('id <', 900)
->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
function TestSelectWhereGet2()
{
if (empty($this->db)) return;
$query = $this->db->select('id, key as k, val')
->where('id !=', 1)
->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
function TestSelectGet()
{
if (empty($this->db)) return;
$query = $this->db->select('id, key as k, val')
->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
function TestSelectFromGet()
{
if (empty($this->db)) return;
$query = $this->db->select('id, key as k, val')
->from('create_test ct')
->where('id >', 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
function TestSelectFromLimitGet()
{
if (empty($this->db)) return;
$query = $this->db->select('id, key as k, val')
->from('create_test ct')
->where('id >', 1)
->limit(3)
->get();
$this->assertIsA($query, 'PDOStatement');
}
function TestOrderBy()
{
if (empty($this->db)) return;
$query = $this->db->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
->order_by('id', 'DESC')
->order_by('k', 'ASC')
->limit(5,2)
->get();
$this->assertIsA($query, 'PDOStatement');
}
function TestOrderByRandom()
{
if (empty($this->db)) return;
$query = $this->db->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
->order_by('id', 'rand')
->limit(5,2)
->get();
$this->assertIsA($query, 'PDOStatement');
}
function TestGroupBy()
{
if (empty($this->db)) return;
$query = $this->db->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
->group_by('k')
->group_by('val')
->order_by('id', 'DESC')
->order_by('k', 'ASC')
->limit(5,2)
->get();
$this->assertIsA($query, 'PDOStatement');
}
function TestOrWhere()
{
if (empty($this->db)) return;
$query = $this->db->select('id, key as k, val')
->from('create_test')
->where(' id ', 1)
->or_where('key >', 0)
->limit(2, 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
function TestLike()
{
if (empty($this->db)) return;
$query = $this->db->from('create_test')
->like('key', 'og')
->get();
$this->assertIsA($query, 'PDOStatement');
}
function TestJoin()
{
if (empty($this->db)) return;
$query = $this->db->from('create_test')
->join('create_join cj', 'cj.id = create_test.id')
->get();
$this->assertIsA($query, 'PDOStatement');
}
function TestInsert()
{
if (empty($this->db)) return;
$query = $this->db->set('id', 4)
->set('key', 4)
->set('val', 5)
->insert('create_test');
$this->assertIsA($query, 'PDOStatement');
}
function TestUpdate()
{
if (empty($this->db)) return;
$query = $this->db->set('id', 4)
->set('key', 'gogle')
->set('val', 'non-word')
->where('id', 4)
->update('create_test');
$this->assertIsA($query, 'PDOStatement');
}
function TestDelete()
{
if (empty($this->db)) return;
$query = $this->db->where('id', 4)->delete('create_test');
$this->assertIsA($query, 'PDOStatement');
}
} }

View File

@ -21,12 +21,159 @@ class MySQLTest extends UnitTestCase {
function __construct() function __construct()
{ {
}
function setUp()
{
// Attempt to connect, if there is a test config file
if (is_file("../test_config.json"))
{
$params = json_decode(file_get_contents("../test_config.json"));
$params = $params->mysql;
$this->db = new MySQL("host={$params->host};port={$params->port};dbname={$params->database}", $params->user, $params->pass);
}
}
function tearDown()
{
unset($this->db);
} }
function TestExists() function TestExists()
{ {
$this->assertTrue(in_array('mysql', pdo_drivers())); $this->assertTrue(in_array('mysql', pdo_drivers()));
} }
function TestConnection()
{
if (empty($this->db)) return;
$this->assertIsA($this->db, 'MySQL');
}
function TestGetTables()
{
if (empty($this->db)) return;
$tables = $this->db->get_tables();
$this->assertTrue(is_array($tables));
}
function TestGetSystemTables()
{
if (empty($this->db)) return;
$tables = $this->db->get_system_tables();
$this->assertTrue(is_array($tables));
}
function TestCreateTransaction()
{
if (empty($this->db)) return;
$res = $this->db->beginTransaction();
$this->assertTrue($res);
}
function TestCreateTable()
{
if (empty($this->db)) return;
//Attempt to create the table
$sql = $this->db->sql->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->sql->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->assertEqual($dbs['create_test'], 'CREATE TABLE "create_test" (id INTEGER PRIMARY KEY, key TEXT , val TEXT )');
}
/*function TestTruncate()
{
if (empty($this->db)) return;
$this->db->truncate('create_test');
$this->assertIsA($this->db->affected_rows(), 'int');
}*/
function TestPreparedStatements()
{
if (empty($this->db)) return;
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$statement = $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
$statement->execute();
}
function TestPrepareExecute()
{
if (empty($this->db)) return;
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->db->prepare_execute($sql, array(
2, "works", 'also?'
));
}
function TestCommitTransaction()
{
if (empty($this->db)) return;
$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);
}
function TestRollbackTransaction()
{
if (empty($this->db)) return;
$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);
}
} }

Binary file not shown.

Binary file not shown.