packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/Column/GeneratedTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Schema\Column;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
15use SqlSemantics\Platform\Sqlite\Statement\Lexical\Word;
16use SqlSemantics\Platform\Sqlite\Statement\Lexical\WordQuote;
17use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\Generated;
18use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\GeneratedStorage;
19use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
20use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\UnknownStorage;
21
22#[CoversClass(Generated::class)]
23#[Medium]
24final class GeneratedTest extends TestCase
25{
26    public function testStorageIsVirtualUnlessStoredIsWritten(): void
27    {
28        self::assertSame(GeneratedStorage::Virtual, (new Generated(new IntegerLiteral('1')))->storage());
29        self::assertSame(GeneratedStorage::Virtual, (new Generated(new IntegerLiteral('1'), new Word(new Name('virtual'))))->storage());
30        self::assertSame(GeneratedStorage::Stored, (new Generated(new IntegerLiteral('1'), new Word(new Name('Stored'))))->storage());
31    }
32
33    public function testStorageIsNullForAnyOtherWordAndForAQuotedWord(): void
34    {
35        self::assertNull((new Generated(new IntegerLiteral('1'), new Word(new Name('persisted'))))->storage());
36        self::assertNull((new Generated(new IntegerLiteral('1'), new Word(new Name('stored'), WordQuote::Double)))->storage());
37    }
38
39    public function testDeriveConstraintResolvesColumnsOfTheTableBeingDefined(): void
40    {
41        $operation = (new Semantics())->inspect('CREATE TABLE t (a INT, b AS (a))', []);
42        $statement = $operation->statement;
43
44        self::assertInstanceOf(CreateTable::class, $statement);
45        $generated = $statement->columns[1]->constraints[0];
46        self::assertInstanceOf(Generated::class, $generated);
47        $resolution = $operation->facts->scalar($generated->expression)->resolution;
48        self::assertInstanceOf(ResolvedColumn::class, $resolution);
49        self::assertSame($operation->declarations()[0]->columns[0], $resolution->slot->column);
50        self::assertSame([], $operation->facts->diagnostics);
51    }
52
53    public function testDeriveConstraintDoesNotSeeTheRowIdentifier(): void
54    {
55        $operation = (new Semantics())->inspect('CREATE TABLE t (a INT, b AS (rowid))', []);
56
57        self::assertCount(1, $operation->facts->diagnostics);
58        self::assertInstanceOf(MissingColumn::class, $operation->facts->diagnostics[0]);
59    }
60
61    public function testDeriveConstraintReportsAnUnknownStorageWord(): void
62    {
63        $operation = (new Semantics())->inspect('CREATE TABLE t (a INT, b AS (a) "stored")', []);
64
65        self::assertCount(1, $operation->facts->diagnostics);
66        self::assertInstanceOf(UnknownStorage::class, $operation->facts->diagnostics[0]);
67    }
68
69    public function testRenderDropsTheOptionalKeywordsAndKeepsTheStorageWord(): void
70    {
71        $semantics = new Semantics();
72
73        self::assertSame('CREATE TABLE t (a INT, b INT NOT NULL AS (a + 1) stored)', $semantics->inspect('create table t (a INT, b INT not null generated always as (a + 1) stored)')->toString());
74        self::assertSame('CREATE TABLE t (a, b AS (a) "stored")', $semantics->inspect('CREATE TABLE t (a, b AS (a) "stored")')->toString());
75        self::assertSame('CREATE TABLE t (a, b AS (a))', $semantics->inspect('CREATE TABLE t (a, b AS (a))')->toString());
76    }
77}
78