packages/sql-semantics-postgres/tests/Unit/Statement/Table/Partition/AttachPartitionTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Table\Partition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Table\Partition\AttachPartition::class)]
12#[Medium]
13final class AttachPartitionTest extends TestCase
14{
15    public function testDeriveClauseResolvesThePartition(): void
16    {
17        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18        $context = [];
19        array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
20        $statement = $semantics->inspect('ALTER TABLE p ATTACH PARTITION t DEFAULT', $context);
21        $n1 = $statement->statement;
22        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\AlterTable::class, $n1);
23        $n2 = $n1->commands[0];
24        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Partition\AttachPartition::class, $n2);
25        $n3 = $statement->facts->relation($n2->partition)->table;
26        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\DeclaredTable::class, $n3);
27        self::assertSame(true, $n3->table === $context[0]);
28    }
29
30    public function testRenderWritesTheClauseAsWritten(): void
31    {
32        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
33        $statement = $semantics->inspect('ALTER TABLE p ATTACH PARTITION p1 FOR VALUES FROM (1) TO (10)', []);
34        self::assertSame('ALTER TABLE p ATTACH PARTITION p1 FOR VALUES FROM (1) TO (10)', $statement->toString());
35    }
36}
37