diff options
Diffstat (limited to 'tests/codeigniter/database/query_builder')
16 files changed, 153 insertions, 122 deletions
diff --git a/tests/codeigniter/database/query_builder/count_test.php b/tests/codeigniter/database/query_builder/count_test.php index 5e691692d..90ac5283e 100644 --- a/tests/codeigniter/database/query_builder/count_test.php +++ b/tests/codeigniter/database/query_builder/count_test.php @@ -22,10 +22,7 @@ class Count_test extends CI_TestCase { */ public function test_count_all() { - $job_count = $this->db->count_all('job'); - - // Check the result - $this->assertEquals(4, $job_count); + $this->assertEquals(4, $this->db->count_all('job')); } // ------------------------------------------------------------------------ @@ -35,10 +32,7 @@ class Count_test extends CI_TestCase { */ public function test_count_all_results() { - $job_count = $this->db->like('name', 'ian') - ->count_all_results('job'); - - // Check the result - $this->assertEquals(2, $job_count); + $this->assertEquals(2, $this->db->like('name', 'ian')->count_all_results('job')); } + }
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/delete_test.php b/tests/codeigniter/database/query_builder/delete_test.php index 84ea7616f..ab9d97f56 100644 --- a/tests/codeigniter/database/query_builder/delete_test.php +++ b/tests/codeigniter/database/query_builder/delete_test.php @@ -23,9 +23,7 @@ class Delete_test extends CI_TestCase { public function test_delete() { // Check initial record - $job1 = $this->db->where('id', 1) - ->get('job') - ->row(); + $job1 = $this->db->where('id', 1)->get('job')->row(); $this->assertEquals('Developer', $job1->name); @@ -33,8 +31,7 @@ class Delete_test extends CI_TestCase { $this->db->delete('job', array('id' => 1)); // Check the record - $job1 = $this->db->where('id', 1) - ->get('job'); + $job1 = $this->db->where('id', 1)->get('job'); $this->assertEmpty($job1->result_array()); } @@ -47,13 +44,8 @@ class Delete_test extends CI_TestCase { public function test_delete_several_tables() { // Check initial record - $user4 = $this->db->where('id', 4) - ->get('user') - ->row(); - - $job4 = $this->db->where('id', 4) - ->get('job') - ->row(); + $user4 = $this->db->where('id', 4)->get('user')->row(); + $job4 = $this->db->where('id', 4)->get('job')->row(); $this->assertEquals('Musician', $job4->name); $this->assertEquals('Chris Martin', $user4->name); diff --git a/tests/codeigniter/database/query_builder/distinct_test.php b/tests/codeigniter/database/query_builder/distinct_test.php index 925eadb19..cc98009ce 100644 --- a/tests/codeigniter/database/query_builder/distinct_test.php +++ b/tests/codeigniter/database/query_builder/distinct_test.php @@ -23,11 +23,10 @@ class Distinct_test extends CI_TestCase { public function test_distinct() { $users = $this->db->select('country') - ->distinct() - ->get('user') - ->result_array(); - - // Check the result + ->distinct() + ->get('user') + ->result_array(); + $this->assertEquals(3, count($users)); } diff --git a/tests/codeigniter/database/query_builder/escape_test.php b/tests/codeigniter/database/query_builder/escape_test.php new file mode 100644 index 000000000..27e678f22 --- /dev/null +++ b/tests/codeigniter/database/query_builder/escape_test.php @@ -0,0 +1,68 @@ +<?php + +class Escape_test extends CI_TestCase { + + /** + * @var object Database/Query Builder holder + */ + protected $db; + + public function set_up() + { + $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); + + Mock_Database_Schema_Skeleton::create_tables(); + Mock_Database_Schema_Skeleton::create_data(); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_escape_like_percent_sign() + { + // Escape the like string + $string = $this->db->escape_like_str('\%foo'); + + if (strpos(DB_DRIVER, 'mysql') !== FALSE) + { + $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%' ESCAPE '!';"; + } + else + { + $sql = 'SELECT "value" FROM "misc" WHERE "key" LIKE \''.$string.'%\' ESCAPE \'!\';'; + } + + $res = $this->db->query($sql)->result_array(); + + // Check the result + $this->assertEquals(1, count($res)); + } + + // ------------------------------------------------------------------------ + + /** + * @see ./mocks/schema/skeleton.php + */ + public function test_escape_like_backslash_sign() + { + // Escape the like string + $string = $this->db->escape_like_str('\\'); + + if (strpos(DB_DRIVER, 'mysql') !== FALSE) + { + $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%' ESCAPE '!';"; + } + else + { + $sql = 'SELECT "value" FROM "misc" WHERE "key" LIKE \''.$string.'%\' ESCAPE \'!\';'; + } + + $res = $this->db->query($sql)->result_array(); + + // Check the result + $this->assertEquals(2, count($res)); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/from_test.php b/tests/codeigniter/database/query_builder/from_test.php index 95ae4dfdb..7aaae348d 100644 --- a/tests/codeigniter/database/query_builder/from_test.php +++ b/tests/codeigniter/database/query_builder/from_test.php @@ -23,10 +23,9 @@ class From_test extends CI_TestCase { public function test_from_simple() { $jobs = $this->db->from('job') - ->get() - ->result_array(); - - // Check items + ->get() + ->result_array(); + $this->assertEquals(4, count($jobs)); } @@ -38,14 +37,13 @@ class From_test extends CI_TestCase { public function test_from_with_where() { $job1 = $this->db->from('job') - ->where('id', 1) - ->get() - ->row(); - - // Check the result + ->where('id', 1) + ->get() + ->row(); + $this->assertEquals('1', $job1->id); $this->assertEquals('Developer', $job1->name); $this->assertEquals('Awesome job, but sometimes makes you bored', $job1->description); } - + }
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/get_test.php b/tests/codeigniter/database/query_builder/get_test.php index 0751c9332..156027537 100644 --- a/tests/codeigniter/database/query_builder/get_test.php +++ b/tests/codeigniter/database/query_builder/get_test.php @@ -23,7 +23,7 @@ class Get_test extends CI_TestCase { public function test_get_simple() { $jobs = $this->db->get('job')->result_array(); - + // Dummy jobs contain 4 rows $this->assertCount(4, $jobs); @@ -41,13 +41,13 @@ class Get_test extends CI_TestCase { */ public function test_get_where() { - $job1 = $this->db->get('job', array('id' => 1))->result_array(); - + $job1 = $this->db->get_where('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/group_test.php b/tests/codeigniter/database/query_builder/group_test.php index 7d8abc33f..5249f7c87 100644 --- a/tests/codeigniter/database/query_builder/group_test.php +++ b/tests/codeigniter/database/query_builder/group_test.php @@ -23,12 +23,11 @@ class Group_test extends CI_TestCase { public function test_group_by() { $jobs = $this->db->select('name') - ->from('job') - ->group_by('name') - ->get() - ->result_array(); - - // Check the result + ->from('job') + ->group_by('name') + ->get() + ->result_array(); + $this->assertEquals(4, count($jobs)); } @@ -40,14 +39,13 @@ class Group_test extends CI_TestCase { public function test_having_by() { $jobs = $this->db->select('name') - ->from('job') - ->group_by('name') - ->having('SUM(id) > 2') - ->get() - ->result_array(); - - // Check the result + ->from('job') + ->group_by('name') + ->having('SUM(id) > 2') + ->get() + ->result_array(); + $this->assertEquals(2, count($jobs)); } - + }
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/insert_test.php b/tests/codeigniter/database/query_builder/insert_test.php index 8ba60e242..a9aafb66e 100644 --- a/tests/codeigniter/database/query_builder/insert_test.php +++ b/tests/codeigniter/database/query_builder/insert_test.php @@ -26,7 +26,7 @@ class Insert_test extends CI_TestCase { public function test_insert() { $job_data = array('id' => 1, 'name' => 'Grocery Sales', 'description' => 'Discount!'); - + // Do normal insert $this->assertTrue($this->db->insert('job', $job_data)); @@ -45,10 +45,10 @@ class Insert_test extends CI_TestCase { public function test_insert_batch() { $job_datas = array( - array('id' => 2, 'name' => 'Commedian', 'description' => 'Theres something in your teeth'), + array('id' => 2, 'name' => 'Commedian', 'description' => 'Theres something in your teeth'), array('id' => 3, 'name' => 'Cab Driver', 'description' => 'Iam yellow'), ); - + // Do insert batch except for sqlite driver if (strpos(DB_DRIVER, 'sqlite') === FALSE) { @@ -62,5 +62,5 @@ class Insert_test extends CI_TestCase { $this->assertEquals('Cab Driver', $job_3->name); } } - + }
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/join_test.php b/tests/codeigniter/database/query_builder/join_test.php index e05329d67..25bd4accb 100644 --- a/tests/codeigniter/database/query_builder/join_test.php +++ b/tests/codeigniter/database/query_builder/join_test.php @@ -34,5 +34,25 @@ class Join_test extends CI_TestCase { $this->assertEquals('Derek Jones', $job_user[0]['user_name']); $this->assertEquals('Developer', $job_user[0]['job_name']); } - + + // ------------------------------------------------------------------------ + + public function test_join_escape_multiple_conditions() + { + // We just need a valid query produced, not one that makes sense + $fields = array($this->db->protect_identifiers('table1.field1'), $this->db->protect_identifiers('table2.field2')); + + $expected = 'SELECT '.implode(', ', $fields) + ."\nFROM ".$this->db->escape_identifiers('table1') + ."\nLEFT JOIN ".$this->db->escape_identifiers('table2').' ON '.implode(' = ', $fields) + .' AND '.$fields[0]." = 'foo' AND ".$fields[1].' = 0'; + + $result = $this->db->select('table1.field1, table2.field2') + ->from('table1') + ->join('table2', "table1.field1 = table2.field2 AND table1.field1 = 'foo' AND table2.field2 = 0", 'LEFT') + ->get_compiled_select(); + + $this->assertEquals($expected, $result); + } + }
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/like_test.php b/tests/codeigniter/database/query_builder/like_test.php index df98c713f..5f3e52228 100644 --- a/tests/codeigniter/database/query_builder/like_test.php +++ b/tests/codeigniter/database/query_builder/like_test.php @@ -86,5 +86,5 @@ class Like_test extends CI_TestCase { $this->assertEquals('Accountant', $jobs[1]['name']); $this->assertEquals('Musician', $jobs[2]['name']); } - + }
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/limit_test.php b/tests/codeigniter/database/query_builder/limit_test.php index 704f3b651..a0954c7ab 100644 --- a/tests/codeigniter/database/query_builder/limit_test.php +++ b/tests/codeigniter/database/query_builder/limit_test.php @@ -25,8 +25,7 @@ class Limit_test extends CI_TestCase { $jobs = $this->db->limit(2) ->get('job') ->result_array(); - - // Check the result + $this->assertEquals(2, count($jobs)); } @@ -40,10 +39,10 @@ class Limit_test extends CI_TestCase { $jobs = $this->db->limit(2, 2) ->get('job') ->result_array(); - - // Check the result + $this->assertEquals(2, count($jobs)); $this->assertEquals('Accountant', $jobs[0]['name']); $this->assertEquals('Musician', $jobs[1]['name']); } + }
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/order_test.php b/tests/codeigniter/database/query_builder/order_test.php index 01aa1c2b4..46f452bae 100644 --- a/tests/codeigniter/database/query_builder/order_test.php +++ b/tests/codeigniter/database/query_builder/order_test.php @@ -25,7 +25,7 @@ class Order_test extends CI_TestCase { $jobs = $this->db->order_by('name', 'asc') ->get('job') ->result_array(); - + // Check the result $this->assertEquals(4, count($jobs)); $this->assertEquals('Accountant', $jobs[0]['name']); @@ -44,12 +44,12 @@ class Order_test extends CI_TestCase { $jobs = $this->db->order_by('name', 'desc') ->get('job') ->result_array(); - - // Check the result + $this->assertEquals(4, count($jobs)); $this->assertEquals('Politician', $jobs[0]['name']); $this->assertEquals('Musician', $jobs[1]['name']); $this->assertEquals('Developer', $jobs[2]['name']); $this->assertEquals('Accountant', $jobs[3]['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 index 0d299ed16..877b5d8c0 100644 --- a/tests/codeigniter/database/query_builder/select_test.php +++ b/tests/codeigniter/database/query_builder/select_test.php @@ -25,7 +25,7 @@ class Select_test extends CI_TestCase { $jobs_name = $this->db->select('name') ->get('job') ->result_array(); - + // Check rows item $this->assertArrayHasKey('name',$jobs_name[0]); $this->assertFalse(array_key_exists('id', $jobs_name[0])); @@ -42,7 +42,7 @@ class Select_test extends CI_TestCase { $job_min = $this->db->select_min('id') ->get('job') ->row(); - + // Minimum id was 1 $this->assertEquals('1', $job_min->id); } @@ -57,7 +57,7 @@ class Select_test extends CI_TestCase { $job_max = $this->db->select_max('id') ->get('job') ->row(); - + // Maximum id was 4 $this->assertEquals('4', $job_max->id); } @@ -72,7 +72,7 @@ class Select_test extends CI_TestCase { $job_avg = $this->db->select_avg('id') ->get('job') ->row(); - + // Average should be 2.5 $this->assertEquals('2.5', $job_avg->id); } @@ -87,9 +87,9 @@ class Select_test extends CI_TestCase { $job_sum = $this->db->select_sum('id') ->get('job') ->row(); - + // Sum of ids should be 10 $this->assertEquals('10', $job_sum->id); } - + }
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/truncate_test.php b/tests/codeigniter/database/query_builder/truncate_test.php index 2a9c8a91e..09923c7f1 100644 --- a/tests/codeigniter/database/query_builder/truncate_test.php +++ b/tests/codeigniter/database/query_builder/truncate_test.php @@ -24,7 +24,6 @@ class Truncate_test extends CI_TestCase { { // Check initial record $jobs = $this->db->get('job')->result_array(); - $this->assertEquals(4, count($jobs)); // Do the empty @@ -32,7 +31,6 @@ class Truncate_test extends CI_TestCase { // Check the record $jobs = $this->db->get('job'); - $this->assertEmpty($jobs->result_array()); } @@ -45,16 +43,13 @@ class Truncate_test extends CI_TestCase { { // Check initial record $users = $this->db->get('user')->result_array(); - $this->assertEquals(4, count($users)); // Do the empty - $this->db->from('user') - ->truncate(); + $this->db->from('user')->truncate(); // Check the record $users = $this->db->get('user'); - $this->assertEmpty($users->result_array()); } diff --git a/tests/codeigniter/database/query_builder/update_test.php b/tests/codeigniter/database/query_builder/update_test.php index f5bbffd4f..27a647c45 100644 --- a/tests/codeigniter/database/query_builder/update_test.php +++ b/tests/codeigniter/database/query_builder/update_test.php @@ -23,23 +23,14 @@ class Update_test extends CI_TestCase { public function test_update() { // Check initial record - $job1 = $this->db->where('id', 1) - ->get('job') - ->row(); - + $job1 = $this->db->where('id', 1)->get('job')->row(); $this->assertEquals('Developer', $job1->name); // Do the update - $job_data = array('name' => 'Programmer'); - - $this->db->where('id', 1) - ->update('job', $job_data); + $this->db->where('id', 1)->update('job', array('name' => 'Programmer')); // Check updated record - $job1 = $this->db->where('id', 1) - ->get('job') - ->row(); - + $job1 = $this->db->where('id', 1)->get('job')->row(); $this->assertEquals('Programmer', $job1->name); } @@ -51,10 +42,7 @@ class Update_test extends CI_TestCase { public function test_update_with_set() { // Check initial record - $job1 = $this->db->where('id', 4) - ->get('job') - ->row(); - + $job1 = $this->db->where('id', 4)->get('job')->row(); $this->assertEquals('Musician', $job1->name); // Do the update @@ -62,10 +50,8 @@ class Update_test extends CI_TestCase { $this->db->update('job', NULL, 'id = 4'); // Check updated record - $job1 = $this->db->where('id', 4) - ->get('job') - ->row(); - + $job1 = $this->db->where('id', 4)->get('job')->row(); $this->assertEquals('Vocalist', $job1->name); } + }
\ No newline at end of file diff --git a/tests/codeigniter/database/query_builder/where_test.php b/tests/codeigniter/database/query_builder/where_test.php index 607eaa076..20b7a567c 100644 --- a/tests/codeigniter/database/query_builder/where_test.php +++ b/tests/codeigniter/database/query_builder/where_test.php @@ -22,11 +22,8 @@ class Where_test extends CI_TestCase { */ public function test_where_simple_key_value() { - $job1 = $this->db->where('id', 1) - ->get('job') - ->row(); + $job1 = $this->db->where('id', 1)->get('job')->row(); - // Check the result $this->assertEquals('1', $job1->id); $this->assertEquals('Developer', $job1->name); } @@ -38,11 +35,7 @@ class Where_test extends CI_TestCase { */ public function test_where_custom_key_value() { - $jobs = $this->db->where('id !=', 1) - ->get('job') - ->result_array(); - - // Check the result + $jobs = $this->db->where('id !=', 1)->get('job')->result_array(); $this->assertEquals(3, count($jobs)); } @@ -54,16 +47,12 @@ class Where_test extends CI_TestCase { public function test_where_associative_array() { $where = array('id >' => 2, 'name !=' => 'Accountant'); - $jobs = $this->db->where($where) - ->get('job') - ->result_array(); + $jobs = $this->db->where($where)->get('job')->result_array(); - // Check the result $this->assertEquals(1, count($jobs)); // Should be Musician $job = current($jobs); - $this->assertEquals('Musician', $job['name']); } @@ -75,16 +64,12 @@ class Where_test extends CI_TestCase { public function test_where_custom_string() { $where = "id > 2 AND name != 'Accountant'"; - $jobs = $this->db->where($where) - ->get('job') - ->result_array(); + $jobs = $this->db->where($where)->get('job')->result_array(); - // Check the result $this->assertEquals(1, count($jobs)); // Should be Musician $job = current($jobs); - $this->assertEquals('Musician', $job['name']); } @@ -100,7 +85,6 @@ class Where_test extends CI_TestCase { ->get('job') ->result_array(); - // Check the result $this->assertEquals(3, count($jobs)); $this->assertEquals('Developer', $jobs[0]['name']); $this->assertEquals('Politician', $jobs[1]['name']); @@ -118,7 +102,6 @@ class Where_test extends CI_TestCase { ->get('job') ->result_array(); - // Check the result $this->assertEquals(2, count($jobs)); $this->assertEquals('Politician', $jobs[0]['name']); $this->assertEquals('Accountant', $jobs[1]['name']); @@ -135,10 +118,9 @@ class Where_test extends CI_TestCase { ->get('job') ->result_array(); - // Check the result $this->assertEquals(2, count($jobs)); $this->assertEquals('Developer', $jobs[0]['name']); $this->assertEquals('Musician', $jobs[1]['name']); } - + }
\ No newline at end of file |