packages/sql-semantics-postgres/tests/Unit/Rules/Table/Definition/DeclarationsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Table\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\Declarations::class)]
12#[Medium]
13final class DeclarationsTest extends TestCase
14{
15    public function testTableDeclaresTheColumnsOfAPartition(): void
16    {
17        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18        $context = [];
19        array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
20        $statement = $semantics->inspect('CREATE TABLE p1 PARTITION OF t (b NOT NULL) DEFAULT', $context);
21        self::assertSame([
22          0 => 'a integer NotNull',
23          1 => 'b integer NotNull',
24          2 => 'c text Nullable',
25        ], array_map(static fn ($column): string => $column->name->value . ' ' . $column->type->name() . ' ' . $column->nullability->name, $statement->declarations()[0]->columns));
26    }
27
28    public function testListedPutsInheritedColumnsFirst(): void
29    {
30        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
31        $context = [];
32        array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
33        $statement = $semantics->inspect('CREATE TABLE n (x int, b int NOT NULL) INHERITS (t)', $context);
34        self::assertSame([
35          0 => 'a integer NotNull',
36          1 => 'b integer NotNull',
37          2 => 'c text Nullable',
38          3 => 'x integer Nullable',
39        ], array_map(static fn ($column): string => $column->name->value . ' ' . $column->type->name() . ' ' . $column->nullability->name, $statement->declarations()[0]->columns));
40    }
41
42    public function testCopyClosesTheDeclarationAtAnUndeclaredSource(): void
43    {
44        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
45        $context = [];
46        array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
47        $statement = $semantics->inspect('CREATE TABLE n (x int, LIKE zz, y int)', $context);
48        self::assertSame([
49          0 =>
50           [
51            0 => 'x integer Nullable',
52          ],
53          1 => false,
54        ], [array_map(static fn ($column): string => $column->name->value . ' ' . $column->type->name() . ' ' . $column->nullability->name, $statement->declarations()[0]->columns), $statement->declarations()[0]->complete]);
55    }
56
57    public function testConstrainAppliesPrimaryKeys(): void
58    {
59        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
60        $context = [];
61        array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
62        $statement = $semantics->inspect('CREATE TABLE n (x int, y int, PRIMARY KEY (y))', $context);
63        self::assertSame([
64          0 => 'x integer Nullable',
65          1 => 'y integer NotNull',
66        ], array_map(static fn ($column): string => $column->name->value . ' ' . $column->type->name() . ' ' . $column->nullability->name, $statement->declarations()[0]->columns));
67    }
68
69    public function testNotNullCountsIdentityColumns(): void
70    {
71        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
72        $context = [];
73        array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
74        $statement = $semantics->inspect('CREATE TABLE n (x int GENERATED ALWAYS AS IDENTITY)', $context);
75        self::assertSame([
76          0 => 'x integer NotNull',
77        ], array_map(static fn ($column): string => $column->name->value . ' ' . $column->type->name() . ' ' . $column->nullability->name, $statement->declarations()[0]->columns));
78    }
79
80    public function testCopyKeepsGeneratedColumnsOfParentsAndPartitions(): void
81    {
82        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
83        $context = [];
84        array_push($context, ...$semantics->inspect('CREATE TABLE t (a int, b int GENERATED ALWAYS AS (a) STORED) PARTITION BY LIST (a)')->declarations());
85        $child = $semantics->inspect('CREATE TABLE c (b int, x int) INHERITS (t)', $context);
86        $partition = $semantics->inspect('CREATE TABLE p1 PARTITION OF t FOR VALUES IN (1)', $context);
87        self::assertSame(
88            [[false, true, false], [false, true]],
89            [array_map(static fn ($column): bool => $column->generated, $child->declarations()[0]->columns), array_map(static fn ($column): bool => $column->generated, $partition->declarations()[0]->columns)],
90        );
91    }
92
93    public function testIncludesGeneratedAppliesTheOptionsInOrder(): void
94    {
95        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
96        $context = [];
97        array_push($context, ...$semantics->inspect('CREATE TABLE t (a int, b int GENERATED ALWAYS AS (a) STORED)')->declarations());
98        self::assertSame(
99            [[false, false], [false, true], [false, true], [false, false]],
100            array_map(
101                static fn (string $sql): array => array_map(static fn ($column): bool => $column->generated, $semantics->inspect($sql, $context)->declarations()[0]->columns),
102                ['CREATE TABLE l (LIKE t)', 'CREATE TABLE l (LIKE t INCLUDING GENERATED)', 'CREATE TABLE l (LIKE t EXCLUDING ALL INCLUDING ALL)', 'CREATE TABLE l (LIKE t INCLUDING GENERATED EXCLUDING ALL)'],
103            ),
104        );
105    }
106
107    public function testGeneratedTellsAGenerationClause(): void
108    {
109        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
110        $statement = $semantics->inspect('CREATE TABLE n (a int, b int GENERATED ALWAYS AS (a) STORED, c int GENERATED ALWAYS AS IDENTITY)');
111        self::assertSame([false, true, false], array_map(static fn ($column): bool => $column->generated, $statement->declarations()[0]->columns));
112    }
113}
114