diff --git a/src/test/kotlin/no/liflig/documentstore/RepositoryTest.kt b/src/test/kotlin/no/liflig/documentstore/RepositoryTest.kt index cd03d64..57b9cb4 100644 --- a/src/test/kotlin/no/liflig/documentstore/RepositoryTest.kt +++ b/src/test/kotlin/no/liflig/documentstore/RepositoryTest.kt @@ -1,5 +1,6 @@ package no.liflig.documentstore +import java.time.Instant import java.util.* import kotlin.concurrent.thread import kotlin.test.assertContains @@ -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 @@ -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 -> @@ -341,9 +351,8 @@ class RepositoryTest { ) verifyJsonSnapshot( - "Example.json", + "ExampleEntity.json", json.encodeToString(entity), - ignoredPaths = listOf("createdAt", "modifiedAt"), ) } } diff --git a/src/test/kotlin/no/liflig/documentstore/VersionedListUtilsTest.kt b/src/test/kotlin/no/liflig/documentstore/VersionedListUtilsTest.kt index 42ab454..5142098 100644 --- a/src/test/kotlin/no/liflig/documentstore/VersionedListUtilsTest.kt +++ b/src/test/kotlin/no/liflig/documentstore/VersionedListUtilsTest.kt @@ -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) @@ -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 -> @@ -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" } @@ -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 } @@ -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) @@ -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 -> @@ -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" } @@ -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 } @@ -125,7 +125,7 @@ class VersionedListUtilsTest { } } -private fun createEntities(vararg texts: String): List> { +private fun createTestEntities(vararg texts: String): List> { return texts.map { text -> Versioned( ExampleEntity(text = text), @@ -136,11 +136,11 @@ private fun createEntities(vararg texts: String): List> } } -private fun createEntitiesWithTotalCount( +private fun createTestEntitiesWithTotalCount( vararg texts: String ): ListWithTotalCount> { return ListWithTotalCount( - list = createEntities(*texts), + list = createTestEntities(*texts), totalCount = 100, ) } diff --git a/src/test/resources/__snapshots__/ExampleEntity.json b/src/test/resources/__snapshots__/ExampleEntity.json new file mode 100644 index 0000000..7349b95 --- /dev/null +++ b/src/test/resources/__snapshots__/ExampleEntity.json @@ -0,0 +1,6 @@ +{ + "id": "928f6ef3-6873-454a-a68d-ef3f5d7963b5", + "text": "hello world", + "moreText": null, + "uniqueField": null +}