Skip to content

Commit

Permalink
Fix failing insert tests on sqlserver
Browse files Browse the repository at this point in the history
  • Loading branch information
Walther Lalk committed Dec 17, 2014
1 parent b23f8c4 commit 001bb58
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 13 deletions.
82 changes: 70 additions & 12 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -2204,14 +2204,19 @@ public function testInsertSimple() {
]);
$result = $query->sql();
$this->assertQuotedQuery(
'INSERT INTO <articles> \(<title>, <body>\) ' .
'INSERT INTO <articles> \(<title>, <body>\) (OUTPUT INSERTED\.\* )?' .
'VALUES \(:c0, :c1\)',
$result,
true
);

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

//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
$this->assertCount(1, $result, '1 row should be inserted');
}

$expected = [
[
Expand Down Expand Up @@ -2240,14 +2245,19 @@ public function testInsertSparseRow() {
]);
$result = $query->sql();
$this->assertQuotedQuery(
'INSERT INTO <articles> \(<title>, <body>\) ' .
'INSERT INTO <articles> \(<title>, <body>\) (OUTPUT INSERTED\.\* )?' .
'VALUES \(:c0, :c1\)',
$result,
true
);

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

//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
$this->assertCount(1, $result, '1 row should be inserted');
}

$expected = [
[
Expand Down Expand Up @@ -2278,7 +2288,12 @@ public function testInsertMultipleRowsSparse() {
]);

$result = $query->execute();
$this->assertCount(2, $result, '2 rows should be inserted');
$result->closeCursor();

//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
$this->assertCount(2, $result, '2 rows should be inserted');
}

$expected = [
[
Expand Down Expand Up @@ -2319,15 +2334,20 @@ public function testInsertFromSelect() {

$result = $query->sql();
$this->assertQuotedQuery(
'INSERT INTO <articles> \(<title>, <body>, <author_id>\) SELECT',
'INSERT INTO <articles> \(<title>, <body>, <author_id>\) (OUTPUT INSERTED\.\* )?SELECT',
$result,
true
);
$this->assertQuotedQuery(
'SELECT <name>, \'some text\', 99 FROM <authors>', $result, true);
$result = $query->execute();
$result->closeCursor();

//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
$this->assertCount(1, $result);
}

$this->assertCount(1, $result);
$result = (new Query($this->connection))->select('*')
->from('articles')
->where(['author_id' => 99])
Expand Down Expand Up @@ -2376,24 +2396,61 @@ public function testInsertFailureMixingTypesQueryFirst() {
*/
public function testInsertExpressionValues() {
$query = new Query($this->connection);
$query->insert(['title'])
$query->insert(['title', 'author_id'])
->into('articles')
->values(['title' => $query->newExpr("SELECT 'jose'")]);
->values(['title' => $query->newExpr("SELECT 'jose'"), 'author_id' => 99]);

$result = $query->execute();
$result->closeCursor();

//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
$this->assertCount(1, $result);
}

$result = (new Query($this->connection))->select('*')
->from('articles')
->where(['author_id' => 99])
->execute();
$this->assertCount(1, $result);
$expected = [
'id' => 4,
'title' => 'jose',
'body' => null,
'author_id' => '99',
'published' => 'N',
];
$this->assertEquals($expected, $result->fetch('assoc'));

$subquery = new Query($this->connection);
$subquery->select(['name'])
->from('authors')
->where(['id' => 1]);

$query = new Query($this->connection);
$query->insert(['title'])
$query->insert(['title', 'author_id'])
->into('articles')
->values(['title' => $subquery]);
->values(['title' => $subquery, 'author_id' => 100]);
$result = $query->execute();
$result->closeCursor();
//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
$this->assertCount(1, $result);
}

$result = (new Query($this->connection))->select('*')
->from('articles')
->where(['author_id' => 100])
->execute();
$this->assertCount(1, $result);
$expected = [
'id' => 5,
'title' => 'mariano',
'body' => null,
'author_id' => '100',
'published' => 'N',
];
$this->assertEquals($expected, $result->fetch('assoc'));
}

/**
Expand Down Expand Up @@ -2937,7 +2994,8 @@ public function testSqlCaseStatement() {
'comment' => 'In limbo',
'published' => 'L'
])
->execute();
->execute()
->closeCursor();

$query = new Query($this->connection);
$conditions = [
Expand Down
9 changes: 8 additions & 1 deletion tests/TestCase/ORM/QueryTest.php
Expand Up @@ -1413,8 +1413,15 @@ public function testInsert() {
->values(['title' => 'Second'])
->execute();

$result->closeCursor();

$this->assertInstanceOf('Cake\Database\StatementInterface', $result);
$this->assertEquals(2, $result->rowCount());
//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
$this->assertEquals(2, $result->rowCount());
} else {
$this->assertEquals(-1, $result->rowCount());
}
}

/**
Expand Down

0 comments on commit 001bb58

Please sign in to comment.