packages/sql-semantics-postgres/tests/Unit/Statement/Object/DefineTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Object;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\PostgreSql\Semantics;
12use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
13use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
14use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
15use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Attribute;
16use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\BooleanArgument;
17use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\AggregateAttribute;
18use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\OperatorAttribute;
19use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\KnownAttribute;
20use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\NameArgument;
21use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\TypeArgument;
22use SqlSemantics\Platform\PostgreSql\Statement\Object\Define;
23use SqlSemantics\Platform\PostgreSql\Statement\Object\Problem\AttributeProblem;
24use SqlSemantics\Platform\PostgreSql\Statement\Object\Problem\AttributeProblemKind;
25use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\AggregateArguments;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
27use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
28
29#[CoversClass(Define::class)]
30#[Medium]
31final class DefineTest extends TestCase
32{
33    public function testDeriveStatementReportsMissingAttributes(): void
34    {
35        $semantics = new Semantics();
36        $aggregate = $semantics->inspect('CREATE AGGREGATE a(int4) (initcond = 0)');
37        $operator = $semantics->inspect('CREATE OPERATOR === (leftarg = int4)');
38        self::assertEquals([new AttributeProblem(AttributeProblemKind::MissingAggregateAttribute, ['stype']), new AttributeProblem(AttributeProblemKind::MissingAggregateAttribute, ['sfunc'])], $aggregate->facts->diagnostics);
39        self::assertEquals([new AttributeProblem(AttributeProblemKind::MissingOperatorFunction), new AttributeProblem(AttributeProblemKind::MissingRightArgument)], $operator->facts->diagnostics);
40    }
41
42    public function testRenderWritesEachForm(): void
43    {
44        $semantics = new Semantics();
45        self::assertSame(
46            ['CREATE OR REPLACE AGGREGATE a (basetype = int4, sfunc = f, stype = int4)', 'CREATE TYPE t', 'CREATE COLLATION IF NOT EXISTS c (locale = \'C\')', 'CREATE TEXT SEARCH DICTIONARY d (template = simple)'],
47            [$semantics->inspect('CREATE OR REPLACE AGGREGATE a (basetype = int4, sfunc = f, stype = int4)')->toString(), $semantics->inspect('CREATE TYPE t')->toString(), $semantics->inspect("CREATE COLLATION IF NOT EXISTS c (locale = 'C')")->toString(), $semantics->inspect('CREATE TEXT SEARCH DICTIONARY d (template = simple)')->toString()],
48        );
49    }
50
51    public function testRejectsAShellOfAnotherKind(): void
52    {
53        $this->expectExceptionMessage('Only a shell type is defined without attributes.');
54        new Define(ObjectKind::Collation, new DottedName([new Name('c')]), null);
55    }
56
57    public function testRejectsAnOperatorNamedByADottedName(): void
58    {
59        $this->expectExceptionMessage('An operator, and only an operator, is named by an operator name written without OPERATOR(...).');
60        new Define(ObjectKind::Operator, new DottedName([new Name('o')]), [new Attribute(new Name('colour'), null)]);
61    }
62
63    public function testRejectsAnOldStyleAttributeWithoutValue(): void
64    {
65        $this->expectExceptionMessage('Every attribute of an old-style aggregate has a value.');
66        new Define(ObjectKind::Aggregate, new DottedName([new Name('a')]), [new Attribute(new Name('hypothetical'), AggregateAttribute::Hypothetical, new BooleanArgument())]);
67    }
68
69    public function testRejectsArgumentsOfAnotherKind(): void
70    {
71        $this->expectExceptionMessage('Only an aggregate has an argument list.');
72        new Define(ObjectKind::Type, new DottedName([new Name('t')]), null, new AggregateArguments([]));
73    }
74
75    public function testRejectsReplaceOfAnotherKind(): void
76    {
77        $this->expectExceptionMessage('Only an aggregate is defined with OR REPLACE.');
78        new Define(ObjectKind::Operator, new OperatorName(new Name('+')), [new Attribute(new Name('colour'), null)], null, true);
79    }
80
81    public function testRejectsIfNotExistsOfAnotherKind(): void
82    {
83        $this->expectExceptionMessage('Only a collation is defined with IF NOT EXISTS.');
84        new Define(ObjectKind::Type, new DottedName([new Name('t')]), null, null, false, true);
85    }
86
87    public function testRejectsAnotherKind(): void
88    {
89        $this->expectExceptionMessage('CREATE with a definition defines an aggregate, an operator, a type, a text search object or a collation.');
90        new Define(ObjectKind::Schema, new DottedName([new Name('s')]), []);
91    }
92
93    public function testRejectsAnAttributeOfAnotherCommand(): void
94    {
95        $this->expectExceptionMessage('A definition attribute is recognized exactly when its command knows its name.');
96        new Define(ObjectKind::Collation, new DottedName([new Name('c')]), [new Attribute(new Name('hashes'), OperatorAttribute::Hashes, new BooleanArgument())]);
97    }
98
99    public function testRejectsAnUnrecognizedAttributeTheCommandKnows(): void
100    {
101        $this->expectExceptionMessage('A definition attribute is recognized exactly when its command knows its name.');
102        new Define(ObjectKind::Operator, new OperatorName(new Name('+')), [new Attribute(new Name('hashes'), null)]);
103    }
104
105    public function testDeriveStatementReadsTheFunctionOfAnOperatorAsARoutine(): void
106    {
107        $define = (new Semantics())->inspect('CREATE OPERATOR === (leftarg = int4, rightarg = int4, function = int4eq)')->statement;
108        self::assertInstanceOf(Define::class, $define);
109        $definition = $define->definition ?? [];
110        self::assertSame([OperatorAttribute::Leftarg, OperatorAttribute::Rightarg, OperatorAttribute::Function], array_map(static fn (Attribute $attribute): ?KnownAttribute => $attribute->known, $definition));
111        self::assertEquals([new TypeArgument(new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))), new NameArgument(ObjectKind::Function, new DottedName([new Name('int4eq')]))], [$definition[0]->value, $definition[2]->value]);
112    }
113
114    public function testRenderKeepsAnOldStyleAttributeSpelledLikeAKeywordQuoted(): void
115    {
116        self::assertSame('CREATE AGGREGATE a ("type" = x, sfunc = f, "select" = int4)', (new Semantics())->inspect('CREATE AGGREGATE a ("type" = x, sfunc = f, "select" = int4)')->toString());
117    }
118
119    public function testRenderWritesANewStyleAttributeAsALabel(): void
120    {
121        self::assertSame('CREATE AGGREGATE a (int4) (type = x, sfunc = f)', (new Semantics())->inspect('CREATE AGGREGATE a (int4) ("type" = x, sfunc = f)')->toString());
122    }
123}
124