packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Fact/FactsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Statement\Fact;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Fact\Facts;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Fact\ScalarFact;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\UndeclaredTable;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
15use SqlSemantics\Platform\Sqlite\Semantics;
16use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
17use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
18use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
19use SqlSemantics\Platform\Sqlite\Statement\Relation\DerivedQuery;
20use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
21
22#[CoversClass(Facts::class)]
23#[Medium]
24final class FactsTest extends TestCase
25{
26 public function testIndexKeysPairsByNodeIdentity(): void
27 {
28 $first = new IntegerLiteral('1');
29 $second = new IntegerLiteral('1');
30 $fact = new ScalarFact(new Known(Storage::Integer), Nullability::NotNull);
31 $facts = new Facts([], [], [], [], null, []);
32
33 $indexed = $facts->index([[$first, $fact], [$second, $fact]]);
34
35 self::assertCount(2, $indexed);
36 self::assertSame([$first, $fact], $indexed[spl_object_id($first)]);
37 self::assertSame([$second, $fact], $indexed[spl_object_id($second)]);
38 }
39
40 public function testIndexRefusesANodeThatOccursTwice(): void
41 {
42 $node = new IntegerLiteral('1');
43 $fact = new ScalarFact(new Known(Storage::Integer), Nullability::NotNull);
44
45 $this->expectExceptionMessage('A statement node occurs at one position only.');
46
47 new Facts([[$node, $fact], [$node, $fact]], [], [], [], null, []);
48 }
49
50 public function testScalarAnswersTheFactOfAnExpressionOfTheOperation(): void
51 {
52 $operation = (new Semantics())->inspect('SELECT 1 + NULL');
53 $expression = $operation->field(0)->expression;
54
55 self::assertNotNull($expression);
56 self::assertSame(Nullability::Nullable, $operation->facts->scalar($expression)->nullability);
57 }
58
59 public function testScalarRefusesAnExpressionOfAnotherOperation(): void
60 {
61 $semantics = new Semantics();
62 $operation = $semantics->inspect('SELECT 1');
63 $foreign = $semantics->inspect('SELECT 1')->field(0)->expression;
64
65 self::assertNotNull($foreign);
66 $this->expectExceptionMessage('The expression is not part of this operation.');
67
68 $operation->facts->scalar($foreign);
69 }
70
71 public function testRelationAnswersTheFactOfAnOccurrenceOfTheOperation(): void
72 {
73 $operation = (new Semantics())->inspect('SELECT a FROM t');
74
75 $fact = $operation->facts->relation($operation->singleNamedInput());
76
77 self::assertInstanceOf(UndeclaredTable::class, $fact->table);
78 self::assertFalse($fact->shape->complete());
79 }
80
81 public function testRelationRefusesAnOccurrenceOfAnotherOperation(): void
82 {
83 $semantics = new Semantics();
84 $operation = $semantics->inspect('SELECT a FROM t');
85 $foreign = $semantics->inspect('SELECT a FROM t')->singleNamedInput();
86
87 $this->expectExceptionMessage('The relation is not part of this operation.');
88
89 $operation->facts->relation($foreign);
90 }
91
92 public function testQueryAnswersTheFactOfAQueryOfTheOperation(): void
93 {
94 $operation = (new Semantics())->inspect('SELECT x FROM (SELECT 1 AS x, 2 AS y)');
95 $statement = $operation->statement;
96
97 self::assertInstanceOf(Select::class, $statement);
98 self::assertInstanceOf(DerivedQuery::class, $statement->from);
99 self::assertCount(2, $operation->facts->query($statement->from->query)->projection);
100 self::assertCount(1, $operation->facts->query($statement)->projection);
101 }
102
103 public function testQueryRefusesAQueryOfAnotherOperation(): void
104 {
105 $semantics = new Semantics();
106 $operation = $semantics->inspect('SELECT 1');
107 $foreign = $semantics->inspect('SELECT 1')->statement;
108
109 self::assertInstanceOf(Select::class, $foreign);
110 $this->expectExceptionMessage('The query is not part of this operation.');
111
112 $operation->facts->query($foreign);
113 }
114
115 public function testCoversTellsNodesOfTheOperationFromOthers(): void
116 {
117 $semantics = new Semantics();
118 $operation = $semantics->inspect('SELECT a FROM t');
119 $statement = $operation->statement;
120
121 self::assertInstanceOf(Select::class, $statement);
122 self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
123 self::assertTrue($operation->facts->covers($statement));
124 self::assertTrue($operation->facts->covers($operation->singleNamedInput()));
125 self::assertTrue($operation->facts->covers($statement->columns[0]->expression));
126 self::assertFalse($operation->facts->covers($statement->columns[0]));
127 self::assertFalse($operation->facts->covers($semantics->inspect('SELECT a FROM t')->singleNamedInput()));
128 }
129
130 public function testDeclarationsAndDiagnosticsAreTypedLists(): void
131 {
132 $semantics = new Semantics();
133 $operation = $semantics->inspect('CREATE TABLE t (a INTEGER, a TEXT)');
134
135 self::assertCount(1, $operation->facts->declarations);
136 self::assertCount(1, $operation->facts->diagnostics);
137 self::assertNull($operation->facts->output);
138 }
139}
140