packages/sql-semantics-postgres/tests/Unit/Rules/Table/Definition/DefinitionsTest.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\Definitions::class)]
12#[Medium]
13final class DefinitionsTest extends TestCase
14{
15    public function testDeriveProvidesTheDeclaration(): 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 n (x int CHECK (x > 0))', $context);
21        $n1 = $statement->statement;
22        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\CreateTable::class, $n1);
23        $n2 = $statement->facts->relation($n1)->table;
24        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\DeclaredTable::class, $n2);
25        self::assertSame(true, $n2->table === $statement->declarations()[0]);
26    }
27
28    public function testFactDoesNotProvideTheDeclaration(): void
29    {
30        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
31        $statement = $semantics->inspect('CREATE TABLE n (x int)', []);
32        $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($statement->context);
33        $n1 = $statement->statement;
34        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\CreateTable::class, $n1);
35        self::assertSame('1 0', count((new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\Definitions())->fact($n1, $derivation, null)->shape->slots) . ' ' . count($derivation->facts()->declarations));
36    }
37
38    public function testNameIsTemporaryForATemporaryTable(): void
39    {
40        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
41        $statement = $semantics->inspect('CREATE TEMP TABLE n (x int)', []);
42        $n1 = $statement->statement;
43        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\CreateTable::class, $n1);
44        self::assertSame('pg_temp', (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\Definitions())->name($n1, null)->schema?->value);
45    }
46
47    public function testElementsResolvesTheParents(): void
48    {
49        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
50        $context = [];
51        array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
52        $statement = $semantics->inspect('CREATE TABLE n () INHERITS (t, zz)', $context);
53        self::assertSame(1, count($statement->facts->diagnostics));
54    }
55
56    public function testReportReportsRepeatedAndSystemColumnNames(): void
57    {
58        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
59        $statement = $semantics->inspect('CREATE TABLE n (a int, a int, xmin int, b serial[], PRIMARY KEY (a), PRIMARY KEY (b))', []);
60        self::assertSame([
61          0 => 'column "a" specified more than once',
62          1 => 'column name "xmin" conflicts with a system column name',
63          2 => 'array of serial is not implemented',
64          3 => 'multiple primary keys for table "n" are not allowed',
65        ], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
66    }
67
68    public function testReportCountsTheColumnsALikeClauseCopies(): void
69    {
70        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
71        $context = $semantics->inspect('CREATE TABLE t (a int NOT NULL, b int)', [])->declarations();
72        $statement = $semantics->inspect('CREATE TABLE n (b text, LIKE t, c int)', $context);
73        self::assertSame(['column "b" specified more than once'], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
74    }
75
76    public function testDeriveReportsATemporaryTableInAPermanentSchema(): void
77    {
78        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
79        $statement = $semantics->inspect('CREATE TEMP TABLE public.n (a int)', []);
80        self::assertSame(['cannot create temporary relation in non-temporary schema'], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
81    }
82
83    public function testKindDeclaresAForeignTableAsOne(): void
84    {
85        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
86        self::assertSame(
87            [\SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\RelationKind::ForeignTable, \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\RelationKind::BaseTable],
88            [$semantics->inspect('CREATE FOREIGN TABLE f (a int) SERVER x')->declarations()[0]->kind, $semantics->inspect('CREATE TABLE t (a int)')->declarations()[0]->kind],
89        );
90    }
91}
92