Giter VIP home page Giter VIP logo

Comments (5)

snuyanzin avatar snuyanzin commented on June 28, 2024 1

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)));

from datafaker.

snuyanzin avatar snuyanzin commented on June 28, 2024

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

from datafaker.

remMirakl avatar remMirakl commented on June 28, 2024

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

from datafaker.

kingthorin avatar kingthorin commented on June 28, 2024

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.)

from datafaker.

bodiam avatar bodiam commented on June 28, 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.

from datafaker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.