Skip to content

Commit abd1ab0

Browse files
committed
tests: some tests fixed to work with SQL Server
1 parent 718f8d7 commit abd1ab0

File tree

6 files changed

+83
-41
lines changed

6 files changed

+83
-41
lines changed

tests/Database/Table/Selection.insert().phpt

+20-17
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,27 @@ if ($driverName !== 'sqlsrv') {
4949
}
5050

5151

52-
switch ($driverName) {
53-
case 'mysql':
54-
$selection = $context->table('author')->select('NULL, id, NULL, CONCAT(?, name), NULL', 'Biography: ');
55-
break;
56-
case 'pgsql':
57-
$selection = $context->table('author')->select('nextval(?), id, NULL, ? || name, NULL', 'book_id_seq', 'Biography: ');
58-
break;
59-
case 'sqlite':
60-
$selection = $context->table('author')->select('NULL, id, NULL, ? || name, NULL', 'Biography: ');
61-
break;
62-
case 'sqlsrv':
63-
$selection = $context->table('author')->select('id, NULL, CONCAT(?, name), NULL', 'Biography: ');
64-
break;
65-
default:
66-
Assert::fail("Unsupported driver $driverName");
52+
// SQL Server 2008 doesn't know CONCAT()
53+
if ($driverName !== 'sqlsrv') {
54+
switch ($driverName) {
55+
case 'mysql':
56+
$selection = $context->table('author')->select('NULL, id, NULL, CONCAT(?, name), NULL', 'Biography: ');
57+
break;
58+
case 'pgsql':
59+
$selection = $context->table('author')->select('nextval(?), id, NULL, ? || name, NULL', 'book_id_seq', 'Biography: ');
60+
break;
61+
case 'sqlite':
62+
$selection = $context->table('author')->select('NULL, id, NULL, ? || name, NULL', 'Biography: ');
63+
break;
64+
case 'sqlsrv':
65+
$selection = $context->table('author')->select('id, NULL, CONCAT(?, name), NULL', 'Biography: ');
66+
break;
67+
default:
68+
Assert::fail("Unsupported driver $driverName");
69+
}
70+
$context->table('book')->insert($selection);
71+
Assert::equal(4, $context->table('book')->where('title LIKE', 'Biography%')->count('*'));
6772
}
68-
$context->table('book')->insert($selection);
69-
Assert::equal(4, $context->table('book')->where('title LIKE', 'Biography%')->count('*'));
7073

7174

7275
// Insert into table without primary key

tests/Database/Table/Selection.page().phpt

+11-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ use Tester\Assert;
99

1010
require __DIR__ . '/../connect.inc.php'; // create $connection
1111

12+
if ($driverName === 'sqlsrv' && $connection->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION) < 11) {
13+
Tester\Environment::skip('Offset is supported since SQL Server 2012');
14+
}
15+
1216
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverName}-nette_test1.sql");
1317

1418
//public function page($page, $itemsPerPage, & $numOfPages = NULL)
@@ -54,7 +58,10 @@ test(function () use ($context) { //less items than $itemsPerPage
5458
Assert::equal(4, count($tags)); //all four items from db
5559
});
5660

57-
test(function () use ($context) { //invalid params
58-
$tags = $context->table('tag')->page('foo', 'bar');
59-
Assert::equal(0, count($tags)); //no items
60-
});
61+
// SQL Server throw PDOException 'The number of rows provided for a FETCH clause must be greater then zero.'
62+
if ($driverName !== 'sqlsrv') {
63+
test(function () use ($context) { //invalid params
64+
$tags = $context->table('tag')->page('foo', 'bar');
65+
Assert::equal(0, count($tags)); //no items
66+
});
67+
}

tests/Database/Table/Table.limit.sqlsrv.phpt

+34-14
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,55 @@ require __DIR__ . '/../connect.inc.php'; // create $connection
1111

1212
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverName}-nette_test1.sql");
1313

14+
$version2008 = $connection->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION) < 11;
1415

1516
Assert::same(
16-
'SELECT TOP 2 * FROM [author]',
17+
$version2008
18+
? 'SELECT TOP 2 * FROM [author] ORDER BY [author].[id]'
19+
: 'SELECT * FROM [author] ORDER BY [author].[id] OFFSET 0 ROWS FETCH NEXT 2 ROWS ONLY',
1720
$context->table('author')->limit(2)->getSql()
1821
);
1922

20-
Assert::exception(function () use ($context) {
21-
$context->table('author')->limit(2, 10)->getSql();
22-
}, Nette\NotSupportedException::class, 'Offset is not supported by this database.');
23-
2423
Assert::same(
25-
'SELECT TOP 2 * FROM [author] ORDER BY [name]',
24+
$version2008
25+
? 'SELECT TOP 2 * FROM [author] ORDER BY [name]'
26+
: 'SELECT * FROM [author] ORDER BY [name] OFFSET 0 ROWS FETCH NEXT 2 ROWS ONLY',
2627
$context->table('author')->order('name')->limit(2)->getSql()
2728
);
2829

2930
Assert::same(
30-
'SELECT TOP 10 * FROM [author]',
31+
$version2008
32+
? 'SELECT TOP 10 * FROM [author] ORDER BY [author].[id]'
33+
: 'SELECT * FROM [author] ORDER BY [author].[id] OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY',
3134
$context->table('author')->page(1, 10)->getSql()
3235
);
3336

3437
Assert::same(
35-
'SELECT TOP 10 * FROM [author]',
38+
$version2008
39+
? 'SELECT TOP 0 * FROM [author] ORDER BY [author].[id]'
40+
: 'SELECT * FROM [author] ORDER BY [author].[id] OFFSET 0 ROWS FETCH NEXT 0 ROWS ONLY',
3641
$context->table('author')->page(0, 10)->getSql()
3742
);
3843

39-
Assert::exception(function () use ($context) {
40-
$context->table('author')->page(2, 10, $count)->getSql();
41-
}, Nette\NotSupportedException::class, 'Offset is not supported by this database.');
44+
if ($version2008) {
45+
Assert::exception(function () use ($context) {
46+
$context->table('author')->page(2, 10, $count)->getSql();
47+
}, Nette\NotSupportedException::class, 'Offset is not supported by this database.');
48+
49+
Assert::exception(function () use ($context) {
50+
$context->table('author')->page(2, 2, $count)->getSql();
51+
}, Nette\NotSupportedException::class, 'Offset is not supported by this database.');
52+
53+
} else {
54+
Assert::same(
55+
reformat('SELECT * FROM [author] ORDER BY [author].[id] OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY'),
56+
$context->table('author')->page(2, 10, $count)->getSql()
57+
);
58+
Assert::same(1, $count);
4259

43-
Assert::exception(function () use ($context) {
44-
$context->table('author')->page(2, 2, $count)->getSql();
45-
}, Nette\NotSupportedException::class, 'Offset is not supported by this database.');
60+
Assert::same(
61+
reformat('SELECT * FROM [author] ORDER BY [author].[id] OFFSET 2 ROWS FETCH NEXT 2 ROWS ONLY'),
62+
$context->table('author')->page(2, 2, $count)->getSql()
63+
);
64+
Assert::same(2, $count);
65+
}

tests/Database/Table/Table.placeholders.phpt

+5-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ test(function () use ($context, $driverName) {
7676
});
7777

7878

79-
test(function () use ($context) { // Test placeholder for GroupedSelection
79+
test(function () use ($context, $driverName) { // Test placeholder for GroupedSelection
80+
if ($driverName === 'sqlsrv') { // This syntax is not supported on SQL Server
81+
return;
82+
}
83+
8084
$books = $context->table('author')->get(11)->related('book')->order('title = ? DESC', 'Test');
8185
foreach ($books as $book) {}
8286

tests/Database/Table/Table.update().phpt

+7-4
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@ $tag2 = $context->table('tag')->insert([
7474
'name' => 'PS4 Game',
7575
]); // INSERT INTO `tag` (`name`) VALUES ('PS4 Game')
7676

77-
$tag2->update([
78-
'id' => 1,
79-
]); // UPDATE `tag` SET `id`=1 WHERE (`id` = (?))
80-
Assert::same(1, $tag2->id);
77+
// SQL Server throw PDOException because does not allow to update identity column
78+
if ($driverName !== 'sqlsrv') {
79+
$tag2->update([
80+
'id' => 1,
81+
]); // UPDATE `tag` SET `id`=1 WHERE (`id` = (?))
82+
Assert::same(1, $tag2->id);
83+
}
8184

8285

8386
$book_tag = $context->table('book_tag')->get([

tests/Database/Table/bugs/bug1356.phpt

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ foreach ($books as $book) {
2424
$book->title;
2525
}
2626

27-
Assert::same(reformat('SELECT * FROM [book] ORDER BY [book].[id] LIMIT 1'), $books->getSql());
27+
Assert::same(reformat([
28+
'sqlsrv' => $connection->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION) < 11
29+
? 'SELECT TOP 1 * FROM [book] ORDER BY [book].[id]'
30+
: 'SELECT * FROM [book] ORDER BY [book].[id] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY',
31+
'SELECT * FROM [book] ORDER BY [book].[id] LIMIT 1',
32+
]), $books->getSql());

0 commit comments

Comments
 (0)