packages/ztd-query-postgres/tests/Unit/Schema/Reflection/Key/IndexRowsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Schema\Reflection\Key;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9
10#[CoversClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Key\IndexRows::class)]
11#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Key\IndexDefinitions::class)]
12final class IndexRowsTest extends TestCase
13{
14    public function testAppendAccumulatesCompositeAndPartialIndexes(): void
15    {
16        $rows = new \ZtdQuery\Platform\Postgres\Schema\Reflection\Key\IndexRows();
17        $rows->append(['constraint_name' => 'unique_pair', 'column_name' => 'tenant']);
18        $rows->append(['constraint_name' => 'unique_pair', 'column_name' => 'id']);
19        $rows->append(['constraint_name' => 'active_email', 'column_name' => 'email', 'predicate' => 'active']);
20        $definitions = $rows->definitions();
21        self::assertSame(['CONSTRAINT "unique_pair" UNIQUE ("tenant", "id")'], $definitions->sql);
22        self::assertSame(['email'], $definitions->partialIndexes['active_email']->columns);
23        self::assertSame('active', $definitions->partialIndexes['active_email']->predicate);
24    }
25
26    public function testDefinitionsExcludesAnEntireExpressionIndex(): void
27    {
28        $rows = new \ZtdQuery\Platform\Postgres\Schema\Reflection\Key\IndexRows();
29        $rows->append(['constraint_name' => 'expression_index', 'column_name' => 'id']);
30        $rows->append(['constraint_name' => 'expression_index', 'column_name' => null]);
31        $rows->append(['constraint_name' => '', 'column_name' => 'ignored']);
32        self::assertSame([], $rows->definitions()->sql);
33        self::assertSame([], $rows->definitions()->partialIndexes);
34    }
35}
36