packages/sql-semantics-sqlite/tests/Unit/Rules/Definition/AlterationProblemsTest.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\Model\Statement\Type\Known;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\Sqlite\Rules\Definition\AlterationProblems;
17use SqlSemantics\Platform\Sqlite\Rules\Definition\TableShapes;
18use SqlSemantics\Platform\Sqlite\Semantics;
19use SqlSemantics\Platform\Sqlite\Statement\Schema\AlterAddColumn;
20use SqlSemantics\Platform\Sqlite\Statement\Schema\Limit\DefaultNotConstant;
21use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\AlterationObstacle;
22use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\AlterationRefused;
23use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\DuplicateColumn;
24use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\GeneratedColumnProblem;
25use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\StrictTypeViolation;
26use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\UnknownColumn;
27
28#[CoversClass(AlterationProblems::class)]
29#[Medium]
30final class AlterationProblemsTest extends TestCase
31{
32 public function testOccurrencesCountsTheColumnsWithANameWithoutRegardToCase(): void
33 {
34 $semantics = new Semantics();
35 $derivation = new Derivation($semantics->context([$semantics->inspect('CREATE TABLE t (a, b)')]));
36 $fact = (new TableShapes())->target($derivation, new QualifiedName(new Name('t')));
37
38 self::assertSame(1, (new AlterationProblems())->occurrences($fact, new Name('A'), $derivation));
39 self::assertSame(0, (new AlterationProblems())->occurrences($fact, new Name('c'), $derivation));
40 }
41
42 public function testOccurrencesIsNullWhileTheColumnsAreNotKnown(): void
43 {
44 $semantics = new Semantics();
45 $open = new Derivation($semantics->context());
46 $missing = new Derivation($semantics->context([]));
47
48 self::assertNull((new AlterationProblems())->occurrences((new TableShapes())->target($open, new QualifiedName(new Name('t'))), new Name('a'), $open));
49 self::assertNull((new AlterationProblems())->occurrences((new TableShapes())->target($missing, new QualifiedName(new Name('t'))), new Name('a'), $missing));
50 }
51
52 public function testStrictIsReadFromTheDeclaredColumns(): void
53 {
54 $semantics = new Semantics();
55 $strict = new Derivation($semantics->context([$semantics->inspect('CREATE TABLE t (a INT) STRICT')]));
56 $plain = new Derivation($semantics->context([$semantics->inspect('CREATE TABLE t (a INT)')]));
57
58 self::assertTrue((new AlterationProblems())->strict((new TableShapes())->target($strict, new QualifiedName(new Name('t')))));
59 self::assertFalse((new AlterationProblems())->strict((new TableShapes())->target($plain, new QualifiedName(new Name('t')))));
60 }
61
62 public function testSlotHasTheRecordedTypeAndTheNullFactOfTheNewColumn(): void
63 {
64 $semantics = new Semantics();
65 $derivation = new Derivation($semantics->context([$semantics->inspect('CREATE TABLE t (a)')]));
66 $statement = $semantics->inspect('ALTER TABLE t ADD b integer NOT NULL DEFAULT 1')->statement;
67
68 self::assertInstanceOf(AlterAddColumn::class, $statement);
69 $slot = (new AlterationProblems())->slot((new TableShapes())->target($derivation, new QualifiedName(new Name('t'))), $statement->column);
70 self::assertSame('b', $slot->name?->value);
71 self::assertSame(Nullability::NotNull, $slot->nullability);
72 self::assertInstanceOf(Known::class, $slot->type);
73 self::assertSame('INTEGER', $slot->type->descriptor->name());
74 }
75
76 public function testAddedReportsEveryObstacleOfTheNewColumn(): void
77 {
78 $semantics = new Semantics();
79 $table = $semantics->inspect('CREATE TABLE t (a INT) STRICT');
80 $obstacles = static fn (string $sql): array => array_map(static fn (Diagnostic $diagnostic): string => $diagnostic instanceof AlterationRefused ? $diagnostic->obstacle->name : $diagnostic::class, $semantics->inspect($sql, [$table])->facts->diagnostics);
81
82 self::assertSame([], $obstacles('ALTER TABLE t ADD b INT NOT NULL DEFAULT 0'));
83 self::assertSame([], $obstacles('ALTER TABLE t ADD b INT AS (a) VIRTUAL NOT NULL'));
84 self::assertSame([AlterationObstacle::UniqueColumn->name], $obstacles('ALTER TABLE t ADD b INT UNIQUE'));
85 self::assertSame([AlterationObstacle::PrimaryKeyColumn->name], $obstacles('ALTER TABLE t ADD b INT PRIMARY KEY'));
86 self::assertSame([AlterationObstacle::StoredColumn->name], $obstacles('ALTER TABLE t ADD b INT AS (a) STORED'));
87 self::assertSame([AlterationObstacle::NotNullWithoutDefault->name], $obstacles('ALTER TABLE t ADD b INT NOT NULL'));
88 self::assertSame([AlterationObstacle::NotNullWithoutDefault->name], $obstacles('ALTER TABLE t ADD b INT NOT NULL DEFAULT NULL'));
89 self::assertSame([DuplicateColumn::class], $obstacles('ALTER TABLE t ADD A INT'));
90 self::assertSame([StrictTypeViolation::class], $obstacles('ALTER TABLE t ADD b VARCHAR'));
91 self::assertSame([AlterationObstacle::NotNullWithoutDefault->name], $obstacles('ALTER TABLE t ADD b INT NOT NULL DEFAULT ((NULL))'));
92 self::assertSame([AlterationObstacle::NotNullWithoutDefault->name], $obstacles('ALTER TABLE t ADD b INT DEFAULT 1 NOT NULL DEFAULT NULL'));
93 self::assertSame([], $obstacles("ALTER TABLE t ADD b INT NOT NULL DEFAULT (-(-'x'))"));
94 self::assertSame([AlterationObstacle::DefaultNotConstant->name], $obstacles('ALTER TABLE t ADD b INT DEFAULT CURRENT_TIMESTAMP'));
95 self::assertSame([AlterationObstacle::DefaultNotConstant->name], $obstacles('ALTER TABLE t ADD b INT DEFAULT (1 + 1)'));
96 self::assertSame([AlterationObstacle::DefaultNotConstant->name], $obstacles('ALTER TABLE t ADD b INT DEFAULT (abs(1))'));
97 self::assertSame([], $obstacles('ALTER TABLE t ADD b INT DEFAULT (1 + 1) DEFAULT 2'));
98 self::assertSame([DefaultNotConstant::class], $obstacles('ALTER TABLE t ADD b INT DEFAULT (?)'));
99 self::assertSame([GeneratedColumnProblem::class], $obstacles('ALTER TABLE t ADD b INT AS (a) DEFAULT (1 + 1)'));
100 }
101
102 public function testDroppedReportsAnUnknownColumnAndTheOnlyColumn(): void
103 {
104 $semantics = new Semantics();
105 $one = new Derivation($semantics->context([$semantics->inspect('CREATE TABLE t (a)')]));
106 $fact = (new TableShapes())->target($one, new QualifiedName(new Name('t')));
107 (new AlterationProblems())->dropped($fact, new Name('zz'), $one);
108 (new AlterationProblems())->dropped($fact, new Name('a'), $one);
109 $diagnostics = $one->facts()->diagnostics;
110
111 self::assertInstanceOf(UnknownColumn::class, $diagnostics[0]);
112 self::assertInstanceOf(AlterationRefused::class, $diagnostics[1]);
113 self::assertSame(AlterationObstacle::LastColumn, $diagnostics[1]->obstacle);
114 }
115
116 public function testRenamedReportsAnUnknownOldNameAndATakenNewName(): void
117 {
118 $semantics = new Semantics();
119 $derivation = new Derivation($semantics->context([$semantics->inspect('CREATE TABLE t (a, b)')]));
120 $fact = (new TableShapes())->target($derivation, new QualifiedName(new Name('t')));
121 (new AlterationProblems())->renamed($fact, new Name('zz'), new Name('c'), $derivation);
122 (new AlterationProblems())->renamed($fact, new Name('a'), new Name('b'), $derivation);
123 (new AlterationProblems())->renamed($fact, new Name('a'), new Name('A'), $derivation);
124 $diagnostics = $derivation->facts()->diagnostics;
125
126 self::assertCount(2, $diagnostics);
127 self::assertInstanceOf(UnknownColumn::class, $diagnostics[0]);
128 self::assertInstanceOf(DuplicateColumn::class, $diagnostics[1]);
129 }
130}
131