packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/TruthWordTest.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\Operation;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ConditionalColumn;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\UndeclaredRelation;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Dependent;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\Sqlite\Semantics;
18use SqlSemantics\Platform\Sqlite\Statement\Expression\TruthWord;
19use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
20use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
21use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
22
23#[CoversClass(TruthWord::class)]
24#[Medium]
25final class TruthWordTest extends TestCase
26{
27    public function testDeriveScalarReadsTheConstantWhenNoColumnCanResolve(): void
28    {
29        $semantics = new Semantics();
30        $create = $semantics->analyze('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
31        $withTable = $semantics->analyze('SELECT true, FALSE FROM t', [$create]);
32        $withoutTable = $semantics->analyze('SELECT true', []);
33        $statement = $withTable->statement;
34
35        self::assertInstanceOf(Select::class, $statement);
36        self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
37        self::assertInstanceOf(ResultColumn::class, $statement->columns[1]);
38        self::assertInstanceOf(TruthWord::class, $statement->columns[0]->expression);
39        self::assertInstanceOf(TruthWord::class, $statement->columns[1]->expression);
40        self::assertTrue($statement->columns[0]->expression->value);
41        self::assertFalse($statement->columns[1]->expression->value);
42        $fact = $withTable->facts->scalar($statement->columns[1]->expression);
43        self::assertEquals(new Known(Storage::Integer), $fact->type);
44        self::assertSame(Nullability::NotNull, $fact->nullability);
45        self::assertNull($fact->resolution);
46        self::assertEquals(new Known(Storage::Integer), $withoutTable->field(0)->type);
47        self::assertSame(Nullability::NotNull, $withoutTable->field(0)->nullability);
48        self::assertSame([], $withTable->facts->diagnostics);
49    }
50
51    public function testDeriveScalarResolvesAColumnNamedTrueOrFalse(): void
52    {
53        $semantics = new Semantics();
54        $create = $semantics->analyze('CREATE TABLE t2 (true INTEGER NOT NULL, "false" TEXT)');
55        $operation = $semantics->analyze('SELECT true, false FROM t2', [$create]);
56        $statement = $operation->statement;
57
58        self::assertInstanceOf(Select::class, $statement);
59        self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
60        $fact = $operation->facts->scalar($statement->columns[0]->expression);
61        self::assertInstanceOf(ResolvedColumn::class, $fact->resolution);
62        self::assertSame($create->declarations()[0]->columns[0], $fact->resolution->slot->column);
63        self::assertSame(Nullability::NotNull, $fact->nullability);
64        self::assertSame($create->declarations()[0]->columns[1], $operation->field(1)->column());
65        self::assertSame(Nullability::Nullable, $operation->field(1)->nullability);
66    }
67
68    public function testDeriveScalarDependsOnAnUndeclaredRelation(): void
69    {
70        $operation = (new Semantics())->analyze('SELECT true FROM u');
71        $statement = $operation->statement;
72
73        self::assertInstanceOf(Select::class, $statement);
74        self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
75        $fact = $operation->facts->scalar($statement->columns[0]->expression);
76        self::assertInstanceOf(ConditionalColumn::class, $fact->resolution);
77        self::assertInstanceOf(Dependent::class, $fact->type);
78        self::assertInstanceOf(UndeclaredRelation::class, $fact->type->missing[0]);
79        self::assertSame(Nullability::Dependent, $fact->nullability);
80    }
81
82    public function testRenderWritesTheBareWordInUpperCase(): void
83    {
84        self::assertSame('SELECT TRUE AS c1, FALSE AS c2', (new Semantics())->analyze('select true AS c1, false AS c2')->toString());
85    }
86
87    public function testRenderWritesANewlyBuiltWord(): void
88    {
89        $semantics = new Semantics();
90        $operation = new Operation($semantics->context(), new Select([new ResultColumn(new TruthWord(false)), new ResultColumn(new TruthWord(true))]));
91
92        self::assertSame('SELECT FALSE, TRUE', $operation->toString());
93        self::assertEquals(new Known(Storage::Integer), $operation->field(0)->type);
94    }
95}
96