packages/sql-semantics-sqlite/tests/Unit/Rules/Definition/TableShapesTest.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\Resolution\ColumnLookup;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\Column;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\Table;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\IncompleteMembers;
18use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\DeclaredTable;
19use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\MissingTable;
20use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\UndeclaredTable;
21use SqlSemantics\Platform\Sqlite\Rules\Definition\TableShapes;
22use SqlSemantics\Platform\Sqlite\Semantics;
23use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateIndex;
24use SqlSemantics\Platform\Sqlite\Statement\Type\ColumnDomain;
25
26#[CoversClass(TableShapes::class)]
27#[Medium]
28final class TableShapesTest extends TestCase
29{
30 public function testShapeHasOneSlotPerDeclaredColumn(): void
31 {
32 $table = (new Semantics())->analyze('CREATE TABLE t (a INTEGER NOT NULL, b)')->declarations()[0];
33 $shape = (new TableShapes())->shape($table);
34
35 self::assertCount(2, $shape->slots);
36 self::assertSame($table->columns[0], $shape->slots[0]->column);
37 self::assertSame($table->columns[0]->nullability, $shape->slots[0]->nullability);
38 self::assertTrue($shape->complete());
39 }
40
41 public function testShapeOfAnIncompleteDeclarationIsOpen(): void
42 {
43 $semantics = new Semantics();
44 $table = new Table(new QualifiedName(new Name('t')), $semantics->profile(), [new Column(new Name('a'), new ColumnDomain(''))], [], false);
45 $shape = (new TableShapes())->shape($table);
46
47 self::assertFalse($shape->complete());
48 self::assertInstanceOf(IncompleteMembers::class, $shape->missing[0]);
49 }
50
51 public function testImplicitPointsAnAliasAtTheSlotOfItsDeclaredColumn(): void
52 {
53 $shapes = new TableShapes();
54 $semantics = new Semantics();
55 $aliased = $semantics->analyze('CREATE TABLE t (a, id INTEGER PRIMARY KEY)')->declarations()[0];
56 $plain = $semantics->analyze('CREATE TABLE t (a)')->declarations()[0];
57 $shape = $shapes->shape($aliased);
58
59 self::assertSame($shape->slots[1], $shapes->implicit($aliased, $shape)[0]->slot);
60 self::assertSame($plain->implicit[0]->column, $shapes->implicit($plain, $shapes->shape($plain))[0]->slot->column);
61 self::assertSame([], $shapes->implicit($semantics->analyze('CREATE TABLE t (a PRIMARY KEY) WITHOUT ROWID')->declarations()[0], $shape));
62 }
63
64 public function testTargetResolvesADeclaredAMissingAndAnUndeclaredTable(): void
65 {
66 $semantics = new Semantics();
67 $table = $semantics->analyze('CREATE TABLE t (a, b)');
68 $shapes = new TableShapes();
69 $declared = $shapes->target(new Derivation($semantics->context([$table])), new QualifiedName(new Name('T')));
70 $missing = $shapes->target(new Derivation($semantics->context([$table])), new QualifiedName(new Name('u')));
71 $undeclared = $shapes->target(new Derivation($semantics->context()), new QualifiedName(new Name('u')));
72
73 self::assertInstanceOf(DeclaredTable::class, $declared->table);
74 self::assertCount(2, $declared->shape->slots);
75 self::assertInstanceOf(MissingTable::class, $missing->table);
76 self::assertTrue($missing->shape->complete());
77 self::assertInstanceOf(UndeclaredTable::class, $undeclared->table);
78 self::assertFalse($undeclared->shape->complete());
79 }
80
81 public function testImplicitOfAnswersTheRowIdentifierOfAResolvedTableOnly(): void
82 {
83 $semantics = new Semantics();
84 $table = $semantics->analyze('CREATE TABLE t (a)');
85 $shapes = new TableShapes();
86
87 self::assertCount(1, $shapes->implicitOf($shapes->target(new Derivation($semantics->context([$table])), new QualifiedName(new Name('t')))));
88 self::assertSame([], $shapes->implicitOf($shapes->target(new Derivation($semantics->context()), new QualifiedName(new Name('t')))));
89 }
90
91 public function testScopeSeesTheRowIdentifierOnlyAtTheRowPosition(): void
92 {
93 $semantics = new Semantics();
94 $table = $semantics->analyze('CREATE TABLE t (a)');
95 $index = $semantics->analyze('CREATE INDEX i ON t (a)')->statement;
96 $derivation = new Derivation($semantics->context([$table]));
97 $shapes = new TableShapes();
98 $fact = $shapes->target($derivation, new QualifiedName(new Name('t')));
99
100 self::assertInstanceOf(CreateIndex::class, $index);
101 $scope = $shapes->scope($derivation, $index, new QualifiedName(new Name('t')), $fact->shape, $shapes->implicitOf($fact));
102 self::assertInstanceOf(ResolvedColumn::class, (new ColumnLookup())->find($scope->row, new Name('rowid')));
103 self::assertNotInstanceOf(ResolvedColumn::class, (new ColumnLookup())->find($scope->columns, new Name('rowid')));
104 self::assertInstanceOf(ResolvedColumn::class, (new ColumnLookup())->find($scope->columns, new Name('a')));
105 self::assertNotInstanceOf(ResolvedColumn::class, (new ColumnLookup())->find($scope->constant, new Name('a')));
106 self::assertFalse($scope->lacks(new Name('A')));
107 self::assertTrue($scope->lacks(new Name('rowid')));
108 }
109}
110