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

The faker.date().birthday() method returns different dates when using seed #1087

Closed
remMirakl opened this issue Feb 13, 2024 · 5 comments
Closed

Comments

@remMirakl
Copy link

Describe the bug
When using a seed, the faker.date().birthday() method returns different birthdates. Indeed, the birthday() method seems to use the current date to generate a random birthdate. So when the method is used another day, the result will be different. This behavior can break tests.
Capture d’écran 2024-02-13 à 15 36 56

To Reproduce
You can easily reproduce this bug by using a seed and use the faker.date().birthday() in a simple test that checks the date returned by the method.

Expected behavior
If you launch the test another day that the day you created it, the test will fail since the date returned will not be the same.

Versions:

  • OS: [e.g. OSX]
  • JDK
  • Faker Version [2.0.1]

Additional context
This was already an issue in the first java-faker

@snuyanzin
Copy link
Collaborator

This test is passing

    @Test
    void testSameBirthday() {
        final Faker faker = new Faker(new Random(1));
        final Date date1 = faker.date().birthday();
        final Faker faker2 = new Faker(new Random(1));
        final Date date2 = faker2.date().birthday();
        assertThat(date1).isEqualTo(date2);

        final Faker faker3 = new Faker(new Random(10));
        final Date date3 = faker3.date().birthday(10, 100);
        final Faker faker4 = new Faker(new Random(10));
        final Date date4 = faker4.date().birthday(10, 100);
        assertThat(date3).isEqualTo(date4);
    }

Please either submit a reproducible TC or I don't see an issue here

@remMirakl
Copy link
Author

The issue is that if you launch the test another day that you have created it, the birthdates generated by faker.date().birthday() will not give the same date so if you hardcoded the expected birthdate, the test will fail.

For example, I have this test :

    @Test
    void testBirthDate(){
        var expectedBirthDate =  LocalDate.of(1993, 6, 21);
        var actualBirthDate = myclass.getBirthDate();
        assertEquals(expectedBirthDate, actualBirthDate);
    }

and in my myclass i have :

    public LocalDate getBirthDate() {
        return faker.date().birthday().toLocalDateTime().toLocalDate();
    }

So if i launch my test today (14/02/24) it works but if i launch it tomorrow (15/02/24), the dates given by the faker will not be the same even when using a seed for the faker.

To show you that, i can change the date of my computer to 15/02/24 and this will happen :

org.opentest4j.AssertionFailedError: 
Expected :1993-06-21
Actual   :1993-06-22

@kingthorin
Copy link
Collaborator

I guess we could make another method where min and max age are only accurate to year (not month or day) and have the year be sticky (instead of using now). Would that be sufficient? (In this case the failure "should" only occur if you happen to run exactly at midnight on New Year's Eve.)

@snuyanzin
Copy link
Collaborator

snuyanzin commented Feb 14, 2024

This is expected behavior
the reason is that birthday returns a timestamp limited by min age and max age (if no args are passed then default values are 18 and 65) . Ages are defined by amount of days in the past from the current day. Each time there is a new date the ages will define new boundaries.
If you want to have something independent on current date then you can use between and pass there strict boundaries like that

Faker faker = new Faker(new Random(1));
System.out.println(faker.date().between(
new Date(LocalDate.of(1900, 1, 1).toEpochDay() * 86400 * 1000), 
new Date(LocalDate.of(2000, 1, 1).toEpochDay() * 86400 * 1000)));

@bodiam
Copy link
Contributor

bodiam commented Feb 14, 2024

Great discussion here. Maybe your actual implementation is slightly different from the example you gave, but I think I wouldn't recommend testing for the strict value of data generated by a random data generator.

The idea is that if you ask for a birth date, you may assume you're getting a birth date, say something which isn't in the future and isn't 200 years ago. That's something you could test for if you wanted, like:

assertThat(birthdate).isInPast(), or something like that.

Testing the exact value would be slightly trickier, and I personally don't much of a value. Maybe your use case is different though.

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

No branches or pull requests

4 participants