packages/sql-semantics-sqlite/tests/Unit/Rules/Definition/ExplainRowsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Comparison;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\UniqueField;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
14use SqlSemantics\Platform\Sqlite\Rules\Definition\ExplainRows;
15use SqlSemantics\Platform\Sqlite\Statement\Inspection\ExplainMode;
16use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
17
18#[CoversClass(ExplainRows::class)]
19#[Small]
20final class ExplainRowsTest extends TestCase
21{
22    public function testFactListsTheEightColumnsOfAProgramReport(): void
23    {
24        $fact = (new ExplainRows())->fact(ExplainMode::Program, Comparison::AsciiInsensitive);
25        $names = array_map(static fn (object $slot): ?string => $slot->name?->value, $fact->shape->slots);
26
27        self::assertSame(['addr', 'opcode', 'p1', 'p2', 'p3', 'p4', 'p5', 'comment'], $names);
28        self::assertTrue($fact->shape->complete());
29        self::assertEquals(new Known(Storage::Integer), $fact->shape->slots[0]->type);
30        self::assertEquals(new Known(Storage::Text), $fact->shape->slots[1]->type);
31        self::assertSame(Nullability::NotNull, $fact->shape->slots[1]->nullability);
32        self::assertSame(Nullability::Nullable, $fact->shape->slots[7]->nullability);
33    }
34
35    public function testFactListsTheFourColumnsOfAQueryPlanReport(): void
36    {
37        $fact = (new ExplainRows())->fact(ExplainMode::QueryPlan, Comparison::AsciiInsensitive);
38        $names = array_map(static fn (object $slot): ?string => $slot->name?->value, $fact->shape->slots);
39
40        self::assertSame(['id', 'parent', 'notused', 'detail'], $names);
41        self::assertInstanceOf(UniqueField::class, $fact->lookup('DETAIL'));
42        self::assertEquals(new Known(Storage::Text), $fact->shape->slots[3]->type);
43    }
44}
45