packages/sql-semantics-sqlite/tests/Unit/Lowering/Definition/TableConstraintRuleTest.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\TableConstraintRule;
11use SqlSemantics\Platform\Sqlite\Semantics;
12use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\ConstraintName;
13use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
14use SqlSemantics\Platform\Sqlite\Statement\Schema\Table\ForeignKey;
15use SqlSemantics\Platform\Sqlite\Statement\Schema\Table\TableCheck;
16use SqlSemantics\Platform\Sqlite\Statement\Schema\Table\TableOptionKind;
17use SqlSemantics\Platform\Sqlite\Statement\Schema\Table\TablePrimaryKey;
18use SqlSemantics\Platform\Sqlite\Statement\Schema\Table\TableUnique;
19
20#[CoversClass(TableConstraintRule::class)]
21#[Medium]
22final class TableConstraintRuleTest extends TestCase
23{
24 public function testRunsAreEmptyWithoutTableConstraints(): void
25 {
26 $statement = (new Semantics())->inspect('CREATE TABLE t (a)')->statement;
27
28 self::assertInstanceOf(CreateTable::class, $statement);
29 self::assertSame([], $statement->constraints);
30 }
31
32 public function testRunsSplitTheConstraintsAtTheWrittenCommas(): void
33 {
34 $statement = (new Semantics())->inspect('CREATE TABLE t (a, UNIQUE (a) CHECK (a > 0), CONSTRAINT n CONSTRAINT m, PRIMARY KEY (a))')->statement;
35
36 self::assertInstanceOf(CreateTable::class, $statement);
37 self::assertSame([2, 2, 1], array_map(static fn (object $run): int => count($run->items), $statement->constraints));
38 }
39
40 public function testConstraintLowersEveryKindOfTableConstraint(): void
41 {
42 $statement = (new Semantics())->inspect('CREATE TABLE t (a, CONSTRAINT n PRIMARY KEY (a) UNIQUE (a) CHECK (a > 0) FOREIGN KEY (a) REFERENCES p)')->statement;
43
44 self::assertInstanceOf(CreateTable::class, $statement);
45 self::assertSame([ConstraintName::class, TablePrimaryKey::class, TableUnique::class, TableCheck::class, ForeignKey::class], array_map(static fn (object $item): string => $item::class, $statement->constraints[0]->items));
46 }
47
48 public function testOptionsKeepTheirWrittenOrder(): void
49 {
50 $semantics = new Semantics();
51 $none = $semantics->inspect('CREATE TABLE t (a)')->statement;
52 $two = $semantics->inspect('CREATE TABLE t (a PRIMARY KEY) STRICT, WITHOUT ROWID')->statement;
53
54 self::assertInstanceOf(CreateTable::class, $none);
55 self::assertInstanceOf(CreateTable::class, $two);
56 self::assertSame([], $none->options);
57 self::assertSame([TableOptionKind::Strict, TableOptionKind::WithoutRowid], array_map(static fn (object $option): ?TableOptionKind => $option->kind(), $two->options));
58 }
59
60 public function testLeadingCommaTellsACommaWrittenBeforeTheFirstOption(): void
61 {
62 $semantics = new Semantics();
63 $written = $semantics->inspect('CREATE TABLE t (a PRIMARY KEY), WITHOUT ROWID')->statement;
64 $plain = $semantics->inspect('CREATE TABLE t (a PRIMARY KEY) WITHOUT ROWID, STRICT')->statement;
65
66 self::assertInstanceOf(CreateTable::class, $written);
67 self::assertInstanceOf(CreateTable::class, $plain);
68 self::assertTrue($written->optionsComma);
69 self::assertFalse($plain->optionsComma);
70 }
71
72 public function testOptionTellsTheWithoutFormFromTheBareForm(): void
73 {
74 $statement = (new Semantics())->inspect('CREATE TABLE t (a) WITHOUT x, y')->statement;
75
76 self::assertInstanceOf(CreateTable::class, $statement);
77 self::assertTrue($statement->options[0]->without);
78 self::assertFalse($statement->options[1]->without);
79 }
80}
81