packages/sql-semantics-postgres/tests/Unit/Rules/Routine/RelationTargetsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Routine;
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\Declaration\Column;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Column\MissingColumn;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\DeclaredTable;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\UndeclaredTable;
20use SqlSemantics\Platform\PostgreSql\Platform;
21use SqlSemantics\Platform\PostgreSql\Rules\Routine\RelationTargets;
22use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
23use SqlSemantics\Platform\PostgreSql\Statement\Name\ImproperName;
24use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
25use SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference;
26use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\MemberName;
27use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\RelationTarget;
28use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\UnqualifiedName;
29use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
30
31#[CoversClass(RelationTargets::class)]
32#[Small]
33final class RelationTargetsTest extends TestCase
34{
35    public function testResolveRecordsTheTableOfAMember(): void
36    {
37        $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::PostgreSql172), [new Column(new Name('a'), Builtin::Int4)]);
38        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [$table], true));
39        $trigger = new MemberName(new Name('g'), new QualifiedName(new Name('t')));
40        $fact = (new RelationTargets())->resolve(ObjectKind::Trigger, $trigger, false, $derivation);
41        self::assertInstanceOf(DeclaredTable::class, $fact?->table);
42        self::assertSame($table, $fact->table->table);
43        self::assertSame($fact, $derivation->facts()->relation($trigger));
44    }
45
46    public function testResolveIgnoresKindsThatNameNoRelation(): void
47    {
48        $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::PostgreSql172), [new Column(new Name('a'), Builtin::Int4)]);
49        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [$table], true));
50        self::assertNull((new RelationTargets())->resolve(ObjectKind::Index, new DottedName([new Name('t')]), false, $derivation));
51        self::assertNull((new RelationTargets())->resolve(ObjectKind::Schema, new UnqualifiedName(new Name('t')), false, $derivation));
52    }
53
54    public function testDottedReportsAnImproperName(): void
55    {
56        $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::PostgreSql172), [new Column(new Name('a'), Builtin::Int4)]);
57        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [$table], true));
58        $name = new DottedName([new Name('a'), new Name('b'), new Name('c'), new Name('d')]);
59        (new RelationTargets())->dotted($name, $name, false, $derivation);
60        self::assertEquals([new ImproperName($name)], $derivation->facts()->diagnostics);
61    }
62
63    public function testRecordKeepsAnUndeclaredRelationOpen(): void
64    {
65        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], false));
66        $target = new RelationTarget(new RelationReference(new QualifiedName(new Name('t'))));
67        $fact = (new RelationTargets())->record($target, new QualifiedName(new Name('t')), false, $derivation);
68        self::assertInstanceOf(UndeclaredTable::class, $fact->table);
69        self::assertFalse($fact->shape->complete());
70    }
71
72    public function testColumnReportsAMissingColumn(): void
73    {
74        $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::PostgreSql172), [new Column(new Name('a'), Builtin::Int4)]);
75        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [$table], true));
76        (new RelationTargets())->column(new DottedName([new Name('t'), new Name('x')]), $derivation);
77        self::assertEquals([new MissingColumn(new Name('x'), new QualifiedName(new Name('t')))], $derivation->facts()->diagnostics);
78    }
79
80    public function testMemberAcceptsADeclaredColumn(): void
81    {
82        $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::PostgreSql172), [new Column(new Name('a'), Builtin::Int4)]);
83        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [$table], true));
84        $fact = (new RelationTargets())->record(new DottedName([new Name('t')]), new QualifiedName(new Name('t')), false, $derivation);
85        (new RelationTargets())->member($fact, new Name('a'), new QualifiedName(new Name('t')), $derivation);
86        self::assertSame([], $derivation->facts()->diagnostics);
87    }
88
89    public function testRenamedReportsAnExistingColumn(): void
90    {
91        $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::PostgreSql172), [new Column(new Name('a'), Builtin::Int4)]);
92        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [$table], true));
93        $fact = (new RelationTargets())->record(new DottedName([new Name('t')]), new QualifiedName(new Name('t')), false, $derivation);
94        (new RelationTargets())->renamed($fact, new Name('a'), $derivation);
95        self::assertSame(['column "a" already exists'], [$derivation->facts()->diagnostics[0]->message()]);
96    }
97
98    public function testHasAnswersNullWithoutADeclaration(): void
99    {
100        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], false));
101        $fact = (new RelationTargets())->record(new DottedName([new Name('t')]), new QualifiedName(new Name('t')), false, $derivation);
102        self::assertNull((new RelationTargets())->has($fact, new Name('a'), $derivation));
103    }
104
105    public function testSpelledJoinsTheParts(): void
106    {
107        self::assertSame('a.b', (new RelationTargets())->spelled([new Name('a'), new Name('b')]));
108    }
109}
110