packages/sql-semantics-postgres/tests/Unit/Lowering/Invocation/JsonRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Invocation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Invocation\JsonRule;
12use SqlSemantics\Platform\PostgreSql\Lowering\Invocation\SyntaxRule;
13use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
15use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
16use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\AnalysisException;
17use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
18use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
19use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
20use SqlSemantics\Platform\PostgreSql\Platform;
21use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonEncoding;
23use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\KeyValueSpelling;
24
25#[CoversClass(JsonRule::class)]
26#[Small]
27final class JsonRuleTest extends TestCase
28{
29 public function testCreationLowersEveryConstructor(): 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('SELECT JSON_OBJECT(\'a\' : 1 NULL ON NULL WITH UNIQUE RETURNING json), JSON_OBJECT(), JSON_OBJECT(\'{a,b}\'), JSON_ARRAY(1 ABSENT ON NULL), JSON_ARRAY(), JSON(1), JSON_SCALAR(1), JSON_SERIALIZE(1)');
33 $node = array_map(static fn ($target) => (new SyntaxRule($lowering))->subexpression($target->find('func_expr_common_subexpr')[0]), $tree->find('target_list')[0]->find('target_el'));
34 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
35 $out->list($node);
36 self::assertSame('JSON_OBJECT(\'a\' : 1 NULL ON NULL WITH UNIQUE KEYS RETURNING JSON), JSON_OBJECT(), JSON_OBJECT(\'{a,b}\'), JSON_ARRAY(1 ABSENT ON NULL), JSON_ARRAY(), JSON(1), JSON_SCALAR(1), JSON_SERIALIZE(1)', (new Lexical())->join($out->pieces()));
37 }
38
39 public function testCreationLowersThePostgreSql16Forms(): void
40 {
41 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql166)), new Leaves(), GrammarRelease::PostgreSql166);
42 $tree = (new PostgreSqlParser('pg-16.6'))->parse('SELECT JSON_OBJECT(\'a\' : 1 RETURNING text FORMAT JSON ENCODING utf8)');
43 $node = (new SyntaxRule($lowering))->subexpression($tree->find('func_expr_common_subexpr')[0]);
44 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
45 $out->node($node);
46 self::assertSame('JSON_OBJECT(\'a\' : 1 RETURNING text FORMAT JSON ENCODING utf8)', (new Lexical())->join($out->pieces()));
47 }
48
49 public function testAggregateLowersTheJsonAggregates(): void
50 {
51 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
52 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_OBJECTAGG(\'a\' VALUE 1 ABSENT ON NULL)');
53 $node = (new JsonRule($lowering))->aggregate($tree->find('json_aggregate_func')[0], null, null);
54 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
55 $out->node($node);
56 self::assertSame('JSON_OBJECTAGG(\'a\' VALUE 1 ABSENT ON NULL)', (new Lexical())->join($out->pieces()));
57 }
58
59 public function testOrderLowersNoOrdering(): void
60 {
61 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
62 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_ARRAYAGG(1)');
63 $node = (new JsonRule($lowering))->order($tree->find('json_array_aggregate_order_by_clause_opt')[0]);
64 self::assertSame([], $node);
65 }
66
67 public function testValueLowersTheFormat(): void
68 {
69 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
70 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON(\'{}\' FORMAT JSON ENCODING UTF8)');
71 $node = (new JsonRule($lowering))->value($tree->find('json_value_expr')[0]);
72 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
73 $out->node($node);
74 self::assertSame('\'{}\' FORMAT JSON ENCODING utf8', (new Lexical())->join($out->pieces()));
75 }
76
77 public function testValuesLowersTheList(): void
78 {
79 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
80 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_ARRAY(1, 2, 3)');
81 $node = (new JsonRule($lowering))->values($tree->find('json_value_expr_list')[0]);
82 self::assertCount(3, $node);
83 }
84
85 public function testPairsLowersTheList(): void
86 {
87 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
88 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_OBJECT(\'a\' : 1, \'b\' : 2)');
89 $node = (new JsonRule($lowering))->pairs($tree->find('json_name_and_value_list')[0]);
90 self::assertCount(2, $node);
91 }
92
93 public function testKeyValueLowersTheColonSpelling(): void
94 {
95 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
96 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_OBJECT(\'a\' : 1)');
97 $node = (new JsonRule($lowering))->keyValue($tree->find('json_name_and_value')[0]);
98 self::assertSame(KeyValueSpelling::Colon, $node->spelling);
99 }
100
101 public function testFormatLowersTheEncoding(): void
102 {
103 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
104 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON(\'{}\' FORMAT JSON ENCODING utf16)');
105 $node = (new JsonRule($lowering))->format($tree->find('json_format_clause_opt')[0]);
106 self::assertSame(JsonEncoding::Utf16, $node?->encoding());
107 }
108
109 public function testEncodingClauseLowersThePostgreSql16Encoding(): void
110 {
111 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql166)), new Leaves(), GrammarRelease::PostgreSql166);
112 $tree = (new PostgreSqlParser('pg-16.6'))->parse('SELECT JSON_ARRAY(1 FORMAT JSON ENCODING utf32)');
113 $node = (new JsonRule($lowering))->encodingClause($tree->find('json_encoding_clause_opt')[0]);
114 self::assertSame('utf32', $node?->value);
115 }
116
117 public function testEncodingRejectsAnUnknownEncoding(): void
118 {
119 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
120 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON(\'{}\' FORMAT JSON ENCODING latin1)');
121 $this->expectException(AnalysisException::class);
122 $this->expectExceptionMessage('unrecognized JSON encoding: latin1');
123 (new JsonRule($lowering))->format($tree->find('json_format_clause_opt')[0]);
124 }
125
126 public function testReturningLowersNoClauseToNull(): void
127 {
128 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
129 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_SERIALIZE(1)');
130 $node = (new JsonRule($lowering))->returning($tree->find('json_returning_clause_opt')[0]);
131 self::assertNull($node);
132 }
133
134 public function testNullsLowersNoClauseToNull(): void
135 {
136 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
137 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_ARRAY(1)');
138 $node = (new JsonRule($lowering))->nulls($tree->find('json_array_constructor_null_clause_opt')[0]);
139 self::assertNull($node);
140 }
141
142 public function testUniquenessLowersNoClauseToNull(): void
143 {
144 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
145 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_OBJECT(\'a\' : 1)');
146 $node = (new JsonRule($lowering))->uniqueness($tree->find('json_key_uniqueness_constraint_opt')[0]);
147 self::assertNull($node);
148 }
149}
150