packages/sql-semantics-sqlite/tests/Unit/Statement/Mutation/RowAssignmentTest.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\Statement\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Operation;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
14use SqlSemantics\Platform\Sqlite\Semantics;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
16use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\TextLiteral;
17use SqlSemantics\Platform\Sqlite\Statement\Expression\RowExpression;
18use SqlSemantics\Platform\Sqlite\Statement\Mutation\MutationTarget;
19use SqlSemantics\Platform\Sqlite\Statement\Mutation\RowAssignment;
20use SqlSemantics\Platform\Sqlite\Statement\Mutation\Update;
21use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\ArityMismatch;
22use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\ArityRule;
23
24#[CoversClass(RowAssignment::class)]
25#[Medium]
26final class RowAssignmentTest extends TestCase
27{
28 public function testRenderWritesTheColumnListAndTheValue(): void
29 {
30 $semantics = new Semantics();
31 $assignment = new RowAssignment([new Name('a'), new Name('b')], new RowExpression([new IntegerLiteral('1'), new TextLiteral('x')]));
32 $built = new Operation($semantics->context(), new Update(new MutationTarget(new QualifiedName(new Name('t'))), [$assignment]));
33 $query = $semantics->inspect("update t set (a, b) = (1, 'x'), (a) = (2)");
34
35 self::assertSame("UPDATE t SET (a, b) = (1, 'x')", $built->toString());
36 self::assertSame("UPDATE t SET (a, b) = (1, 'x'), (a) = (2)", $query->toString());
37 self::assertInstanceOf(Update::class, $query->statement);
38 self::assertInstanceOf(RowAssignment::class, $query->statement->assignments[0]);
39 self::assertSame(['a', 'b'], array_map(static fn (Name $name): string => $name->value, $query->statement->assignments[0]->columns));
40 self::assertInstanceOf(RowExpression::class, $query->statement->assignments[0]->value);
41 }
42
43 public function testColumnsMustMatchTheWidthOfTheValue(): void
44 {
45 $semantics = new Semantics();
46 $t = $semantics->inspect('CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT)');
47 $wide = $semantics->inspect('UPDATE t SET (a, b) = (1, 2, 3)', [$t]);
48 $narrow = $semantics->inspect('UPDATE t SET (a) = (1, 2)', [$t]);
49 $exact = $semantics->inspect('UPDATE t SET (a, b) = (1, 2)', [$t]);
50 $missing = $semantics->inspect('UPDATE t SET (a, zz) = (1, 2)', [$t]);
51
52 self::assertInstanceOf(ArityMismatch::class, $wide->facts->diagnostics[0]);
53 self::assertSame(ArityRule::RowAssignment, $wide->facts->diagnostics[0]->rule);
54 self::assertSame('2 columns assigned 3 values.', $wide->facts->diagnostics[0]->message());
55 self::assertInstanceOf(ArityMismatch::class, $narrow->facts->diagnostics[0]);
56 self::assertSame('1 columns assigned 2 values.', $narrow->facts->diagnostics[0]->message());
57 self::assertSame([], $exact->facts->diagnostics);
58 self::assertInstanceOf(MissingColumn::class, $missing->facts->diagnostics[0]);
59 self::assertSame('zz', $missing->facts->diagnostics[0]->name->value);
60 }
61
62 public function testRejectsAnEmptyColumnList(): void
63 {
64 $this->expectExceptionMessage('A row assignment names at least one column.');
65
66 new RowAssignment([], new IntegerLiteral('1'));
67 }
68}
69