packages/sql-semantics-postgres/tests/Unit/Rules/Table/Definition/ColumnSetTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Table\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\ColumnSet::class)]
12#[Medium]
13final class ColumnSetTest extends TestCase
14{
15 public function testAddKeepsTheFirstColumnOfAName(): void
16 {
17 $set = new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\ColumnSet();
18 $set->add(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text, true);
19 self::assertSame(false, $set->add(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4, false));
20 }
21
22 public function testInheritMergesTheNotNullFact(): void
23 {
24 $set = new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\ColumnSet();
25 $set->add(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text, false);
26 $set->inherit(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull));
27 self::assertSame(\SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull, $set->columns()[0]->nullability);
28 }
29
30 public function testRequireMarksAColumnNotNull(): void
31 {
32 $set = new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\ColumnSet();
33 $set->add(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text, false);
34 $set->require(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'));
35 self::assertSame(\SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull, $set->columns()[0]->nullability);
36 }
37
38 public function testCloseStopsAddingColumns(): void
39 {
40 $set = new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\ColumnSet();
41 $set->close();
42 self::assertSame([
43 0 => false,
44 1 => 0,
45 ], [$set->add(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text, false), count($set->columns())]);
46 }
47
48 public function testClosedIsFalseForANewSet(): void
49 {
50 self::assertSame(false, (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\ColumnSet())->closed());
51 }
52
53 public function testColumnsAnswersTheColumnsInOrder(): void
54 {
55 $set = new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\ColumnSet();
56 $set->add(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text, false);
57 $set->add(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text, false);
58 self::assertSame([
59 0 => 'b',
60 1 => 'a',
61 ], array_map(static fn ($column): string => $column->name->value, $set->columns()));
62 }
63
64 public function testAddKeepsTheGeneratedFlag(): void
65 {
66 $set = new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\ColumnSet();
67 $set->add(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4, false, true);
68 self::assertSame(true, $set->columns()[0]->generated);
69 }
70
71 public function testInheritMergesTheGeneratedFlag(): void
72 {
73 $set = new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\ColumnSet();
74 $set->add(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4, false);
75 $set->inherit(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::Nullable, true));
76 self::assertSame([true, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::Nullable], [$set->columns()[0]->generated, $set->columns()[0]->nullability]);
77 }
78}
79