packages/sql-semantics-postgres/tests/Unit/Statement/Catalog/Domain/CreateDomainTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Catalog\Domain;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Domain\CreateDomain::class)]
12#[Medium]
13final class CreateDomainTest extends TestCase
14{
15 public function testRenderDropsAs(): void
16 {
17 self::assertSame('CREATE DOMAIN posint int4 CHECK (value > 0)', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE DOMAIN posint AS int4 CHECK (VALUE > 0)')->toString());
18 }
19
20 public function testDeriveStatementResolvesValue(): void
21 {
22 self::assertSame([], (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE DOMAIN posint AS int4 NOT NULL DEFAULT 1 CHECK (VALUE > 0)')->facts->diagnostics);
23 }
24
25 public function testDeriveStatementReportsAnotherColumn(): void
26 {
27 self::assertSame('Column x does not exist.', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE DOMAIN d int4 CHECK (x > 0)')->facts->diagnostics[0]->message());
28 }
29
30 public function testDeriveStatementReportsAUniqueConstraint(): void
31 {
32 self::assertSame('unique constraints not possible for domains', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE DOMAIN d int4 UNIQUE')->facts->diagnostics[0]->message());
33 }
34
35 public function testDeriveStatementReportsConflictingNullability(): void
36 {
37 self::assertSame('conflicting NULL/NOT NULL constraints', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE DOMAIN d int4 NOT NULL NULL')->facts->diagnostics[0]->message());
38 }
39
40 public function testDeriveRelationOffersAnEmptyRow(): void
41 {
42 $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE DOMAIN d int4 CHECK (VALUE > 0)');
43 $statement = $operation->statement;
44 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Domain\CreateDomain::class, $statement);
45 self::assertSame([], $operation->facts->relation($statement)->shape->slots);
46 }
47}
48