|
| 1 | +package io.github.mfvanek.spring.boot3.test; |
| 2 | + |
| 3 | +import io.github.mfvanek.pg.common.maintenance.DatabaseCheckOnHost; |
| 4 | +import io.github.mfvanek.pg.common.maintenance.Diagnostic; |
| 5 | +import io.github.mfvanek.pg.model.DbObject; |
| 6 | +import io.github.mfvanek.pg.model.column.Column; |
| 7 | +import io.github.mfvanek.pg.model.table.Table; |
| 8 | +import io.github.mfvanek.spring.boot3.test.support.TestBase; |
| 9 | +import org.junit.jupiter.api.DisplayName; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.assertj.core.api.InstanceOfAssertFactories.list; |
| 17 | + |
| 18 | +class IndexesMaintenanceTest extends TestBase { |
| 19 | + |
| 20 | + @Autowired |
| 21 | + private List<DatabaseCheckOnHost<? extends DbObject>> checks; |
| 22 | + |
| 23 | + @Test |
| 24 | + @DisplayName("Always check PostgreSQL version in your tests") |
| 25 | + void checkPostgresVersion() { |
| 26 | + final String pgVersion = jdbcTemplate.queryForObject("select version();", String.class); |
| 27 | + assertThat(pgVersion) |
| 28 | + .startsWith("PostgreSQL 16.4"); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void databaseStructureCheckForPublicSchema() { |
| 33 | + assertThat(checks) |
| 34 | + .hasSameSizeAs(Diagnostic.values()); |
| 35 | + |
| 36 | + checks.forEach(check -> { |
| 37 | + switch (check.getDiagnostic()) { |
| 38 | + case TABLES_WITHOUT_PRIMARY_KEY, TABLES_WITHOUT_DESCRIPTION -> assertThat(check.check()) |
| 39 | + .asInstanceOf(list(Table.class)) |
| 40 | + .hasSize(1) |
| 41 | + .containsExactly(Table.of("databasechangelog", 0L)); |
| 42 | + |
| 43 | + case COLUMNS_WITHOUT_DESCRIPTION -> assertThat(check.check()) |
| 44 | + .asInstanceOf(list(Column.class)) |
| 45 | + .hasSize(14) |
| 46 | + .allSatisfy(column -> assertThat(column.getTableName()).isEqualTo("databasechangelog")); |
| 47 | + |
| 48 | + case TABLES_WITH_MISSING_INDEXES -> assertThat(check.check()) |
| 49 | + .hasSizeLessThanOrEqualTo(1); // TODO skip runtime checks after https://github.com/mfvanek/pg-index-health/issues/456 |
| 50 | + |
| 51 | + default -> assertThat(check.check()) |
| 52 | + .as(check.getDiagnostic().name()) |
| 53 | + .isEmpty(); |
| 54 | + } |
| 55 | + }); |
| 56 | + } |
| 57 | +} |
0 commit comments