packages/sql-semantics-postgres/tests/Unit/Rules/Resolution/FunctionShapesTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Resolution;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Resolution\FunctionShapes::class)]
12#[Medium]
13final class FunctionShapesTest extends TestCase
14{
15 public function testDeriveIsOpenForAnUndeclaredFunction(): void
16 {
17 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18 $query = $semantics->inspect('SELECT * FROM f()');
19 self::assertNull($query->fields());
20 }
21
22 public function testFunctionNameIsTheCalledName(): void
23 {
24 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
25 $query = $semantics->inspect('SELECT * FROM f() AS (a integer)');
26 $select = $query->statement;
27 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Select::class, $select);
28 $from = $select->from;
29 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Relation\FunctionTable::class, $from);
30 self::assertSame('f', (new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\FunctionShapes())->functionName($from->functions[0])?->value);
31 }
32
33 public function testDeriveReportsDefinitionsAfterSeveralFunctions(): void
34 {
35 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
36 $query = $semantics->inspect('SELECT * FROM ROWS FROM (f(), g()) AS s (a int)');
37 self::assertSame('ROWS FROM() with multiple functions cannot have a column definition list', $query->facts->diagnostics[0]->message());
38 }
39
40 public function testDeriveDerivesRepeatedDefinitions(): void
41 {
42 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
43 $query = $semantics->inspect('SELECT * FROM ROWS FROM (f() AS (x int)) AS s (a b.c ((1 IS FALSE)))');
44 self::assertSame('multiple column definition lists are not allowed for the same function', $query->facts->diagnostics[0]->message());
45 }
46
47 public function testDefinedHasTheDefinedTypes(): void
48 {
49 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
50 $query = $semantics->inspect('SELECT * FROM f() AS (a boolean)');
51 self::assertSame('boolean', ($query->field(0)->type instanceof \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known ? $query->field(0)->type->descriptor->name() : null));
52 }
53
54 public function testPaddedMakesSlotsNullable(): void
55 {
56 self::assertSame(\SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::Nullable, (new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\FunctionShapes())->padded([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)])[0]->nullability);
57 }
58
59 public function testDistinctRemovesRepeatedInputs(): void
60 {
61 $missing = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRoutine(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('f')));
62 self::assertCount(1, (new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\FunctionShapes())->distinct([$missing, $missing]));
63 }
64}
65