packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/Table/TablePrimaryKeyTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Schema\Table;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
12use SqlSemantics\Platform\Sqlite\Semantics;
13use SqlSemantics\Platform\Sqlite\Statement\Mutation\ConflictResolution;
14use SqlSemantics\Platform\Sqlite\Statement\Query\Ordering\SortDirection;
15use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
16use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\PrimaryKeyFlaw;
17use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\PrimaryKeyProblem;
18use SqlSemantics\Platform\Sqlite\Statement\Schema\Table\TablePrimaryKey;
19
20#[CoversClass(TablePrimaryKey::class)]
21#[Medium]
22final class TablePrimaryKeyTest extends TestCase
23{
24    public function testDeriveConstraintResolvesTheKeyColumns(): void
25    {
26        $operation = (new Semantics())->inspect('CREATE TABLE t (a, b, PRIMARY KEY (b, a))', []);
27        $statement = $operation->statement;
28
29        self::assertInstanceOf(CreateTable::class, $statement);
30        $key = $statement->constraints[0]->items[0];
31        self::assertInstanceOf(TablePrimaryKey::class, $key);
32        $resolution = $operation->facts->scalar($key->terms[0]->expression)->resolution;
33        self::assertInstanceOf(ResolvedColumn::class, $resolution);
34        self::assertSame($operation->declarations()[0]->columns[1], $resolution->slot->column);
35        self::assertSame([], $operation->facts->diagnostics);
36    }
37
38    public function testDeriveConstraintReportsAMissingKeyColumn(): void
39    {
40        $operation = (new Semantics())->inspect('CREATE TABLE t (a, PRIMARY KEY (zz))', []);
41
42        self::assertCount(1, $operation->facts->diagnostics);
43        self::assertInstanceOf(MissingColumn::class, $operation->facts->diagnostics[0]);
44    }
45
46    public function testDeriveConstraintReportsATermThatIsNoColumnName(): void
47    {
48        $operation = (new Semantics())->inspect('CREATE TABLE t (a, PRIMARY KEY (a + 1))', []);
49
50        self::assertCount(1, $operation->facts->diagnostics);
51        self::assertInstanceOf(PrimaryKeyProblem::class, $operation->facts->diagnostics[0]);
52        self::assertSame(PrimaryKeyFlaw::ExpressionTerm, $operation->facts->diagnostics[0]->flaw);
53    }
54
55    public function testRenderWritesAutoincrementInsideTheParentheses(): void
56    {
57        $operation = (new Semantics())->inspect('create table t (id integer, primary key (id desc autoincrement) on conflict replace)');
58        $statement = $operation->statement;
59
60        self::assertInstanceOf(CreateTable::class, $statement);
61        $key = $statement->constraints[0]->items[0];
62        self::assertInstanceOf(TablePrimaryKey::class, $key);
63        self::assertTrue($key->autoincrement);
64        self::assertSame(SortDirection::Descending, $key->terms[0]->direction);
65        self::assertSame(ConflictResolution::Replace, $key->conflict);
66        self::assertSame('CREATE TABLE t (id integer, PRIMARY KEY (id DESC AUTOINCREMENT) ON CONFLICT REPLACE)', $operation->toString());
67    }
68
69    public function testRefusesAStringLiteralTermThatNamesAColumn(): void
70    {
71        $this->expectExceptionMessage('A string literal written as a key term names a column: write it as a LiteralColumn.');
72
73        new TablePrimaryKey([new \SqlSemantics\Platform\Sqlite\Statement\Query\Ordering\SortTerm(new \SqlSemantics\Platform\Sqlite\Statement\Expression\Collate(new \SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\TextLiteral('a'), new \SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name('nocase')))]);
74    }
75}
76