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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\PostgreSql\Rules\Routine\DefineChecks;
14use SqlSemantics\Platform\PostgreSql\Semantics;
15use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
16use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Attribute;
17use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\BooleanArgument;
18use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Choice\CollationProvider;
19use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\OperatorAttribute;
20use SqlSemantics\Platform\PostgreSql\Statement\Object\Problem\AttributeProblem;
21use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\AggregateArguments;
22
23#[CoversClass(DefineChecks::class)]
24#[Medium]
25final class DefineChecksTest extends TestCase
26{
27    public function testDeriveAcceptsTheAlternativeSpellings(): void
28    {
29        $semantics = new Semantics();
30        self::assertSame([[], []], [$semantics->inspect('CREATE AGGREGATE a (basetype = int4, SFUNC1 = f, stype1 = int4)')->facts->diagnostics, $semantics->inspect('CREATE OPERATOR + (rightarg = int4, procedure = f)')->facts->diagnostics]);
31    }
32
33    public function testDeriveReportsTheProblemsOfEachKind(): void
34    {
35        $semantics = new Semantics();
36        self::assertSame(
37            [
38                ['aggregate stype must be specified', 'aggregate sfunc must be specified'],
39                ['type input function must be specified', 'type modifier output function is useless without a type modifier input function'],
40                ['parameter "lc_collate" must be specified', 'parameter "lc_ctype" must be specified', 'ICU rules cannot be specified unless locale provider is ICU'],
41                ['text search parser start method is required', 'text search parser lextypes method is required'],
42                ['text search template lexize method is required'],
43                ['text search template is required'],
44                [],
45            ],
46            [
47                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('CREATE AGGREGATE a(int4) (initcond = 0)')->facts->diagnostics),
48                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('CREATE TYPE t (output = o, typmod_out = m)')->facts->diagnostics),
49                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect("CREATE COLLATION c (rules = '')")->facts->diagnostics),
50                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('CREATE TEXT SEARCH PARSER p (gettoken = g, "end" = e)')->facts->diagnostics),
51                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('CREATE TEXT SEARCH TEMPLATE t (init = i)')->facts->diagnostics),
52                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('CREATE TEXT SEARCH DICTIONARY d (stopwords = english)')->facts->diagnostics),
53                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('CREATE TYPE t')->facts->diagnostics),
54            ],
55        );
56    }
57
58    public function testRangeReportsTheMissingSubtype(): void
59    {
60        self::assertSame(['type attribute "subtype" is required'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect('CREATE TYPE r AS RANGE (subtype_diff = float8mi)')->facts->diagnostics));
61    }
62
63    public function testPresentKeepsTheLastRecognizedAttributeOfEachName(): void
64    {
65        $last = new Attribute(new Name('hashes'), OperatorAttribute::Hashes, new BooleanArgument());
66        self::assertSame(['hashes' => $last], (new DefineChecks())->present([new Attribute(new Name('hashes'), OperatorAttribute::Hashes, new BooleanArgument()), new Attribute(new Name('x'), null), $last]));
67    }
68
69    public function testOperatorTellsAMissingRightArgumentFromMissingArguments(): void
70    {
71        $checks = new DefineChecks();
72        self::assertSame(
73            [['operator function must be specified', 'operator argument types must be specified'], ['operator right argument type must be specified']],
74            [array_map(static fn (AttributeProblem $problem): string => $problem->message(), $checks->operator([])), array_map(static fn (AttributeProblem $problem): string => $problem->message(), $checks->operator(['function' => new Attribute(new Name('hashes'), OperatorAttribute::Hashes, new BooleanArgument()), 'leftarg' => new Attribute(new Name('hashes'), OperatorAttribute::Hashes, new BooleanArgument())]))],
75        );
76    }
77
78    public function testAggregateReportsMovingSerializationHypotheticalAndBaseType(): void
79    {
80        $semantics = new Semantics();
81        self::assertSame(
82            [
83                ['aggregate msfunc must not be specified without mstype', 'aggregate msspace must not be specified without mstype', 'only ordered-set aggregates can be hypothetical', 'basetype is redundant with aggregate input type specification', 'must specify both or neither of serialization and deserialization functions'],
84                ['aggregate minvfunc must be specified when mstype is specified', 'aggregate input type must be specified'],
85                [],
86            ],
87            [
88                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('CREATE AGGREGATE a(int4) (sfunc = f, stype = int4, msfunc = m, msspace = 4, hypothetical, basetype = int4, serialfunc = s)')->facts->diagnostics),
89                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('CREATE AGGREGATE a (sfunc = f, stype = int4, mstype = int4, msfunc = m)')->facts->diagnostics),
90                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('CREATE AGGREGATE a(int4 ORDER BY int4) (sfunc = f, stype = int4, hypothetical, msspace = 0)')->facts->diagnostics),
91            ],
92        );
93        self::assertSame([], (new DefineChecks())->aggregate(['stype' => new Attribute(new Name('hashes'), OperatorAttribute::Hashes, new BooleanArgument()), 'sfunc' => new Attribute(new Name('hashes'), OperatorAttribute::Hashes, new BooleanArgument())], new AggregateArguments([])));
94    }
95
96    public function testBaseTypeReportsTheMissingOutputFunction(): void
97    {
98        self::assertSame(['type output function must be specified'], array_map(static fn (AttributeProblem $problem): string => $problem->message(), (new DefineChecks())->baseType(['input' => new Attribute(new Name('hashes'), OperatorAttribute::Hashes, new BooleanArgument())])));
99    }
100
101    public function testCollationFollowsTheProvider(): void
102    {
103        $semantics = new Semantics();
104        self::assertSame(
105            [['conflicting or redundant options'], ['conflicting or redundant options'], ['nondeterministic collations not supported with this provider'], ['parameter "locale" must be specified'], []],
106            [
107                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect("CREATE COLLATION c (locale = 'C', lc_ctype = 'C')")->facts->diagnostics),
108                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect("CREATE COLLATION c (from = \"C\", locale = 'C')")->facts->diagnostics),
109                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect("CREATE COLLATION c (locale = 'C', deterministic = false)")->facts->diagnostics),
110                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('CREATE COLLATION c (provider = builtin)')->facts->diagnostics),
111                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect("CREATE COLLATION c (provider = icu, locale = 'und', deterministic = false, rules = '')")->facts->diagnostics),
112            ],
113        );
114        self::assertSame([], (new DefineChecks())->collation(['provider' => new Attribute(new Name('hashes'), OperatorAttribute::Hashes, new BooleanArgument())], 1, GrammarRelease::PostgreSql166));
115    }
116
117    public function testProvidedReportsWhatOnlyIcuTakes(): void
118    {
119        $attribute = new Attribute(new Name('hashes'), OperatorAttribute::Hashes, new BooleanArgument());
120        self::assertSame(['ICU rules cannot be specified unless locale provider is ICU'], array_map(static fn (AttributeProblem $problem): string => $problem->message(), (new DefineChecks())->provided(CollationProvider::Builtin, ['locale' => $attribute, 'rules' => $attribute])));
121    }
122
123    public function testSearchReportsAConfigurationWithParserAndCopy(): void
124    {
125        $checks = new DefineChecks();
126        $attribute = new Attribute(new Name('hashes'), OperatorAttribute::Hashes, new BooleanArgument());
127        self::assertSame(
128            [['cannot specify both PARSER and COPY options'], ['text search parser is required']],
129            [array_map(static fn (AttributeProblem $problem): string => $problem->message(), $checks->search(ObjectKind::TextSearchConfiguration, ['parser' => $attribute, 'copy' => $attribute])), array_map(static fn (AttributeProblem $problem): string => $problem->message(), $checks->search(ObjectKind::TextSearchConfiguration, []))],
130        );
131    }
132}
133