Skip to content

Commit

Permalink
Merge pull request #101 from amoraleda/add-test-containers-jfleet-core
Browse files Browse the repository at this point in the history
Add TestContainers to JFleet core and drop the requirement of prelaunched databases
  • Loading branch information
jerolba authored Oct 21, 2024
2 parents c427101 + 636767c commit a08b1b0
Show file tree
Hide file tree
Showing 36 changed files with 202 additions and 152 deletions.
22 changes: 0 additions & 22 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@ jobs:
build:
docker:
- image: eclipse-temurin:17.0.10_7-jdk-jammy
- image: circleci/mysql:5.7
environment:
- MYSQL_USER=test
- MYSQL_PASSWORD=test
- MYSQL_DATABASE=testdb
- image: circleci/postgres:12.5
environment:
- POSTGRES_USER=test
- POSTGRES_PASSWORD=test
- POSTGRES_DB=testdb

working_directory: ~/repo

Expand All @@ -33,18 +23,6 @@ jobs:
build-latest:
docker:
- image: eclipse-temurin:17.0.10_7-jdk-jammy
- image: circleci/mysql:8.0
environment:
- MYSQL_USER=test
- MYSQL_PASSWORD=test
- MYSQL_DATABASE=testdb
#In MySql 8 local-infile is not active by default
command: mysqld --local-infile=1
- image: circleci/postgres:13.1
environment:
- POSTGRES_USER=test
- POSTGRES_PASSWORD=test
- POSTGRES_DB=testdb

working_directory: ~/repo

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.gradle
bin
build
.idea
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,13 @@ Any database engine with a standard JDBC driver should be used with the `JdbcBul

## Running the tests

Tests need a MySQL and a PostgreSQL instances running in localhost. A database called `testdb` must exist and an user `test` with password `test` must have `CREATE TABLE` and `DROP TABLE` permissions.
The tests utilize [TestContainers](https://testcontainers.com/) to create lightweight, temporary instances of common databases.
TestContainers automatically handles downloading the required database images and managing their lifecycle.

You can modify this settings changing locally [mysql-test.properties](https://github.com/jerolba/jfleet/blob/master/jfleet-core/src/test/resources/mysql-test.properties) and [postgres-test.properties](https://github.com/jerolba/jfleet/blob/master/jfleet-core/src/test/resources/postgres-test.properties) files.
During test execution, two databases—MySQL and PostgreSQL—are instantiated and will automatically shut down once the tests complete.
For details on how these containers are configured, refer to the `DatabaseContainers` class in the `jfleet-core` module.

If you have docker installed, you can launch them using the following commands:

```bash
docker run --name mysql-jfleet -e MYSQL_ROOT_PASSWORD=jfleet -e MYSQL_USER=test -e MYSQL_PASSWORD=test -e MYSQL_DATABASE=testdb -p 3306:3306 -d mysql:5.7

docker run --name postgres-jfleet -e POSTGRES_USER=test -e POSTGRES_PASSWORD=test -e POSTGRES_DB=testdb -p 5432:5432 -d postgres:12.5
```
To run the tests locally, ensure that Docker or a compatible Docker-API runtime is installed. For more information on configuring TestContainers, please consult the [System Requirements guide](https://java.testcontainers.org/supported_docker_environment/).

To execute all test you must execute the command:

Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ version = 0.6.7

parquetVersion = 1.13.1
hadoopVersion = 3.3.5
junitVersion = 5.10.2
junitVersion = 5.10.2
testContainersVersion = 1.20.2
5 changes: 5 additions & 0 deletions jfleet-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ dependencies {
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junitVersion
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: junitVersion
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitVersion
testImplementation group: 'org.testcontainers', name: 'testcontainers', version: testContainersVersion
testImplementation group: 'org.testcontainers', name: 'junit-jupiter', version: testContainersVersion
testImplementation group: 'org.testcontainers', name: 'mysql', version: testContainersVersion
testImplementation group: 'org.testcontainers', name: 'postgresql', version: testContainersVersion

}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;

import org.jfleet.FieldInfo;
import org.jfleet.EntityFieldType.FieldTypeEnum;
import org.jfleet.FieldInfo;
import org.junit.jupiter.api.Test;

public class EmbeddedIdInspectorTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.jfleet.FieldInfo;
import org.jfleet.EntityFieldType.FieldTypeEnum;
import org.jfleet.FieldInfo;
import org.jfleet.entities.Company;
import org.jfleet.entities.Person;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;

import org.jfleet.FieldInfo;
import org.jfleet.EntityFieldType.FieldTypeEnum;
import org.jfleet.FieldInfo;
import org.junit.jupiter.api.Test;

public class FailedManyToOneEntityInspectorTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import javax.persistence.TemporalType;

import org.jfleet.EntityFieldType;
import org.jfleet.FieldInfo;
import org.jfleet.EntityFieldType.FieldTypeEnum;
import org.jfleet.FieldInfo;
import org.junit.jupiter.api.Test;

public class FieldTypeInspectorTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import org.jfleet.FieldInfo;
import org.jfleet.EntityFieldType.FieldTypeEnum;
import org.jfleet.FieldInfo;
import org.junit.jupiter.api.Test;

public class ManyToOneEntityInspectorTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

import org.jfleet.FieldInfo;
import org.jfleet.EntityFieldType.FieldTypeEnum;
import org.jfleet.FieldInfo;
import org.junit.jupiter.api.Test;

public class OneToOneEntityInspectorTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.jfleet.mysql;

import static org.jfleet.parameterized.Databases.MySql;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -29,12 +30,15 @@
import javax.persistence.Table;

import org.jfleet.BulkInsert;
import org.jfleet.util.MySqlDatabase;
import org.jfleet.parameterized.DatabaseProvider;
import org.jfleet.util.Database;
import org.jfleet.util.SqlUtil;
import org.junit.jupiter.api.Test;

public class MySqlReservedWordTest {

private final Database database = DatabaseProvider.getDatabase(MySql);

@Entity
@Table(name = "\"select\"")
public class ReservedWordEntity {
Expand Down Expand Up @@ -65,7 +69,7 @@ public void canPersistWithReservedWords() throws Exception {
Stream<ReservedWordEntity> stream = IntStream.range(0, times)
.mapToObj(i -> new ReservedWordEntity(i, "current_user_" + i));

try (Connection conn = new MySqlDatabase().getConnection()) {
try (Connection conn = database.getConnection()) {
SqlUtil.createTableForEntity(conn, ReservedWordEntity.class);
insert.insertAll(conn, stream);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.jfleet.mysql;

import static org.jfleet.mysql.LoadDataConfiguration.LoadDataConfigurationBuilder.from;
import static org.jfleet.parameterized.Databases.MySql;
import static org.jfleet.util.TransactionPolicyTestHelper.employeesWithForeignKeyError;
import static org.jfleet.util.TransactionPolicyTestHelper.employeesWithMultipleConstraintsErrors;
import static org.jfleet.util.TransactionPolicyTestHelper.employeesWithOutErrors;
Expand All @@ -32,8 +33,8 @@
import org.jfleet.BulkInsert;
import org.jfleet.JFleetException;
import org.jfleet.entities.Employee;
import org.jfleet.parameterized.DatabaseProvider;
import org.jfleet.util.Database;
import org.jfleet.util.MySqlDatabase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
Expand All @@ -44,17 +45,18 @@ public class MySqlTransactionPolicyTest {
private static Logger logger = LoggerFactory.getLogger(MySqlTransactionPolicyTest.class);
private static final int VERY_LOW_SIZE_TO_FREQUENT_LOAD_DATA = 10;

private Database database = new MySqlDatabase();
private final Database database = DatabaseProvider.getDatabase(MySql);

@BeforeEach
public void setup() throws IOException, SQLException {
public void setup() throws SQLException, IOException {
try (Connection connection = database.getConnection()) {
setupDatabase(connection);
}
}

@Test
public void longTransactionExecuteMultipleLoadDataOperationsTransactionaly() throws Exception {

try (Connection connection = database.getConnection()) {
connection.setAutoCommit(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.jfleet.mysql.error;

import static org.jfleet.mysql.LoadDataConfiguration.LoadDataConfigurationBuilder.from;
import static org.jfleet.parameterized.Databases.MySql;
import static org.jfleet.util.TransactionPolicyTestHelper.numberOfRowsInEmployeeTable;
import static org.jfleet.util.TransactionPolicyTestHelper.setupDatabase;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -33,8 +34,8 @@
import org.jfleet.entities.Employee;
import org.jfleet.mysql.LoadDataBulkInsert;
import org.jfleet.mysql.LoadDataConfiguration;
import org.jfleet.parameterized.DatabaseProvider;
import org.jfleet.util.Database;
import org.jfleet.util.MySqlDatabase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
Expand All @@ -44,13 +45,13 @@ public class LockRetryTest {

private static Logger logger = LoggerFactory.getLogger(LockRetryTest.class);

private Database database = new MySqlDatabase();
private final Database database = DatabaseProvider.getDatabase(MySql);

private static City city1 = new City(1, "Madrid");
private static City city2 = new City(2, "Barcelona");

@BeforeEach
public void setup() throws IOException, SQLException {
public void setup() throws SQLException, IOException {
try (Connection connection = database.getConnection()) {
setupDatabase(connection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.jfleet.mysql.manual;

import static org.jfleet.parameterized.Databases.MySql;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -25,18 +26,21 @@
import java.sql.ResultSet;
import java.sql.SQLException;

import org.jfleet.parameterized.DatabaseProvider;
import org.jfleet.parameterized.IsMySql5Present;
import org.jfleet.util.MySqlDatabase;
import org.jfleet.util.Database;
import org.junit.jupiter.api.Test;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class ConnectionTest {

private final Database database = DatabaseProvider.getDatabase(MySql);

@Test
public void canConnectToTestDB() throws SQLException, IOException {
try (java.sql.Connection conn = new MySqlDatabase().getConnection()) {
try (java.sql.Connection conn = database.getConnection()) {
assertNotNull(conn);
}
}
Expand All @@ -47,7 +51,7 @@ public void canExecuteLoadData() throws SQLException, IOException {
int someValue = 12345;
String otherValue = "foobar";

try (Connection conn = (Connection) new MySqlDatabase().getConnection()) {
try (Connection conn = (Connection) database.getConnection()) {
conn.setAllowLoadLocalInfile(true);
try (Statement stmt = (Statement) conn.createStatement()) {
stmt.execute("CREATE TEMPORARY TABLE simple_table (some_column INTEGER, other_column VARCHAR(255))");
Expand Down Expand Up @@ -75,7 +79,7 @@ public void canExecuteLoadDataWithNullValues() throws SQLException, IOException
int someValue = 12345;
String otherValue = "foobar";

try (Connection conn = (Connection) new MySqlDatabase().getConnection()) {
try (Connection conn = (Connection) database.getConnection()) {
conn.setAllowLoadLocalInfile(true);
try (Statement stmt = (Statement) conn.createStatement()) {
stmt.execute("CREATE TEMPORARY TABLE simple_table (some_column INTEGER, other_column VARCHAR(255))");
Expand Down
10 changes: 7 additions & 3 deletions jfleet-core/src/test/java/org/jfleet/mysql/manual/IdsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.jfleet.mysql.manual;

import static org.jfleet.parameterized.Databases.MySql;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -24,8 +25,9 @@
import java.sql.ResultSet;
import java.sql.SQLException;

import org.jfleet.parameterized.DatabaseProvider;
import org.jfleet.parameterized.IsMySql5Present;
import org.jfleet.util.MySqlDatabase;
import org.jfleet.util.Database;
import org.junit.jupiter.api.Test;

import com.mysql.jdbc.Connection;
Expand All @@ -34,9 +36,11 @@
@IsMySql5Present
public class IdsTest {

private final Database database = DatabaseProvider.getDatabase(MySql);

@Test
public void canExecuteLoadDataWithAutoIncrement() throws SQLException, IOException {
try (Connection conn = (Connection) new MySqlDatabase().getConnection()) {
try (Connection conn = (Connection) database.getConnection()) {
conn.setAllowLoadLocalInfile(true);
try (Statement stmt = (Statement) conn.createStatement()) {
stmt.execute("CREATE TEMPORARY TABLE table_with_id "
Expand Down Expand Up @@ -66,7 +70,7 @@ public void canExecuteLoadDataWithAutoIncrement() throws SQLException, IOExcepti

@Test
public void canExecuteLoadDataWithAutoIncrementIdSetted() throws SQLException, IOException {
try (Connection conn = (Connection) new MySqlDatabase().getConnection()) {
try (Connection conn = (Connection) database.getConnection()) {
conn.setAllowLoadLocalInfile(true);
try (Statement stmt = (Statement) conn.createStatement()) {
stmt.execute("CREATE TEMPORARY TABLE table_with_id "
Expand Down
Loading

0 comments on commit a08b1b0

Please sign in to comment.