packages/sql-semantics-sqlite/tests/Unit/Rules/Definition/TableProblemsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Construction\Derivation;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Fact\Diagnostic;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\Sqlite\Rules\Definition\TableProblems;
15use SqlSemantics\Platform\Sqlite\Semantics;
16use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\ColumnDefinition;
17use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\DuplicateColumn;
18use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\GeneratedColumnProblem;
19use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\PrimaryKeyFlaw;
20use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\PrimaryKeyProblem;
21use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\QualifiedTemporaryName;
22use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\StrictTypeViolation;
23use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\UnknownTableOption;
24
25#[CoversClass(TableProblems::class)]
26#[Medium]
27final class TableProblemsTest extends TestCase
28{
29    public function testReportFindsNothingInASoundDefinition(): void
30    {
31        $operation = (new Semantics())->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY AUTOINCREMENT, a TEXT) STRICT', []);
32
33        self::assertSame([], $operation->facts->diagnostics);
34    }
35
36    public function testReportFindsAnUnknownOptionAndANonStandardStrictType(): void
37    {
38        $operation = (new Semantics())->inspect('CREATE TABLE t (a VARCHAR(10), b) STRICT, fast', []);
39
40        self::assertSame([UnknownTableOption::class, StrictTypeViolation::class, StrictTypeViolation::class], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic::class, $operation->facts->diagnostics));
41    }
42
43    public function testReportFindsEveryPrimaryKeyFlaw(): void
44    {
45        $semantics = new Semantics();
46        $flaw = static fn (string $sql): ?PrimaryKeyFlaw => array_map(static fn (Diagnostic $diagnostic): ?PrimaryKeyFlaw => $diagnostic instanceof PrimaryKeyProblem ? $diagnostic->flaw : null, $semantics->inspect($sql, [])->facts->diagnostics)[0] ?? null;
47
48        self::assertSame(PrimaryKeyFlaw::Repeated, $flaw('CREATE TABLE t (a PRIMARY KEY, b PRIMARY KEY)'));
49        self::assertSame(PrimaryKeyFlaw::MissingWithoutRowid, $flaw('CREATE TABLE t (a) WITHOUT ROWID'));
50        self::assertSame(PrimaryKeyFlaw::AutoincrementNotIntegerKey, $flaw('CREATE TABLE t (a INTEGER PRIMARY KEY DESC AUTOINCREMENT)'));
51        self::assertSame(PrimaryKeyFlaw::AutoincrementWithoutRowid, $flaw('CREATE TABLE t (a INTEGER PRIMARY KEY AUTOINCREMENT) WITHOUT ROWID'));
52    }
53
54    public function testGeneratedReportsAKeyColumnThatIsGeneratedAndATableWithoutAPlainColumn(): void
55    {
56        $semantics = new Semantics();
57        $problems = static fn (string $sql): array => array_map(static fn (Diagnostic $diagnostic): array => $diagnostic instanceof GeneratedColumnProblem ? [$diagnostic->flaw->name, $diagnostic->column?->value] : [$diagnostic::class], $semantics->inspect($sql, [])->facts->diagnostics);
58
59        self::assertSame([['InPrimaryKey', 'b']], $problems('CREATE TABLE t (a, b AS (a) PRIMARY KEY)'));
60        self::assertSame([['InPrimaryKey', 'c']], $problems('CREATE TABLE t (a, b AS (a), c AS (a), PRIMARY KEY (a, "c"))'));
61        self::assertSame([['InPrimaryKey', 'a'], ['NoPlainColumn', null]], $problems('CREATE TABLE t (a AS (1) PRIMARY KEY)'));
62        self::assertSame([], $problems('CREATE TABLE t (a PRIMARY KEY, b AS (a))'));
63    }
64
65    public function testTemporaryReportsOnlyAQualifierOtherThanTemp(): void
66    {
67        $semantics = new Semantics();
68
69        self::assertInstanceOf(QualifiedTemporaryName::class, $semantics->inspect('CREATE TEMP TABLE main.t (a)')->facts->diagnostics[0]);
70        self::assertSame([], $semantics->inspect('CREATE TEMP TABLE TEMP.t (a)')->facts->diagnostics);
71        self::assertSame([], $semantics->inspect('CREATE TABLE main.t (a)')->facts->diagnostics);
72    }
73
74    public function testTemporaryReportsThroughTheDerivation(): void
75    {
76        $derivation = new Derivation((new Semantics())->context());
77        (new TableProblems())->temporary(new QualifiedName(new Name('t'), new Name('aux')), true, $derivation);
78
79        self::assertInstanceOf(QualifiedTemporaryName::class, $derivation->facts()->diagnostics[0]);
80    }
81
82    public function testDuplicatesReportsEachLaterColumnWithATakenName(): void
83    {
84        $derivation = new Derivation((new Semantics())->context());
85        (new TableProblems())->duplicates([new ColumnDefinition(new Name('a')), new ColumnDefinition(new Name('b')), new ColumnDefinition(new Name('A')), new ColumnDefinition(new Name('a'))], $derivation);
86        $diagnostics = $derivation->facts()->diagnostics;
87
88        self::assertCount(2, $diagnostics);
89        self::assertInstanceOf(DuplicateColumn::class, $diagnostics[0]);
90        self::assertSame('A', $diagnostics[0]->column->value);
91    }
92}
93