Skip to content

Commit a8348c5

Browse files
committed
Add tests for database structure #1
1 parent af81bbc commit a8348c5

File tree

9 files changed

+70
-11
lines changed

9 files changed

+70
-11
lines changed

docker/docker-compose-base.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ services:
2222
- jaeger-example
2323

2424
zookeeper1:
25-
image: confluentinc/cp-zookeeper:7.5.3
25+
image: confluentinc/cp-zookeeper:7.7.1
2626
hostname: zookeeper1
2727
container_name: zookeeper1
2828
ports:
@@ -35,7 +35,7 @@ services:
3535
- jaeger-example
3636

3737
kafka1:
38-
image: confluentinc/cp-kafka:7.5.3
38+
image: confluentinc/cp-kafka:7.7.1
3939
hostname: kafka1
4040
container_name: kafka1
4141
ports:
@@ -86,7 +86,7 @@ services:
8686

8787
postgres:
8888
container_name: postgres
89-
image: postgres:16.2
89+
image: postgres:16.4
9090
shm_size: "2gb"
9191
environment:
9292
POSTGRES_DB: "otel_demo_db"

spring-boot-2-demo-app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ dependencies {
4242
testImplementation("org.testcontainers:kafka")
4343
testImplementation("org.springframework.kafka:spring-kafka-test")
4444
testImplementation("org.awaitility:awaitility")
45+
testImplementation("io.github.mfvanek:pg-index-health-test-starter")
4546
}
4647

4748
springBoot {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.github.mfvanek.spring.boot2.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.boot2.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+
}

spring-boot-2-demo-app/src/test/java/io/github/mfvanek/spring/boot2/test/support/KafkaInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class KafkaInitializer implements ApplicationContextInitializer<Configura
1616
private static final String KAFKA_USER_NAME = "sb-ot-demo-user";
1717
private static final String KAFKA_USER_PASSWORD = "pwdForSbOtDemoApp";
1818

19-
private static final DockerImageName IMAGE_NAME = DockerImageName.parse("confluentinc/cp-kafka:7.5.3");
19+
private static final DockerImageName IMAGE_NAME = DockerImageName.parse("confluentinc/cp-kafka:7.7.1");
2020

2121
private static final KafkaContainer KAFKA_CONTAINER = new KafkaContainer(IMAGE_NAME)
2222
.withEnv("KAFKA_LISTENER_SECURITY_PROTOCOL_MAP", "PLAINTEXT:SASL_PLAINTEXT,BROKER:PLAINTEXT")

spring-boot-2-demo-app/src/test/java/io/github/mfvanek/spring/boot2/test/support/PostgresInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public class PostgresInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
1212

13-
private static final DockerImageName IMAGE = DockerImageName.parse("postgres:16.2");
13+
private static final DockerImageName IMAGE = DockerImageName.parse("postgres:16.4");
1414
private static final Network NETWORK = Network.newNetwork();
1515
private static final PostgreSQLContainer<?> CONTAINER = new PostgreSQLContainer<>(IMAGE);
1616

spring-boot-3-demo-app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dependencies {
3434
testImplementation("org.testcontainers:kafka")
3535
testImplementation("org.springframework.kafka:spring-kafka-test")
3636
testImplementation("org.awaitility:awaitility")
37+
testImplementation("io.github.mfvanek:pg-index-health-test-starter")
3738
}
3839

3940
springBoot {

spring-boot-3-demo-app/src/test/java/io/github/mfvanek/spring/boot3/test/controllers/TimeControllerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class TimeControllerTest extends TestBase {
4747
@Autowired
4848
private Clock clock;
4949
@Autowired
50-
private NamedParameterJdbcTemplate jdbcTemplate;
50+
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
5151

5252
@BeforeAll
5353
void setUpKafkaConsumer() {
@@ -64,7 +64,7 @@ void tearDownKafkaConsumer() {
6464

6565
@BeforeEach
6666
void cleanUpDatabase() {
67-
jdbcTemplate.getJdbcTemplate().execute("truncate table otel_demo.storage");
67+
jdbcTemplate.execute("truncate table otel_demo.storage");
6868
}
6969

7070
@SneakyThrows
@@ -111,14 +111,14 @@ void spanShouldBeReportedInLogs(@Nonnull final CapturedOutput output) {
111111
.until(() -> countRecordsInTable() >= 1L);
112112
assertThat(output.getAll())
113113
.contains("Received record: " + received.value() + " with traceId " + traceId);
114-
final String messageFromDb = jdbcTemplate.queryForObject("select message from otel_demo.storage where trace_id = :traceId",
114+
final String messageFromDb = namedParameterJdbcTemplate.queryForObject("select message from otel_demo.storage where trace_id = :traceId",
115115
Map.of("traceId", traceId), String.class);
116116
assertThat(messageFromDb)
117117
.isEqualTo(received.value());
118118
}
119119

120120
private long countRecordsInTable() {
121-
final Long queryResult = jdbcTemplate.getJdbcTemplate().queryForObject("select count(*) from otel_demo.storage", Long.class);
121+
final Long queryResult = jdbcTemplate.queryForObject("select count(*) from otel_demo.storage", Long.class);
122122
return Objects.requireNonNullElse(queryResult, 0L);
123123
}
124124
}

spring-boot-3-demo-app/src/test/java/io/github/mfvanek/spring/boot3/test/support/KafkaInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class KafkaInitializer implements ApplicationContextInitializer<Configura
1616
private static final String KAFKA_USER_NAME = "sb-ot-demo-user";
1717
private static final String KAFKA_USER_PASSWORD = "pwdForSbOtDemoApp";
1818

19-
private static final DockerImageName IMAGE_NAME = DockerImageName.parse("confluentinc/cp-kafka:7.5.3");
19+
private static final DockerImageName IMAGE_NAME = DockerImageName.parse("confluentinc/cp-kafka:7.7.1");
2020

2121
private static final KafkaContainer KAFKA_CONTAINER = new KafkaContainer(IMAGE_NAME)
2222
.withEnv("KAFKA_LISTENER_SECURITY_PROTOCOL_MAP", "PLAINTEXT:SASL_PLAINTEXT,BROKER:PLAINTEXT")

spring-boot-3-demo-app/src/test/java/io/github/mfvanek/spring/boot3/test/support/PostgresInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public class PostgresInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
1212

13-
private static final DockerImageName IMAGE = DockerImageName.parse("postgres:16.2");
13+
private static final DockerImageName IMAGE = DockerImageName.parse("postgres:16.4");
1414
private static final Network NETWORK = Network.newNetwork();
1515
private static final PostgreSQLContainer<?> CONTAINER = new PostgreSQLContainer<>(IMAGE);
1616

0 commit comments

Comments
 (0)