packages/sql-semantics-postgres/tests/Unit/Rules/Expression/Typing/SublinksTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Expression\Typing;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\QueryFact;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRelation;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\Field;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OpenStar;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent;
21use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
22use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
23use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
24use SqlSemantics\Platform\PostgreSql\Platform;
25use SqlSemantics\Platform\PostgreSql\Rules\Expression\Typing\Sublinks;
26use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\Composite;
27use SqlSemantics\Platform\PostgreSql\Statement\Expression\Problem\RowArity;
28use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
29use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\ArrayOf;
30use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
31
32#[CoversClass(Sublinks::class)]
33#[Small]
34final class SublinksTest extends TestCase
35{
36 public function testValueIsARowForSeveralColumns(): void
37 {
38 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
39 $slots = [new OutputSlot(new Name('a'), new Known(Builtin::Int4), Nullability::NotNull), new OutputSlot(new Name('b'), new Known(Builtin::Text), Nullability::Nullable)];
40 $fact = new QueryFact([new Field(0, $slots[0]), new Field(1, $slots[1])], $derivation->context->columnNames);
41 self::assertEquals(new Known(new Composite($slots)), (new Sublinks())->value($derivation, $fact, 'a scalar subquery'));
42 }
43
44 public function testArrayIsAnArrayOfTheColumnType(): void
45 {
46 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
47 $fact = new QueryFact([new Field(0, new OutputSlot(new Name('a'), new Known(new ArrayOf(Builtin::Int4)), Nullability::NotNull))], $derivation->context->columnNames);
48 self::assertEquals(new Known(new ArrayOf(Builtin::Int4)), (new Sublinks())->array($derivation, $fact));
49 }
50
51 public function testComparedReportsAWrongNumberOfColumns(): void
52 {
53 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
54 $fact = new QueryFact([], $derivation->context->columnNames);
55 self::assertEquals(new Invalid(new RowArity('a subquery comparison', 1, 0)), (new Sublinks())->compared($derivation, new Known(Builtin::Int4), '=', $fact));
56 }
57
58 public function testElementsReadsAStringAsAnArrayOfTheValueType(): void
59 {
60 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
61 self::assertEquals(new Known(Builtin::Bool), (new Sublinks())->elements($derivation, new Known(Builtin::Int4), new OperatorName(new Name('=')), new Known(Builtin::Unknown)));
62 }
63
64 public function testSlotsDependOnAnOpenShape(): void
65 {
66 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
67 $missing = new UndeclaredRelation(new QualifiedName(new Name('t')));
68 self::assertEquals(new Dependent([$missing]), (new Sublinks())->slots(new QueryFact([new OpenStar([$missing])], $derivation->context->columnNames)));
69 }
70
71 public function testArityReportsTheProblem(): void
72 {
73 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
74 (new Sublinks())->arity($derivation, 'an ARRAY subquery', 1, 2);
75 self::assertEquals([new RowArity('an ARRAY subquery', 1, 2)], $derivation->facts()->diagnostics);
76 }
77}
78