packages/sql-semantics-sqlite/tests/Unit/Rules/Definition/PrimaryKeyRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Comparison;
11use SqlSemantics\Platform\Sqlite\Rules\Definition\PrimaryKeyRule;
12use SqlSemantics\Platform\Sqlite\Rules\Definition\TableDeclaration;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
15use SqlSemantics\Platform\Sqlite\Statement\Schema\Table\TablePrimaryKey;
16
17#[CoversClass(PrimaryKeyRule::class)]
18#[Medium]
19final class PrimaryKeyRuleTest extends TestCase
20{
21    public function testKeyMakesAnIntegerPrimaryKeyColumnTheRowid(): void
22    {
23        $statement = (new Semantics())->inspect('CREATE TABLE t (a, id INTEGER PRIMARY KEY)')->statement;
24
25        self::assertInstanceOf(CreateTable::class, $statement);
26        $key = (new PrimaryKeyRule())->key($statement, (new TableDeclaration())->domains($statement), Comparison::AsciiInsensitive);
27        self::assertSame([1], $key->columns);
28        self::assertSame(1, $key->rowid);
29        self::assertSame(1, $key->constraints);
30    }
31
32    public function testKeyDoesNotMakeADescendingColumnConstraintTheRowid(): void
33    {
34        $statement = (new Semantics())->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY DESC)')->statement;
35
36        self::assertInstanceOf(CreateTable::class, $statement);
37        $key = (new PrimaryKeyRule())->key($statement, (new TableDeclaration())->domains($statement), Comparison::AsciiInsensitive);
38        self::assertSame([0], $key->columns);
39        self::assertNull($key->rowid);
40    }
41
42    public function testKeyDoesNotMakeAnotherTypeTheRowid(): void
43    {
44        $statement = (new Semantics())->inspect('CREATE TABLE t (id INT PRIMARY KEY, b BIGINT)')->statement;
45
46        self::assertInstanceOf(CreateTable::class, $statement);
47        self::assertNull((new PrimaryKeyRule())->key($statement, (new TableDeclaration())->domains($statement), Comparison::AsciiInsensitive)->rowid);
48    }
49
50    public function testKeyCountsEveryPrimaryKeyConstraintAndUsesTheFirst(): void
51    {
52        $statement = (new Semantics())->inspect('CREATE TABLE t (a PRIMARY KEY, b, PRIMARY KEY (b))')->statement;
53
54        self::assertInstanceOf(CreateTable::class, $statement);
55        $key = (new PrimaryKeyRule())->key($statement, (new TableDeclaration())->domains($statement), Comparison::AsciiInsensitive);
56        self::assertSame(2, $key->constraints);
57        self::assertSame([0], $key->columns);
58    }
59
60    public function testKeyIsEmptyWithoutAPrimaryKey(): void
61    {
62        $statement = (new Semantics())->inspect('CREATE TABLE t (a, b)')->statement;
63
64        self::assertInstanceOf(CreateTable::class, $statement);
65        $key = (new PrimaryKeyRule())->key($statement, (new TableDeclaration())->domains($statement), Comparison::AsciiInsensitive);
66        self::assertSame(0, $key->constraints);
67        self::assertSame([], $key->columns);
68        self::assertNull($key->rowid);
69    }
70
71    public function testTableKeyMakesASingleIntegerColumnTheRowidWhateverItsSortOrder(): void
72    {
73        $statement = (new Semantics())->inspect('CREATE TABLE t (a, id INTEGER, PRIMARY KEY (ID DESC AUTOINCREMENT))')->statement;
74
75        self::assertInstanceOf(CreateTable::class, $statement);
76        $constraint = $statement->constraints[0]->items[0];
77        self::assertInstanceOf(TablePrimaryKey::class, $constraint);
78        $key = (new PrimaryKeyRule())->tableKey($statement, $constraint, (new TableDeclaration())->domains($statement), Comparison::AsciiInsensitive);
79        self::assertSame([1], $key->columns);
80        self::assertSame(1, $key->rowid);
81        self::assertTrue($key->autoincrement);
82    }
83
84    public function testTableKeyListsTheColumnsOfACompositeKeyInKeyOrder(): void
85    {
86        $statement = (new Semantics())->inspect("CREATE TABLE t (a INTEGER, b, c, PRIMARY KEY (c, 'a', (b) COLLATE nocase, zz, a + 1))")->statement;
87
88        self::assertInstanceOf(CreateTable::class, $statement);
89        $constraint = $statement->constraints[0]->items[0];
90        self::assertInstanceOf(TablePrimaryKey::class, $constraint);
91        $key = (new PrimaryKeyRule())->tableKey($statement, $constraint, (new TableDeclaration())->domains($statement), Comparison::AsciiInsensitive);
92        self::assertSame([2, 0, 1], $key->columns);
93        self::assertNull($key->rowid);
94    }
95}
96