Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class Select_test extends CI_TestCase { |
| 4 | |
| 5 | /** |
| 6 | * @var object Database/Query Builder holder |
| 7 | */ |
Taufan Aditya | 0793b1c | 2012-04-05 21:47:53 +0700 | [diff] [blame] | 8 | protected $db; |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 9 | |
| 10 | public function set_up() |
| 11 | { |
Taufan Aditya | 0793b1c | 2012-04-05 21:47:53 +0700 | [diff] [blame] | 12 | $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 13 | |
| 14 | Mock_Database_Schema_Skeleton::create_tables(); |
| 15 | Mock_Database_Schema_Skeleton::create_data(); |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | // ------------------------------------------------------------------------ |
| 19 | |
| 20 | /** |
| 21 | * @see ./mocks/schema/skeleton.php |
| 22 | */ |
| 23 | public function test_select_only_one_collumn() |
| 24 | { |
Taufan Aditya | 0793b1c | 2012-04-05 21:47:53 +0700 | [diff] [blame] | 25 | $jobs_name = $this->db->select('name') |
| 26 | ->get('job') |
| 27 | ->result_array(); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 28 | |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 29 | // Check rows item |
| 30 | $this->assertArrayHasKey('name',$jobs_name[0]); |
Andrey Andreev | 20d9b0a | 2017-12-20 19:57:39 +0200 | [diff] [blame^] | 31 | $this->assertArrayNotHasKey('id', $jobs_name[0]); |
| 32 | $this->assertArrayNotHasKey('description', $jobs_name[0]); |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 33 | } |
Taufan Aditya | fdc0ea8 | 2012-04-05 19:08:00 +0700 | [diff] [blame] | 34 | |
| 35 | // ------------------------------------------------------------------------ |
| 36 | |
| 37 | /** |
| 38 | * @see ./mocks/schema/skeleton.php |
| 39 | */ |
| 40 | public function test_select_min() |
| 41 | { |
Taufan Aditya | 0793b1c | 2012-04-05 21:47:53 +0700 | [diff] [blame] | 42 | $job_min = $this->db->select_min('id') |
| 43 | ->get('job') |
Taufan Aditya | 190c6bb | 2012-05-03 12:13:39 +0700 | [diff] [blame] | 44 | ->row(); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 45 | |
Taufan Aditya | fdc0ea8 | 2012-04-05 19:08:00 +0700 | [diff] [blame] | 46 | // Minimum id was 1 |
Taufan Aditya | 190c6bb | 2012-05-03 12:13:39 +0700 | [diff] [blame] | 47 | $this->assertEquals('1', $job_min->id); |
Taufan Aditya | fdc0ea8 | 2012-04-05 19:08:00 +0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
| 52 | /** |
| 53 | * @see ./mocks/schema/skeleton.php |
| 54 | */ |
| 55 | public function test_select_max() |
| 56 | { |
Taufan Aditya | 0793b1c | 2012-04-05 21:47:53 +0700 | [diff] [blame] | 57 | $job_max = $this->db->select_max('id') |
| 58 | ->get('job') |
Taufan Aditya | 190c6bb | 2012-05-03 12:13:39 +0700 | [diff] [blame] | 59 | ->row(); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 60 | |
Taufan Aditya | fdc0ea8 | 2012-04-05 19:08:00 +0700 | [diff] [blame] | 61 | // Maximum id was 4 |
Taufan Aditya | 190c6bb | 2012-05-03 12:13:39 +0700 | [diff] [blame] | 62 | $this->assertEquals('4', $job_max->id); |
Taufan Aditya | fdc0ea8 | 2012-04-05 19:08:00 +0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // ------------------------------------------------------------------------ |
| 66 | |
| 67 | /** |
| 68 | * @see ./mocks/schema/skeleton.php |
| 69 | */ |
| 70 | public function test_select_avg() |
| 71 | { |
Taufan Aditya | 0793b1c | 2012-04-05 21:47:53 +0700 | [diff] [blame] | 72 | $job_avg = $this->db->select_avg('id') |
| 73 | ->get('job') |
Taufan Aditya | 190c6bb | 2012-05-03 12:13:39 +0700 | [diff] [blame] | 74 | ->row(); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 75 | |
Taufan Aditya | fdc0ea8 | 2012-04-05 19:08:00 +0700 | [diff] [blame] | 76 | // Average should be 2.5 |
Taufan Aditya | 190c6bb | 2012-05-03 12:13:39 +0700 | [diff] [blame] | 77 | $this->assertEquals('2.5', $job_avg->id); |
Taufan Aditya | fdc0ea8 | 2012-04-05 19:08:00 +0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // ------------------------------------------------------------------------ |
| 81 | |
| 82 | /** |
| 83 | * @see ./mocks/schema/skeleton.php |
| 84 | */ |
| 85 | public function test_select_sum() |
| 86 | { |
Taufan Aditya | 0793b1c | 2012-04-05 21:47:53 +0700 | [diff] [blame] | 87 | $job_sum = $this->db->select_sum('id') |
| 88 | ->get('job') |
Taufan Aditya | 190c6bb | 2012-05-03 12:13:39 +0700 | [diff] [blame] | 89 | ->row(); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 90 | |
Taufan Aditya | fdc0ea8 | 2012-04-05 19:08:00 +0700 | [diff] [blame] | 91 | // Sum of ids should be 10 |
Taufan Aditya | 190c6bb | 2012-05-03 12:13:39 +0700 | [diff] [blame] | 92 | $this->assertEquals('10', $job_sum->id); |
Taufan Aditya | fdc0ea8 | 2012-04-05 19:08:00 +0700 | [diff] [blame] | 93 | } |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 94 | |
Andrey Andreev | 20d9b0a | 2017-12-20 19:57:39 +0200 | [diff] [blame^] | 95 | } |