Merge pull request #2746 from ryanhaining/dev

Fixes timezone-dependent failing DateUtilTest
This commit is contained in:
Milos Kozak 2023-09-08 08:18:35 +02:00 committed by GitHub
commit ffb73ef14b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,21 +1,44 @@
package info.nightscout.core.utils package info.nightscout.core.utils
import android.content.Context import android.content.Context
import com.google.common.truth.Truth.assertThat
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.T import info.nightscout.shared.utils.T
import info.nightscout.sharedtests.TestBase import info.nightscout.sharedtests.TestBase
import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.AfterAll
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito.`when` import org.mockito.Mockito.`when`
import java.util.Date import java.util.Date
import java.util.SimpleTimeZone
import java.util.TimeZone
class DateUtilTest : TestBase() { class DateUtilTest : TestBase() {
@Mock lateinit var context: Context @Mock lateinit var context: Context
@Mock lateinit var rh: ResourceHelper @Mock lateinit var rh: ResourceHelper
companion object {
private lateinit var savedTimeZone: TimeZone
@BeforeAll
@JvmStatic
fun setDefaultTimezoneUtc() {
savedTimeZone = TimeZone.getDefault()
TimeZone.setDefault(SimpleTimeZone(0, "UTC"))
}
@AfterAll
@JvmStatic
fun restoreDefaultTimezone() {
TimeZone.setDefault(savedTimeZone)
}
}
@Test @Test
fun fromISODateStringTest() { fun fromISODateStringTest() {
Assertions.assertEquals(1511124634417L, DateUtil(context).fromISODateString("2017-11-19T22:50:34.417+0200")) Assertions.assertEquals(1511124634417L, DateUtil(context).fromISODateString("2017-11-19T22:50:34.417+0200"))
@ -40,7 +63,7 @@ class DateUtilTest : TestBase() {
} }
@Test fun dateStringTest() { @Test fun dateStringTest() {
Assertions.assertTrue(DateUtil(context).dateString(1513902750000L).contains("22")) assertThat(DateUtil(context).dateString(1513902750000L)).contains("22")
} }
@Test fun timeStringTest() { @Test fun timeStringTest() {
@ -48,15 +71,15 @@ class DateUtilTest : TestBase() {
} }
@Test fun dateAndTimeStringTest() { @Test fun dateAndTimeStringTest() {
Assertions.assertTrue(DateUtil(context).dateAndTimeString(1513902750000L).contains("22")) assertThat(DateUtil(context).dateAndTimeString(1513902750000L)).contains("22")
Assertions.assertTrue(DateUtil(context).dateAndTimeString(1513902750000L).contains("32")) assertThat(DateUtil(context).dateAndTimeString(1513902750000L)).contains("32")
} }
@Test fun dateAndTimeRangeStringTest() { @Test fun dateAndTimeRangeStringTest() {
Assertions.assertTrue(DateUtil(context).dateAndTimeRangeString(1513902750000L, 1513902750000L).contains("22")) assertThat(DateUtil(context).dateAndTimeRangeString(1513902750000L, 1513902750000L)).contains("22")
Assertions.assertTrue(DateUtil(context).dateAndTimeRangeString(1513902750000L, 1513902750000L).contains("32")) assertThat(DateUtil(context).dateAndTimeRangeString(1513902750000L, 1513902750000L)).contains("32")
Assertions.assertTrue(DateUtil(context).dateAndTimeRangeString(1513902750000L, 1513902750000L).contains("22")) assertThat(DateUtil(context).dateAndTimeRangeString(1513902750000L, 1513902750000L)).contains("22")
Assertions.assertTrue(DateUtil(context).dateAndTimeRangeString(1513902750000L, 1513902750000L).contains("32")) assertThat(DateUtil(context).dateAndTimeRangeString(1513902750000L, 1513902750000L)).contains("32")
} }
/* /*
@ -69,4 +92,4 @@ class DateUtilTest : TestBase() {
`when`(rh.gs(info.nightscout.shared.R.string.shorthour)).thenReturn("h") `when`(rh.gs(info.nightscout.shared.R.string.shorthour)).thenReturn("h")
Assertions.assertEquals("(1h 1')", DateUtil(context).timeFrameString(T.hours(1).msecs() + T.mins(1).msecs(), rh)) Assertions.assertEquals("(1h 1')", DateUtil(context).timeFrameString(T.hours(1).msecs() + T.mins(1).msecs(), rh))
} }
} }