packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Fact/QueryFactTest.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\QueryFact;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Comparison;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\UndeclaredRelation;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\AbsentField;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\DependentField;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\Field;
18use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\OpenStar;
19use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\OutputSlot;
20use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\UniqueField;
21use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
22use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
23use SqlSemantics\Platform\Sqlite\Semantics;
24use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
25
26#[CoversClass(QueryFact::class)]
27#[Medium]
28final class QueryFactTest extends TestCase
29{
30    public function testFieldsListsACompleteProjectionInOrder(): void
31    {
32        $first = new Field(0, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull));
33        $second = new Field(1, new OutputSlot(null, new Known(Storage::Text), Nullability::Nullable));
34
35        $fact = new QueryFact([$first, $second], Comparison::Sensitive);
36
37        self::assertSame([$first, $second], $fact->fields()?->items);
38        self::assertTrue($fact->shape->complete());
39        self::assertSame([$first->slot, $second->slot], $fact->shape->slots);
40    }
41
42    public function testFieldsIsNullWhileAStarCannotBeExpanded(): void
43    {
44        $missing = new UndeclaredRelation(new QualifiedName(new Name('t')));
45        $field = new Field(0, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull));
46
47        $fact = new QueryFact([new OpenStar([$missing]), $field], Comparison::Sensitive);
48
49        self::assertNull($fact->fields());
50        self::assertFalse($fact->shape->complete());
51        self::assertSame([$missing], $fact->shape->missing);
52        self::assertSame([$field->slot], $fact->shape->slots);
53    }
54
55    public function testFieldsOfAnAnalyzedOpenStarStayNull(): void
56    {
57        $fact = (new Semantics())->analyze('SELECT *, 1 AS one FROM t')->facts->output;
58
59        self::assertNotNull($fact);
60        self::assertInstanceOf(OpenStar::class, $fact->projection[0]);
61        self::assertNull($fact->fields());
62    }
63
64    public function testLookupDistinguishesUniqueAndAbsentInACompleteShape(): void
65    {
66        $field = new Field(0, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull));
67        $fact = new QueryFact([$field], Comparison::AsciiInsensitive);
68
69        $unique = $fact->lookup('A');
70
71        self::assertInstanceOf(UniqueField::class, $unique);
72        self::assertSame($field, $unique->field);
73        self::assertInstanceOf(AbsentField::class, $fact->lookup('b'));
74    }
75
76    public function testLookupIsDependentInAnOpenShapeEvenWhenACandidateIsKnown(): void
77    {
78        $missing = new UndeclaredRelation(new QualifiedName(new Name('t')));
79        $field = new Field(0, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull));
80        $fact = new QueryFact([new OpenStar([$missing]), $field], Comparison::Sensitive);
81
82        $lookup = $fact->lookup('a');
83
84        self::assertInstanceOf(DependentField::class, $lookup);
85        self::assertSame([$field], $lookup->candidates);
86        self::assertSame([$missing], $lookup->missing);
87        self::assertSame('a', $lookup->name);
88    }
89
90    public function testLookupIsNotReachedForAProjectionItemThatIsNeitherFieldNorStar(): void
91    {
92        $this->expectExceptionMessage('A projection holds fields and open stars.');
93
94        new QueryFact([new Name('a')], Comparison::Sensitive);
95    }
96
97    public function testLookupIsNotReachedForAProjectionThatIsNoList(): void
98    {
99        $field = new Field(0, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull));
100
101        $this->expectExceptionMessage('A projection is an ordered list.');
102
103        new QueryFact(['a' => $field], Comparison::Sensitive);
104    }
105}
106