packages/sql-semantics-postgres/tests/Unit/Statement/Constraint/Column/IdentityTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Constraint\Column;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Constraint\Column\Identity::class)]
12#[Medium]
13final class IdentityTest extends TestCase
14{
15    public function testKindIsIdentity(): void
16    {
17        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18        $statement = $semantics->analyze('CREATE TABLE t (a int GENERATED ALWAYS AS IDENTITY)', []);
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[0];
24        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Element\ColumnDefinition::class, $n3);
25        $n4 = $n3->qualifiers[0];
26        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Constraint\Column\Identity::class, $n4);
27        self::assertSame(\SqlSemantics\Platform\PostgreSql\Statement\Constraint\ConstraintKind::Identity, $n4->kind());
28    }
29
30    public function testDeriveClauseMakesTheColumnNotNull(): void
31    {
32        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
33        $statement = $semantics->analyze('CREATE TABLE t (a int GENERATED BY DEFAULT AS IDENTITY (START WITH 5 CYCLE))', []);
34        self::assertSame(\SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull, $statement->declarations()[0]->columns[0]->nullability);
35    }
36
37    public function testRenderWritesTheClauseAsWritten(): void
38    {
39        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
40        $statement = $semantics->analyze('CREATE TABLE t (a int GENERATED BY DEFAULT AS IDENTITY (START WITH 5 INCREMENT BY 2 CYCLE))', []);
41        self::assertSame('CREATE TABLE t (a INT GENERATED BY DEFAULT AS IDENTITY (START 5 INCREMENT 2 CYCLE))', $statement->toString());
42    }
43}
44