|
| 1 | +/* |
| 2 | + * Copyright (c) 2019-2026. Ivan Vakhrushev and others. |
| 3 | + * https://github.com/mfvanek/pg-index-health |
| 4 | + * |
| 5 | + * This file is a part of "pg-index-health" - an embeddable schema linter for PostgreSQL |
| 6 | + * that detects common anti-patterns and promotes best practices. |
| 7 | + * |
| 8 | + * Licensed under the Apache License 2.0 |
| 9 | + */ |
| 10 | + |
| 11 | +package io.github.mfvanek.pg.core.checks.host; |
| 12 | + |
| 13 | +import io.github.mfvanek.pg.core.checks.common.DatabaseCheckOnHost; |
| 14 | +import io.github.mfvanek.pg.core.checks.common.Diagnostic; |
| 15 | +import io.github.mfvanek.pg.core.fixtures.support.DatabaseAwareTestBase; |
| 16 | +import io.github.mfvanek.pg.core.fixtures.support.DatabasePopulator; |
| 17 | +import io.github.mfvanek.pg.model.column.Column; |
| 18 | +import io.github.mfvanek.pg.model.context.PgContext; |
| 19 | +import io.github.mfvanek.pg.model.table.Table; |
| 20 | +import io.github.mfvanek.pg.model.table.TableWithColumns; |
| 21 | +import org.jspecify.annotations.NonNull; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.ValueSource; |
| 25 | + |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import static io.github.mfvanek.pg.core.support.AbstractCheckOnHostAssert.assertThat; |
| 29 | + |
| 30 | +class TablesWithIncrementingColumnsCheckOnHostTest extends DatabaseAwareTestBase { |
| 31 | + |
| 32 | + private final DatabaseCheckOnHost<@NonNull TableWithColumns> check = new TablesWithIncrementingColumnsCheckOnHost(getPgConnection()); |
| 33 | + |
| 34 | + @Test |
| 35 | + void shouldSatisfyContract() { |
| 36 | + assertThat(check) |
| 37 | + .hasType(TableWithColumns.class) |
| 38 | + .hasDiagnostic(Diagnostic.TABLES_WITH_INCREMENTING_COLUMNS) |
| 39 | + .hasHost(getHost()) |
| 40 | + .isStatic(); |
| 41 | + } |
| 42 | + |
| 43 | + @ParameterizedTest |
| 44 | + @ValueSource(strings = {PgContext.DEFAULT_SCHEMA_NAME, "custom"}) |
| 45 | + void onDatabaseWithThem(final String schemaName) { |
| 46 | + executeTestOnDatabase(schemaName, DatabasePopulator::withIncrementingColumns, ctx -> |
| 47 | + assertThat(check) |
| 48 | + .executing(ctx) |
| 49 | + .hasSize(1) |
| 50 | + .usingRecursiveFieldByFieldElementComparator() |
| 51 | + .containsExactly( |
| 52 | + TableWithColumns.of( |
| 53 | + Table.of(ctx, "orders", 8_192L), |
| 54 | + List.of( |
| 55 | + Column.ofNullable(ctx, "orders", "phone1"), |
| 56 | + Column.ofNullable(ctx, "orders", "phone2"), |
| 57 | + Column.ofNullable(ctx, "orders", "address1"), |
| 58 | + Column.ofNullable(ctx, "orders", "address2"), |
| 59 | + Column.ofNullable(ctx, "orders", "address3") |
| 60 | + ) |
| 61 | + ) |
| 62 | + )); |
| 63 | + } |
| 64 | + |
| 65 | + @ParameterizedTest |
| 66 | + @ValueSource(strings = {PgContext.DEFAULT_SCHEMA_NAME, "custom"}) |
| 67 | + void shouldWorkWithPartitionedTables(final String schemaName) { |
| 68 | + executeTestOnDatabase(schemaName, DatabasePopulator::withIncrementingColumnsInPartitionedTable, ctx -> |
| 69 | + assertThat(check) |
| 70 | + .executing(ctx) |
| 71 | + .hasSize(1) |
| 72 | + .usingRecursiveFieldByFieldElementComparator() |
| 73 | + .containsExactly( |
| 74 | + TableWithColumns.of( |
| 75 | + Table.of(ctx, "events"), |
| 76 | + List.of( |
| 77 | + Column.ofNullable(ctx, "events", "tag1"), |
| 78 | + Column.ofNullable(ctx, "events", "tag2") |
| 79 | + ) |
| 80 | + ) |
| 81 | + )); |
| 82 | + } |
| 83 | +} |
0 commit comments