-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from andrei-punko/82-add-int-tests-for-db-vers…
…ioning-modules Fix #82: add integration tests for `db-versioning` modules
- Loading branch information
Showing
10 changed files
with
369 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
db-versioning/flyway-db/src/main/java/by/andd3dfx/model/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package by.andd3dfx.model; | ||
|
||
import lombok.Data; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
|
||
@Data | ||
@Entity | ||
@Table(name = "PERSON") | ||
public class Person { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@Column | ||
private String name; | ||
} |
18 changes: 18 additions & 0 deletions
18
db-versioning/flyway-db/src/main/resources/hibernate.cfg.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!DOCTYPE hibernate-configuration PUBLIC | ||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" | ||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> | ||
|
||
<hibernate-configuration> | ||
<session-factory> | ||
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> | ||
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/db?allowPublicKeyRetrieval=true&useSSL=false</property> | ||
<property name="hibernate.connection.username">root</property> | ||
<property name="hibernate.connection.password">some_password</property> | ||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> | ||
<property name="show_sql">true</property> | ||
<property name="hibernate.id.new_generator_mappings">false</property> | ||
|
||
<mapping class="by.andd3dfx.model.Person"/> | ||
</session-factory> | ||
</hibernate-configuration> |
41 changes: 41 additions & 0 deletions
41
db-versioning/flyway-db/src/test/java/by/andd3dfx/MigrationIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package by.andd3dfx; | ||
|
||
import by.andd3dfx.model.Person; | ||
import lombok.SneakyThrows; | ||
import org.hibernate.cfg.Configuration; | ||
import org.junit.Test; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.criteria.CriteriaBuilder; | ||
import javax.persistence.criteria.CriteriaQuery; | ||
import javax.persistence.criteria.Root; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class MigrationIT { | ||
|
||
@SneakyThrows | ||
@Test | ||
public void checkRecordsAmount() { | ||
try (var sessionFactory = new Configuration().configure().buildSessionFactory()) { | ||
try (var session = sessionFactory.openSession()) { | ||
var emFactory = session.getEntityManagerFactory(); | ||
var em = emFactory.createEntityManager(); | ||
|
||
getFromDbAndCheck(em, Person.class, 3); | ||
} | ||
} | ||
} | ||
|
||
private <T> void getFromDbAndCheck(EntityManager em, Class<T> clazz, int expectedItemsCount) { | ||
CriteriaBuilder builder = em.getCriteriaBuilder(); | ||
CriteriaQuery<T> query = builder.createQuery(clazz); | ||
Root<T> variableRoot = query.from(clazz); | ||
query.select(variableRoot); | ||
var items = em.createQuery(query).getResultList(); | ||
|
||
System.out.println("Retrieved next items: " + items); | ||
assertThat("Wrong items count", items.size(), is(expectedItemsCount)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.