Skip to content

Commit

Permalink
Added Pg11 support in unit tests (#7)
Browse files Browse the repository at this point in the history
* Added Pg11 into unit testing

* Code cleanup and transactions

* Added some tests to improve coverage
  • Loading branch information
mfvanek authored Dec 7, 2019
1 parent 7908aa9 commit c541c41
Show file tree
Hide file tree
Showing 16 changed files with 324 additions and 89 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
**pg-index-health** is a Java library for analyzing and maintaining indexes health in [Postgresql](https://www.postgresql.org/) databases.

## Supported PostgreSQL versions
* 9.6
* 10
* 11

## Available checks
**pg-index-health** allows you to detect the following problems:
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ dependencies {
testImplementation 'org.hamcrest:hamcrest:2.2'
testImplementation 'ch.qos.logback:logback-classic:1.2.3'
testImplementation 'org.mockito:mockito-core:3.2.0'
testImplementation 'io.zonky.test:embedded-postgres:1.2.6'
testImplementation enforcedPlatform('io.zonky.test.postgres:embedded-postgres-binaries-bom:11.5.0')
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.math.BigDecimal;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -43,6 +45,9 @@ void equalsAndHashCode() {
final var theSame = PgConnectionImpl.ofMaster(embeddedPostgres.getTestDatabase());
final var second = PgConnectionImpl.of(embeddedPostgres.getTestDatabase(), PgHostImpl.ofName("second"));

assertNotEquals(first, null);
assertNotEquals(first, BigDecimal.ZERO);

assertEquals(first, first);
assertEquals(first.hashCode(), first.hashCode());

Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/mfvanek/pg/connection/PgHostImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -66,6 +68,9 @@ void equalsAndHashCode() {
final var withDifferentHostsOrder = PgHostImpl.ofUrl("jdbc:postgresql://host-2:5432,host-1:4432,host-4:3432,host-3:2432/db_name?ssl=true&sslmode=require");
final var second = PgHostImpl.ofMaster();

assertNotEquals(first, null);
assertNotEquals(first, BigDecimal.ZERO);

assertEquals(first, first);
assertEquals(first.hashCode(), first.hashCode());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@
import com.mfvanek.pg.index.maintenance.IndexMaintenanceFactoryImpl;
import com.mfvanek.pg.model.IndexWithSize;
import com.mfvanek.pg.model.UnusedIndex;
import com.mfvanek.pg.utils.DatabaseAwareTestBase;
import com.mfvanek.pg.utils.DatabasePopulator;
import com.mfvanek.pg.utils.TestExecutor;
import com.opentable.db.postgres.junit5.EmbeddedPostgresExtension;
import com.opentable.db.postgres.junit5.PreparedDbExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import javax.annotation.Nonnull;
import java.sql.SQLException;
import java.util.function.Consumer;
import javax.sql.DataSource;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toSet;
Expand All @@ -31,17 +27,16 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class IndexesHealthImplTest {
abstract class IndexesHealthImplTestBase extends DatabaseAwareTestBase {

@RegisterExtension
static final PreparedDbExtension embeddedPostgres =
EmbeddedPostgresExtension.preparedDatabase(ds -> {
});
private final IndexesHealth indexesHealth;

private final HighAvailabilityPgConnection haPgConnection = HighAvailabilityPgConnectionImpl.of(
PgConnectionImpl.ofMaster(embeddedPostgres.getTestDatabase()));
private final IndexesHealth indexesHealth = new IndexesHealthImpl(haPgConnection,
new IndexMaintenanceFactoryImpl());
IndexesHealthImplTestBase(@Nonnull final DataSource dataSource) {
super(dataSource);
final HighAvailabilityPgConnection haPgConnection = HighAvailabilityPgConnectionImpl.of(
PgConnectionImpl.ofMaster(dataSource));
this.indexesHealth = new IndexesHealthImpl(haPgConnection, new IndexMaintenanceFactoryImpl());
}

@Test
void getInvalidIndexesOnEmptyDatabase() {
Expand All @@ -51,7 +46,7 @@ void getInvalidIndexesOnEmptyDatabase() {
}

@Test
void getInvalidIndexesOnDatabaseWithoutThem() throws SQLException {
void getInvalidIndexesOnDatabaseWithoutThem() {
executeTestOnDatabase(DatabasePopulator::populateOnlyTablesAndReferences,
() -> {
final var invalidIndexes = indexesHealth.getInvalidIndexes();
Expand All @@ -61,7 +56,7 @@ void getInvalidIndexesOnDatabaseWithoutThem() throws SQLException {
}

@Test
void getInvalidIndexesOnDatabaseWithThem() throws SQLException {
void getInvalidIndexesOnDatabaseWithThem() {
executeTestOnDatabase(databasePopulator -> {
databasePopulator.populateWithDataAndReferences();
databasePopulator.createInvalidIndex();
Expand All @@ -84,7 +79,7 @@ void getDuplicatedIndexesOnEmptyDatabase() {
}

@Test
void getDuplicatedIndexesOnDatabaseWithoutThem() throws SQLException {
void getDuplicatedIndexesOnDatabaseWithoutThem() {
executeTestOnDatabase(DatabasePopulator::populateOnlyTablesAndReferences,
() -> {
final var duplicatedIndexes = indexesHealth.getDuplicatedIndexes();
Expand All @@ -94,7 +89,7 @@ void getDuplicatedIndexesOnDatabaseWithoutThem() throws SQLException {
}

@Test
void getDuplicatedIndexesOnDatabaseWithThem() throws SQLException {
void getDuplicatedIndexesOnDatabaseWithThem() {
executeTestOnDatabase(databasePopulator -> {
databasePopulator.populateWithDataAndReferences();
databasePopulator.createDuplicatedIndex();
Expand Down Expand Up @@ -123,7 +118,7 @@ void getIntersectedIndexesOnEmptyDatabase() {
}

@Test
void getIntersectedIndexesOnDatabaseWithoutThem() throws SQLException {
void getIntersectedIndexesOnDatabaseWithoutThem() {
executeTestOnDatabase(DatabasePopulator::populateOnlyTablesAndReferences,
() -> {
final var intersectedIndexes = indexesHealth.getIntersectedIndexes();
Expand All @@ -133,7 +128,7 @@ void getIntersectedIndexesOnDatabaseWithoutThem() throws SQLException {
}

@Test
void getIntersectedIndexesOnDatabaseWithThem() throws SQLException {
void getIntersectedIndexesOnDatabaseWithThem() {
executeTestOnDatabase(databasePopulator -> {
databasePopulator.populateWithDataAndReferences();
databasePopulator.createDuplicatedIndex();
Expand Down Expand Up @@ -162,7 +157,7 @@ void getUnusedIndexesOnEmptyDatabase() {
}

@Test
void getUnusedIndexesOnDatabaseWithoutThem() throws SQLException {
void getUnusedIndexesOnDatabaseWithoutThem() {
executeTestOnDatabase(DatabasePopulator::populateOnlyTablesAndReferences,
() -> {
final var unusedIndexes = indexesHealth.getUnusedIndexes();
Expand All @@ -172,7 +167,7 @@ void getUnusedIndexesOnDatabaseWithoutThem() throws SQLException {
}

@Test
void getUnusedIndexesOnDatabaseWithThem() throws SQLException {
void getUnusedIndexesOnDatabaseWithThem() {
executeTestOnDatabase(databasePopulator -> {
databasePopulator.populateWithDataAndReferences();
databasePopulator.createDuplicatedIndex();
Expand All @@ -194,7 +189,7 @@ void getForeignKeysNotCoveredWithIndexOnEmptyDatabase() {
}

@Test
void getForeignKeysNotCoveredWithIndexOnDatabaseWithoutThem() throws SQLException {
void getForeignKeysNotCoveredWithIndexOnDatabaseWithoutThem() {
executeTestOnDatabase(DatabasePopulator::populateOnlyTables,
() -> {
final var foreignKeys = indexesHealth.getForeignKeysNotCoveredWithIndex();
Expand All @@ -204,7 +199,7 @@ void getForeignKeysNotCoveredWithIndexOnDatabaseWithoutThem() throws SQLExceptio
}

@Test
void getForeignKeysNotCoveredWithIndexOnDatabaseWithThem() throws SQLException {
void getForeignKeysNotCoveredWithIndexOnDatabaseWithThem() {
executeTestOnDatabase(DatabasePopulator::populateOnlyTablesAndReferences,
() -> {
var foreignKeys = indexesHealth.getForeignKeysNotCoveredWithIndex();
Expand All @@ -217,7 +212,7 @@ void getForeignKeysNotCoveredWithIndexOnDatabaseWithThem() throws SQLException {
}

@Test
void getForeignKeysNotCoveredWithIndexOnDatabaseWithNotSuitableIndex() throws SQLException {
void getForeignKeysNotCoveredWithIndexOnDatabaseWithNotSuitableIndex() {
executeTestOnDatabase(databasePopulator -> {
databasePopulator.populateOnlyTablesAndReferences();
databasePopulator.createNotSuitableIndexForForeignKey();
Expand All @@ -233,7 +228,7 @@ void getForeignKeysNotCoveredWithIndexOnDatabaseWithNotSuitableIndex() throws SQ
}

@Test
void getForeignKeysNotCoveredWithIndexOnDatabaseWithSuitableIndex() throws SQLException {
void getForeignKeysNotCoveredWithIndexOnDatabaseWithSuitableIndex() {
executeTestOnDatabase(databasePopulator -> {
databasePopulator.populateOnlyTablesAndReferences();
databasePopulator.createSuitableIndexForForeignKey();
Expand All @@ -253,7 +248,7 @@ void getTablesWithMissingIndexesOnEmptyDatabase() {
}

@Test
void getTablesWithMissingIndexesOnDatabaseWithoutThem() throws SQLException {
void getTablesWithMissingIndexesOnDatabaseWithoutThem() {
executeTestOnDatabase(DatabasePopulator::populateWithDataAndReferences,
() -> {
final var tables = indexesHealth.getTablesWithMissingIndexes();
Expand All @@ -263,7 +258,7 @@ void getTablesWithMissingIndexesOnDatabaseWithoutThem() throws SQLException {
}

@Test
void getTablesWithMissingIndexesOnDatabaseWithThem() throws SQLException {
void getTablesWithMissingIndexesOnDatabaseWithThem() {
executeTestOnDatabase(databasePopulator -> {
databasePopulator.populateWithDataAndReferences();
databasePopulator.tryToFindAccountByClientId(101);
Expand All @@ -287,7 +282,7 @@ void getTablesWithoutPrimaryKeyOnEmptyDatabase() {
}

@Test
void getTablesWithoutPrimaryKeyOnDatabaseWithoutThem() throws SQLException {
void getTablesWithoutPrimaryKeyOnDatabaseWithoutThem() {
executeTestOnDatabase(DatabasePopulator::populateWithDataAndReferences,
() -> {
final var tables = indexesHealth.getTablesWithoutPrimaryKey();
Expand All @@ -297,7 +292,7 @@ void getTablesWithoutPrimaryKeyOnDatabaseWithoutThem() throws SQLException {
}

@Test
void getTablesWithoutPrimaryKeyOnDatabaseWithThem() throws SQLException {
void getTablesWithoutPrimaryKeyOnDatabaseWithThem() {
executeTestOnDatabase(databasePopulator -> {
databasePopulator.populateWithDataAndReferences();
databasePopulator.createTableWithoutPrimaryKey();
Expand All @@ -319,7 +314,7 @@ void getIndexesWithNullValuesOnEmptyDatabase() {
}

@Test
void getIndexesWithNullValuesOnDatabaseWithoutThem() throws SQLException {
void getIndexesWithNullValuesOnDatabaseWithoutThem() {
executeTestOnDatabase(DatabasePopulator::populateWithDataAndReferences,
() -> {
final var indexes = indexesHealth.getIndexesWithNullValues();
Expand All @@ -329,7 +324,7 @@ void getIndexesWithNullValuesOnDatabaseWithoutThem() throws SQLException {
}

@Test
void getIndexesWithNullValuesOnDatabaseWithThem() throws SQLException {
void getIndexesWithNullValuesOnDatabaseWithThem() {
executeTestOnDatabase(databasePopulator -> {
databasePopulator.populateWithDataAndReferences();
databasePopulator.createIndexWithNulls();
Expand All @@ -340,13 +335,4 @@ void getIndexesWithNullValuesOnDatabaseWithThem() throws SQLException {
assertEquals(1, indexes.size());
});
}

private void executeTestOnDatabase(@Nonnull final Consumer<DatabasePopulator> databasePopulatorConsumer,
@Nonnull final TestExecutor testExecutor)
throws SQLException {
try (DatabasePopulator databasePopulator = new DatabasePopulator(embeddedPostgres.getTestDatabase())) {
databasePopulatorConsumer.accept(databasePopulator);
testExecutor.execute();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2019. Ivan Vakhrushev. All rights reserved.
* https://github.com/mfvanek
*/

package com.mfvanek.pg.index.health;

import com.opentable.db.postgres.junit5.EmbeddedPostgresExtension;
import com.opentable.db.postgres.junit5.PreparedDbExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.sql.SQLException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

class IndexesHealthImplTestPg10 extends IndexesHealthImplTestBase {

@RegisterExtension
static final PreparedDbExtension embeddedPostgres =
EmbeddedPostgresExtension.preparedDatabase(ds -> {
});

IndexesHealthImplTestPg10() {
super(embeddedPostgres.getTestDatabase());
}

@Test
void pgVersion() throws SQLException {
try (var databasePopulator = createDatabasePopulator()) {
assertThat(databasePopulator.getPgVersion(), containsString("PostgreSQL 10.6"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2019. Ivan Vakhrushev. All rights reserved.
* https://github.com/mfvanek
*/

package com.mfvanek.pg.index.health;

import io.zonky.test.db.postgres.junit5.EmbeddedPostgresExtension;
import io.zonky.test.db.postgres.junit5.PreparedDbExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.sql.SQLException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

class IndexesHealthImplTestPg11 extends IndexesHealthImplTestBase {

@RegisterExtension
static final PreparedDbExtension embeddedPostgres =
EmbeddedPostgresExtension.preparedDatabase(ds -> {
});

IndexesHealthImplTestPg11() {
super(embeddedPostgres.getTestDatabase());
}

@Test
void pgVersion() throws SQLException {
try (var databasePopulator = createDatabasePopulator()) {
assertThat(databasePopulator.getPgVersion(), containsString("PostgreSQL 11.5"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class ExclusionsTest {
Expand Down Expand Up @@ -48,4 +49,13 @@ void emptyTest() {
assertNotNull(exclusions.getIndexesWithNullValuesExclusions());
assertThat(exclusions.getIndexesWithNullValuesExclusions(), hasSize(0));
}

@Test
void toStringTest() {
final var exclusions = Exclusions.empty();
assertEquals("Exclusions{duplicatedIndexesExclusions=[], " +
"intersectedIndexesExclusions=[], unusedIndexesExclusions=[], " +
"tablesWithMissingIndexesExclusions=[], tablesWithoutPrimaryKeyExclusions=[], " +
"indexesWithNullValuesExclusions=[]}", exclusions.toString());
}
}
Loading

0 comments on commit c541c41

Please sign in to comment.