summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTaufan Aditya <toopay@taufanaditya.com>2012-04-05 14:01:34 +0200
committerTaufan Aditya <toopay@taufanaditya.com>2012-04-05 14:01:34 +0200
commit55f622f76371949ae615ccdff76ed1e4bab55170 (patch)
tree674a8ee2bd694f77b2f3775055e66a085c167f51
parent36897fba4c825e612e3e7d772174c95093aa7f96 (diff)
Add select clause test
-rw-r--r--tests/codeigniter/database/query_builder/get_test.php31
-rw-r--r--tests/codeigniter/database/query_builder/select_test.php37
-rw-r--r--tests/mocks/database/schema/skeleton.php55
3 files changed, 104 insertions, 19 deletions
diff --git a/tests/codeigniter/database/query_builder/get_test.php b/tests/codeigniter/database/query_builder/get_test.php
index e6fe319c5..dd946a40c 100644
--- a/tests/codeigniter/database/query_builder/get_test.php
+++ b/tests/codeigniter/database/query_builder/get_test.php
@@ -9,19 +9,10 @@ class Get_test extends CI_TestCase {
public function set_up()
{
- $config = Mock_Database_DB::config(DB_DRIVER);
- $connection = new Mock_Database_DB($config);
- $db = Mock_Database_DB::DB($connection->set_dsn(DB_DRIVER), TRUE);
+ $db = Mock_Database_Schema_Skeleton::init(DB_DRIVER);
- $this->ci_instance_var('db', $db);
-
- $loader = new Mock_Core_Loader();
- $loader->dbforge();
-
- $forge = $this->ci_instance->dbforge;
-
- Mock_Database_Schema_Skeleton::create_tables($forge, DB_DRIVER);
- Mock_Database_Schema_Skeleton::create_data($db);
+ Mock_Database_Schema_Skeleton::create_tables();
+ Mock_Database_Schema_Skeleton::create_data();
$this->query_builder = $db;
}
@@ -44,5 +35,21 @@ class Get_test extends CI_TestCase {
$this->assertEquals('Accountant', $jobs[2]['name']);
$this->assertEquals('Musician', $jobs[3]['name']);
}
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * @see ./mocks/schema/skeleton.php
+ */
+ public function test_get_where()
+ {
+ $job1 = $this->query_builder->get('job', array('id' => 1))->result_array();
+
+ // Dummy jobs contain 1 rows
+ $this->assertCount(1, $job1);
+
+ // Check rows item
+ $this->assertEquals('Developer', $job1[0]['name']);
+ }
} \ No newline at end of file
diff --git a/tests/codeigniter/database/query_builder/select_test.php b/tests/codeigniter/database/query_builder/select_test.php
new file mode 100644
index 000000000..ba2f21b64
--- /dev/null
+++ b/tests/codeigniter/database/query_builder/select_test.php
@@ -0,0 +1,37 @@
+<?php
+
+class Select_test extends CI_TestCase {
+
+ /**
+ * @var object Database/Query Builder holder
+ */
+ protected $query_builder;
+
+ public function set_up()
+ {
+ $db = Mock_Database_Schema_Skeleton::init(DB_DRIVER);
+
+ Mock_Database_Schema_Skeleton::create_tables();
+ Mock_Database_Schema_Skeleton::create_data();
+
+ $this->query_builder = $db;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * @see ./mocks/schema/skeleton.php
+ */
+ public function test_select_only_one_collumn()
+ {
+ $jobs_name = $this->query_builder->select('name')
+ ->get('job')
+ ->result_array();
+
+ // Check rows item
+ $this->assertArrayHasKey('name',$jobs_name[0]);
+ $this->assertFalse(array_key_exists('id', $jobs_name[0]));
+ $this->assertFalse(array_key_exists('description', $jobs_name[0]));
+ }
+
+} \ No newline at end of file
diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php
index 6610d6b59..a3d5bac65 100644
--- a/tests/mocks/database/schema/skeleton.php
+++ b/tests/mocks/database/schema/skeleton.php
@@ -1,16 +1,57 @@
<?php
class Mock_Database_Schema_Skeleton {
+
+ /**
+ * @var object Database Holder
+ */
+ public static $db;
+
+ /**
+ * @var object Forge Holder
+ */
+ public static $forge;
+
+ /**
+ * @var object Driver Holder
+ */
+ public static $driver;
+
+ /**
+ * Initialize both database and forge components
+ */
+ public static function init($driver)
+ {
+ if (empty(static::$db) && empty(static::$forge))
+ {
+ $config = Mock_Database_DB::config($driver);
+ $connection = new Mock_Database_DB($config);
+ $db = Mock_Database_DB::DB($connection->set_dsn($driver), TRUE);
+
+ CI_TestCase::instance()->ci_instance_var('db', $db);
+
+ $loader = new Mock_Core_Loader();
+ $loader->dbforge();
+ $forge = CI_TestCase::instance()->ci_instance_var('dbforge');
+
+ static::$db = $db;
+ static::$forge = $forge;
+ static::$driver = $driver;
+ }
+
+ return static::$db;
+ }
+
/**
* Create the dummy tables
*
* @return void
*/
- public static function create_tables($forge, $driver)
+ public static function create_tables()
{
// Job Table
- $forge->add_field(array(
+ static::$forge->add_field(array(
'id' => array(
'type' => 'INTEGER',
'constraint' => 3,
@@ -23,8 +64,8 @@ class Mock_Database_Schema_Skeleton {
'type' => 'TEXT',
),
));
- $forge->add_key('id', TRUE);
- $forge->create_table('job', (strpos($driver, 'pgsql') === FALSE));
+ static::$forge->add_key('id', TRUE);
+ static::$forge->create_table('job', (strpos(static::$driver, 'pgsql') === FALSE));
}
/**
@@ -32,7 +73,7 @@ class Mock_Database_Schema_Skeleton {
*
* @return void
*/
- public static function create_data($db)
+ public static function create_data()
{
// Job Data
$data = array(
@@ -46,11 +87,11 @@ class Mock_Database_Schema_Skeleton {
foreach ($data as $table => $dummy_data)
{
- $db->truncate($table);
+ static::$db->truncate($table);
foreach ($dummy_data as $single_dummy_data)
{
- $db->insert($table, $single_dummy_data);
+ static::$db->insert($table, $single_dummy_data);
}
}
}