packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/CaseExpressionTest.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\Reference\Missing\UnboundParameter;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Choice;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Dependent;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
15use SqlSemantics\Platform\Sqlite\Semantics;
16use SqlSemantics\Platform\Sqlite\Statement\Expression\CaseExpression;
17use SqlSemantics\Platform\Sqlite\Statement\Expression\ColumnUse;
18use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
19use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\TextLiteral;
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(CaseExpression::class)]
27#[Medium]
28final class CaseExpressionTest extends TestCase
29{
30    public function testDeriveScalarChoosesAmongTheResultTypesAndIsNotNullWithElse(): void
31    {
32        $semantics = new Semantics();
33        $create = $semantics->inspect('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
34        $operation = $semantics->inspect("SELECT CASE a WHEN 1 THEN 'x' ELSE 2 END FROM t", [$create]);
35        $statement = $operation->statement;
36
37        self::assertInstanceOf(Select::class, $statement);
38        self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
39        self::assertInstanceOf(CaseExpression::class, $statement->columns[0]->expression);
40        $case = $statement->columns[0]->expression;
41        self::assertInstanceOf(ColumnUse::class, $case->base);
42        self::assertInstanceOf(IntegerLiteral::class, $case->branches[0]->when);
43        self::assertInstanceOf(TextLiteral::class, $case->branches[0]->then);
44        self::assertInstanceOf(IntegerLiteral::class, $case->otherwise);
45        self::assertEquals(new Choice([Storage::Integer, Storage::Text]), $operation->facts->scalar($case)->type);
46        self::assertSame(Nullability::NotNull, $operation->facts->scalar($case)->nullability);
47        self::assertSame([], $operation->facts->diagnostics);
48    }
49
50    public function testDeriveScalarIsNullableWithoutElse(): void
51    {
52        $semantics = new Semantics();
53        $create = $semantics->inspect('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
54        $operation = $semantics->inspect('SELECT CASE WHEN a > 1 THEN 1 END FROM t', [$create]);
55
56        self::assertEquals(new Known(Storage::Integer), $operation->field(0)->type);
57        self::assertSame(Nullability::Nullable, $operation->field(0)->nullability);
58    }
59
60    public function testDeriveScalarIsNullableWhenAResultCanBeNull(): void
61    {
62        $semantics = new Semantics();
63        $create = $semantics->inspect('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
64        $withNullResult = $semantics->inspect('SELECT CASE WHEN a THEN 1 ELSE NULL END FROM t', [$create]);
65        $withNullableColumn = $semantics->inspect('SELECT CASE WHEN 1 THEN b ELSE b END FROM t', [$create]);
66
67        self::assertEquals(new Known(Storage::Integer), $withNullResult->field(0)->type);
68        self::assertSame(Nullability::Nullable, $withNullResult->field(0)->nullability);
69        self::assertEquals(new Choice([Storage::Text, Storage::Blob]), $withNullableColumn->field(0)->type);
70        self::assertSame(Nullability::Nullable, $withNullableColumn->field(0)->nullability);
71    }
72
73    public function testDeriveScalarMergesEqualResultTypesIntoOne(): void
74    {
75        $operation = (new Semantics())->inspect('SELECT CASE 1 WHEN 1 THEN 1 WHEN 2 THEN 2 ELSE 3 END', []);
76
77        self::assertEquals(new Known(Storage::Integer), $operation->field(0)->type);
78        self::assertSame(Nullability::NotNull, $operation->field(0)->nullability);
79    }
80
81    public function testDeriveScalarDependsOnAParameterResult(): void
82    {
83        $operation = (new Semantics())->inspect('SELECT CASE WHEN 1 THEN ? END', []);
84
85        self::assertEquals(new Dependent([new UnboundParameter('?')]), $operation->field(0)->type);
86        self::assertSame(Nullability::Nullable, $operation->field(0)->nullability);
87    }
88
89    public function testDeriveScalarReportsARowValueCondition(): void
90    {
91        $operation = (new Semantics())->inspect('SELECT CASE WHEN (1, 2) THEN 1 END', []);
92
93        self::assertEquals([new Misuse(MisuseRule::TooManyValueColumns)], $operation->facts->diagnostics);
94        self::assertSame('row value misused', $operation->facts->diagnostics[0]->message());
95        self::assertEquals(new Known(Storage::Integer), $operation->field(0)->type);
96    }
97
98    public function testDeriveScalarReportsARowValueResult(): void
99    {
100        $operation = (new Semantics())->inspect('SELECT CASE WHEN 1 THEN (1, 2) END', []);
101
102        self::assertEquals([new Misuse(MisuseRule::TooManyValueColumns)], $operation->facts->diagnostics);
103    }
104
105    public function testDeriveScalarReportsARowValueComparedWithASingleBase(): void
106    {
107        $operation = (new Semantics())->inspect('SELECT CASE 1 WHEN (1, 2) THEN 1 END', []);
108
109        self::assertEquals([new Misuse(MisuseRule::TooManyValueColumns)], $operation->facts->diagnostics);
110    }
111
112    public function testDeriveScalarAcceptsRowValuesOfTheSameWidthAroundABase(): void
113    {
114        $operation = (new Semantics())->inspect('SELECT CASE (1, 2) WHEN (1, 2) THEN 1 END', []);
115
116        self::assertSame([], $operation->facts->diagnostics);
117        self::assertEquals(new Known(Storage::Integer), $operation->field(0)->type);
118    }
119
120    public function testRenderWritesEveryPartInOrder(): void
121    {
122        $operation = (new Semantics())->inspect('select case a when 1 then \'x\' when 2 then \'y\' else \'z\' end AS c1, case when a then 1 end AS c2 from t');
123
124        self::assertSame('SELECT CASE a WHEN 1 THEN \'x\' WHEN 2 THEN \'y\' ELSE \'z\' END AS c1, CASE WHEN a THEN 1 END AS c2 FROM t', $operation->toString());
125    }
126}
127