packages/sql-semantics-postgres/tests/Unit/Statement/Table/Element/ColumnDefinitionTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Table\Element;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Table\Element\ColumnDefinition::class)]
12#[Medium]
13final class ColumnDefinitionTest extends TestCase
14{
15    public function testDeriveClauseReportsTheProblemsOfTheQualifiers(): void
16    {
17        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18        $statement = $semantics->inspect('CREATE TABLE t (a int COLLATE "C" COLLATE "POSIX" DEFERRABLE GENERATED ALWAYS AS IDENTITY GENERATED ALWAYS AS IDENTITY)', []);
19        self::assertSame([
20          0 => 'multiple COLLATE clauses not allowed',
21          1 => 'misplaced DEFERRABLE clause',
22          2 => 'multiple identity specifications for column "a"',
23        ], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
24    }
25
26    public function testRenderWritesTheClauseAsWritten(): void
27    {
28        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
29        $statement = $semantics->inspect('CREATE TABLE t (a text STORAGE external COMPRESSION lz4 OPTIONS (x \'y\') COLLATE "C" NOT NULL)', []);
30        self::assertSame('CREATE TABLE t (a text STORAGE external COMPRESSION lz4 OPTIONS (x \'y\') COLLATE "C" NOT NULL)', $statement->toString());
31    }
32
33    public function testDeriveClauseChecksTheTypeOfAnIdentityColumn(): void
34    {
35        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
36        $statement = $semantics->inspect('CREATE TABLE t (a smallint GENERATED ALWAYS AS IDENTITY, b numeric GENERATED BY DEFAULT AS IDENTITY, c text)', []);
37        self::assertSame(['identity column type must be smallint, integer, or bigint'], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
38    }
39}
40