From 46cbabfb8c67ca3151174465a132b84fea6fb9be Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Fri, 16 Feb 2024 16:40:42 -0500 Subject: [PATCH] Enhance tests deleting errors in AbstractApplicationErrorDaoTest (#356) * Update the tests for deleting resolved and unresolved errors so they work as long as the database timestamp precision is at least milliseconds (e.g., when using SQLite can only get to millis) * Enhance those tests with additional assertions to verify only the expected ApplicationErrors were deleted --- .../dao/AbstractApplicationErrorDaoTest.java | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/kiwiproject/dropwizard/error/dao/AbstractApplicationErrorDaoTest.java b/src/test/java/org/kiwiproject/dropwizard/error/dao/AbstractApplicationErrorDaoTest.java index 777598c..3128170 100644 --- a/src/test/java/org/kiwiproject/dropwizard/error/dao/AbstractApplicationErrorDaoTest.java +++ b/src/test/java/org/kiwiproject/dropwizard/error/dao/AbstractApplicationErrorDaoTest.java @@ -15,6 +15,7 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.kiwiproject.base.DefaultEnvironment; import org.kiwiproject.dropwizard.error.model.ApplicationError; import org.kiwiproject.dropwizard.error.model.ApplicationError.Resolved; import org.kiwiproject.dropwizard.error.test.junit.jupiter.ApplicationErrorExtension; @@ -248,29 +249,47 @@ void shouldChangeOnlyUnresolvedErrors() { } @Test - void shouldDeleteAllResolvedErrors() { - insertApplicationError(randomResolvedApplicationError()); - insertApplicationError(randomUnresolvedApplicationError()); + void shouldDeleteResolvedErrors_BeforeReferenceDate() { + var resolvedId1 = insertApplicationError(randomResolvedApplicationError()); + var unresolvedId1 = insertApplicationError(randomUnresolvedApplicationError()); + + // Ensure the next createdAt is AFTER previous ones as long as + // the database timestamp precision is at least milliseconds + sleep5ms(); var timeInBetween = ZonedDateTime.now(); - insertApplicationError(randomResolvedApplicationError()); + var resolvedId2 = insertApplicationError(randomResolvedApplicationError()); var count = errorDao.deleteResolvedErrorsBefore(timeInBetween); assertThat(count).isOne(); + assertThat(errorDao.getById(resolvedId1)).isEmpty(); + assertThat(errorDao.getById(unresolvedId1)).isPresent(); + assertThat(errorDao.getById(resolvedId2)).isPresent(); } @Test - void shouldDeleteUnresolvedErrors() { - insertApplicationError(randomResolvedApplicationError()); - insertApplicationError(randomUnresolvedApplicationError()); + void shouldDeleteUnresolvedErrors_BeforeReferenceDate() { + var resolvedId1 = insertApplicationError(randomResolvedApplicationError()); + var unresolvedId1 = insertApplicationError(randomUnresolvedApplicationError()); + + // Ensure the createdAt is AFTER previous ones as long as + // the database timestamp precision is at least milliseconds + sleep5ms(); var timeInBetween = ZonedDateTime.now(); - insertApplicationError(randomResolvedApplicationError()); + var unresolvedId2 = insertApplicationError(randomUnresolvedApplicationError()); var count = errorDao.deleteUnresolvedErrorsBefore(timeInBetween); assertThat(count).isOne(); + assertThat(errorDao.getById(resolvedId1)).isPresent(); + assertThat(errorDao.getById(unresolvedId1)).isEmpty(); + assertThat(errorDao.getById(unresolvedId2)).isPresent(); + } + + private static void sleep5ms() { + new DefaultEnvironment().sleepQuietly(5); } @Test