Skip to content

Commit be9bd29

Browse files
author
Miloslav Hůla
committed
tests: added PostgreSQL autoincrement test (#227)
1 parent c46a1de commit be9bd29

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/**
4+
* Test: PostgreSQL 10+ SERIAL and IDENTITY imply autoincrement on primary keys
5+
* @dataProvider? databases.ini postgresql
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Tester\Assert;
11+
use Tester\Environment;
12+
13+
require __DIR__ . '/connect.inc.php'; // create $connection
14+
15+
16+
$ver = $connection->query('SHOW server_version')->fetchField();
17+
if (version_compare($ver, '10') < 0) {
18+
Environment::skip("For PostgreSQL 10 or newer but running with $ver.");
19+
}
20+
21+
22+
Nette\Database\Helpers::loadFromFile($connection, Tester\FileMock::create('
23+
DROP SCHEMA IF EXISTS "reflection_10" CASCADE;
24+
CREATE SCHEMA "reflection_10";
25+
26+
CREATE TABLE "reflection_10"."serial" ("id" SERIAL);
27+
CREATE TABLE "reflection_10"."serial_pk" ("id" SERIAL PRIMARY KEY);
28+
29+
CREATE TABLE "reflection_10"."identity_always" ("id" INTEGER GENERATED ALWAYS AS IDENTITY);
30+
CREATE TABLE "reflection_10"."identity_always_pk" ("id" INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY);
31+
32+
CREATE TABLE "reflection_10"."identity_by_default" ("id" INTEGER GENERATED BY DEFAULT AS IDENTITY);
33+
CREATE TABLE "reflection_10"."identity_by_default_pk" ("id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY);
34+
'));
35+
$driver = $connection->getDriver();
36+
37+
38+
function filter(array $columns): array
39+
{
40+
return array_map(function (array $col): array {
41+
return [
42+
'name' => $col['name'],
43+
'autoincrement' => $col['autoincrement'],
44+
'sequence' => $col['vendor']['sequence'],
45+
];
46+
}, $columns);
47+
}
48+
49+
50+
// Autoincrement columns by SERIAL and IDENTITY
51+
$connection->query('SET search_path TO reflection_10');
52+
53+
$columns = [
54+
'serial' => filter($driver->getColumns('serial')),
55+
'serial_pk' => filter($driver->getColumns('serial_pk')),
56+
'identity_always' => filter($driver->getColumns('identity_always')),
57+
'identity_always_pk' => filter($driver->getColumns('identity_always_pk')),
58+
'identity_by_default' => filter($driver->getColumns('identity_by_default')),
59+
'identity_by_default_pk' => filter($driver->getColumns('identity_by_default_pk')),
60+
];
61+
62+
63+
Assert::same([
64+
'serial' => [[
65+
'name' => 'id',
66+
'autoincrement' => false,
67+
'sequence' => 'serial_id_seq',
68+
]],
69+
70+
'serial_pk' => [[
71+
'name' => 'id',
72+
'autoincrement' => true,
73+
'sequence' => 'serial_pk_id_seq',
74+
]],
75+
76+
'identity_always' => [[
77+
'name' => 'id',
78+
'autoincrement' => false,
79+
'sequence' => 'identity_always_id_seq',
80+
]],
81+
82+
'identity_always_pk' => [[
83+
'name' => 'id',
84+
'autoincrement' => true,
85+
'sequence' => 'identity_always_pk_id_seq',
86+
]],
87+
88+
'identity_by_default' => [[
89+
'name' => 'id',
90+
'autoincrement' => false,
91+
'sequence' => 'identity_by_default_id_seq',
92+
]],
93+
94+
'identity_by_default_pk' => [[
95+
'name' => 'id',
96+
'autoincrement' => true,
97+
'sequence' => 'identity_by_default_pk_id_seq',
98+
]],
99+
], $columns);

0 commit comments

Comments
 (0)