packages/sql-semantics-sqlite/tests/Unit/Model/Statement/OperationTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Statement;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Operation;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\AbsentField;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\AmbiguousFields;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\DependentField;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\UniqueField;
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\JoinChain;
20
21#[CoversClass(Operation::class)]
22#[Medium]
23final class OperationTest extends TestCase
24{
25 public function testToStringRendersFromTheStructureAndNotFromTheInput(): void
26 {
27 $operation = (new Semantics())->analyze('select a ,b from t where a>1 /* c */');
28
29 self::assertSame('SELECT a, b FROM t WHERE a > 1', $operation->toString());
30 }
31
32 public function testToStringOfANewRootBuiltFromExplicitStructure(): void
33 {
34 $semantics = new Semantics();
35 $context = $semantics->context([]);
36
37 $operation = new Operation($context, new Select([new ResultColumn(new IntegerLiteral('7'))]));
38
39 self::assertSame('SELECT 7', $operation->toString());
40 self::assertSame($context, $operation->context);
41 self::assertInstanceOf(IntegerLiteral::class, $operation->field(0)->expression);
42 self::assertSame('7', $operation->field(0)->expression->digits);
43 }
44
45 public function testToStringIsNotReachedForANodeUsedAtTwoPositions(): void
46 {
47 $semantics = new Semantics();
48 $literal = new IntegerLiteral('1');
49
50 $this->expectExceptionMessage('A statement node occurs at one position only.');
51
52 new Operation($semantics->context([]), new Select([new ResultColumn($literal), new ResultColumn($literal)]));
53 }
54
55 public function testProfileIsTheProfileOfTheContext(): void
56 {
57 $semantics = new Semantics();
58
59 $operation = $semantics->analyze('SELECT 1');
60
61 self::assertSame($semantics->profile(), $operation->profile());
62 self::assertSame($operation->context->profile, $operation->profile());
63 }
64
65 public function testDeclarationsAnswerTheTablesAStatementProvides(): void
66 {
67 $operation = (new Semantics())->analyze('CREATE TABLE t (a INTEGER, b TEXT)');
68
69 self::assertCount(1, $operation->declarations());
70 self::assertSame('t', $operation->declarations()[0]->name->name->value);
71 self::assertSame(['a', 'b'], array_map(static fn ($column): string => $column->name->value, $operation->declarations()[0]->columns));
72 self::assertSame([], (new Semantics())->analyze('SELECT 1')->declarations());
73 }
74
75 public function testShapeIsNullWhenTheStatementReturnsNoRows(): void
76 {
77 self::assertNull((new Semantics())->analyze('CREATE TABLE t (a INTEGER)')->shape());
78 }
79
80 public function testShapeStaysOpenWhileAStarCannotBeExpanded(): void
81 {
82 $shape = (new Semantics())->analyze('SELECT *, 1 FROM t')->shape();
83
84 self::assertNotNull($shape);
85 self::assertFalse($shape->complete());
86 self::assertCount(1, $shape->slots);
87 }
88
89 public function testFieldsAreNullForAnOpenShapeAndForNoRows(): void
90 {
91 $semantics = new Semantics();
92
93 self::assertNull($semantics->analyze('SELECT * FROM t')->fields());
94 self::assertNull($semantics->analyze('DELETE FROM t')->fields());
95 self::assertCount(2, $semantics->analyze('SELECT 1, 2')->fields() ?? []);
96 }
97
98 public function testFieldFindsByPositionAndByUniqueName(): void
99 {
100 $operation = (new Semantics())->analyze('SELECT 1 AS a, 2 AS b');
101
102 self::assertSame(1, $operation->field('b')->position);
103 self::assertSame('a', $operation->field(0)->name?->value);
104 }
105
106 public function testFieldRefusesANameSeveralFieldsShare(): void
107 {
108 $operation = (new Semantics())->analyze('SELECT 1 AS a, 2 AS a');
109
110 $this->expectExceptionMessage('The name does not denote exactly one output field.');
111
112 $operation->field('a');
113 }
114
115 public function testFieldRefusesAPositionOfAnOpenShape(): void
116 {
117 $operation = (new Semantics())->analyze('SELECT * FROM t');
118
119 $this->expectExceptionMessage('The statement has no complete field list.');
120
121 $operation->field(0);
122 }
123
124 public function testLookupFieldTellsTheOutcomesApart(): void
125 {
126 $semantics = new Semantics();
127 $operation = $semantics->analyze('SELECT 1 AS a, 2 AS a, 3 AS b');
128
129 self::assertInstanceOf(UniqueField::class, $operation->lookupField('b'));
130 self::assertInstanceOf(AmbiguousFields::class, $operation->lookupField('a'));
131 self::assertInstanceOf(AbsentField::class, $operation->lookupField('c'));
132 self::assertInstanceOf(DependentField::class, $semantics->analyze('SELECT * FROM t')->lookupField('a'));
133 }
134
135 public function testLookupFieldRefusesAStatementWithoutRows(): void
136 {
137 $operation = (new Semantics())->analyze('DELETE FROM t');
138
139 $this->expectExceptionMessage('The statement returns no rows.');
140
141 $operation->lookupField('a');
142 }
143
144 public function testInputRelationIsTheFromStructureOfASelection(): void
145 {
146 $semantics = new Semantics();
147
148 self::assertInstanceOf(JoinChain::class, $semantics->analyze('SELECT a FROM t JOIN u ON t.a = u.a')->inputRelation());
149 self::assertNull($semantics->analyze('SELECT 1')->inputRelation());
150 self::assertNull($semantics->analyze('DELETE FROM t')->inputRelation());
151 }
152
153 public function testSingleNamedInputAnswersTheOnlyNamedRelation(): void
154 {
155 $operation = (new Semantics())->analyze('SELECT a FROM t AS x');
156
157 self::assertSame('t', $operation->singleNamedInput()->name()->name->value);
158 self::assertSame('x', $operation->singleNamedInput()->alias()?->value);
159 }
160
161 public function testSingleNamedInputRefusesAJoin(): void
162 {
163 $operation = (new Semantics())->analyze('SELECT a FROM t, u');
164
165 $this->expectExceptionMessage('The statement does not read from exactly one named relation.');
166
167 $operation->singleNamedInput();
168 }
169
170 public function testSingleNamedInputRefusesADerivedRelation(): void
171 {
172 $operation = (new Semantics())->analyze('SELECT x FROM (SELECT 1 AS x)');
173
174 $this->expectExceptionMessage('The statement does not read from exactly one named relation.');
175
176 $operation->singleNamedInput();
177 }
178}
179