packages/sql-semantics-postgres/tests/Unit/Statement/Utility/Maintenance/VacuumTest.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\Vacuum;
14
15#[CoversClass(Vacuum::class)]
16#[Medium]
17final class VacuumTest extends TestCase
18{
19 public function testDeriveStatementResolvesEachTable(): void
20 {
21 $operation = (new Semantics())->inspect('VACUUM a, s.b');
22 self::assertInstanceOf(Vacuum::class, $operation->statement);
23 self::assertInstanceOf(UndeclaredTable::class, $operation->facts->relation($operation->statement->targets[0])->table);
24 }
25
26 public function testDeriveStatementReportsColumnsWithoutAnalyze(): void
27 {
28 self::assertSame(['ANALYZE option must be specified when a column list is provided'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect('VACUUM FULL t (a)')->facts->diagnostics));
29 }
30
31 public function testDeriveStatementReportsAnUnknownOption(): void
32 {
33 self::assertSame(['unrecognized VACUUM option "fast"'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect('VACUUM (fast) t')->facts->diagnostics));
34 }
35
36 public function testRenderWritesEachSyntax(): void
37 {
38 $semantics = new Semantics();
39 self::assertSame(
40 ['VACUUM', 'VACUUM FREEZE ANALYZE t (a)', 'VACUUM (full FALSE, index_cleanup auto) t, u'],
41 [$semantics->inspect('VACUUM')->toString(), $semantics->inspect('VACUUM FREEZE ANALYZE t (a)')->toString(), $semantics->inspect('VACUUM (FULL false, INDEX_CLEANUP auto) t, u')->toString()],
42 );
43 }
44}
45