packages/sql-semantics-sqlite/tests/Unit/Statement/Mutation/InsertDefaultsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Mutation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Construction\Derivation;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Operation;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\DeclaredTable;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\Field;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\Sqlite\Semantics;
19use SqlSemantics\Platform\Sqlite\Statement\Mutation\InsertDefaults;
20use SqlSemantics\Platform\Sqlite\Statement\Mutation\InsertInto;
21use SqlSemantics\Platform\Sqlite\Statement\Mutation\MutationTarget;
22use SqlSemantics\Platform\Sqlite\Statement\Query\Star;
23
24#[CoversClass(InsertDefaults::class)]
25#[Medium]
26final class InsertDefaultsTest extends TestCase
27{
28 public function testDeriveStatementReturnsTheRowOfTheTarget(): void
29 {
30 $semantics = new Semantics();
31 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
32 $star = $semantics->inspect('INSERT INTO t DEFAULT VALUES RETURNING *', [$t]);
33 $plain = $semantics->inspect('INSERT INTO t DEFAULT VALUES', [$t]);
34 $fields = $star->fields();
35
36 self::assertInstanceOf(InsertDefaults::class, $star->statement);
37 self::assertInstanceOf(DeclaredTable::class, $star->facts->relation($star->statement->into->target)->table);
38 self::assertNotNull($fields);
39 self::assertSame(['id', 'a', 'b'], array_map(static fn (Field $field): ?string => $field->name?->value, $fields->items));
40 self::assertSame($t->declarations()[0]->columns[1], $star->field('a')->column());
41 self::assertSame(Nullability::NotNull, $star->field('a')->nullability);
42 self::assertSame([], $star->facts->diagnostics);
43 self::assertNull($plain->shape());
44 }
45
46 public function testDeriveStatementReportsAColumnTheTableLacks(): void
47 {
48 $semantics = new Semantics();
49 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
50 $query = $semantics->inspect('INSERT INTO t (zz) DEFAULT VALUES', [$t]);
51
52 self::assertInstanceOf(MissingColumn::class, $query->facts->diagnostics[0]);
53 self::assertSame('zz', $query->facts->diagnostics[0]->name->value);
54 self::assertSame([], $semantics->inspect('INSERT INTO t (zz) DEFAULT VALUES')->facts->diagnostics);
55 }
56
57 public function testDeriveWithinAnswersNullWithoutReturning(): void
58 {
59 $semantics = new Semantics();
60 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
61 $statement = $semantics->inspect('INSERT INTO t DEFAULT VALUES', [$t])->statement;
62 $returning = $semantics->inspect('INSERT INTO t DEFAULT VALUES RETURNING id', [$t])->statement;
63 $derivation = new Derivation($semantics->context([$t]));
64
65 self::assertInstanceOf(InsertDefaults::class, $statement);
66 self::assertInstanceOf(InsertDefaults::class, $returning);
67 self::assertNull($statement->deriveWithin($derivation, $derivation->environment()));
68 self::assertSame('id', $returning->deriveWithin($derivation, $derivation->environment())?->shape->slots[0]->name?->value);
69 }
70
71 public function testRenderWritesDefaultValuesAndReturning(): void
72 {
73 $semantics = new Semantics();
74 $built = new Operation($semantics->context(), new InsertDefaults(new InsertInto(new MutationTarget(new QualifiedName(new Name('t')))), [new Star()]));
75
76 self::assertSame('INSERT INTO t DEFAULT VALUES RETURNING *', $built->toString());
77 self::assertSame('REPLACE INTO main.t (a) DEFAULT VALUES', $semantics->inspect('replace into main.t (a) default values')->toString());
78 }
79}
80