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

Issue 534 fix date utils test #535

Merged
merged 31 commits into from
Feb 3, 2020
Merged

Issue 534 fix date utils test #535

merged 31 commits into from
Feb 3, 2020

Conversation

ggalmazor
Copy link
Contributor

Closes #534

This PR has become more than simply fixing the DateUtilsTest class into a review of the whole test suite to ensure we always deal with timezones and locales in a way that's consistent, safe and easier to understand.

What has been done to verify that this works as intended?

  • Run automated tests
  • Use external tools to double-check that the java.time values used to replace long timestamps actually represent the same instant.

Why is this the best possible solution? Were any other approaches considered?

  • Using java.time as much as possible is the safest way to move forward even if the implementation codebase will use Jodatime while we support Android API levels that don't include it.

    This is the standard way to deal with dates in Java starting from Java8 and it's very well documented with lots of examples.

    Also, using a java.time serves as a safe and solid baseline that provides an extra sanity check over our code and tests.

  • Using human readable date and time definitions in our tests makes them easier to understand. When something goes wrong, it will be easier to detect problems if we can read the values involved, as opposed to using timestamps.

  • Although Using the @Before and @After hooks to prevent side-effects due to setting default locales and time zones in test methods is super safe, using the methods provided by SystemHelper (new in this PR) make more explicit where this is actually required.

How does this change affect users? Describe intentional changes to behavior and behavior that could have accidentally been affected by code changes. In other words, what are the regression risks?

This PR doesn't change the implementation codebase, so no behavior change expected whatsoever.

Do we need any specific form for testing your changes? If so, please attach one.

No

Does this change require updates to documentation? If so, please file an issue here and include the link below.

No

a +2/-2 hours offset will only fail around midnight, whereas a +12/-13 hours offset ensures that any system will fail using one of them because the current date will fall into a different day.
Every time there's a time offset change, we need to ensure we use an updated "today at start of day" epoch
Using epoch values was working but it's harder to understand by humans than actual time strings.

Also, by using OffsetDateTime values and parsing strings with java.time we are ensuring as well that our test values are correctly parsed from string to date times because java.time is a battle tested Java API.

Now the only thing that could be wrong in the tests are the input and output time strings (both in human readable format, easy to check) and the offset definitions.
This way, we can parameterize the test and add more cases and context
Same reasons as a couple of commits before, where we argued that using OffsetDateTime.parse is easier to understand and safer.
Same as we did with DateUtils.parseTime
It doesn't feel important now that we're improving the test suite and adding more context to DateUtils
Let's use LocalDate.parse to verify we produce valid XML date strings
Previously, different JVMs could produce different textual formats for date parts (even the same JVM vendor and version in different environments)

The test now verifies what we really want to verify: that DateUtils.format produces localized outputs
The creation of the values for January and Sunday only depends on the locale
The default locale is reset after the block is run
Now the expected values are created where they're used. Less indirection
@codecov-io
Copy link

Codecov Report

Merging #535 into master will decrease coverage by 0.09%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             master    #535     +/-   ##
==========================================
- Coverage        53%   52.9%   -0.1%     
  Complexity     3111    3111             
==========================================
  Files           245     245             
  Lines         13340   13340             
  Branches       2566    2566             
==========================================
- Hits           7071    7058     -13     
- Misses         5451    5464     +13     
  Partials        818     818
Impacted Files Coverage Δ Complexity Δ
.../java/org/javarosa/core/model/utils/DateUtils.java 54.59% <0%> (-3.74%) 75% <0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 827cc1c...362a664. Read the comment docs.

@@ -77,27 +60,25 @@ public void testParseTime() {
// - We're effectively binding all times to the EPOCH date
Copy link
Member

Choose a reason for hiding this comment

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

Is this comment still accurate? Aren't all times now bound to whatever the current time of the device is?

@lognaturel lognaturel merged commit c845cac into getodk:master Feb 3, 2020
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class DateUtilsFormatSanityCheckTests {
Copy link
Member

Choose a reason for hiding this comment

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

This could probably be renamed. DateUtilsFormatTests would be better, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one was tricky to name. "Format" tells half of the story. These tests format a date and then parse it to verify we get the same value back.

@ggalmazor ggalmazor deleted the issue_534_fix_DateUtilsTest branch February 4, 2020 09:44
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.

Wrong parsing of time during certain hours of the day
3 participants