diff options
author | Taufan Aditya <toopay@taufanaditya.com> | 2012-04-04 20:24:50 +0200 |
---|---|---|
committer | Taufan Aditya <toopay@taufanaditya.com> | 2012-04-04 20:24:50 +0200 |
commit | 5b421665bac7609192917f7b0f34838534be099e (patch) | |
tree | 0222ee5891a628737d483229949998ad7f949ee8 | |
parent | 61ff054fb905f0514b942bf3e70a6c882bd64f7a (diff) |
Simple query builder tests
-rw-r--r-- | tests/codeigniter/database/query_builder/get_test.php | 48 | ||||
-rw-r--r-- | tests/mocks/database/db/querybuilder.php | 10 | ||||
-rw-r--r-- | tests/mocks/database/schema/skeleton.php | 65 |
3 files changed, 123 insertions, 0 deletions
diff --git a/tests/codeigniter/database/query_builder/get_test.php b/tests/codeigniter/database/query_builder/get_test.php new file mode 100644 index 000000000..6f98ded4f --- /dev/null +++ b/tests/codeigniter/database/query_builder/get_test.php @@ -0,0 +1,48 @@ +<?php + +class Get_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $query_builder; + + 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); + + $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); + Mock_Database_Schema_Skeleton::create_data($db); + + $this->query_builder = $db; + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_get_simple() + { + $jobs = $this->query_builder->get('job')->result_array(); + + // Dummy jobs contain 4 rows + $this->assertCount(4, $jobs); + + // Check rows item + $this->assertEquals('Developer', $jobs[0]['name']); + $this->assertEquals('Politician', $jobs[1]['name']); + $this->assertEquals('Accountant', $jobs[2]['name']); + $this->assertEquals('Musician', $jobs[3]['name']); + } + +}
\ No newline at end of file diff --git a/tests/mocks/database/db/querybuilder.php b/tests/mocks/database/db/querybuilder.php new file mode 100644 index 000000000..1b95c92af --- /dev/null +++ b/tests/mocks/database/db/querybuilder.php @@ -0,0 +1,10 @@ +<?php + +if ( ! class_exists('CI_DB_query_builder')) +{ + class Mock_Database_DB_QueryBuilder extends CI_DB_active_record {} +} +else +{ + class Mock_Database_DB_QueryBuilder extends CI_DB_query_builder {} +} diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php new file mode 100644 index 000000000..bf9e75b45 --- /dev/null +++ b/tests/mocks/database/schema/skeleton.php @@ -0,0 +1,65 @@ +<?php + +class Mock_Database_Schema_Skeleton { + + /** + * Create the dummy tables + * + * @return void + */ + public static function create_tables($forge) + { + // Job Table + $forge->add_field(array( + 'id' => array( + 'type' => 'INT', + 'constraint' => 3, + ), + 'name' => array( + 'type' => 'VARCHAR', + 'constraint' => 40, + ), + 'description' => array( + 'type' => 'TEXT', + 'constraint' => 0, + ), + )); + $forge->add_key('id', TRUE); + $forge->create_table('job', TRUE); + } + + /** + * Create the dummy datas + * + * @return void + */ + public static function create_data($db) + { + // Job Data + $data = array( + 'job' => array( + array('id' => 1, 'name' => 'Developer', 'description' => 'Awesome job, but sometimes makes you bored'), + array('id' => 2, 'name' => 'Politician', 'description' => 'This is not really a job'), + array('id' => 3, 'name' => 'Accountant', 'description' => 'Boring job, but you will get free snack at lunch'), + array('id' => 4, 'name' => 'Musician', 'description' => 'Only Coldplay can actually called Musician'), + ), + ); + + foreach ($data as $table => $dummy_data) + { + $db->truncate($table); + + if (strpos(DB_DRIVER, 'sqlite') === FALSE) + { + $db->insert_batch($table, $dummy_data); + } + else + { + foreach ($dummy_data as $single_dummy_data) + { + $db->insert($table, $single_dummy_data); + } + } + } + } +}
\ No newline at end of file |