packages/sql-semantics-postgres/tests/Unit/Rules/Table/WritingTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Table;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Table\Writing::class)]
12#[Medium]
13final class WritingTest extends TestCase
14{
15    public function testNamesWritesNamesSeparatedByCommas(): void
16    {
17        $out = new \SqlSemantics\Platform\PostgreSql\Model\Rendering\Output(new \SqlSemantics\Platform\PostgreSql\Rendering\Codec(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172));
18        (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Writing())->names($out, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('B')]);
19        self::assertSame('a , "B"', implode(' ', array_map(static fn ($piece): string => $piece->text, $out->pieces())));
20    }
21
22    public function testParenthesizedWritesNamesBetweenParentheses(): void
23    {
24        $out = new \SqlSemantics\Platform\PostgreSql\Model\Rendering\Output(new \SqlSemantics\Platform\PostgreSql\Rendering\Codec(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172));
25        (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Writing())->parenthesized($out, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a')]);
26        self::assertSame('( a )', implode(' ', array_map(static fn ($piece): string => $piece->text, $out->pieces())));
27    }
28
29    public function testConstraintNameWritesConstraintAndTheName(): void
30    {
31        $out = new \SqlSemantics\Platform\PostgreSql\Model\Rendering\Output(new \SqlSemantics\Platform\PostgreSql\Rendering\Codec(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172));
32        (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Writing())->constraintName($out, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('c'));
33        self::assertSame('CONSTRAINT c', implode(' ', array_map(static fn ($piece): string => $piece->text, $out->pieces())));
34    }
35
36    public function testDefinitionsWritesNothingForNoDefinition(): void
37    {
38        $out = new \SqlSemantics\Platform\PostgreSql\Model\Rendering\Output(new \SqlSemantics\Platform\PostgreSql\Rendering\Codec(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172));
39        (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Writing())->definitions($out, []);
40        self::assertSame('', implode(' ', array_map(static fn ($piece): string => $piece->text, $out->pieces())));
41    }
42
43    public function testSequenceWritesNodesOneAfterAnother(): void
44    {
45        $out = new \SqlSemantics\Platform\PostgreSql\Model\Rendering\Output(new \SqlSemantics\Platform\PostgreSql\Rendering\Codec(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172));
46        (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Writing())->sequence($out, [\SqlSemantics\Platform\PostgreSql\Statement\Constraint\ConstraintAttribute::Deferrable, \SqlSemantics\Platform\PostgreSql\Statement\Constraint\ConstraintAttribute::NotValid]);
47        self::assertSame('DEFERRABLE NOT VALID', implode(' ', array_map(static fn ($piece): string => $piece->text, $out->pieces())));
48    }
49
50    public function testNullTreatmentWritesNullsNotDistinct(): void
51    {
52        $out = new \SqlSemantics\Platform\PostgreSql\Model\Rendering\Output(new \SqlSemantics\Platform\PostgreSql\Rendering\Codec(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172));
53        (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Writing())->nullTreatment($out, false);
54        self::assertSame('NULLS NOT DISTINCT', implode(' ', array_map(static fn ($piece): string => $piece->text, $out->pieces())));
55    }
56
57    public function testIndexParametersWritesTheTablespace(): void
58    {
59        $out = new \SqlSemantics\Platform\PostgreSql\Model\Rendering\Output(new \SqlSemantics\Platform\PostgreSql\Rendering\Codec(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172));
60        (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Writing())->indexParameters($out, [], new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('s'));
61        self::assertSame('USING INDEX TABLESPACE s', implode(' ', array_map(static fn ($piece): string => $piece->text, $out->pieces())));
62    }
63
64    public function testWithDataWritesWithNoData(): void
65    {
66        $out = new \SqlSemantics\Platform\PostgreSql\Model\Rendering\Output(new \SqlSemantics\Platform\PostgreSql\Rendering\Codec(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172));
67        (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Writing())->withData($out, false);
68        self::assertSame('WITH NO DATA', implode(' ', array_map(static fn ($piece): string => $piece->text, $out->pieces())));
69    }
70
71    public function testSeparatedWritesTheKeywordBetweenNodes(): void
72    {
73        $out = new \SqlSemantics\Platform\PostgreSql\Model\Rendering\Output(new \SqlSemantics\Platform\PostgreSql\Rendering\Codec(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172));
74        (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Writing())->separated($out, [\SqlSemantics\Platform\PostgreSql\Statement\Constraint\ConstraintAttribute::Deferrable, \SqlSemantics\Platform\PostgreSql\Statement\Constraint\ConstraintAttribute::NotValid], 'OR');
75        self::assertSame('DEFERRABLE OR NOT VALID', implode(' ', array_map(static fn ($piece): string => $piece->text, $out->pieces())));
76    }
77}
78