packages/sql-semantics-sqlite/tests/Unit/Lowering/Leaf/ConflictRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Leaf;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Lowering\Leaf\ConflictRule;
11use SqlSemantics\Platform\Sqlite\Semantics;
12use SqlSemantics\Platform\Sqlite\Statement\Expression\Raise;
13use SqlSemantics\Platform\Sqlite\Statement\Expression\RaiseAction;
14use SqlSemantics\Platform\Sqlite\Statement\Mutation\ConflictResolution;
15use SqlSemantics\Platform\Sqlite\Statement\Mutation\InsertRows;
16use SqlSemantics\Platform\Sqlite\Statement\Mutation\Update;
17use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
18use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
19use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\ColumnPrimaryKey;
20use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\ColumnUnique;
21use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
22use SqlSemantics\Platform\Sqlite\Statement\Trigger\CreateTrigger;
23
24#[CoversClass(ConflictRule::class)]
25#[Medium]
26final class ConflictRuleTest extends TestCase
27{
28    public function testResolutionLowersEachOfTheFiveAlgorithms(): void
29    {
30        $semantics = new Semantics();
31        $ignore = $semantics->analyze('INSERT OR IGNORE INTO t VALUES (1)')->statement;
32        $replace = $semantics->analyze('INSERT OR REPLACE INTO t VALUES (1)')->statement;
33        $rollback = $semantics->analyze('INSERT OR ROLLBACK INTO t VALUES (1)')->statement;
34        $abort = $semantics->analyze('INSERT OR ABORT INTO t VALUES (1)')->statement;
35        $fail = $semantics->analyze('INSERT OR FAIL INTO t VALUES (1)')->statement;
36
37        self::assertInstanceOf(InsertRows::class, $ignore);
38        self::assertInstanceOf(InsertRows::class, $replace);
39        self::assertInstanceOf(InsertRows::class, $rollback);
40        self::assertInstanceOf(InsertRows::class, $abort);
41        self::assertInstanceOf(InsertRows::class, $fail);
42        self::assertSame(ConflictResolution::Ignore, $ignore->into->resolution);
43        self::assertSame(ConflictResolution::Replace, $replace->into->resolution);
44        self::assertSame(ConflictResolution::Rollback, $rollback->into->resolution);
45        self::assertSame(ConflictResolution::Abort, $abort->into->resolution);
46        self::assertSame(ConflictResolution::Fail, $fail->into->resolution);
47    }
48
49    public function testResolutionRendersTheAlgorithmAsWritten(): void
50    {
51        $semantics = new Semantics();
52
53        self::assertSame('INSERT OR IGNORE INTO t VALUES (1)', $semantics->analyze('insert or ignore into t values (1)')->toString());
54        self::assertSame('UPDATE OR REPLACE t SET a = 1', $semantics->analyze('update or replace t set a = 1')->toString());
55    }
56
57    public function testRaisedLowersTheAlgorithmsRaiseCanName(): void
58    {
59        $trigger = (new Semantics())->analyze("CREATE TRIGGER tr INSERT ON t BEGIN SELECT RAISE(FAIL, 'x'), RAISE(ROLLBACK, 'y'), RAISE(ABORT, 'z'); END")->statement;
60
61        self::assertInstanceOf(CreateTrigger::class, $trigger);
62        $select = $trigger->steps[0];
63        self::assertInstanceOf(Select::class, $select);
64        self::assertSame([RaiseAction::Fail, RaiseAction::Rollback, RaiseAction::Abort], array_map(static function (object $column): RaiseAction {
65            self::assertInstanceOf(ResultColumn::class, $column);
66            self::assertInstanceOf(Raise::class, $column->expression);
67
68            return $column->expression->action;
69        }, $select->columns));
70    }
71
72    public function testOnConflictLowersTheAlgorithmOfAConstraintAndNullWithoutTheClause(): void
73    {
74        $table = (new Semantics())->analyze('CREATE TABLE t (a UNIQUE ON CONFLICT IGNORE, b UNIQUE, c PRIMARY KEY ON CONFLICT ABORT)')->statement;
75
76        self::assertInstanceOf(CreateTable::class, $table);
77        $ignore = $table->columns[0]->constraints[0];
78        $plain = $table->columns[1]->constraints[0];
79        $abort = $table->columns[2]->constraints[0];
80        self::assertInstanceOf(ColumnUnique::class, $ignore);
81        self::assertInstanceOf(ColumnUnique::class, $plain);
82        self::assertInstanceOf(ColumnPrimaryKey::class, $abort);
83        self::assertSame(ConflictResolution::Ignore, $ignore->conflict);
84        self::assertNull($plain->conflict);
85        self::assertSame(ConflictResolution::Abort, $abort->conflict);
86    }
87
88    public function testOrConflictLowersTheAlgorithmAfterOrAndNullWithoutTheClause(): void
89    {
90        $semantics = new Semantics();
91        $plain = $semantics->analyze('UPDATE t SET a = 1')->statement;
92        $fail = $semantics->analyze('UPDATE OR FAIL t SET a = 1')->statement;
93        $insert = $semantics->analyze('INSERT INTO t VALUES (1)')->statement;
94
95        self::assertInstanceOf(Update::class, $plain);
96        self::assertInstanceOf(Update::class, $fail);
97        self::assertInstanceOf(InsertRows::class, $insert);
98        self::assertNull($plain->resolution);
99        self::assertSame(ConflictResolution::Fail, $fail->resolution);
100        self::assertNull($insert->into->resolution);
101    }
102}
103