Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flush and clear database in AbstractSpringBootNoDirtyContextTest #63

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/persistence-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ jobs:
./gradlew -q :persistence:dependencies
exit 1;
fi
- run: ./gradlew :persistence:test --tests "*"
- run: ./gradlew :persistence:test --tests "*" --stacktrace
2 changes: 1 addition & 1 deletion .github/workflows/web-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ jobs:
./gradlew -q :web:dependencies
exit 1;
fi
- run: ./gradlew :web:test --tests "*"
- run: ./gradlew :web:test --tests "*" --stacktrace
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ abstract class AbstractSpringBootNoDirtyContextTest {
@Autowired
protected lateinit var blogEntryRepository: BlogEntryRepository

@Autowired
protected lateinit var entityManager: EntityManager

@Autowired
protected lateinit var guestBookRepository: GuestBookRepository

Expand Down Expand Up @@ -75,4 +72,17 @@ abstract class AbstractSpringBootNoDirtyContextTest {

@Value("\${server.servlet.context-path}")
protected lateinit var contextPath: String

// database operations

@Autowired
private lateinit var entityManager: EntityManager

protected fun<T> flush(databaseOperation: () -> T): T {
val entity = databaseOperation.invoke()
entityManager.flush()
entityManager.clear()

return entity
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,31 @@ import assertk.assertions.isPresent
internal class AddressRepositoryTest : AbstractSpringBootNoDirtyContextTest() {
@Test
fun `should fetch address entities`() {
addressRepository.save(
AddressBuilder
.new(
addressInternalDto = AddressInternalDto(
zipCode = "1234",
addressLine1 = "somewhere out there",
city = "Rud"
flush {
addressRepository.save(
AddressBuilder
.new(
addressInternalDto = AddressInternalDto(
zipCode = "1234",
addressLine1 = "somewhere out there",
city = "Rud"
)
)
)
.build()
)
.build()
)

addressRepository.save(
AddressBuilder
.new(
addressInternalDto = AddressInternalDto(
zipCode = "1234",
addressLine1 = "somewhere in there",
city = "Rud"
addressRepository.save(
AddressBuilder
.new(
addressInternalDto = AddressInternalDto(
zipCode = "1234",
addressLine1 = "somewhere in there",
city = "Rud"
)
)
)
.build()
)

entityManager.flush()
entityManager.clear()

.build()
)
}
val addressEntities = addressRepository.findByZipCode(zipCode = "1234")

assertAll {
Expand All @@ -66,9 +64,7 @@ internal class AddressRepositoryTest : AbstractSpringBootNoDirtyContextTest() {
)
).build()

addressRepository.save(addressEntityToPersist)
entityManager.flush()
entityManager.clear()
flush { addressRepository.save(addressEntityToPersist) }

val possibleAddressEntityById = addressRepository.findById(addressEntityToPersist.id!!)

Expand Down Expand Up @@ -99,9 +95,7 @@ internal class AddressRepositoryTest : AbstractSpringBootNoDirtyContextTest() {
)
.build()

addressRepository.save(addressEntityToPersist)
entityManager.flush()
entityManager.clear()
flush { addressRepository.save(addressEntityToPersist) }

val addressEntitySaved =
addressRepository.findById(addressEntityToPersist.id!!).orElseThrow { addressNotFound() }!!
Expand All @@ -112,9 +106,7 @@ internal class AddressRepositoryTest : AbstractSpringBootNoDirtyContextTest() {
addressEntitySaved.city = "Cloud city"
addressEntitySaved.country = "XX"

addressRepository.save(addressEntitySaved)
entityManager.flush()
entityManager.clear()
flush { addressRepository.save(addressEntitySaved) }

val possibleAddressEntityById = addressRepository.findById(addressEntityToPersist.id!!)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import assertk.assertions.hasSize
import assertk.assertions.isEqualTo
import assertk.assertions.isStrictlyBetween

internal class BlogEntryRepositoryTest: AbstractSpringBootNoDirtyContextTest() {
internal class BlogEntryRepositoryTest : AbstractSpringBootNoDirtyContextTest() {
@Test
fun `should save then read blog entry`() {
val addressDto = AddressInternalDto(
Expand Down Expand Up @@ -47,9 +47,7 @@ internal class BlogEntryRepositoryTest: AbstractSpringBootNoDirtyContextTest() {
blogEntryDto = BlogEntryDto(blog = blogDto, creatorName = "smith", entry = "once upon a time")
)

blogEntryRepository.save(blogData.buildBlogEntryEntity())
entityManager.flush()
entityManager.clear()
flush { blogEntryRepository.save(blogData.buildBlogEntryEntity()) }

val blogEntries = blogEntryRepository.findAll().toList()

Expand Down Expand Up @@ -91,9 +89,7 @@ internal class BlogEntryRepositoryTest: AbstractSpringBootNoDirtyContextTest() {
.withEntry(BlogEntryDto(creatorName = "smith", entry = "once upon a time"))
.buildBlogEntryEntity()

blogEntryRepository.save(blogEntryToSave)
entityManager.flush()
entityManager.clear()
flush { blogEntryRepository.save(blogEntryToSave) }

val blogEntries = blogEntryRepository.findAll().toList()

Expand All @@ -102,9 +98,7 @@ internal class BlogEntryRepositoryTest: AbstractSpringBootNoDirtyContextTest() {
val blogEntry = blogEntries.iterator().next()
blogEntry.modify("happily ever after", "luke")

blogEntryRepository.save(blogEntry)
entityManager.flush()
entityManager.clear()
flush { blogEntryRepository.save(blogEntry) }

val modifiedEntries = blogEntryRepository.findAll().toList()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ internal class BlogRepositoryTest : AbstractSpringBootNoDirtyContextTest(){
.buildBlogEntity()

blogRepository.save(blogEntityToSave)
entityManager.flush()
entityManager.clear()

val blogs = blogRepository.findAll().toList()
assertThat(blogs).hasSize(1)
Expand Down Expand Up @@ -89,8 +87,6 @@ internal class BlogRepositoryTest : AbstractSpringBootNoDirtyContextTest(){
.buildBlogEntity()

blogRepository.save(blogEntityToSave)
entityManager.flush()
entityManager.clear()

val blogs = blogRepository.findBlogsByTitle("Blah")
assertThat(blogs).hasSize(1)
Expand All @@ -99,8 +95,6 @@ internal class BlogRepositoryTest : AbstractSpringBootNoDirtyContextTest(){
blogEntitySaved.title = "Duh"

blogRepository.save(blogEntitySaved)
entityManager.flush()
entityManager.clear()

val modifiedBlogs = blogRepository.findBlogsByTitle("Duh")
assertThat(modifiedBlogs).hasSize(1)
Expand Down Expand Up @@ -139,8 +133,6 @@ internal class BlogRepositoryTest : AbstractSpringBootNoDirtyContextTest(){
.buildBlogEntity()

blogRepository.save(blogEntityToSave)
entityManager.flush()
entityManager.clear()

val blogs = blogRepository.findBlogsByTitle("Blah")

Expand Down Expand Up @@ -194,8 +186,6 @@ internal class BlogRepositoryTest : AbstractSpringBootNoDirtyContextTest(){

blogEntityToSave.add(blogEntryToSave)
blogRepository.save(blogEntityToSave)
entityManager.flush()
entityManager.clear()

val blogs = blogRepository.findBlogsByTitle("Blah")
assertThat(blogs).hasSize(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import assertk.assertions.hasSize
import assertk.assertions.isEqualTo
import assertk.assertions.isNotNull

internal class GuestBookEntryRepositoryTest: AbstractSpringBootNoDirtyContextTest() {
internal class GuestBookEntryRepositoryTest : AbstractSpringBootNoDirtyContextTest() {
@Test
fun `should save then read guest book entry entity`() {
val addressDto = AddressBuilder.new(
Expand Down Expand Up @@ -59,9 +59,7 @@ internal class GuestBookEntryRepositoryTest: AbstractSpringBootNoDirtyContextTes
)
)

guestBookEntryRepository.save(guestBookData.buildGuestBookEntryEntity())
entityManager.flush()
entityManager.clear()
flush { guestBookEntryRepository.save(guestBookData.buildGuestBookEntryEntity()) }

val entriesByGuestBook = guestBookEntryRepository.findByGuestBook(savedGuestBook)
assertThat(entriesByGuestBook).hasSize(1)
Expand Down Expand Up @@ -102,27 +100,24 @@ internal class GuestBookEntryRepositoryTest: AbstractSpringBootNoDirtyContextTes
)

val savedGuestBook = guestBookRepository.save(guestBookData.buildGuestBookEntity())
guestBookEntryRepository.save(
guestBookData.withEntry(
GuestBookEntryDto(
guestBook = savedGuestBook.asDto(),
creatorName = "Harry",
entry = "Draco Dormiens Nunquam Tittilandus"
)
).buildGuestBookEntryEntity()
)

entityManager.flush()
entityManager.clear()
flush {
guestBookEntryRepository.save(
guestBookData.withEntry(
GuestBookEntryDto(
guestBook = savedGuestBook.asDto(),
creatorName = "Harry",
entry = "Draco Dormiens Nunquam Tittilandus"
)
).buildGuestBookEntryEntity()
)
}

val entriesByGuestBook = guestBookEntryRepository.findByGuestBook(savedGuestBook)
assertThat(entriesByGuestBook).hasSize(1)
entriesByGuestBook.iterator().next().modify("Willie", "On the road again")

guestBookEntryRepository.save<GuestBookEntryEntity>(entriesByGuestBook.iterator().next())

entityManager.flush()
entityManager.clear()
flush { guestBookEntryRepository.save<GuestBookEntryEntity>(entriesByGuestBook.iterator().next()) }

val modifiedEntriesByGuestBook = guestBookEntryRepository.findByGuestBook(savedGuestBook)
assertThat(modifiedEntriesByGuestBook).hasSize(1)
Expand Down Expand Up @@ -200,9 +195,7 @@ internal class GuestBookEntryRepositoryTest: AbstractSpringBootNoDirtyContextTes
)
).buildGuestBookEntryEntity()

guestBookEntryRepository.save(anotherEntry)
entityManager.flush()
entityManager.clear()
flush { guestBookEntryRepository.save(anotherEntry) }

val lastEntry = guestBookRepository.findAll().toList()
.flatMap { it.getEntries() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.jactor.persistence.repository

import java.util.UUID
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.fail
import com.github.jactor.persistence.AbstractSpringBootNoDirtyContextTest
import com.github.jactor.persistence.dto.AddressInternalDto
import com.github.jactor.persistence.dto.GuestBookDto
Expand All @@ -12,12 +13,13 @@ import com.github.jactor.persistence.entity.AddressBuilder
import com.github.jactor.persistence.entity.GuestBookBuilder
import com.github.jactor.persistence.entity.PersonBuilder
import com.github.jactor.persistence.entity.UserBuilder
import com.github.jactor.persistence.entity.UserEntity
import assertk.assertAll
import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isNotNull

internal class GuestBookRepositoryTest: AbstractSpringBootNoDirtyContextTest() {
internal class GuestBookRepositoryTest : AbstractSpringBootNoDirtyContextTest() {
@Test
fun `should write then read guest book`() {
val addressDto = AddressBuilder
Expand Down Expand Up @@ -55,8 +57,7 @@ internal class GuestBookRepositoryTest: AbstractSpringBootNoDirtyContextTest() {
).buildGuestBookEntity()
)

entityManager.flush()
entityManager.clear()
flush { }

val guestBookEntity = guestBookRepository.findByUser(userEntity)

Expand Down Expand Up @@ -96,17 +97,13 @@ internal class GuestBookRepositoryTest: AbstractSpringBootNoDirtyContextTest() {
).buildGuestBookEntity()
)

guestBookRepository.save(userEntity.guestBook!!)
entityManager.flush()
entityManager.clear()
flush { guestBookRepository.save(userEntity.guestBook ?: fail(message = "User missing guest book")) }

val guestBookEntityToUpdate = guestBookRepository.findByUser(userEntity)

guestBookEntityToUpdate!!.title = "5000 thousands miles away from home"

guestBookRepository.save(guestBookEntityToUpdate)
entityManager.flush()
entityManager.clear()
flush { guestBookRepository.save(guestBookEntityToUpdate) }

val guestBookEntity = guestBookRepository.findByUser(userEntity)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ internal class PersonRepositoryTest: AbstractSpringBootNoDirtyContextTest() {
)
).build()

personRepository.save(personToPersist)
entityManager.flush()
entityManager.clear()
flush { personRepository.save(personToPersist) }

val people = personRepository.findAll().toList()
assertThat(people).hasSize(allreadyPresentPeople + 1)
Expand Down Expand Up @@ -82,9 +80,7 @@ internal class PersonRepositoryTest: AbstractSpringBootNoDirtyContextTest() {
)
).build()

personRepository.save(personToPersist)
entityManager.flush()
entityManager.clear()
flush { personRepository.save(personToPersist) }

val mine = personRepository.findBySurname("Mine")
val person = mine.iterator().next()
Expand All @@ -94,9 +90,7 @@ internal class PersonRepositoryTest: AbstractSpringBootNoDirtyContextTest() {
person.firstName = "Dr. A."
person.surname = "Cula"

personRepository.save(person)
entityManager.flush()
entityManager.clear()
flush { personRepository.save(person) }

val foundCula = personRepository.findBySurname("Cula")
val personEntity = foundCula.iterator().next()
Expand Down Expand Up @@ -131,9 +125,7 @@ internal class PersonRepositoryTest: AbstractSpringBootNoDirtyContextTest() {
val userEntity = UserBuilder.new(userDto = userInternalDto).build()
val personToPersist = userEntity.fetchPerson()

personRepository.save<PersonEntity>(personToPersist)
entityManager.flush()
entityManager.clear()
flush { personRepository.save<PersonEntity>(personToPersist) }

assertThat(personRepository.findAll().toList()).hasSize(alreadyPresentPeople + 1)
val personEntity = personRepository.findBySurname("Adder").iterator().next()
Expand Down
Loading
Loading