packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/Operator/NullTestTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Operator;
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\ResolvedColumn;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\Field;
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\Literal\IntegerLiteral;
17use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\NullLiteral;
18use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\Binary;
19use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\BinaryOperator;
20use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\NullTest;
21use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\NullTestForm;
22use SqlSemantics\Platform\Sqlite\Statement\Expression\Subquery\InList;
23use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
24use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
25use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
26
27#[CoversClass(NullTest::class)]
28#[Medium]
29final class NullTestTest extends TestCase
30{
31    public function testDeriveScalarIsAnIntegerThatIsNeverNull(): void
32    {
33        $semantics = new Semantics();
34        $query = $semantics->analyze('SELECT a ISNULL, b NOTNULL, b NOT NULL, NULL ISNULL, ? NOTNULL FROM t', [$semantics->analyze('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)')]);
35        $nullability = array_map(static fn (Field $field): Nullability => $field->nullability, $query->fields()->items ?? []);
36
37        self::assertSame([Nullability::NotNull, Nullability::NotNull, Nullability::NotNull, Nullability::NotNull, Nullability::NotNull], $nullability);
38        self::assertInstanceOf(Known::class, $query->field(3)->type);
39        self::assertSame(Storage::Integer, $query->field(3)->type->descriptor);
40        self::assertInstanceOf(Known::class, $query->field(4)->type);
41        self::assertSame(Storage::Integer, $query->field(4)->type->descriptor);
42    }
43
44    public function testDeriveScalarRecordsTheFactOfTheOperand(): void
45    {
46        $semantics = new Semantics();
47        $query = $semantics->analyze('SELECT b ISNULL FROM t', [$semantics->analyze('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)')]);
48        $test = $query->field(0)->expression;
49
50        self::assertInstanceOf(NullTest::class, $test);
51        self::assertInstanceOf(ResolvedColumn::class, $query->facts->scalar($test->operand)->resolution);
52        self::assertSame(Nullability::Nullable, $query->facts->scalar($test->operand)->nullability);
53    }
54
55    public function testRenderWritesEachForm(): void
56    {
57        $query = (new Semantics())->analyze('select a isnull AS c1, a notnull AS c2, a not null AS c3 from t');
58
59        self::assertSame('SELECT a ISNULL AS c1, a NOTNULL AS c2, a NOT NULL AS c3 FROM t', $query->toString());
60    }
61
62    public function testRenderAppliesToAnOperandOfTheEqualityGroup(): void
63    {
64        $semantics = new Semantics();
65        $membership = $semantics->analyze('SELECT 1 IN (2) ISNULL');
66        $repeated = $semantics->analyze('SELECT 1 ISNULL ISNULL');
67        $outer = $repeated->field(0)->expression;
68
69        self::assertInstanceOf(NullTest::class, $membership->field(0)->expression);
70        self::assertInstanceOf(InList::class, $membership->field(0)->expression->operand);
71        self::assertSame('SELECT 1 IN (2) ISNULL', $membership->toString());
72        self::assertInstanceOf(NullTest::class, $outer);
73        self::assertInstanceOf(NullTest::class, $outer->operand);
74        self::assertSame('SELECT 1 ISNULL ISNULL', $repeated->toString());
75    }
76
77    public function testRenderWritesANewlyBuiltTest(): void
78    {
79        $semantics = new Semantics();
80        $operation = new Operation($semantics->context(), new Select([new ResultColumn(new NullTest(new NullLiteral(), NullTestForm::NotNullWords))]));
81
82        self::assertSame('SELECT NULL NOT NULL', $operation->toString());
83        self::assertSame(Nullability::NotNull, $operation->field(0)->nullability);
84    }
85
86    public function testRefusesAnOperandThatEndsInAWeakerOperator(): void
87    {
88        $disjunction = new Binary(BinaryOperator::Or, new IntegerLiteral('1'), new IntegerLiteral('2'));
89
90        $this->expectExceptionMessage('The operand needs parentheses to keep its place.');
91
92        new NullTest($disjunction, NullTestForm::IsNull);
93    }
94}
95