packages/sql-semantics-postgres/tests/Unit/Rules/Expression/ComparisonTypingTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRoutine;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\NullOnly;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Rules\Expression\ComparisonTyping;
20use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
21use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\ArrayOf;
22use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
23use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\IntervalSpan;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Parameterized;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\IntervalFields;
26
27#[CoversClass(ComparisonTyping::class)]
28#[Small]
29final class ComparisonTypingTest extends TestCase
30{
31 public function testResultIsBooleanForAnExactCatalogOperator(): void
32 {
33 $equal = new OperatorName(new Name('='));
34 self::assertEquals(new Known(Builtin::Bool), (new ComparisonTyping())->result((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true), $equal, new Known(Builtin::Int4), new Known(Builtin::Int8)));
35 self::assertEquals(new Known(Builtin::Bool), (new ComparisonTyping())->result((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true), $equal, new Known(Builtin::Unknown), new Known(new Parameterized(Builtin::Numeric, 10))));
36 self::assertEquals(new Known(Builtin::Bool), (new ComparisonTyping())->result((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true), $equal, new Known(Builtin::Unknown), new NullOnly()));
37 }
38
39 public function testResultDependsOnTheOperatorWhenTheCatalogHasNoExactOne(): void
40 {
41 $equal = new OperatorName(new Name('='));
42 $fact = (new ComparisonTyping())->result((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true), $equal, new Known(Builtin::Varchar), new Known(Builtin::Varchar));
43 self::assertInstanceOf(Dependent::class, $fact);
44 self::assertInstanceOf(UndeclaredRoutine::class, $fact->missing[0]);
45 self::assertInstanceOf(Dependent::class, (new ComparisonTyping())->result((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true), $equal, new Known(Builtin::Json), new Known(Builtin::Json)));
46 }
47
48 public function testResultPassesOnAnOperandThatIsNotKnown(): void
49 {
50 $operand = new Dependent([new UndeclaredRoutine(new QualifiedName(new Name('f')))]);
51 self::assertSame($operand, (new ComparisonTyping())->result((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true), new OperatorName(new Name('<')), new Known(Builtin::Int4), $operand));
52 }
53
54 public function testBaseReadsTheCatalogTypeOfAnOperand(): void
55 {
56 self::assertSame(Builtin::Unknown, (new ComparisonTyping())->base(new NullOnly()));
57 self::assertSame(Builtin::Interval, (new ComparisonTyping())->base(new Known(new IntervalSpan(IntervalFields::Day))));
58 self::assertNull((new ComparisonTyping())->base(new Known(new ArrayOf(Builtin::Int4))));
59 }
60
61 public function testCatalogRequiresTheCatalogToBeSearchedFirst(): void
62 {
63 $less = new OperatorName(new Name('<'));
64 self::assertTrue((new ComparisonTyping())->catalog((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], false), $less));
65 $shadowed = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), ['app', 'pg_catalog'], [], true);
66 self::assertFalse((new ComparisonTyping())->catalog($shadowed, $less));
67 self::assertTrue((new ComparisonTyping())->catalog($shadowed, new OperatorName(new Name('<'), [new Name('pg_catalog')], true)));
68 self::assertFalse((new ComparisonTyping())->catalog((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], false), new OperatorName(new Name('@>'))));
69 }
70
71 public function testExactListsThePairsTheCatalogCompares(): void
72 {
73 self::assertTrue((new ComparisonTyping())->exact(Builtin::Text, Builtin::Text));
74 self::assertTrue((new ComparisonTyping())->exact(Builtin::Date, Builtin::Timestamptz));
75 self::assertFalse((new ComparisonTyping())->exact(Builtin::Int4, Builtin::Numeric));
76 self::assertFalse((new ComparisonTyping())->exact(Builtin::Xml, Builtin::Xml));
77 }
78}
79