packages/sql-semantics-postgres/tests/Unit/Lowering/Routine/DefineRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
12use SqlSemantics\Platform\PostgreSql\Lowering\Routine\DefineRule;
13use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
15use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Statement\Catalog\Type\CreateEnum;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
20use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
21use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\OperatorAttribute;
22use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\RangeAttribute;
23use SqlSemantics\Platform\PostgreSql\Statement\Object\Define;
24
25#[CoversClass(DefineRule::class)]
26#[Small]
27final class DefineRuleTest extends TestCase
28{
29 public function testDefineLowersAggregatesAndOperators(): void
30 {
31 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
32 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE AGGREGATE a(int) (sfunc = f, stype = int); CREATE OPERATOR s.=== (function = f)');
33 $rule = new DefineRule($lowering);
34 $aggregate = $rule->define($tree->find('DefineStmt')[0]);
35 $operator = $rule->define($tree->find('DefineStmt')[1]);
36 self::assertInstanceOf(Define::class, $aggregate);
37 self::assertInstanceOf(Define::class, $operator);
38 self::assertSame([ObjectKind::Aggregate, ObjectKind::Operator], [$aggregate->kind, $operator->kind]);
39 }
40
41 public function testTypeLowersAnEnum(): void
42 {
43 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
44 $tree = (new PostgreSqlParser('pg-17.2'))->parse("CREATE TYPE mood AS ENUM ('sad', 'ok')");
45 self::assertInstanceOf(CreateEnum::class, (new DefineRule($lowering))->type($tree->find('DefineStmt')[0]));
46 }
47
48 public function testOldLowersEveryAttribute(): void
49 {
50 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
51 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE AGGREGATE a (basetype = int, sfunc = f, stype = int)');
52 self::assertCount(3, (new DefineRule($lowering))->old($tree->find('old_aggr_definition')[0]));
53 }
54
55 public function testAttributesReadsTheAttributesOfTheCommand(): void
56 {
57 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
58 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE OPERATOR === (function = f, colour = red)');
59 $attributes = (new DefineRule($lowering))->attributes(ObjectKind::Operator, $tree->find('definition')[0]);
60 self::assertSame([OperatorAttribute::Function, null], [$attributes[0]->known, $attributes[1]->known]);
61 }
62
63 public function testReadBuildsAnAttributeForEachElement(): void
64 {
65 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
66 $attributes = (new DefineRule($lowering))->read(RangeAttribute::class, [[new Name('subtype'), new StringConstant('int4')], [new Name('foo'), null]]);
67 self::assertSame([RangeAttribute::Subtype, null], [$attributes[0]->known, $attributes[1]->known]);
68 }
69}
70