Skip to content

Commit

Permalink
fix(logger): remove extra whitespace from constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
saku-koodari committed Jan 8, 2025
1 parent 05fd20e commit 67bbb19
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/src/main/kotlin/fi/oph/kitu/logging/EventLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class EventLogger<T>(

event.add(
"table" to table,
"constraint" to constraint,
"constraint" to constraint?.trim(),
)
}

Expand Down
31 changes: 31 additions & 0 deletions server/src/test/kotlin/fi/oph/kitu/logging/EventLoggerTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fi.oph.kitu.logging
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.dao.DuplicateKeyException
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
Expand All @@ -15,6 +16,8 @@ class EventLoggerTests {

// runCatching is eager so it runs the action.
val logger = EventLogger(runCatching(actionToRun), event)

// Act
logger.addDefaults("unit-test")

val result = logger.getOrThrow()
Expand Down Expand Up @@ -50,6 +53,8 @@ class EventLoggerTests {
}

val logger = EventLogger(actionToRun, event)

// Act
logger.addDefaults("unit-test")

assertThrows<ExceptionThatThrows> {
Expand All @@ -69,6 +74,32 @@ class EventLoggerTests {
assertEquals(1, logsCount, "logs_count should be 1")
}

@Test
fun `addDatabaseLogs do database logging when it throws DuplicateKeyException`() {
val event = MockEvent()
val actionToRun = {
val cause = DuplicateKeyException("unique constraint \"my_constraint \"")

throw DuplicateKeyException("INSERT INTO \"my_table\"", cause)
}

// runCatching is eager so it runs the action.
val logger = EventLogger(runCatching(actionToRun), event)

// Act
logger.addDatabaseLogs()

assertThrows<DuplicateKeyException> {
logger.getOrThrow()
}

val table = event.getValueOrNullByKey<String>("table")
assertEquals("my_table", table)

val constraint = event.getValueOrNullByKey<String>("constraint")
assertEquals("my_constraint", constraint)
}

@Test
fun `withEventAndPerformanceCheck does performance logging when running an action`() {
val event = MockEvent()
Expand Down

0 comments on commit 67bbb19

Please sign in to comment.