Skip to content

Commit 33306ea

Browse files
committed
improved tests
1 parent fdebf73 commit 33306ea

10 files changed

+253
-153
lines changed

tests/Database/Connection.query.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $connection = connectToDB()->getConnection();
1515
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1616

1717

18-
test('', function () use ($connection) {
18+
test('executes parameterized query and returns ResultSet', function () use ($connection) {
1919
$res = $connection->query('SELECT id FROM author WHERE id = ?', 11);
2020
Assert::type(Nette\Database\ResultSet::class, $res);
2121
Assert::same('SELECT id FROM author WHERE id = ?', $res->getQueryString());
@@ -24,14 +24,14 @@ test('', function () use ($connection) {
2424
});
2525

2626

27-
test('', function () use ($connection) {
27+
test('multiple query parameters', function () use ($connection) {
2828
$res = $connection->query('SELECT id FROM author WHERE id = ? OR id = ?', 11, 12);
2929
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
3030
Assert::same([11, 12], $res->getParameters());
3131
});
3232

3333

34-
test('', function () use ($connection) {
34+
test('query with array of parameters', function () use ($connection) {
3535
$res = $connection->queryArgs('SELECT id FROM author WHERE id = ? OR id = ?', [11, 12]);
3636
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
3737
Assert::same([11, 12], $res->getParameters());

tests/Database/Connection.transaction.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $connection = connectToDB()->getConnection();
1616
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1717

1818

19-
test('', function () use ($connection) {
19+
test('rolls back transaction after manual control', function () use ($connection) {
2020
$connection->beginTransaction();
2121
$connection->query('DELETE FROM book');
2222
$connection->rollBack();
@@ -25,7 +25,7 @@ test('', function () use ($connection) {
2525
});
2626

2727

28-
test('', function () use ($connection) {
28+
test('rolls back transaction on exception', function () use ($connection) {
2929
Assert::exception(
3030
fn() => $connection->transaction(function (Connection $connection) {
3131
$connection->query('DELETE FROM book');
@@ -39,7 +39,7 @@ test('', function () use ($connection) {
3939
});
4040

4141

42-
test('', function () use ($connection) {
42+
test('commits transaction successfully', function () use ($connection) {
4343
$connection->beginTransaction();
4444
$connection->query('DELETE FROM book');
4545
$connection->commit();

tests/Database/Explorer.query.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ $connection = $explorer->getConnection();
1717
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1818

1919

20-
test('', function () use ($explorer) {
20+
test('parameterized query through explorer', function () use ($explorer) {
2121
$res = $explorer->query('SELECT id FROM author WHERE id = ?', 11);
2222
Assert::type(Nette\Database\ResultSet::class, $res);
2323
Assert::same('SELECT id FROM author WHERE id = ?', $res->getQueryString());
2424
Assert::same([11], $res->getParameters());
2525
});
2626

2727

28-
test('', function () use ($explorer) {
28+
test('multiple parameters in explorer query', function () use ($explorer) {
2929
$res = $explorer->query('SELECT id FROM author WHERE id = ? OR id = ?', 11, 12);
3030
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
3131
Assert::same([11, 12], $res->getParameters());
3232
});
3333

3434

35-
test('', function () use ($explorer) {
35+
test('explorer query with array of parameters', function () use ($explorer) {
3636
$res = $explorer->queryArgs('SELECT id FROM author WHERE id = ? OR id = ?', [11, 12]);
3737
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
3838
Assert::same([11, 12], $res->getParameters());

tests/Database/Explorer.transaction.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $connection = $explorer->getConnection();
1818
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1919

2020

21-
test('', function () use ($explorer) {
21+
test('rolls back explorer transaction after manual control', function () use ($explorer) {
2222
$explorer->beginTransaction();
2323
$explorer->query('DELETE FROM book');
2424
$explorer->rollBack();
@@ -27,7 +27,7 @@ test('', function () use ($explorer) {
2727
});
2828

2929

30-
test('', function () use ($explorer) {
30+
test('rolls back explorer transaction on exception', function () use ($explorer) {
3131
Assert::exception(
3232
fn() => $explorer->transaction(function (Explorer $explorer) {
3333
$explorer->query('DELETE FROM book');
@@ -41,7 +41,7 @@ test('', function () use ($explorer) {
4141
});
4242

4343

44-
test('', function () use ($explorer) {
44+
test('commits explorer transaction successfully', function () use ($explorer) {
4545
$explorer->beginTransaction();
4646
$explorer->query('DELETE FROM book');
4747
$explorer->commit();

tests/Database/ResultSet.fetch().phpt

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $connection = connectToDB()->getConnection();
1515
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1616

1717

18-
test('', function () use ($connection, $driverName) {
18+
test('detects duplicate column names in simple query', function () use ($connection, $driverName) {
1919
$res = $connection->query('SELECT name, name FROM author');
2020
$message = match ($driverName) {
2121
'mysql' => "Found duplicate columns in database result set: 'name' (from author).",
@@ -35,7 +35,7 @@ test('', function () use ($connection, $driverName) {
3535
});
3636

3737

38-
test('tests closeCursor()', function () use ($connection, $driverName) {
38+
test('handles cursor management in stored procedures', function () use ($connection, $driverName) {
3939
if ($driverName === 'mysql') {
4040
$connection->query('CREATE DEFINER = CURRENT_USER PROCEDURE `testProc`(IN param int(10) unsigned) BEGIN SELECT * FROM book WHERE id != param; END;;');
4141
$connection->getPdo()->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
@@ -51,7 +51,7 @@ test('tests closeCursor()', function () use ($connection, $driverName) {
5151
});
5252

5353

54-
test('', function () use ($connection, $driverName) {
54+
test('detects duplicate columns in JOINed tables', function () use ($connection, $driverName) {
5555
$res = $connection->query('SELECT book.id, author.id, author.name, translator.name FROM book JOIN author ON (author.id = book.author_id) JOIN author translator ON (translator.id = book.translator_id)');
5656
$message = match ($driverName) {
5757
'mysql' => "Found duplicate columns in database result set: 'id' (from book, author), 'name' (from author, translator).",
@@ -69,7 +69,7 @@ test('', function () use ($connection, $driverName) {
6969
});
7070

7171

72-
test('', function () use ($connection, $driverName) {
72+
test('returns null for empty result set', function () use ($connection, $driverName) {
7373
$res = $connection->query('SELECT id FROM author WHERE id = ?', 666);
7474

7575
Assert::null($res->fetch());

tests/Database/ResultSet.fetchAssoc().phpt

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $connection = connectToDB()->getConnection();
1515
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1616

1717

18-
test('', function () use ($connection) {
18+
test('fetches associative array with id-title pairs', function () use ($connection) {
1919
$res = $connection->query('SELECT * FROM book ORDER BY title');
2020
Assert::same([
2121
1 => '1001 tipu a triku pro PHP',
@@ -26,7 +26,7 @@ test('', function () use ($connection) {
2626
});
2727

2828

29-
test('', function () use ($connection) {
29+
test('fetches associative array with single column id key', function () use ($connection) {
3030
$pairs = $connection->query('SELECT id FROM book ORDER BY id')->fetchAssoc('id');
3131
Assert::same([
3232
1 => ['id' => 1],
@@ -36,7 +36,8 @@ test('', function () use ($connection) {
3636
], $pairs);
3737
});
3838

39-
test('', function () use ($connection) {
39+
40+
test('fetches associative array with array values', function () use ($connection) {
4041
$pairs = $connection->query('SELECT id FROM book ORDER BY id')->fetchAssoc('id[]=id');
4142
Assert::same([
4243
1 => [1],
@@ -47,7 +48,7 @@ test('', function () use ($connection) {
4748
});
4849

4950

50-
test('', function () use ($connection) {
51+
test('fetches associative array using datetime values as keys', function () use ($connection) {
5152
$pairs = $connection->query('UPDATE author SET born = ? WHERE id = 11', new DateTime('2002-02-20'));
5253
$pairs = $connection->query('UPDATE author SET born = ? WHERE id = 12', new DateTime('2002-02-02'));
5354
$pairs = $connection->query('SELECT * FROM author WHERE born IS NOT NULL ORDER BY born')->fetchAssoc('born=name');

tests/Database/ResultSet.fetchField().phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ $connection = connectToDB()->getConnection();
1515
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1616

1717

18-
test('', function () use ($connection) {
18+
test('fetches first field from first row', function () use ($connection) {
1919
$res = $connection->query('SELECT name, id FROM author ORDER BY id');
2020

2121
Assert::same('Jakub Vrana', $res->fetchField());
2222
});
2323

2424

25-
test('', function () use ($connection) {
25+
test('returns null when no rows found', function () use ($connection) {
2626
$res = $connection->query('SELECT id FROM author WHERE id = ?', 666);
2727

2828
Assert::null($res->fetchField());

tests/Database/ResultSet.fetchList().phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ $connection = connectToDB()->getConnection();
1515
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1616

1717

18-
test('', function () use ($connection) {
18+
test('fetches array of values from first row', function () use ($connection) {
1919
$res = $connection->query('SELECT name, id FROM author ORDER BY id');
2020

2121
Assert::same(['Jakub Vrana', 11], $res->fetchList());
2222
});
2323

2424

25-
test('', function () use ($connection) {
25+
test('returns null when no rows found', function () use ($connection) {
2626
$res = $connection->query('SELECT id FROM author WHERE id = ?', 666);
2727

2828
Assert::null($res->fetchList());

tests/Database/ResultSet.fetchPairs().phpt

+77-63
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $connection = connectToDB()->getConnection();
1515
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1616

1717

18-
test('', function () use ($connection) {
18+
test('fetches key-value pairs using column names in both directions', function () use ($connection) {
1919
$res = $connection->query('SELECT * FROM book ORDER BY title');
2020
Assert::same([
2121
1 => '1001 tipu a triku pro PHP',
@@ -33,7 +33,7 @@ test('', function () use ($connection) {
3333
});
3434

3535

36-
test('', function () use ($connection) {
36+
test('fetches pairs using column indexes', function () use ($connection) {
3737
$pairs = $connection->query('SELECT title, id FROM book ORDER BY title')->fetchPairs(1, 0);
3838
Assert::same([
3939
1 => '1001 tipu a triku pro PHP',
@@ -44,7 +44,7 @@ test('', function () use ($connection) {
4444
});
4545

4646

47-
test('', function () use ($connection) {
47+
test('fetches pairs with same column for key and value', function () use ($connection) {
4848
$pairs = $connection->query('SELECT * FROM book ORDER BY id')->fetchPairs('id', 'id');
4949
Assert::same([
5050
1 => 1,
@@ -55,7 +55,7 @@ test('', function () use ($connection) {
5555
});
5656

5757

58-
test('', function () use ($connection) {
58+
test('fetches pairs using key column only', function () use ($connection) {
5959
$pairs = $connection->query('SELECT id FROM book ORDER BY id')->fetchPairs('id');
6060
Assert::equal([
6161
1 => Nette\Database\Row::from(['id' => 1]),
@@ -66,7 +66,7 @@ test('', function () use ($connection) {
6666
});
6767

6868

69-
test('', function () use ($connection) {
69+
test('fetches pairs using datetime values as keys', function () use ($connection) {
7070
$pairs = $connection->query('UPDATE author SET born = ? WHERE id = 11', new DateTime('2002-02-20'));
7171
$pairs = $connection->query('UPDATE author SET born = ? WHERE id = 12', new DateTime('2002-02-02'));
7272
$pairs = $connection->query('SELECT * FROM author WHERE born IS NOT NULL ORDER BY born')->fetchPairs('born', 'name');
@@ -77,64 +77,78 @@ test('', function () use ($connection) {
7777
});
7878

7979

80-
$pairs = $connection->query('SELECT id FROM book ORDER BY id')->fetchPairs('id');
81-
Assert::equal([
82-
1 => Nette\Database\Row::from(['id' => 1]),
83-
2 => Nette\Database\Row::from(['id' => 2]),
84-
3 => Nette\Database\Row::from(['id' => 3]),
85-
4 => Nette\Database\Row::from(['id' => 4]),
86-
], $pairs);
87-
88-
89-
$pairs = $connection->query('SELECT id FROM book ORDER BY id')->fetchPairs(null, 'id');
90-
Assert::same([
91-
0 => 1,
92-
1 => 2,
93-
2 => 3,
94-
3 => 4,
95-
], $pairs);
96-
97-
98-
$pairs = $connection->query('SELECT id FROM book ORDER BY id')->fetchPairs();
99-
Assert::same([
100-
0 => 1,
101-
1 => 2,
102-
2 => 3,
103-
3 => 4,
104-
], $pairs);
105-
106-
107-
$pairs = $connection->query('SELECT id, id + 1 AS id1 FROM book ORDER BY id')->fetchPairs();
108-
Assert::same([
109-
1 => 2,
110-
2 => 3,
111-
3 => 4,
112-
4 => 5,
113-
], $pairs);
114-
115-
116-
$pairs = $connection->query('SELECT id, id + 1 AS id1, title FROM book ORDER BY id')->fetchPairs();
117-
Assert::same([
118-
1 => 2,
119-
2 => 3,
120-
3 => 4,
121-
4 => 5,
122-
], $pairs);
123-
124-
125-
$pairs = $connection->query('UPDATE author SET born = ? WHERE id = 11', new DateTime('2002-02-20'));
126-
$pairs = $connection->query('UPDATE author SET born = ? WHERE id = 12', new DateTime('2002-02-02'));
127-
$pairs = $connection->query('SELECT * FROM author WHERE born IS NOT NULL ORDER BY born')->fetchPairs('born', 'name');
128-
Assert::same([
129-
'2002-02-02 00:00:00.000000' => 'David Grudl',
130-
'2002-02-20 00:00:00.000000' => 'Jakub Vrana',
131-
], $pairs);
132-
133-
134-
$pairs = $connection->query('SELECT 1.5 AS k, 1 AS v')->fetchPairs();
135-
Assert::same([
136-
'1.5' => 1,
137-
], $pairs);
80+
test('fetches pairs using id column with no key specified', function () use ($connection) {
81+
$pairs = $connection->query('SELECT id FROM book ORDER BY id')->fetchPairs('id');
82+
Assert::equal([
83+
1 => Nette\Database\Row::from(['id' => 1]),
84+
2 => Nette\Database\Row::from(['id' => 2]),
85+
3 => Nette\Database\Row::from(['id' => 3]),
86+
4 => Nette\Database\Row::from(['id' => 4]),
87+
], $pairs);
88+
});
89+
90+
91+
test('fetches pairs with numeric keys when no key column specified', function () use ($connection) {
92+
$pairs = $connection->query('SELECT id FROM book ORDER BY id')->fetchPairs(null, 'id');
93+
Assert::same([
94+
0 => 1,
95+
1 => 2,
96+
2 => 3,
97+
3 => 4,
98+
], $pairs);
99+
});
100+
101+
102+
test('fetches pairs with default parameters', function () use ($connection) {
103+
$pairs = $connection->query('SELECT id FROM book ORDER BY id')->fetchPairs();
104+
Assert::same([
105+
0 => 1,
106+
1 => 2,
107+
2 => 3,
108+
3 => 4,
109+
], $pairs);
110+
});
111+
112+
113+
test('fetches pairs using expression as value', function () use ($connection) {
114+
$pairs = $connection->query('SELECT id, id + 1 AS id1 FROM book ORDER BY id')->fetchPairs();
115+
Assert::same([
116+
1 => 2,
117+
2 => 3,
118+
3 => 4,
119+
4 => 5,
120+
], $pairs);
121+
});
122+
123+
124+
test('fetches pairs with additional columns', function () use ($connection) {
125+
$pairs = $connection->query('SELECT id, id + 1 AS id1, title FROM book ORDER BY id')->fetchPairs();
126+
Assert::same([
127+
1 => 2,
128+
2 => 3,
129+
3 => 4,
130+
4 => 5,
131+
], $pairs);
132+
});
133+
134+
135+
test('fetches pairs with datetime values', function () use ($connection) {
136+
$pairs = $connection->query('UPDATE author SET born = ? WHERE id = 11', new DateTime('2002-02-20'));
137+
$pairs = $connection->query('UPDATE author SET born = ? WHERE id = 12', new DateTime('2002-02-02'));
138+
$pairs = $connection->query('SELECT * FROM author WHERE born IS NOT NULL ORDER BY born')->fetchPairs('born', 'name');
139+
Assert::same([
140+
'2002-02-02 00:00:00.000000' => 'David Grudl',
141+
'2002-02-20 00:00:00.000000' => 'Jakub Vrana',
142+
], $pairs);
143+
});
144+
145+
146+
test('fetches pairs with float key', function () use ($connection) {
147+
$pairs = $connection->query('SELECT 1.5 AS k, 1 AS v')->fetchPairs();
148+
Assert::same([
149+
'1.5' => 1,
150+
], $pairs);
151+
});
138152

139153

140154
test('with callback', function () use ($connection) {

0 commit comments

Comments
 (0)