packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/Column/ColumnDefinitionTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Schema\Column;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
12use SqlSemantics\Platform\Sqlite\Semantics;
13use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
14use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\ColumnDefinition;
15use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\ColumnPrimaryKey;
16use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\Generated;
17use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\NotNull;
18use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
19use SqlSemantics\Platform\Sqlite\Statement\Schema\Limit\DefaultNotConstant;
20use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\GeneratedColumnFlaw;
21use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\GeneratedColumnProblem;
22use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
23
24#[CoversClass(ColumnDefinition::class)]
25#[Medium]
26final class ColumnDefinitionTest extends TestCase
27{
28 public function testNotNullTellsWhetherTheConstraintIsWritten(): void
29 {
30 self::assertTrue((new ColumnDefinition(new Name('a'), null, [new NotNull()]))->notNull());
31 self::assertFalse((new ColumnDefinition(new Name('a')))->notNull());
32 }
33
34 public function testPrimaryKeyAnswersTheFirstWrittenKeyConstraint(): void
35 {
36 $first = new ColumnPrimaryKey();
37 $column = new ColumnDefinition(new Name('a'), null, [new NotNull(), $first, new ColumnPrimaryKey(null, null, true)]);
38
39 self::assertSame($first, $column->primaryKey());
40 self::assertNull((new ColumnDefinition(new Name('a')))->primaryKey());
41 }
42
43 public function testGeneratedAnswersTheGeneratedColumnExpression(): void
44 {
45 $generated = new Generated(new IntegerLiteral('1'));
46
47 self::assertSame($generated, (new ColumnDefinition(new Name('a'), null, [$generated]))->generated());
48 self::assertNull((new ColumnDefinition(new Name('a')))->generated());
49 }
50
51 public function testDeriveColumnDerivesTheTypeArgumentsAndTheConstraints(): void
52 {
53 $operation = (new Semantics())->inspect('CREATE TABLE t (a DECIMAL(10, 2) DEFAULT 0)', []);
54 $statement = $operation->statement;
55
56 self::assertInstanceOf(CreateTable::class, $statement);
57 $type = $statement->columns[0]->type;
58 self::assertNotNull($type);
59 self::assertEquals(new Known(Storage::Integer), $operation->facts->scalar($type->arguments[1]->number)->type);
60 self::assertSame([], $operation->facts->diagnostics);
61 }
62
63 public function testDeriveColumnReportsADefaultNotConstantAndADefaultOnAGeneratedColumn(): void
64 {
65 $semantics = new Semantics();
66 $constant = $semantics->inspect('CREATE TABLE t (a DEFAULT (abs(?)))', []);
67 $generated = $semantics->inspect('CREATE TABLE t (a, b AS (a) DEFAULT 1, c DEFAULT word AS (a))', []);
68
69 self::assertCount(1, $constant->facts->diagnostics);
70 self::assertInstanceOf(DefaultNotConstant::class, $constant->facts->diagnostics[0]);
71 self::assertSame('a', $constant->facts->diagnostics[0]->column->value);
72 self::assertCount(2, $generated->facts->diagnostics);
73 self::assertInstanceOf(GeneratedColumnProblem::class, $generated->facts->diagnostics[0]);
74 self::assertSame(GeneratedColumnFlaw::WithDefault, $generated->facts->diagnostics[0]->flaw);
75 $second = $generated->facts->diagnostics[1];
76 self::assertInstanceOf(GeneratedColumnProblem::class, $second);
77 self::assertSame('c', $second->column?->value);
78 }
79
80 public function testRenderWritesTheNameTheTypeAndTheConstraintsInOrder(): void
81 {
82 $operation = (new Semantics())->inspect('create table t ("my col" unsigned big int default 1 not null)');
83
84 self::assertSame('CREATE TABLE t (`my col` unsigned big int DEFAULT 1 NOT NULL)', $operation->toString());
85 }
86
87 public function testRenderKeepsTheQuotingOfTypeWords(): void
88 {
89 $operation = (new Semantics())->inspect('CREATE TABLE t (a "big int", b [x] INT, c \'text\')');
90
91 self::assertSame('CREATE TABLE t (a "big int", b [x] INT, c \'text\')', $operation->toString());
92 }
93}
94