packages/sql-semantics-postgres/tests/Unit/Rules/Utility/VacuumChecksTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Utility;
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\Rules\Utility\VacuumChecks;
12use SqlSemantics\Platform\PostgreSql\Semantics;
13
14#[CoversClass(VacuumChecks::class)]
15#[Medium]
16final class VacuumChecksTest extends TestCase
17{
18    public function testCheckReportsWhatFullExcludes(): void
19    {
20        self::assertSame(
21            ['BUFFER_USAGE_LIMIT cannot be specified for VACUUM FULL', 'VACUUM FULL cannot be performed in parallel', 'VACUUM option DISABLE_PAGE_SKIPPING cannot be used with FULL', 'PROCESS_TOAST required with VACUUM FULL'],
22            array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect("VACUUM (FULL, BUFFER_USAGE_LIMIT '1MB', PARALLEL 2, DISABLE_PAGE_SKIPPING, PROCESS_TOAST off) t")->facts->diagnostics),
23        );
24    }
25
26    public function testCheckAcceptsFullWithAnalyzeAndNoParallelWorkers(): void
27    {
28        self::assertSame([], (new Semantics())->inspect("VACUUM (FULL, ANALYZE, BUFFER_USAGE_LIMIT '1MB', PARALLEL 0, DISABLE_PAGE_SKIPPING false) t (a)")->facts->diagnostics);
29    }
30
31    public function testCheckReportsColumnsWithoutAnalyze(): void
32    {
33        self::assertSame(
34            ['ANALYZE option must be specified when a column list is provided'],
35            array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect('VACUUM (ANALYZE off) t (a)')->facts->diagnostics),
36        );
37    }
38
39    public function testCheckReportsAnIndexCleanupThatIsNeitherAutoNorBoolean(): void
40    {
41        self::assertSame(
42            ['index_cleanup requires a Boolean value'],
43            array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect("VACUUM (INDEX_CLEANUP 'AUTO', INDEX_CLEANUP, INDEX_CLEANUP on, INDEX_CLEANUP sometimes)")->facts->diagnostics),
44        );
45    }
46
47    public function testCheckReportsDatabaseStatsWithTables(): void
48    {
49        self::assertSame(
50            ['ONLY_DATABASE_STATS cannot be specified with a list of tables'],
51            array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect('VACUUM (ONLY_DATABASE_STATS, FREEZE) t')->facts->diagnostics),
52        );
53    }
54
55    public function testWorkersReportsEachInvalidValue(): void
56    {
57        self::assertSame(
58            ['parallel option requires a value between 0 and 1024', 'parallel requires an integer value', 'parallel workers for vacuum must be between 0 and 1024', 'parallel workers for vacuum must be between 0 and 1024'],
59            array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect("VACUUM (PARALLEL, PARALLEL '2', PARALLEL 1025, PARALLEL -1, PARALLEL 1024)")->facts->diagnostics),
60        );
61    }
62
63    public function testCombinedReportsAnOptionDatabaseStatsExcludes(): void
64    {
65        self::assertSame(
66            ['ONLY_DATABASE_STATS cannot be specified with other VACUUM options'],
67            array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect('VACUUM (ONLY_DATABASE_STATS, SKIP_LOCKED)')->facts->diagnostics),
68        );
69    }
70
71    public function testCombinedAcceptsTheOptionsDatabaseStatsAllows(): void
72    {
73        self::assertSame([], (new Semantics())->inspect('VACUUM (ONLY_DATABASE_STATS, VERBOSE, PROCESS_MAIN, PROCESS_TOAST, INDEX_CLEANUP off, TRUNCATE, PARALLEL 2, FREEZE false)')->facts->diagnostics);
74    }
75}
76