packages/sql-semantics-postgres/tests/Unit/Rules/Table/Definition/CreationSchemasTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Table\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
15use SqlSemantics\Platform\PostgreSql\Platform;
16use SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\CreationSchemas;
17use SqlSemantics\Platform\PostgreSql\Statement\Table\Persistence;
18
19#[CoversClass(CreationSchemas::class)]
20#[Small]
21final class CreationSchemasTest extends TestCase
22{
23 public function testCheckReportsATemporaryRelationInAPermanentSchema(): void
24 {
25 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
26 (new CreationSchemas())->check($derivation, new QualifiedName(new Name('t'), new Name('public')), Persistence::Temp);
27 self::assertSame(['cannot create temporary relation in non-temporary schema'], array_map(static fn ($problem): string => $problem->message(), $derivation->facts()->diagnostics));
28 }
29
30 public function testCheckReportsAnUnloggedRelationInTheTemporarySchema(): void
31 {
32 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
33 (new CreationSchemas())->check($derivation, new QualifiedName(new Name('t'), new Name('pg_temp')), Persistence::Unlogged);
34 self::assertSame(['only temporary relations may be created in temporary schemas'], array_map(static fn ($problem): string => $problem->message(), $derivation->facts()->diagnostics));
35 }
36
37 public function testCheckAdmitsMatchingSchemasAndUnqualifiedNames(): void
38 {
39 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
40 (new CreationSchemas())->check($derivation, new QualifiedName(new Name('t'), new Name('pg_temp')), Persistence::Permanent);
41 (new CreationSchemas())->check($derivation, new QualifiedName(new Name('t'), new Name('pg_temp_3')), Persistence::Temporary);
42 (new CreationSchemas())->check($derivation, new QualifiedName(new Name('t')), Persistence::Temp);
43 (new CreationSchemas())->check($derivation, new QualifiedName(new Name('t'), new Name('public')), Persistence::Unlogged);
44 self::assertSame([], $derivation->facts()->diagnostics);
45 }
46}
47