packages/sql-semantics-postgres/tests/Unit/Statement/Constraint/Table/ExclusionTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Constraint\Table;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Constraint\Table\Exclusion::class)]
12#[Medium]
13final class ExclusionTest extends TestCase
14{
15    public function testKindIsExclusion(): void
16    {
17        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18        $statement = $semantics->inspect('CREATE TABLE t (a int, EXCLUDE (a WITH =))', []);
19        $n1 = $statement->statement;
20        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\CreateTable::class, $n1);
21        $n2 = $n1->definition;
22        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Element\ListedColumns::class, $n2);
23        $n3 = $n2->elements[1];
24        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Constraint\Table\Exclusion::class, $n3);
25        self::assertSame(\SqlSemantics\Platform\PostgreSql\Statement\Constraint\ConstraintKind::Exclusion, $n3->kind());
26    }
27
28    public function testDeriveClauseDerivesTheElementsAndThePredicate(): void
29    {
30        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
31        $statement = $semantics->inspect('CREATE TABLE t (a int, EXCLUDE (zz WITH =) INCLUDE (yy) WHERE (a) NOT VALID)', []);
32        self::assertSame([
33          0 => 'Column zz does not exist.',
34          1 => 'column "yy" named in key does not exist',
35          2 => 'argument of WHERE must be type boolean',
36          3 => 'EXCLUDE constraints cannot be marked NOT VALID',
37        ], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
38    }
39
40    public function testRenderWritesTheClauseAsWritten(): void
41    {
42        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
43        $statement = $semantics->inspect('CREATE TABLE t (a int, CONSTRAINT x EXCLUDE USING gist (a WITH =, (a + 1) WITH OPERATOR(pg_catalog.=)) INCLUDE (a) WITH (fillfactor = 70) USING INDEX TABLESPACE s WHERE (a > 0) DEFERRABLE)', []);
44        self::assertSame('CREATE TABLE t (a INT, CONSTRAINT x EXCLUDE USING gist (a WITH =, (a + 1) WITH OPERATOR (pg_catalog.=)) INCLUDE (a) WITH (fillfactor = 70) USING INDEX TABLESPACE s WHERE (a > 0) DEFERRABLE)', $statement->toString());
45    }
46}
47