packages/sql-semantics-postgres/tests/Unit/Statement/Utility/Maintenance/ReindexTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Utility\Maintenance;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\UndeclaredTable;
12use SqlSemantics\Platform\PostgreSql\Semantics;
13use SqlSemantics\Platform\PostgreSql\Statement\Utility\Maintenance\Reindex;
14
15#[CoversClass(Reindex::class)]
16#[Medium]
17final class ReindexTest extends TestCase
18{
19    public function testDeriveStatementResolvesTheTableOfReindexTable(): void
20    {
21        $operation = (new Semantics())->inspect('REINDEX TABLE s.t');
22        self::assertInstanceOf(Reindex::class, $operation->statement);
23        self::assertInstanceOf(UndeclaredTable::class, $operation->facts->relation($operation->statement)->table);
24    }
25
26    public function testDeriveStatementReportsSystemCatalogsRebuiltConcurrently(): void
27    {
28        $semantics = new Semantics();
29        self::assertSame(
30            [['cannot reindex system catalogs concurrently'], ['cannot reindex system catalogs concurrently'], []],
31            [
32                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('REINDEX SYSTEM CONCURRENTLY')->facts->diagnostics),
33                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('REINDEX (CONCURRENTLY) SYSTEM d')->facts->diagnostics),
34                array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('REINDEX (CONCURRENTLY false) SYSTEM')->facts->diagnostics),
35            ],
36        );
37    }
38
39    public function testDeriveStatementReportsATablespaceWithoutAName(): void
40    {
41        self::assertSame(['tablespace requires a parameter'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect('REINDEX (TABLESPACE) INDEX i')->facts->diagnostics));
42    }
43
44    public function testRenderWritesEachTarget(): void
45    {
46        $semantics = new Semantics();
47        self::assertSame(
48            ['REINDEX INDEX CONCURRENTLY s.i', 'REINDEX (verbose, tablespace x) SCHEMA "S"', 'REINDEX DATABASE', 'REINDEX SYSTEM d'],
49            [$semantics->inspect('REINDEX INDEX CONCURRENTLY s.i')->toString(), $semantics->inspect('REINDEX (VERBOSE, TABLESPACE x) SCHEMA "S"')->toString(), $semantics->inspect('REINDEX DATABASE')->toString(), $semantics->inspect('REINDEX SYSTEM d')->toString()],
50        );
51    }
52
53    public function testDeriveStatementReportsASequence(): void
54    {
55        $semantics = new Semantics();
56        $context = [$semantics->inspect('CREATE SEQUENCE s')];
57        self::assertSame(['"s" is not a table or materialized view'], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('REINDEX TABLE s', $context)->facts->diagnostics));
58    }
59}
60