packages/sql-semantics-postgres/tests/Unit/Statement/Table/CreateForeignTableTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Table;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Table\CreateForeignTable::class)]
12#[Medium]
13final class CreateForeignTableTest extends TestCase
14{
15 public function testDeriveStatementDeclaresThePartition(): 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('CREATE FOREIGN TABLE f PARTITION OF t FOR VALUES IN (1) SERVER s', $context);
21 self::assertSame([
22 0 => 'a integer NotNull',
23 1 => 'b integer Nullable',
24 2 => 'c text Nullable',
25 ], array_map(static fn ($column): string => $column->name->value . ' ' . $column->type->name() . ' ' . $column->nullability->name, $statement->declarations()[0]->columns));
26 }
27
28 public function testDeriveRelationAnswersTheNewTable(): void
29 {
30 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
31 $statement = $semantics->inspect('CREATE FOREIGN TABLE f (a int) SERVER s', []);
32 $n1 = $statement->statement;
33 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\CreateForeignTable::class, $n1);
34 self::assertSame(1, count($n1->deriveRelation(new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($statement->context), new \SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment($statement->context))->shape->slots));
35 }
36
37 public function testRenderWritesTheClauseAsWritten(): void
38 {
39 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
40 $statement = $semantics->inspect('CREATE FOREIGN TABLE IF NOT EXISTS f (a int OPTIONS (x \'y\') NOT NULL) INHERITS (p) SERVER s OPTIONS (t \'u\')', []);
41 self::assertSame('CREATE FOREIGN TABLE IF NOT EXISTS f (a INT OPTIONS (x \'y\') NOT NULL) INHERITS (p) SERVER s OPTIONS (t \'u\')', $statement->toString());
42 }
43}
44