packages/sql-semantics-sqlite/tests/Unit/Statement/Relation/IndexChoiceTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Relation;
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\Identifier\QualifiedName;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Operation;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Mutation\Delete;
15use SqlSemantics\Platform\Sqlite\Statement\Mutation\MutationTarget;
16use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
17use SqlSemantics\Platform\Sqlite\Statement\Query\Star;
18use SqlSemantics\Platform\Sqlite\Statement\Relation\IndexChoice;
19use SqlSemantics\Platform\Sqlite\Statement\Relation\TableInput;
20
21#[CoversClass(IndexChoice::class)]
22#[Medium]
23final class IndexChoiceTest extends TestCase
24{
25 public function testRenderWritesIndexedByWithTheIndexName(): void
26 {
27 $semantics = new Semantics();
28 $built = new Operation($semantics->context(), new Select([new Star()], new TableInput(new QualifiedName(new Name('t')), null, new IndexChoice(new Name('i')))));
29 $query = $semantics->analyze('select * from t indexed by i');
30
31 self::assertSame('SELECT * FROM t INDEXED BY i', $built->toString());
32 self::assertSame('SELECT * FROM t INDEXED BY i', $query->toString());
33 self::assertInstanceOf(TableInput::class, $query->singleNamedInput());
34 self::assertSame('i', $query->singleNamedInput()->index?->index?->value);
35 }
36
37 public function testRenderWritesNotIndexedWhenNoIndexIsNamed(): void
38 {
39 $semantics = new Semantics();
40 $built = new Operation($semantics->context(), new Delete(new MutationTarget(new QualifiedName(new Name('t')), null, new IndexChoice())));
41 $query = $semantics->analyze('SELECT * FROM t NOT INDEXED');
42
43 self::assertSame('DELETE FROM t NOT INDEXED', $built->toString());
44 self::assertSame('SELECT * FROM t NOT INDEXED', $query->toString());
45 self::assertInstanceOf(TableInput::class, $query->singleNamedInput());
46 self::assertNotNull($query->singleNamedInput()->index);
47 self::assertNull($query->singleNamedInput()->index->index);
48 }
49}
50