packages/sql-semantics-postgres/tests/Unit/Lowering/Invocation/JsonQueryRuleTest.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\JsonQueryRule;
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\Lowering\Leaves;
17use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
18use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
19use SqlSemantics\Platform\PostgreSql\Platform;
20use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
21
22#[CoversClass(JsonQueryRule::class)]
23#[Small]
24final class JsonQueryRuleTest extends TestCase
25{
26 public function testFunctionLowersTheThreeQueryFunctions(): void
27 {
28 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
29 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_QUERY(\'{}\', \'$\' RETURNING text WITH CONDITIONAL WRAPPER KEEP QUOTES EMPTY ON EMPTY), JSON_EXISTS(\'{}\', \'$\' UNKNOWN ON ERROR), JSON_VALUE(\'{}\', \'$\' NULL ON ERROR)');
30 $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'));
31 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
32 $out->list($node);
33 self::assertSame('JSON_QUERY(\'{}\', \'$\' RETURNING text WITH CONDITIONAL WRAPPER KEEP QUOTES EMPTY ON EMPTY), JSON_EXISTS(\'{}\', \'$\' UNKNOWN ON ERROR), JSON_VALUE(\'{}\', \'$\' NULL ON ERROR)', (new Lexical())->join($out->pieces()));
34 }
35
36 public function testPassingLowersNoClauseToNoArgument(): void
37 {
38 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
39 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_EXISTS(\'{}\', \'$\')');
40 $node = (new JsonQueryRule($lowering))->passing($tree->find('json_passing_clause_opt')[0]);
41 self::assertSame([], $node);
42 }
43
44 public function testBehaviorsLowersOnEmptyAlone(): void
45 {
46 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
47 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_VALUE(\'{}\', \'$\' ERROR ON EMPTY)');
48 $node = (new JsonQueryRule($lowering))->behaviors($tree->find('json_behavior_clause_opt')[0]);
49 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
50 $out->node($node);
51 self::assertSame('ERROR ON EMPTY', (new Lexical())->join($out->pieces()));
52 }
53
54 public function testBehaviorLowersEveryFixedBehavior(): void
55 {
56 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
57 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_QUERY(\'{}\', \'$\' EMPTY ARRAY ON EMPTY EMPTY OBJECT ON ERROR)');
58 $node = (new JsonQueryRule($lowering))->behavior($tree->find('json_behavior')[1]);
59 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
60 $out->node($node);
61 self::assertSame('EMPTY OBJECT', (new Lexical())->join($out->pieces()));
62 }
63
64 public function testWrapperLowersNoClauseToNull(): void
65 {
66 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
67 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_QUERY(\'{}\', \'$\')');
68 $node = (new JsonQueryRule($lowering))->wrapper($tree->find('json_wrapper_behavior')[0]);
69 self::assertNull($node);
70 }
71
72 public function testQuotesLowersNoClauseToNull(): void
73 {
74 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
75 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT JSON_QUERY(\'{}\', \'$\')');
76 $node = (new JsonQueryRule($lowering))->quotes($tree->find('json_quotes_clause_opt')[0]);
77 self::assertNull($node);
78 }
79}
80