Skip to content

Commit

Permalink
Add DAO tests for SQLite databases (#357)
Browse files Browse the repository at this point in the history
* Add DAO tests for SQLite databases

* Add SQLite driver test dependency
* Add JDBI3 and JDBC DAO tests  for SQLite
* Add new utilities to TestHelpers
* Add custom SQLite migration file
  • Loading branch information
sleberknight authored Feb 16, 2024
1 parent 46cbabf commit e75135e
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<!-- Versions for test dependencies -->
<kiwi-test.version>3.2.1</kiwi-test.version>
<mysql-connector-j.version>8.3.0</mysql-connector-j.version>
<sqlite-jdbc.version>3.45.1.0</sqlite-jdbc.version>

<!-- Sonar properties -->
<sonar.projectKey>kiwiproject_dropwizard-application-errors</sonar.projectKey>
Expand Down Expand Up @@ -187,6 +188,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>${sqlite-jdbc.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.kiwiproject.dropwizard.error.dao.jdbi3;

import static org.kiwiproject.dropwizard.error.util.TestHelpers.migrateDatabase;
import static org.kiwiproject.dropwizard.error.util.TestHelpers.newInMemorySqliteDataSource;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.kiwiproject.test.jdbc.SimpleSingleConnectionDataSource;
import org.kiwiproject.test.junit.jupiter.Jdbi3DaoExtension;

import java.sql.SQLException;

@DisplayName("Jdbi3ApplicationErrorDao (SQLite)")
class SqliteJdbi3ApplicationErrorDaoTest extends AbstractJdbi3ApplicationErrorDaoTest {

private static SimpleSingleConnectionDataSource DATA_SOURCE;

@BeforeAll
static void beforeAll() throws SQLException {
DATA_SOURCE = newInMemorySqliteDataSource();
migrateDatabase(DATA_SOURCE, "dropwizard-app-errors-migrations-sqlite.xml");
}

@RegisterExtension
final Jdbi3DaoExtension<Jdbi3ApplicationErrorDao> jdbi3DaoExtension =
Jdbi3DaoExtension.<Jdbi3ApplicationErrorDao>builder()
.daoType(Jdbi3ApplicationErrorDao.class)
.dataSource(DATA_SOURCE)
.build();

@Override
Jdbi3DaoExtension<Jdbi3ApplicationErrorDao> getTestExtension() {
return jdbi3DaoExtension;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.kiwiproject.dropwizard.error.dao.jdk;

import static org.kiwiproject.dropwizard.error.util.TestHelpers.migrateDatabase;
import static org.kiwiproject.dropwizard.error.util.TestHelpers.newInMemorySqliteDataSource;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.kiwiproject.test.jdbc.SimpleSingleConnectionDataSource;

import java.sql.SQLException;

@DisplayName("JdbcApplicationErrorDao (SQLite)")
class SqliteJdbcApplicationErrorDaoTest extends AbstractJdbcApplicationErrorDaoTest {

private static SimpleSingleConnectionDataSource DATA_SOURCE;

@BeforeAll
static void beforeAll() throws SQLException {
DATA_SOURCE = newInMemorySqliteDataSource();
migrateDatabase(DATA_SOURCE, "dropwizard-app-errors-migrations-sqlite.xml");
}

@Override
protected SimpleSingleConnectionDataSource getDataSource() {
return DATA_SOURCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.extern.slf4j.Slf4j;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorJdbc;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorJdbc.ApplicationErrorJdbcException;
import org.kiwiproject.test.jdbc.SimpleSingleConnectionDataSource;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;
Expand Down Expand Up @@ -62,4 +63,28 @@ public static void migrateDatabase(JdbcDatabaseContainer<?> container, String mi

LOG.info("Completed migrating {} container database using file {}", dockerImageName, migrationsFilename);
}

/**
* Create an in-memory SQLite database and return a single-connection DataSource for testing.
*
* @return a new instance of {@link SimpleSingleConnectionDataSource}
*/
public static SimpleSingleConnectionDataSource newInMemorySqliteDataSource() {
return new SimpleSingleConnectionDataSource("jdbc:sqlite::memory:", "");
}

/**
* Use the given single-connection DataSource to run Liquibase migrations.
*
* @param dataSource
*/
public static void migrateDatabase(SimpleSingleConnectionDataSource dataSource, String migrationsFilename) {
LOG.info("Migrating database with JDBC URL {} using file {}", dataSource.getUrl(), migrationsFilename);
try {
ApplicationErrorJdbc.migrateDatabase(dataSource.getConnection(), migrationsFilename);
} catch (SQLException e) {
throw new ApplicationErrorJdbcException(e);
}
LOG.info("Completed migrating database using file {}", migrationsFilename);
}
}
52 changes: 52 additions & 0 deletions src/test/resources/dropwizard-app-errors-migrations-sqlite.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Migrations file for integration tests using SQLite as the database.
-->

<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

<changeSet id="0001-add-application-errors-table" author="dropwizard-application-errors">
<createTable tableName="application_errors" remarks="Stores application errors">

<!-- SQLite auto increment columns must be integer -->
<column name="id" type="integer" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>

<!--
SQLite doesn't have date/time/timestamp types. This migration uses the integer type,
which will result in numeric timestamps stored in the database.
See Datatypes In SQLite: https://www.sqlite.org/datatype3.html
You can also read this tutorial with examples: https://www.sqlitetutorial.net/sqlite-date/
-->
<column name="created_at" type="integer" defaultValueComputed="current_timestamp">
<constraints nullable="false"/>
</column>
<column name="updated_at" type="integer" defaultValueComputed="current_timestamp">
<constraints nullable="false"/>
</column>
<column name="num_times_occurred" type="integer" defaultValueNumeric="1">
<constraints nullable="false"/>
</column>
<column name="description" type="text">
<constraints nullable="false"/>
</column>
<column name="exception_type" type="text"/>
<column name="exception_message" type="text"/>
<column name="exception_cause_type" type="text"/>
<column name="exception_cause_message" type="text"/>
<column name="stack_trace" type="text"/>
<column name="resolved" type="boolean" defaultValueBoolean="false">
<constraints nullable="false"/>
</column>
<column name="host_name" type="text"/>
<column name="ip_address" type="text"/>
<column name="port" type="integer"/>
</createTable>
</changeSet>

</databaseChangeLog>

0 comments on commit e75135e

Please sign in to comment.