packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/CollateTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression;
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\Operation;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Choice;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Invalid;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\Sqlite\Semantics;
17use SqlSemantics\Platform\Sqlite\Statement\Expression\Collate;
18use SqlSemantics\Platform\Sqlite\Statement\Expression\ColumnUse;
19use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
20use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\Misuse;
21use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\MisuseRule;
22use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
23use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
24use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
25
26#[CoversClass(Collate::class)]
27#[Medium]
28final class CollateTest extends TestCase
29{
30 public function testAcceptsAPrefixOperandWhichBindsTighter(): void
31 {
32 $statement = (new Semantics())->inspect('SELECT -1')->statement;
33
34 self::assertInstanceOf(Select::class, $statement);
35 self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
36 $collate = new Collate($statement->columns[0]->expression, new Name('nocase'));
37 self::assertSame($statement->columns[0]->expression, $collate->operand);
38 self::assertSame('nocase', $collate->collation->value);
39 }
40
41 public function testDeriveScalarKeepsTheTypeAndNullFactAndLeavesTheResolutionToTheOperand(): void
42 {
43 $semantics = new Semantics();
44 $create = $semantics->inspect('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
45 $operation = $semantics->inspect('SELECT a COLLATE NOCASE FROM t', [$create]);
46 $statement = $operation->statement;
47
48 self::assertInstanceOf(Select::class, $statement);
49 self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
50 self::assertInstanceOf(Collate::class, $statement->columns[0]->expression);
51 $collate = $statement->columns[0]->expression;
52 self::assertInstanceOf(ColumnUse::class, $collate->operand);
53 self::assertSame('NOCASE', $collate->collation->value);
54 $fact = $operation->facts->scalar($collate);
55 self::assertNull($fact->resolution);
56 self::assertInstanceOf(ResolvedColumn::class, $operation->facts->scalar($collate->operand)->resolution);
57 self::assertNull($operation->field(0)->resolution);
58 self::assertSame(Nullability::NotNull, $fact->nullability);
59 self::assertSame($operation->facts->scalar($collate->operand)->type, $fact->type);
60 self::assertSame([], $operation->facts->diagnostics);
61 }
62
63 public function testDeriveScalarCollatesAPrefixExpression(): void
64 {
65 $semantics = new Semantics();
66 $create = $semantics->inspect('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
67 $operation = $semantics->inspect('SELECT -a COLLATE nocase AS c1 FROM t', [$create]);
68 $statement = $operation->statement;
69
70 self::assertInstanceOf(Select::class, $statement);
71 self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
72 self::assertInstanceOf(Collate::class, $statement->columns[0]->expression);
73 self::assertEquals(new Choice([Storage::Integer, Storage::Real]), $operation->field(0)->type);
74 self::assertSame('SELECT - a COLLATE nocase AS c1 FROM t', $operation->toString());
75 }
76
77 public function testDeriveScalarReportsACollatedRowValue(): void
78 {
79 $operation = (new Semantics())->inspect('SELECT (1, 2) COLLATE nocase', []);
80 $statement = $operation->statement;
81
82 self::assertInstanceOf(Select::class, $statement);
83 self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
84 $fact = $operation->facts->scalar($statement->columns[0]->expression);
85 self::assertInstanceOf(Invalid::class, $fact->type);
86 self::assertEquals(new Misuse(MisuseRule::TooManyValueColumns), $fact->type->cause);
87 self::assertSame('row value misused', $operation->facts->diagnostics[0]->message());
88 self::assertContains($fact->type->cause, $operation->facts->diagnostics);
89 }
90
91 public function testRenderWritesTheOperandAndTheCollationName(): void
92 {
93 $operation = (new Semantics())->inspect('select a collate nocase AS c1, (a) collate "nocase" AS c2, b collate BINARY collate rtrim AS c3 from t');
94
95 self::assertSame('SELECT a COLLATE nocase AS c1, (a) COLLATE nocase AS c2, b COLLATE BINARY COLLATE rtrim AS c3 FROM t', $operation->toString());
96 }
97
98 public function testRenderWritesANewlyBuiltCollation(): void
99 {
100 $semantics = new Semantics();
101 $operation = new Operation($semantics->context(), new Select([new ResultColumn(new Collate(new IntegerLiteral('1'), new Name('NOCASE')))]));
102
103 self::assertSame('SELECT 1 COLLATE NOCASE', $operation->toString());
104 }
105}
106