packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Syntax/ValueFunctionTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Invocation\Syntax;
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\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
20use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Syntax\ValueFunction;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Syntax\ValueFunctionKind;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
23use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Parameterized;
25
26#[CoversClass(ValueFunction::class)]
27#[Small]
28final class ValueFunctionTest extends TestCase
29{
30 public function testRejectsAPrecisionOnANameFunction(): void
31 {
32 $this->expectExceptionMessage('Only the time functions take a precision.');
33 new ValueFunction(ValueFunctionKind::User, new IntegerConstant('1'));
34 }
35
36 public function testOutputNameIsTheKeywordInLowerCase(): void
37 {
38 self::assertSame('current_catalog', (new ValueFunction(ValueFunctionKind::CurrentCatalog))->outputName()->value);
39 }
40
41 public function testDeriveScalarReducesThePrecisionToSix(): void
42 {
43 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
44 $fact = $derivation->scalar(new ValueFunction(ValueFunctionKind::CurrentTimestamp, new IntegerConstant('9')), $derivation->environment());
45 self::assertEquals(new ScalarFact(new Known(new Parameterized(Builtin::Timestamptz, 6)), Nullability::NotNull), $fact);
46 }
47
48 public function testDeriveScalarMakesCurrentSchemaNullable(): void
49 {
50 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
51 $fact = $derivation->scalar(new ValueFunction(ValueFunctionKind::CurrentSchema), $derivation->environment());
52 self::assertEquals(new ScalarFact(new Known(Builtin::Name), Nullability::Nullable), $fact);
53 }
54
55 public function testRenderWritesTheKeywordAndThePrecision(): void
56 {
57 $value = new ValueFunction(ValueFunctionKind::Localtimestamp, new IntegerConstant('2'));
58 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
59 $value->render($out);
60 self::assertSame('LOCALTIMESTAMP(2)', (new Lexical())->join($out->pieces()));
61 }
62}
63