packages/sql-semantics-sqlite/tests/Unit/Lowering/Definition/ColumnRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Lowering\Definition\ColumnRule;
11use SqlSemantics\Platform\Sqlite\Semantics;
12use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\Collation;
13use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\ColumnCheck;
14use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\ColumnPrimaryKey;
15use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\ColumnUnique;
16use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\ConstraintName;
17use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\DefaultExpression;
18use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\DefaultLiteral;
19use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\DefaultWord;
20use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\Generated;
21use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\NotNull;
22use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\NullAllowed;
23use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
24use SqlSemantics\Platform\Sqlite\Statement\Schema\Reference\Deferrability;
25use SqlSemantics\Platform\Sqlite\Statement\Schema\Reference\ForeignKeyClause;
26use SqlSemantics\Platform\Sqlite\Statement\Type\NumberSign;
27
28#[CoversClass(ColumnRule::class)]
29#[Medium]
30final class ColumnRuleTest extends TestCase
31{
32 public function testColumnLowersTheNameTheTypeAndTheConstraints(): void
33 {
34 $statement = (new Semantics())->inspect('CREATE TABLE t (price DECIMAL(10, 2) NOT NULL, plain)')->statement;
35
36 self::assertInstanceOf(CreateTable::class, $statement);
37 self::assertSame('price', $statement->columns[0]->name->value);
38 self::assertCount(2, $statement->columns[0]->type->arguments ?? []);
39 self::assertCount(1, $statement->columns[0]->constraints);
40 self::assertNull($statement->columns[1]->type);
41 self::assertSame([], $statement->columns[1]->constraints);
42 }
43
44 public function testConstraintsKeepTheirWrittenOrder(): void
45 {
46 $statement = (new Semantics())->inspect('CREATE TABLE t (a UNIQUE NOT NULL CONSTRAINT c CHECK (a > 0) COLLATE nocase)')->statement;
47
48 self::assertInstanceOf(CreateTable::class, $statement);
49 self::assertSame([ColumnUnique::class, NotNull::class, ConstraintName::class, ColumnCheck::class, Collation::class], array_map(static fn (object $constraint): string => $constraint::class, $statement->columns[0]->constraints));
50 }
51
52 public function testConstraintLowersEveryKindOfConstraint(): void
53 {
54 $statement = (new Semantics())->inspect('CREATE TABLE t (a NULL PRIMARY KEY REFERENCES p DEFERRABLE, b GENERATED ALWAYS AS (a), c AS (a))')->statement;
55
56 self::assertInstanceOf(CreateTable::class, $statement);
57 self::assertSame([NullAllowed::class, ColumnPrimaryKey::class, ForeignKeyClause::class, Deferrability::class], array_map(static fn (object $constraint): string => $constraint::class, $statement->columns[0]->constraints));
58 self::assertInstanceOf(Generated::class, $statement->columns[2]->constraints[0]);
59 }
60
61 public function testDefaultLowersEveryFormOfDefault(): void
62 {
63 $statement = (new Semantics())->inspect('CREATE TABLE t (a DEFAULT 1, b DEFAULT +1, c DEFAULT -1, d DEFAULT (1), e DEFAULT word)')->statement;
64
65 self::assertInstanceOf(CreateTable::class, $statement);
66 $defaults = array_map(static fn (object $column): object => $column->constraints[0], $statement->columns);
67 self::assertSame([DefaultLiteral::class, DefaultLiteral::class, DefaultLiteral::class, DefaultExpression::class, DefaultWord::class], array_map(static fn (object $default): string => $default::class, $defaults));
68 self::assertSame([null, NumberSign::Plus, NumberSign::Minus], array_map(static fn (object $default): ?NumberSign => $default instanceof DefaultLiteral ? $default->sign : null, array_slice($defaults, 0, 3)));
69 }
70
71 public function testScannedAnswersWhatFollowsTheEmptyMarker(): void
72 {
73 $operation = (new Semantics())->inspect("CREATE TABLE t (a DEFAULT 'x')");
74
75 self::assertSame("CREATE TABLE t (a DEFAULT 'x')", $operation->toString());
76 }
77
78 public function testAutoincrementLowersTheOptionalKeyword(): void
79 {
80 $statement = (new Semantics())->inspect('CREATE TABLE t (a INTEGER PRIMARY KEY AUTOINCREMENT, b PRIMARY KEY)')->statement;
81
82 self::assertInstanceOf(CreateTable::class, $statement);
83 self::assertTrue($statement->columns[0]->primaryKey()?->autoincrement);
84 self::assertFalse($statement->columns[1]->primaryKey()?->autoincrement);
85 }
86
87 public function testGeneratedLowersTheExpressionAndTheOptionalWord(): void
88 {
89 $statement = (new Semantics())->inspect('CREATE TABLE t (a, b AS (a), c AS (a) STORED)')->statement;
90
91 self::assertInstanceOf(CreateTable::class, $statement);
92 self::assertNull($statement->columns[1]->generated()?->word);
93 self::assertSame('STORED', $statement->columns[2]->generated()?->word?->name->value);
94 }
95}
96