packages/ztd-query-postgres/tests/Unit/Schema/Reflection/Column/ColumnDefinitionSqlTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Schema\Reflection\Column;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9
10#[CoversClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Column\ColumnDefinitionSql::class)]
11#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\PgSqlIdentifierQuoter::class)]
12#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Column\NativeTypeSql::class)]
13final class ColumnDefinitionSqlTest extends TestCase
14{
15 public function testBuildColumnDefinition(): void
16 {
17 self::assertSame('"id" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY', (new \ZtdQuery\Platform\Postgres\Schema\Reflection\Column\ColumnDefinitionSql())->buildColumnDefinition(['column_name' => 'id', 'data_type' => 'integer', 'is_nullable' => 'NO', 'is_identity' => 'YES', 'identity_generation' => 'ALWAYS']));
18
19 self::assertSame('"total" NUMERIC(10,2) GENERATED ALWAYS AS (price * quantity) STORED', (new \ZtdQuery\Platform\Postgres\Schema\Reflection\Column\ColumnDefinitionSql())->buildColumnDefinition(['column_name' => 'total', 'data_type' => 'numeric', 'numeric_precision' => 10, 'numeric_scale' => 2, 'is_generated' => 'ALWAYS', 'generation_expression' => 'price * quantity']));
20
21 self::assertSame('"label" VARCHAR(32) DEFAULT \'guest\'::text', (new \ZtdQuery\Platform\Postgres\Schema\Reflection\Column\ColumnDefinitionSql())->buildColumnDefinition(['column_name' => 'label', 'data_type' => 'character varying', 'character_maximum_length' => 32, 'column_default' => "'guest'::text"]));
22 }
23
24 public function testDomainTypeSql(): void
25 {
26 self::assertSame('"app"."positive_int"', (new \ZtdQuery\Platform\Postgres\Schema\Reflection\Column\ColumnDefinitionSql())->domainTypeSql(['domain_schema' => 'app', 'domain_name' => 'positive_int']));
27
28 self::assertSame(null, (new \ZtdQuery\Platform\Postgres\Schema\Reflection\Column\ColumnDefinitionSql())->domainTypeSql([]));
29 }
30 public function testGenerationClausePrefersStoredExpressionsOverIdentityOrDefaults(): void
31 {
32 $column = new \ZtdQuery\Platform\Postgres\Schema\Reflection\Column\ColumnDefinitionSql();
33 self::assertSame(' GENERATED ALWAYS AS (a + b) STORED', $column->generationClause(['is_generated' => 'ALWAYS', 'generation_expression' => 'a + b', 'is_identity' => 'YES', 'column_default' => '5']));
34 self::assertSame(' DEFAULT 5', $column->generationClause(['column_default' => '5']));
35 }
36}
37