packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/Column/ColumnPrimaryKeyTest.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\Semantics;
11use SqlSemantics\Platform\Sqlite\Statement\Mutation\ConflictResolution;
12use SqlSemantics\Platform\Sqlite\Statement\Query\Ordering\SortDirection;
13use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\ColumnPrimaryKey;
14use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
15
16#[CoversClass(ColumnPrimaryKey::class)]
17#[Medium]
18final class ColumnPrimaryKeyTest extends TestCase
19{
20    public function testDeriveConstraintRecordsNothing(): void
21    {
22        $operation = (new Semantics())->analyze('CREATE TABLE t (a PRIMARY KEY)', []);
23
24        self::assertSame([], $operation->facts->diagnostics);
25    }
26
27    public function testRenderKeepsTheSortOrderTheConflictResolutionAndAutoincrement(): void
28    {
29        $operation = (new Semantics())->analyze('create table t (id integer primary key asc on conflict abort autoincrement)');
30        $statement = $operation->statement;
31
32        self::assertInstanceOf(CreateTable::class, $statement);
33        $key = $statement->columns[0]->constraints[0];
34        self::assertInstanceOf(ColumnPrimaryKey::class, $key);
35        self::assertSame(SortDirection::Ascending, $key->direction);
36        self::assertSame(ConflictResolution::Abort, $key->conflict);
37        self::assertTrue($key->autoincrement);
38        self::assertSame('CREATE TABLE t (id integer PRIMARY KEY ASC ON CONFLICT ABORT AUTOINCREMENT)', $operation->toString());
39    }
40
41    public function testRenderWritesABareKey(): void
42    {
43        $operation = (new Semantics())->analyze('CREATE TABLE t (id PRIMARY KEY)');
44        $statement = $operation->statement;
45
46        self::assertInstanceOf(CreateTable::class, $statement);
47        $key = $statement->columns[0]->constraints[0];
48        self::assertInstanceOf(ColumnPrimaryKey::class, $key);
49        self::assertNull($key->direction);
50        self::assertFalse($key->autoincrement);
51        self::assertSame('CREATE TABLE t (id PRIMARY KEY)', $operation->toString());
52    }
53}
54