blob: 93b5c3d464e54db7996e01761b9462ab9b92e641 [file] [log] [blame]
Taufan Aditya55f622f2012-04-05 19:01:34 +07001<?php
2
3class Select_test extends CI_TestCase {
4
5 /**
6 * @var object Database/Query Builder holder
7 */
Taufan Aditya0793b1c2012-04-05 21:47:53 +07008 protected $db;
Taufan Aditya55f622f2012-04-05 19:01:34 +07009
10 public function set_up()
11 {
Taufan Aditya0793b1c2012-04-05 21:47:53 +070012 $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER);
Taufan Aditya55f622f2012-04-05 19:01:34 +070013
14 Mock_Database_Schema_Skeleton::create_tables();
15 Mock_Database_Schema_Skeleton::create_data();
Taufan Aditya55f622f2012-04-05 19:01:34 +070016 }
17
18 // ------------------------------------------------------------------------
19
20 /**
21 * @see ./mocks/schema/skeleton.php
22 */
23 public function test_select_only_one_collumn()
24 {
Taufan Aditya0793b1c2012-04-05 21:47:53 +070025 $jobs_name = $this->db->select('name')
26 ->get('job')
27 ->result_array();
Andrey Andreevc1862882012-06-09 23:16:58 +030028
Taufan Aditya55f622f2012-04-05 19:01:34 +070029 // Check rows item
30 $this->assertArrayHasKey('name',$jobs_name[0]);
Andrey Andreev20d9b0a2017-12-20 19:57:39 +020031 $this->assertArrayNotHasKey('id', $jobs_name[0]);
32 $this->assertArrayNotHasKey('description', $jobs_name[0]);
Taufan Aditya55f622f2012-04-05 19:01:34 +070033 }
Taufan Adityafdc0ea82012-04-05 19:08:00 +070034
35 // ------------------------------------------------------------------------
36
37 /**
38 * @see ./mocks/schema/skeleton.php
39 */
40 public function test_select_min()
41 {
Taufan Aditya0793b1c2012-04-05 21:47:53 +070042 $job_min = $this->db->select_min('id')
43 ->get('job')
Taufan Aditya190c6bb2012-05-03 12:13:39 +070044 ->row();
Andrey Andreevc1862882012-06-09 23:16:58 +030045
Taufan Adityafdc0ea82012-04-05 19:08:00 +070046 // Minimum id was 1
Taufan Aditya190c6bb2012-05-03 12:13:39 +070047 $this->assertEquals('1', $job_min->id);
Taufan Adityafdc0ea82012-04-05 19:08:00 +070048 }
49
50 // ------------------------------------------------------------------------
51
52 /**
53 * @see ./mocks/schema/skeleton.php
54 */
55 public function test_select_max()
56 {
Taufan Aditya0793b1c2012-04-05 21:47:53 +070057 $job_max = $this->db->select_max('id')
58 ->get('job')
Taufan Aditya190c6bb2012-05-03 12:13:39 +070059 ->row();
Andrey Andreevc1862882012-06-09 23:16:58 +030060
Taufan Adityafdc0ea82012-04-05 19:08:00 +070061 // Maximum id was 4
Taufan Aditya190c6bb2012-05-03 12:13:39 +070062 $this->assertEquals('4', $job_max->id);
Taufan Adityafdc0ea82012-04-05 19:08:00 +070063 }
64
65 // ------------------------------------------------------------------------
66
67 /**
68 * @see ./mocks/schema/skeleton.php
69 */
70 public function test_select_avg()
71 {
Taufan Aditya0793b1c2012-04-05 21:47:53 +070072 $job_avg = $this->db->select_avg('id')
73 ->get('job')
Taufan Aditya190c6bb2012-05-03 12:13:39 +070074 ->row();
Andrey Andreevc1862882012-06-09 23:16:58 +030075
Taufan Adityafdc0ea82012-04-05 19:08:00 +070076 // Average should be 2.5
Taufan Aditya190c6bb2012-05-03 12:13:39 +070077 $this->assertEquals('2.5', $job_avg->id);
Taufan Adityafdc0ea82012-04-05 19:08:00 +070078 }
79
80 // ------------------------------------------------------------------------
81
82 /**
83 * @see ./mocks/schema/skeleton.php
84 */
85 public function test_select_sum()
86 {
Taufan Aditya0793b1c2012-04-05 21:47:53 +070087 $job_sum = $this->db->select_sum('id')
88 ->get('job')
Taufan Aditya190c6bb2012-05-03 12:13:39 +070089 ->row();
Andrey Andreevc1862882012-06-09 23:16:58 +030090
Taufan Adityafdc0ea82012-04-05 19:08:00 +070091 // Sum of ids should be 10
Taufan Aditya190c6bb2012-05-03 12:13:39 +070092 $this->assertEquals('10', $job_sum->id);
Taufan Adityafdc0ea82012-04-05 19:08:00 +070093 }
Andrey Andreevc1862882012-06-09 23:16:58 +030094
Andrey Andreev20d9b0a2017-12-20 19:57:39 +020095}