packages/ztd-query-sqlite/tests/Unit/Schema/Create/VirtualTableParserTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Schema\Create;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use ZtdQuery\Platform\Sqlite\Schema\Create\VirtualTableParser;
11
12#[CoversClass(VirtualTableParser::class)]
13#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Dml\Expression\AssignmentColumnParser::class)]
14#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Dml\Expression\AssignmentParser::class)]
15#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Dml\Expression\ValueListParser::class)]
16#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Dml\Insert\InsertClauseParser::class)]
17#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Lexing\ExpressionSpan::class)]
18#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Lexing\IdentifierDecoder::class)]
19#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Lexing\LiteralMasker::class)]
20#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Lexing\OpaqueSqlSpan::class)]
21#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Lexing\QuotedSpan::class)]
22#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Lexing\TopLevelKeywordScanner::class)]
23#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Relation\RelationSourceParser::class)]
24#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Statement\StatementClassifier::class)]
25#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Statement\StatementStructure::class)]
26#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Statement\TargetTableParser::class)]
27#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Dml\Update\UpdateClauseParser::class)]
28#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\SqliteLexerProfile::class)]
29#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\SqliteLexicalMasker::class)]
30#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\SqliteParser::class)]
31#[UsesClass(\ZtdQuery\Platform\Sqlite\Sql\Relation\SqliteSelectRelationParser::class)]
32final class VirtualTableParserTest extends TestCase
33{
34    public function testParseFts5VirtualTableKeepsColumnsAndSkipsModuleOptions(): void
35    {
36        $parser = new VirtualTableParser();
37        $definition = $parser->parseFts5VirtualTable("CREATE VIRTUAL TABLE docs USING fts5(title, body UNINDEXED, tokenize='unicode61')");
38        self::assertNotNull($definition);
39        self::assertSame(['title', 'body'], $definition->columns);
40        self::assertSame(['title' => 'TEXT', 'body' => 'TEXT'], $definition->columnTypes);
41        self::assertNull($parser->parseFts5VirtualTable("CREATE VIRTUAL TABLE docs USING fts5(tokenize='unicode61')"));
42        self::assertNull($parser->parseFts5VirtualTable('CREATE VIRTUAL TABLE docs USING fts5(body INVALID)'));
43        self::assertNull($parser->parseFts5VirtualTable('CREATE TABLE docs(body TEXT)'));
44    }
45
46    public function testBodyRequiresOneFts5ModuleAndCompleteFraming(): void
47    {
48        $parser = new VirtualTableParser();
49        self::assertSame('title, body', $parser->body('CREATE VIRTUAL TABLE docs USING fts5(title, body);'));
50        self::assertNull($parser->body('CREATE VIRTUAL TABLE docs USING fts4(body)'));
51        self::assertNull($parser->body('CREATE VIRTUAL TABLE docs USING fts5(body) trailing'));
52        self::assertNull($parser->body('CREATE VIRTUAL TABLE docs USING fts5(body'));
53        self::assertNull($parser->body('CREATE VIRTUAL TABLE docs'));
54        self::assertNull($parser->body('CREATE VIRTUAL TABLE docs USING fts5(body) USING fts5(x)'));
55    }
56
57}
58