packages/sql-semantics-postgres/tests/Unit/Rules/Routine/AttributeChecksTest.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\AttributeChecks;
14use SqlSemantics\Platform\PostgreSql\Semantics;
15use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
16use SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber;
17use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
18use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Attribute;
19use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Choice\CollationProvider;
20use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\ChoiceArgument;
21use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\CollationAttribute;
22use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\DictionaryAttribute;
23use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\OperatorAttribute;
24use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Reading;
25use SqlSemantics\Platform\PostgreSql\Statement\Object\Define;
26
27#[CoversClass(AttributeChecks::class)]
28#[Medium]
29final class AttributeChecksTest extends TestCase
30{
31 public function testDeriveReportsUnrecognizedUnreadableAndRepeatedAttributes(): void
32 {
33 $semantics = new Semantics();
34 self::assertSame(
35 [['operator attribute "colour" not recognized', 'argument of function must be a name'], ['type attribute "foo" not recognized', 'conflicting or redundant options'], []],
36 [
37 array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('CREATE OPERATOR === (rightarg = int4, colour = red, function = 1)')->facts->diagnostics),
38 array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('CREATE TYPE t (input = i, output = o, foo, analyze = a, analyse = b)')->facts->diagnostics),
39 array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('CREATE TEXT SEARCH DICTIONARY d (template = simple, stopwords = english, accept = false)')->facts->diagnostics),
40 ],
41 );
42 }
43
44 public function testUnrecognizedAnswersNothingForATemplateOption(): void
45 {
46 $checks = new AttributeChecks();
47 self::assertSame([null, 'collation attribute "x" not recognized'], [$checks->unrecognized(DictionaryAttribute::class, new Attribute(new Name('x'), null)), $checks->unrecognized(CollationAttribute::class, new Attribute(new Name('x'), null))?->message()]);
48 }
49
50 public function testValueReportsWhatTheCommandChecksOnAReadValue(): void
51 {
52 $checks = new AttributeChecks();
53 $builtin = new Attribute(new Name('provider'), CollationAttribute::Provider, new ChoiceArgument(new StringConstant('Builtin'), CollationProvider::Builtin));
54 self::assertSame(['unrecognized collation provider: Builtin', null], [$checks->value($builtin, CollationAttribute::Provider, GrammarRelease::PostgreSql166)?->message(), $checks->value($builtin, CollationAttribute::Provider, GrammarRelease::PostgreSql172)?->message()]);
55 }
56
57 public function testValueReportsSetofInAnOperatorArgument(): void
58 {
59 self::assertSame('SETOF type not allowed for operator argument', (new Semantics())->inspect('CREATE OPERATOR === (leftarg = SETOF int4, rightarg = int4, function = f)')->facts->diagnostics[0]->message());
60 }
61
62 public function testRejectedAnswersTheMessageOfEachReading(): void
63 {
64 $checks = new AttributeChecks();
65 $number = new SignedNumber(false, new IntegerConstant('5'));
66 $word = new StringConstant('odd');
67 self::assertSame(
68 ['x requires a parameter', 'x requires an integer value', 'argument of x must be a type name', 'x requires a Boolean value', 'x requires an integer value', 'invalid argument for x: "odd"', 'parameter "parallel" must be SAFE, RESTRICTED, or UNSAFE', 'parameter "x" must be READ_ONLY, SHAREABLE, or READ_WRITE', 'alignment "odd" not recognized', 'storage "odd" not recognized', 'unrecognized collation provider: odd', null],
69 [
70 $checks->rejected(Reading::Function, 'x', null)?->message(),
71 $checks->rejected(Reading::Integer, 'x', null)?->message(),
72 $checks->rejected(Reading::Type, 'x', $number)?->message(),
73 $checks->rejected(Reading::Boolean, 'x', $word)?->message(),
74 $checks->rejected(Reading::Length, 'x', $number)?->message(),
75 $checks->rejected(Reading::Length, 'x', $word)?->message(),
76 $checks->rejected(Reading::Parallelism, 'x', $word)?->message(),
77 $checks->rejected(Reading::FinalModification, 'x', $word)?->message(),
78 $checks->rejected(Reading::Alignment, 'x', $word)?->message(),
79 $checks->rejected(Reading::Storage, 'x', $word)?->message(),
80 $checks->rejected(Reading::Provider, 'x', $word)?->message(),
81 $checks->rejected(Reading::Ignored, 'x', null)?->message(),
82 ],
83 );
84 }
85
86 public function testPrintableAcceptsOnlyPrintableAscii(): void
87 {
88 $checks = new AttributeChecks();
89 self::assertSame([true, false, false, false], [$checks->printable('U'), $checks->printable(''), $checks->printable("\t"), $checks->printable("\u{e9}")]);
90 }
91
92 public function testDeriveIgnoresTheValueOfAnObsoleteOperatorAttribute(): void
93 {
94 self::assertSame([], (new Semantics())->inspect('CREATE OPERATOR === (rightarg = int4, function = f, sort1 = 1, ltcmp)')->facts->diagnostics);
95 $define = (new Semantics())->inspect('CREATE OPERATOR === (rightarg = int4, function = f, sort1 = 1)')->statement;
96 self::assertInstanceOf(Define::class, $define);
97 self::assertSame(OperatorAttribute::Sort1, ($define->definition ?? [])[2]->known);
98 }
99}
100