|
| 1 | +package org.simple.clinic.medicalhistory |
| 2 | + |
| 3 | + |
| 4 | +import org.junit.Assert.assertEquals |
| 5 | +import org.junit.Assert.assertNull |
| 6 | +import org.junit.Test |
| 7 | +import org.simple.clinic.medicalhistory.Answer.No |
| 8 | +import org.simple.clinic.medicalhistory.Answer.Suspected |
| 9 | +import org.simple.clinic.medicalhistory.Answer.Unanswered |
| 10 | +import org.simple.clinic.medicalhistory.Answer.Yes |
| 11 | +import java.time.Instant |
| 12 | + |
| 13 | +class DiagnosedAtTest { |
| 14 | + private val now = Instant.parse("2025-11-25T10:15:30Z") |
| 15 | + private val existingTs = Instant.parse("2025-01-01T00:00:00Z") |
| 16 | + |
| 17 | + private fun call( |
| 18 | + existingAnswer: Answer?, |
| 19 | + newAnswer: Answer, |
| 20 | + existingTimestamp: Instant?, |
| 21 | + now: Instant |
| 22 | + ): Instant? { |
| 23 | + return diagnosedAt(existingAnswer, newAnswer, existingTimestamp, now) |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + fun `when new answer is Suspected then returns null even if existing timestamp present`() { |
| 28 | + assertNull(call(existingAnswer = Yes, newAnswer = Suspected, existingTimestamp = existingTs, now = now)) |
| 29 | + assertNull(call(existingAnswer = null, newAnswer = Suspected, existingTimestamp = existingTs, now = now)) |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + fun `when existing timestamp is present and new answer is definitive then preserve the existing timestamp`() { |
| 34 | + // If timestamp exists it should be returned (write-once). |
| 35 | + assertEquals(existingTs, call(existingAnswer = Yes, newAnswer = Yes, existingTimestamp = existingTs, now = now)) |
| 36 | + assertEquals(existingTs, call(existingAnswer = Suspected, newAnswer = No, existingTimestamp = existingTs, now = now)) |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `when existingAnswer is null and new answer is yes or no then return now`() { |
| 41 | + assertEquals(now, call(existingAnswer = null, newAnswer = Yes, existingTimestamp = null, now = now)) |
| 42 | + assertEquals(now, call(existingAnswer = null, newAnswer = No, existingTimestamp = null, now = now)) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `when existingAnswer is null and new answer is Suspected or Unanswered then return null`() { |
| 47 | + assertNull(call(existingAnswer = null, newAnswer = Suspected, existingTimestamp = null, now = now)) |
| 48 | + assertNull(call(existingAnswer = null, newAnswer = Unanswered, existingTimestamp = null, now = now)) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `when existingAnswer is suspected or unanswered and new answer is yes or no then return now`() { |
| 53 | + // Previously Suspected -> new definitive should stamp now |
| 54 | + assertEquals(now, call(existingAnswer = Suspected, newAnswer = Yes, existingTimestamp = null, now = now)) |
| 55 | + assertEquals(now, call(existingAnswer = Unanswered, newAnswer = No, existingTimestamp = null, now = now)) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `when existingAnswer is yes or no with no timestamp and new answer is Yes or No then return null`() { |
| 60 | + assertNull(call(existingAnswer = Yes, newAnswer = No, existingTimestamp = null, now = now)) |
| 61 | + assertNull(call(existingAnswer = No, newAnswer = Yes, existingTimestamp = null, now = now)) |
| 62 | + assertNull(call(existingAnswer = Yes, newAnswer = Yes, existingTimestamp = null, now = now)) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `When existing timestamp is present then always preserve it except when suspected where return null`() { |
| 67 | + assertEquals(existingTs, call(existingAnswer = Yes, newAnswer = Yes, existingTimestamp = existingTs, now = now)) |
| 68 | + assertNull(call(existingAnswer = Yes, newAnswer = Suspected, existingTimestamp = existingTs, now = now)) |
| 69 | + } |
| 70 | +} |
0 commit comments