packages/sql-semantics-postgres/tests/Unit/Rules/Invocation/JsonChecksTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Invocation;
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\Statement\Fact\ScalarFact;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Rules\Invocation\JsonChecks;
19use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonFormat;
20use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonReturning;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonValueExpression;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonBehavior;
23use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonBehaviorClause;
24use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonBehaviorKind;
25use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonFunctionKind;
26use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonQueryFunction;
27use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonQuoting;
28use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonWrapperKind;
29use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonWrapping;
30use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\JsonProblem;
31use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\JsonProblemKind;
32use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
33use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
34use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
35use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
36use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
37use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
38use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
39
40#[CoversClass(JsonChecks::class)]
41#[Small]
42final class JsonChecksTest extends TestCase
43{
44 public function testInputReportsAnEncodingOnANonByteaValue(): void
45 {
46 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
47 (new JsonChecks())->input($derivation, new JsonValueExpression(new Constant(new IntegerConstant('1')), new JsonFormat(new Name('utf8'))), new ScalarFact(new Known(Builtin::Int4), Nullability::NotNull));
48 self::assertEquals([new JsonProblem(JsonProblemKind::EncodingWithoutBytea)], $derivation->facts()->diagnostics);
49 }
50
51 public function testQueryReportsFormatInReturningAndQuotesWithWrapper(): void
52 {
53 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
54 (new JsonChecks())->query($derivation, new JsonQueryFunction(JsonFunctionKind::Query, new JsonValueExpression(new Constant(new StringConstant('{}'))), new Constant(new StringConstant('$')), [], null, new JsonWrapping(JsonWrapperKind::Unconditional), new JsonQuoting(false)));
55 (new JsonChecks())->query($derivation, new JsonQueryFunction(JsonFunctionKind::Value, new JsonValueExpression(new Constant(new StringConstant('{}'))), new Constant(new StringConstant('$')), [], new JsonReturning(new TypeName(new NamedDesignation(new DottedName([new Name('text')]))), new JsonFormat())));
56 self::assertEquals([new JsonProblem(JsonProblemKind::QuotesWithWrapper), new JsonProblem(JsonProblemKind::FormatInReturning, ['json_value'])], $derivation->facts()->diagnostics);
57 }
58
59 public function testBehaviorsNamesTheColumn(): void
60 {
61 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
62 (new JsonChecks())->behaviors($derivation, JsonFunctionKind::Value, new JsonBehaviorClause(new JsonBehavior(JsonBehaviorKind::True), null), new Name('c'));
63 self::assertSame('invalid ON EMPTY behavior for column "c"', $derivation->facts()->diagnostics[0]->message());
64 }
65
66 public function testAcceptedFollowsTheFunction(): void
67 {
68 $checks = new JsonChecks();
69 self::assertSame([true, false, true], [$checks->accepted(JsonFunctionKind::Query, JsonBehaviorKind::EmptyObject), $checks->accepted(JsonFunctionKind::Value, JsonBehaviorKind::Empty), $checks->accepted(JsonFunctionKind::Exists, JsonBehaviorKind::Unknown)]);
70 }
71}
72