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

[#8557] Standardize locale #13162

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

itstrueitstrueitsrealitsreal
Copy link
Contributor

@itstrueitstrueitsrealitsreal itstrueitstrueitsrealitsreal commented Aug 6, 2024

Fixes #8557

OS: macOS Sonoma 14.2.1 arm64
Host: MacBook Pro (14-inch, 2021)
Locale: en_GB.UTF-8, de_DE.UTF-8

When running tests locally, some tests fail because of differences in locale, like teammates.common.util.TimeHelperTest.testGetMidnightAdjustedInstantBasedOnZone. Even though this might not be a problem when running tests as part of a workflow, it might cause confusion to some users who are running tests locally and encountering errors.

Testing on en_GB.UTF-8:

org.gradle.internal.serialize.PlaceholderAssertionError: expected: <Sun, 29 Nov 2015, 11:59 PM UTC> but was: <Sun, 29 Nov 2015, 11:59 pm UTC>

Testing on de_DE.UTF-8:

org.gradle.internal.serialize.PlaceholderAssertionError: expected: <Sun, 29 Nov 2015, 11:59 PM UTC> but was: <So., 29 Nov. 2015, 11:59 PM UTC>

For me, 5 tests were failing locally, so I ran this command to run the 5 tests which were failing.

./gradlew unitTests \ 
    --tests "teammates.common.util.TimeHelperTest.testFormatDateTimeForDisplay" \
    --tests "teammates.common.util.TimeHelperTest.testGetMidnightAdjustedInstantBasedOnZone" \
    --tests "teammates.logic.api.EmailGeneratorTest.testGenerateFeedbackSessionEmails" \
    --tests "teammates.logic.api.EmailGeneratorTest.testGenerateFeedbackSessionEmails_testSanitization" \
    --tests "teammates.logic.api.EmailGeneratorTest.testGenerateFeedbackSessionEmails_testUsersWithDeadlineExtensions"

Outline of Solution
I updated teammates/common/util/TimeHelper::formatInstant to use the US locale when generating dates, as seen below:

        DateTimeFormatter formatter = DateTimeFormatter
                .ofPattern(processedPattern)
                .withLocale(Locale.US);

This standardises the locale used in the app to use a US locale, so that the tests which are related to datetime formatting can pass locally.

After making this change, local tests pass on both de_DE.UTF-8 and en_GB.UTF-8.

I have yet to test this on Linux, which was what was reported in the referenced issue, but I believe that the issues that we are facing are somewhat related.

@itstrueitstrueitsrealitsreal itstrueitstrueitsrealitsreal marked this pull request as ready for review August 6, 2024 07:55
@mingyuanc
Copy link
Contributor

@itstrueitstrueitsrealitsreal Good solution, is there anyway you could test it on an linux enviroment before we merge?

@itstrueitstrueitsrealitsreal
Copy link
Contributor Author

@mingyuanc I'll get it tested by EOD.

@itstrueitstrueitsrealitsreal
Copy link
Contributor Author

itstrueitstrueitsrealitsreal commented Aug 7, 2024

OS: Arch Linux x86_64
Host: 20L8SA2N16 (ThinkPad T480s)
Locale: de_DE.UTF-8

Tests failed before changes:

component-tests > component-tests > teammates.common.util.TimeHelperTest > testGetMidnightAdjustedInstantBasedOnZone FAILED
component-tests > component-tests > teammates.common.util.TimeHelperTest > testFormatDateTimeForDisplay FAILED
component-tests > component-tests > teammates.common.util.TimeHelperTest > testEndOfYearDates FAILED
component-tests > component-tests > teammates.common.datatransfer.questions.FeedbackNumericalScaleQuestionDetailsTest > tesValidateResponseDetails FAILED
component-tests > component-tests > teammates.logic.api.EmailGeneratorTest > testGenerateFeedbackSessionEmails_testUsersWithDeadlineExtensions FAILED
component-tests > component-tests > teammates.logic.api.EmailGeneratorTest > testGenerateFeedbackSessionEmails_testUsersWithDeadlineExtensions FAILED
component-tests > component-tests > teammates.logic.api.EmailGeneratorTest > testGenerateFeedbackSessionEmails_testSanitization FAILED

Test failed after changes:

org.gradle.internal.serialize.PlaceholderAssertionError: expected: <5.1 is out of the range for Numerical-scale question.(min=3, max=5)> but was: <5,1 is out of the range for Numerical-scale question.(min=3, max=5)>
        at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
        at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
        at org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
        at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1145)
        at teammates.test.BaseTestCase.assertEquals(BaseTestCase.java:321)
        at teammates.common.datatransfer.questions.FeedbackNumericalScaleQuestionDetailsTest.tesValidateResponseDetails(FeedbackNumericalScaleQuestionDetailsTest.java:77)
        ...

component-tests > component-tests > teammates.common.datatransfer.questions.FeedbackNumericalScaleQuestionDetailsTest > tesValidateResponseDetails FAILED
    org.opentest4j.AssertionFailedError at FeedbackNumericalScaleQuestionDetailsTest.java:77

This seems to be a result of the DecimalFormat class using , as a delimiter in the locale I set my machine to, so I added a workaround where I used java.text.DecimalFormatSymbols::setDecimalSeparator to set the delimiter to . in StringHelper::toDecimalFormatString, as follows:

    /**
     * Converts a double value between 0 and 1 to 3dp-string.
     */
    public static String toDecimalFormatString(double doubleVal) {
        DecimalFormatSymbols syms = new DecimalFormatSymbols();
        syms.setDecimalSeparator('.');
        DecimalFormat df = new DecimalFormat("0.###", syms);
        return df.format(doubleVal);
    }

After making these changes, I changed my locale to:

en_US.UTF-8
en_SG.UTF-8

and ran the tests again, and they passed on all 3 locales.

For convenience, here's the command to run the 7 tests:

 ./gradlew unitTests --tests "teammates.common.util.TimeHelperTest.testGetMidnightAdjustedInstantBasedOnZone" \
    --tests "teammates.common.util.TimeHelperTest.testFormatDateTimeForDisplay" \
    --tests "teammates.common.util.TimeHelperTest.testEndOfYearDates" \
    --tests "teammates.common.datatransfer.questions.FeedbackNumericalScaleQuestionDetailsTest.tesValidateResponseDetails" \
    --tests "teammates.logic.api.EmailGeneratorTest.testGenerateFeedbackSessionEmails_testUsersWithDeadlineExtensions" \
    --tests "teammates.logic.api.EmailGeneratorTest.testGenerateFeedbackSessionEmails_testUsersWithDeadlineExtensions" \
    --tests "teammates.logic.api.EmailGeneratorTest.testGenerateFeedbackSessionEmails_testSanitization" \

@nusoss-bot
Copy link

Folks, This PR seems to be stalling (no activities for the past 7 days). 🐌 😢
Hope someone can get it to move forward again soon...

1 similar comment
@nusoss-bot
Copy link

Folks, This PR seems to be stalling (no activities for the past 7 days). 🐌 😢
Hope someone can get it to move forward again soon...

@nusoss-bot
Copy link

Folks, This PR seems to be stalling (no activities for the past 9 days). 🐌 😢
Hope someone can get it to move forward again soon...

Copy link
Contributor

@weiquu weiquu left a comment

Choose a reason for hiding this comment

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

LGTM! Will merge in after #13173 is fixed

@weiquu weiquu added the s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging label Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tests: certain tests are locale-dependent
4 participants