packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Json/Query/JsonQueryFunctionTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Invocation\Json\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\ScalarFact;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
19use SqlSemantics\Platform\PostgreSql\Platform;
20use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Predicate\JsonItemKind;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Predicate\JsonTest;
23use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonReturning;
24use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonValueExpression;
25use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonArgument;
26use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonBehavior;
27use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonBehaviorClause;
28use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonBehaviorKind;
29use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonFunctionKind;
30use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonQueryFunction;
31use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonQuoting;
32use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonWrapperKind;
33use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonWrapping;
34use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
35use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
36use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
37use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
38use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
39use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
40use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
41
42#[CoversClass(JsonQueryFunction::class)]
43#[Small]
44final class JsonQueryFunctionTest extends TestCase
45{
46    public function testRejectsAWrapperOutsideJsonQuery(): void
47    {
48        $this->expectExceptionMessage('Only JSON_QUERY takes a wrapper or quotes clause.');
49        new JsonQueryFunction(JsonFunctionKind::Value, new JsonValueExpression(new Constant(new StringConstant('{}'))), new Constant(new StringConstant('$.a')), [], null, new JsonWrapping(JsonWrapperKind::Without));
50    }
51
52    public function testRejectsReturningOnJsonExists(): void
53    {
54        $this->expectExceptionMessage('JSON_EXISTS takes no RETURNING and no ON EMPTY.');
55        new JsonQueryFunction(JsonFunctionKind::Exists, new JsonValueExpression(new Constant(new StringConstant('{}'))), new Constant(new StringConstant('$.a')), [], new JsonReturning(new TypeName(new NamedDesignation(new DottedName([new Name('text')])))));
56    }
57
58    public function testOutputNameIsTheFunction(): void
59    {
60        self::assertSame('json_value', (new JsonQueryFunction(JsonFunctionKind::Value, new JsonValueExpression(new Constant(new StringConstant('{}'))), new Constant(new StringConstant('$.a'))))->outputName()->value);
61    }
62
63    public function testDeriveScalarIsTheReturningType(): void
64    {
65        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
66        $fact = $derivation->scalar(new JsonQueryFunction(JsonFunctionKind::Value, new JsonValueExpression(new Constant(new StringConstant('{}'))), new Constant(new StringConstant('$.a')), [], new JsonReturning(new TypeName(new NamedDesignation(new DottedName([new Name('text')]))))), $derivation->environment());
67        self::assertEquals(new ScalarFact(new Known(Builtin::Text), Nullability::Nullable), $fact);
68    }
69
70    public function testDeriveScalarReportsAnInvalidBehavior(): void
71    {
72        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
73        $fact = $derivation->scalar(new JsonQueryFunction(JsonFunctionKind::Exists, new JsonValueExpression(new Constant(new StringConstant('{}'))), new Constant(new StringConstant('$.a')), [], null, null, null, new JsonBehaviorClause(null, new JsonBehavior(JsonBehaviorKind::Null))), $derivation->environment());
74        self::assertEquals(new Known(Builtin::Bool), $fact->type);
75        self::assertSame('invalid ON ERROR behavior', $derivation->facts()->diagnostics[0]->message());
76    }
77
78    public function testRenderWritesEveryClauseInOrder(): void
79    {
80        $query = new JsonQueryFunction(JsonFunctionKind::Query, new JsonValueExpression(new Constant(new StringConstant('{}'))), new Constant(new StringConstant('$.a')), [new JsonArgument(new JsonValueExpression(new Constant(new IntegerConstant('1'))), new Name('x'))], new JsonReturning(new TypeName(new NamedDesignation(new DottedName([new Name('text')])))), new JsonWrapping(JsonWrapperKind::Unconditional), new JsonQuoting(true), new JsonBehaviorClause(null, new JsonBehavior(JsonBehaviorKind::Error)));
81        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
82        $query->render($out);
83        self::assertSame('JSON_QUERY(\'{}\', \'$.a\' PASSING 1 AS x RETURNING text WITH WRAPPER KEEP QUOTES ERROR ON ERROR)', (new Lexical())->join($out->pieces()));
84    }
85
86    public function testRejectsAPathThatWouldTakeTheWrapper(): void
87    {
88        $this->expectExceptionMessage('A path ending in IS JSON without a uniqueness clause would take the wrapper clause; group it.');
89        new JsonQueryFunction(JsonFunctionKind::Query, new JsonValueExpression(new Constant(new StringConstant('{}'))), new JsonTest(new Constant(new StringConstant('$.a')), false, JsonItemKind::Json), [], null, new JsonWrapping(JsonWrapperKind::Without));
90    }
91}
92