Skip to content

Commit

Permalink
Test that createdAt and modifiedAt are returned as expected
Browse files Browse the repository at this point in the history
...and improve naming of test functions/resources.
  • Loading branch information
hermannm committed Aug 29, 2024
1 parent a3aca8c commit b94d7c8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
19 changes: 14 additions & 5 deletions src/test/kotlin/no/liflig/documentstore/RepositoryTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.liflig.documentstore

import java.time.Instant
import java.util.*
import kotlin.concurrent.thread
import kotlin.test.assertContains
Expand Down Expand Up @@ -39,12 +40,21 @@ class RepositoryTest {
@Test
fun `store and retrieve new entity`() {
val entity = ExampleEntity(text = "hello world")

val timeBeforeCreate = Instant.now()
exampleRepo.create(entity)
val timeAfterCreate = Instant.now()

val retrievedEntity = exampleRepo.get(entity.id)

assertNotNull(retrievedEntity)
assertEquals(Version.initial(), retrievedEntity.version)
assertEquals(entity, retrievedEntity.item)

for (timestamp in sequenceOf(retrievedEntity.createdAt, retrievedEntity.modifiedAt)) {
assert(timestamp.isAfter(timeBeforeCreate))
assert(timestamp.isBefore(timeAfterCreate))
}
}

@Test
Expand Down Expand Up @@ -137,9 +147,9 @@ class RepositoryTest {
fun `transaction prevents race conditions`() {
val (entity1, _) = exampleRepo.create(ExampleEntity(text = "Text"))

// Without locking, we should be getting ConflictDaoException when concurrent processes
// attempt to update the same row. With locking, each transaction will wait until lock
// is released before reading
// Without locking, we should be getting ConflictRepositoryException when concurrent processes
// attempt to update the same row. With locking, each transaction will wait until lock is
// released before reading.
val threads =
(1 until 100)
.map { index ->
Expand Down Expand Up @@ -341,9 +351,8 @@ class RepositoryTest {
)

verifyJsonSnapshot(
"Example.json",
"ExampleEntity.json",
json.encodeToString(entity),
ignoredPaths = listOf("createdAt", "modifiedAt"),
)
}
}
22 changes: 11 additions & 11 deletions src/test/kotlin/no/liflig/documentstore/VersionedListUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.junit.jupiter.api.Test
class VersionedListUtilsTest {
@Test
fun `test mapEntities`() {
val entities = createEntities("test1", "test2")
val entities = createTestEntities("test1", "test2")

val mappedEntities = entities.mapEntities { entity -> entity.copy(moreText = "New text!") }
assertEquals(entities.size, mappedEntities.size)
Expand All @@ -32,7 +32,7 @@ class VersionedListUtilsTest {

@Test
fun `test mapEntitiesNotNull`() {
val entities = createEntities("test1", "test2")
val entities = createTestEntities("test1", "test2")

val mappedEntities =
entities.mapEntitiesNotNull { entity ->
Expand All @@ -49,7 +49,7 @@ class VersionedListUtilsTest {

@Test
fun `test filterEntities`() {
val entities = createEntities("test1", "test2")
val entities = createTestEntities("test1", "test2")

val mappedEntities = entities.filterEntities { entity -> entity.text == "test2" }

Expand All @@ -59,7 +59,7 @@ class VersionedListUtilsTest {

@Test
fun `test forEachEntity`() {
val entities = createEntities("test1", "test2")
val entities = createTestEntities("test1", "test2")

var combinedText = ""
entities.forEachEntity { entity -> combinedText += entity.text }
Expand All @@ -69,7 +69,7 @@ class VersionedListUtilsTest {

@Test
fun `test mapEntities for ListWithTotalCount`() {
val entities = createEntitiesWithTotalCount("test1", "test2")
val entities = createTestEntitiesWithTotalCount("test1", "test2")

val mappedEntities = entities.mapEntities { entity -> entity.copy(moreText = "New text!") }
assertEquals(entities.list.size, mappedEntities.list.size)
Expand All @@ -87,7 +87,7 @@ class VersionedListUtilsTest {

@Test
fun `test mapEntitiesNotNull for ListWithTotalCount`() {
val entities = createEntitiesWithTotalCount("test1", "test2")
val entities = createTestEntitiesWithTotalCount("test1", "test2")

val mappedEntities =
entities.mapEntitiesNotNull { entity ->
Expand All @@ -105,7 +105,7 @@ class VersionedListUtilsTest {

@Test
fun `test filterEntities for ListWithTotalCount`() {
val entities = createEntitiesWithTotalCount("test1", "test2")
val entities = createTestEntitiesWithTotalCount("test1", "test2")

val mappedEntities = entities.filterEntities { entity -> entity.text == "test2" }

Expand All @@ -116,7 +116,7 @@ class VersionedListUtilsTest {

@Test
fun `test forEachEntity for ListWithTotalCount`() {
val entities = createEntitiesWithTotalCount("test1", "test2")
val entities = createTestEntitiesWithTotalCount("test1", "test2")

var combinedText = ""
entities.forEachEntity { entity -> combinedText += entity.text }
Expand All @@ -125,7 +125,7 @@ class VersionedListUtilsTest {
}
}

private fun createEntities(vararg texts: String): List<Versioned<ExampleEntity>> {
private fun createTestEntities(vararg texts: String): List<Versioned<ExampleEntity>> {
return texts.map { text ->
Versioned(
ExampleEntity(text = text),
Expand All @@ -136,11 +136,11 @@ private fun createEntities(vararg texts: String): List<Versioned<ExampleEntity>>
}
}

private fun createEntitiesWithTotalCount(
private fun createTestEntitiesWithTotalCount(
vararg texts: String
): ListWithTotalCount<Versioned<ExampleEntity>> {
return ListWithTotalCount(
list = createEntities(*texts),
list = createTestEntities(*texts),
totalCount = 100,
)
}
6 changes: 6 additions & 0 deletions src/test/resources/__snapshots__/ExampleEntity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "928f6ef3-6873-454a-a68d-ef3f5d7963b5",
"text": "hello world",
"moreText": null,
"uniqueField": null
}

0 comments on commit b94d7c8

Please sign in to comment.