-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Add Faker::Date.day_of_week_between #2713
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the only issue that I mentioned before is if there is no valid day of the week inside the date range. I'm not sure to just return a date outside the range or raise an error about impossible days.
Hum, good question. If the result is outside the date range, it would be misleading. If it returns an error, since the result is generated randomly, it would not be a good UX to check the calendar for a "valid" date. I'm not sure what the solution is. I can think of is telling users what to do instead. For example: "No date generated between the date range. Try adding a wider range, or a different from
or to
". What do you think?
5a4f7b6
to
486fcde
Compare
I agree that that is probably best instead of generating a date outside the range. So now if you ask for a day of the week that is outside the date range, it raises an
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! Just left one suggestion for using a test helper.
from = Date.parse('2012-01-01') | ||
to = Date.parse('2012-02-01') | ||
|
||
100.times do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a deterministically_verify
test helper that is handy for this test.
@stefannibrasil I updated the test to use the |
from = Date.parse('2012-01-01') | ||
to = Date.parse('2012-02-01') | ||
|
||
deterministically_verify -> { @tester.on_day_of_week_between(day: days, from: from, to: to) } do |date| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Summary
This pull requests adds a
day_of_week_between
method to Faker::Date. It works similar to the existingFaker::Date::between
method, but you can ask for only specific days of the week.For example
Faker::Date.day_of_week_between(day: [:saturday, :sunday], from: 1.year.ago, to: Date.today)
will produce a random Saturday or Sunday in the last year.Possible Issues
It's possible that the given date range doesn't include a specific day of week. For example, if the date range is from Monday to Friday and the day of the week is supposed to be a Saturday, that won't work.
Right now it will return a Saturday outside the date range. I'm not familiar enough with Faker how that should be handled. Should that simply return a date outside the range or raise an error?
Other Information
I'm using this generator to create bookings for an app for a company that only accepts bookings on certain weekdays.