Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for DateUtils and FileExtensions #1111

Merged
merged 9 commits into from
Nov 4, 2021

Conversation

sgallese
Copy link
Contributor

@sgallese sgallese commented Sep 20, 2021

Test coverage for FileExtensions and DateUtils
Prepare classes for migration in #1075

@sgallese sgallese force-pushed the feature/file-extensions-test branch 3 times, most recently from 95e20be to eb61444 Compare September 20, 2021 15:04
@sgallese
Copy link
Contributor Author

@iSoron These tests pass, but I am a bit confused by the DateUtil implementations for:
getStartOfTodayCalendarWithOffset() (test1, test 2)
getStartOfTodayWithOffset() (test1, test2)

For the above methods, these implementations are returning a non-offset time/calendar.
For example, if the offset is 3 hours, they still return a Midnight time/calendar.

This differs from the behavior of getStartOfTomorrowWithOffset() (test 1, test 2)
For example, if the offset is 3 hours, they return a 3AM time/calendar.

If this is not expected, let me know the correct expectation and I can create an issue and resolve.

@sgallese sgallese mentioned this pull request Sep 25, 2021
3 tasks
Copy link
Owner

@iSoron iSoron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the tests, @sgallese. I think they are good. I left just some minor comments below.

Comment on lines 145 to 163
@Test
fun testGetStartOfTomorrowWithOffset_priorToOffset() {
val hourOffset = 3
setStartDayOffset(hourOffset, 0)
setFixedTimeZone(TimeZone.getTimeZone("GMT"))
val startOfTomorrowWithOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset, 0)
val priorToOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset - 1, 0)
setFixedLocalTime(priorToOffset)
val startOfTomorrow = DateUtils.getStartOfTomorrowWithOffset()
assertThat(startOfTomorrowWithOffset, equalTo(startOfTomorrow))
}

@Test
fun testGetStartOfTomorrowWithOffset_afterOffset() {
val hourOffset = 3
setStartDayOffset(hourOffset, 0)
setFixedTimeZone(TimeZone.getTimeZone("GMT"))
val startOfTomorrowWithOffset = unixTime(2017, Calendar.JANUARY, 2, hourOffset, 0)
val afterOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset + 1, 0)
setFixedLocalTime(afterOffset)
val startOfTomorrow = DateUtils.getStartOfTomorrowWithOffset()
assertThat(startOfTomorrowWithOffset, equalTo(startOfTomorrow))
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two tests share too much code; could you please merge them into one? It's fine to to more than one assertion per test if it removes a lot of duplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to move the first three lines to a shared helper function, but the rest of the lines are different. If I'm missing something else obvious for DRYer test code, let me know! 707b2b4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, ended up writing much DRYer offset tests 4fbf8a8
See if these are better.
Once merged, I'll want to update the new non-JVM tests to reflect this logic in #1120

Comment on lines 329 to 354
@Test
fun testGetStartOfTodayCalendarWithOffset_priorToOffset() {
val hourOffset = 3
setStartDayOffset(hourOffset, 0)
val priorToOffset = unixTime(2017, Calendar.JANUARY, 2, hourOffset - 1, 0)
setFixedLocalTime(priorToOffset)
setFixedLocale(Locale.GERMANY)
val startOfYesterday = unixTime(2017, Calendar.JANUARY, 2, 0, 0)
val expectedCalendar = GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.GERMANY)
expectedCalendar.timeInMillis = startOfYesterday
val startOfTodayCalendar = DateUtils.getStartOfTodayCalendar()
assertThat(expectedCalendar, equalTo(startOfTodayCalendar))
}

@Test
fun testGetStartOfTodayCalendarWithOffset_afterOffset() {
val hourOffset = 3
setStartDayOffset(hourOffset, 0)
val afterOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset + 1, 0)
setFixedLocalTime(afterOffset)
setFixedLocale(Locale.GERMANY)
val startOfToday = unixTime(2017, Calendar.JANUARY, 1, 0, 0)
val expectedCalendar = GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.GERMANY)
expectedCalendar.timeInMillis = startOfToday
val startOfTodayCalendar = DateUtils.getStartOfTodayCalendar()
assertThat(expectedCalendar, equalTo(startOfTodayCalendar))
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment for these two tests.

@iSoron
Copy link
Owner

iSoron commented Oct 1, 2021

For the above methods, these implementations are returning a non-offset time/calendar.
For example, if the offset is 3 hours, they still return a Midnight time/calendar.

It's also unclear to me. I suggest that we write tests for the current behavior. Changing it might introduce subtle bugs.

The underlying issue here is that, historically, we have stored all dates as milliseconds since Jan 1, 1970, but, since we don't actually care about hours/minutes/seconds in most situations, we have also implicitly assumed that most millis are a multiple of DAY_LENGTH. I suspect that the reason these functions are returning midnight is to keep this assumption valid. We should eventually move everything that doesn't care about hours/minutes/seconds to LocalDate, which solves this ambiguity by storing time at a lower granularity.

@sgallese sgallese force-pushed the feature/file-extensions-test branch from 4f87dc3 to 707b2b4 Compare October 4, 2021 04:19
@sgallese sgallese requested a review from iSoron October 4, 2021 05:45
@sgallese
Copy link
Contributor Author

@iSoron OK to merge this PR?

@iSoron iSoron merged commit eb041bf into iSoron:dev Nov 4, 2021
@iSoron
Copy link
Owner

iSoron commented Nov 4, 2021

@sgallese Sorry, I missed the 'review requested' notification. It looks good now, thanks!

@sgallese sgallese deleted the feature/file-extensions-test branch November 4, 2021 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants