forked from bcit-ci/CodeIgniter
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request bcit-ci#1324 from toopay/db-tests
Improved DB tests
- Loading branch information
Showing
26 changed files
with
856 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
class Count_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_count_all() | ||
{ | ||
$job_count = $this->db->count_all('job'); | ||
|
||
// Check the result | ||
$this->assertEquals(4, $job_count); | ||
} | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
/** | ||
* @see ./mocks/schema/skeleton.php | ||
*/ | ||
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
class Delete_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_delete() | ||
{ | ||
// Check initial record | ||
$job1 = $this->db->where('id', 1) | ||
->get('job') | ||
->row(); | ||
|
||
$this->assertEquals('Developer', $job1->name); | ||
|
||
// Do the delete | ||
$this->db->delete('job', array('id' => 1)); | ||
|
||
// Check the record | ||
$job1 = $this->db->where('id', 1) | ||
->get('job'); | ||
|
||
$this->assertEmpty($job1->result_array()); | ||
} | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
/** | ||
* @see ./mocks/schema/skeleton.php | ||
*/ | ||
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(); | ||
|
||
$this->assertEquals('Musician', $job4->name); | ||
$this->assertEquals('Chris Martin', $user4->name); | ||
|
||
// Do the delete | ||
$this->db->delete(array('job', 'user'), array('id' => 4)); | ||
|
||
// Check the record | ||
$job4 = $this->db->where('id', 4)->get('job'); | ||
$user4 = $this->db->where('id', 4)->get('user'); | ||
|
||
$this->assertEmpty($job4->result_array()); | ||
$this->assertEmpty($user4->result_array()); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
tests/codeigniter/database/query_builder/distinct_test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
class Distinct_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_distinct() | ||
{ | ||
$users = $this->db->select('country') | ||
->distinct() | ||
->get('user') | ||
->result_array(); | ||
|
||
// Check the result | ||
$this->assertEquals(3, count($users)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
class Empty_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_empty_table() | ||
{ | ||
// Check initial record | ||
$jobs = $this->db->get('job')->result_array(); | ||
|
||
$this->assertEquals(4, count($jobs)); | ||
|
||
// Do the empty | ||
$this->db->empty_table('job'); | ||
|
||
// Check the record | ||
$jobs = $this->db->get('job'); | ||
|
||
$this->assertEmpty($jobs->result_array()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
class From_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_from_simple() | ||
{ | ||
$jobs = $this->db->from('job') | ||
->get() | ||
->result_array(); | ||
|
||
// Check items | ||
$this->assertEquals(4, count($jobs)); | ||
} | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
/** | ||
* @see ./mocks/schema/skeleton.php | ||
*/ | ||
public function test_from_with_where() | ||
{ | ||
$job1 = $this->db->from('job') | ||
->where('id', 1) | ||
->get() | ||
->row(); | ||
|
||
// Check the result | ||
$this->assertEquals('1', $job1->id); | ||
$this->assertEquals('Developer', $job1->name); | ||
$this->assertEquals('Awesome job, but sometimes makes you bored', $job1->description); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
class Group_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_group_by() | ||
{ | ||
$jobs = $this->db->select('name') | ||
->from('job') | ||
->group_by('name') | ||
->get() | ||
->result_array(); | ||
|
||
// Check the result | ||
$this->assertEquals(4, count($jobs)); | ||
} | ||
|
||
// ------------------------------------------------------------------------ | ||
|
||
/** | ||
* @see ./mocks/schema/skeleton.php | ||
*/ | ||
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 | ||
$this->assertEquals(2, count($jobs)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.