Skip to content

Commit

Permalink
Update tests to be more accurate and exhaustive.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 4, 2013
1 parent 70c5b6b commit cc37aa6
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php
Expand Up @@ -35,22 +35,33 @@ public function setUp() {
}

/**
* Auxiliary function to insert a couple rows in a newly created table
* Test helper for creating tables.
*
* @return void
**/
protected function _insertTwoRecords() {
*/
protected function _createAuthorsAndArticles() {
$table = 'CREATE TEMPORARY TABLE authors(id int, name varchar(50))';
$this->connection->execute($table);

$table = 'CREATE TEMPORARY TABLE articles(id int, title varchar(20), body varchar(50), author_id int)';
$this->connection->execute($table);
}

/**
* Auxiliary function to insert a couple rows in a newly created table
*
* @return void
*/
protected function _insertTwoRecords() {
$this->_createAuthorsAndArticles();

$data = ['id' => '1', 'name' => 'Chuck Norris'];
$result = $this->connection->insert('authors', $data, ['id' => 'integer', 'name' => 'string']);

$result->bindValue(1, '2', 'integer');
$result->bindValue(2, 'Bruce Lee');
$result->execute();

$table = 'CREATE TEMPORARY TABLE articles(id int, title varchar(20), body varchar(50), author_id int)';
$this->connection->execute($table);
$data = ['id' => '1', 'title' => 'a title', 'body' => 'a body', 'author_id' => 1];
$result = $this->connection->insert(
'articles',
Expand Down Expand Up @@ -1732,27 +1743,36 @@ public function testUpdateWithExpression() {
* @return void
*/
public function testInsertSimple() {
$this->_insertTwoRecords();
$this->_createAuthorsAndArticles();

$query = new Query($this->connection);
$query->insert('articles', ['title', 'body'])
$query->insert('articles', ['id', 'title', 'body'])
->values([
'id' => 1,
'title' => 'mark',
'body' => 'test insert'
]);
$result = $query->sql(false);
$this->assertEquals(
'INSERT INTO articles (title, body) VALUES (?, ?)',
'INSERT INTO articles (id, title, body) VALUES (?, ?, ?)',
$result
);

$result = $query->execute();
$this->assertCount(1, $result, '1 row should be inserted');

$result = (new Query($this->connection))->select('COUNT(*)')
$result = (new Query($this->connection))->select('*')
->from('articles')
->execute();
$this->assertEquals(3, $result->fetch()[0]);
$this->assertCount(1, $result);

$expected = [
'id' => 1,
'author_id' => null,
'title' => 'mark',
'body' => 'test insert'
];
$this->assertEquals($expected, $result->fetchAll('assoc')[0]);
}

public function testInsertSparseRow() {
Expand Down

0 comments on commit cc37aa6

Please sign in to comment.