-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from mmacia/master
Added Faker::Date generators for date factories
- Loading branch information
Showing
3 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
require "./spec_helper" | ||
|
||
describe Faker::Date do | ||
describe "#between" do | ||
it "should return between date" do | ||
from = Time.parse("2019-01-01", "%Y-%m-%d", Time::Location::UTC) | ||
to = Time.parse("2022-01-01", "%Y-%m-%d", Time::Location::UTC) | ||
|
||
100.times do | ||
random_date = Faker::Date.between(from: from, to: to) | ||
random_date.should be >= from | ||
random_date.should be <= to | ||
end | ||
end | ||
|
||
it "should raise and exception on invalid date" do | ||
from = "2019-01-01" | ||
to = "0000-00-00" | ||
|
||
expect_raises(ArgumentError, "Invalid time") do | ||
Faker::Date.between(from: from, to: to) | ||
end | ||
end | ||
end | ||
|
||
describe "#between_except" do | ||
it "should return between date except gvien one" do | ||
from = "2012-01-01" | ||
to = "2012-01-05" | ||
excepted = "2012-01-03" | ||
|
||
100.times do | ||
random_date = Faker::Date.between_except(from: from, to: to, excepted: excepted) | ||
random_date.should_not be_nil | ||
random_date.should_not eq(Time.parse(excepted, "%Y-%m-%d", Time::Location::UTC)) | ||
end | ||
end | ||
|
||
it "should raise an excpetion when all args are the same" do | ||
from = "2012-01-01" | ||
to = "2012-01-01" | ||
excepted = "2012-01-01" | ||
|
||
expect_raises(ArgumentError, "From date, to date and excepted date must not be the same") do | ||
Faker::Date.between_except(from: from, to: to, excepted: excepted) | ||
end | ||
end | ||
end | ||
|
||
describe "#birthday" do | ||
it "should return a birthday" do | ||
min = 40 | ||
max = 90 | ||
|
||
t = Time.utc | ||
birthday_min = Time.utc(t.year - max, t.month, t.day) | ||
birthday_max = Time.utc(t.year - min, t.month, t.day) | ||
|
||
100.times do | ||
birthday = Faker::Date.birthday(min_age: min, max_age: max) | ||
|
||
birthday.should be >= birthday_min | ||
birthday.should be <= birthday_max | ||
end | ||
end | ||
|
||
it "should return today when min_age and max_age are the same" do | ||
min = 0 | ||
max = 0 | ||
|
||
t = Time.utc | ||
birthday = Faker::Date.birthday(min_age: min, max_age: max) | ||
|
||
birthday.should eq Time.utc(t.year, t.month, t.day) | ||
end | ||
end | ||
|
||
it "should return a forward date" do | ||
today = Time.utc | ||
|
||
100.times do | ||
random_date = Faker::Date.forward(days: 5) | ||
random_date.should be > today | ||
end | ||
end | ||
|
||
it "should return a backward date" do | ||
today = Time.utc | ||
|
||
100.times do | ||
random_date = Faker::Date.backward(days: 5) | ||
random_date.should be < today | ||
end | ||
end | ||
|
||
describe "#date_in_period" do | ||
it "should work with default params" do | ||
current_year = Time.utc.year | ||
|
||
10.times do | ||
date = Faker::Date.in_date_period | ||
date.year.should eq(current_year) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
module Faker | ||
class Date < Base | ||
def self.between(from : Time | String, to : Time | String) : Time | ||
from = parse_date(from) | ||
to = parse_date(to) | ||
|
||
Time.unix(Faker.rand_in_range(from.to_unix, to.to_unix)).at_beginning_of_day | ||
end | ||
|
||
def self.between_except(from : Time | String, to : Time | String, excepted : Time | String) | ||
raise ArgumentError.new("From date, to date and excepted date must not be the same") if from == to && to == excepted | ||
|
||
excepted = parse_date(excepted) | ||
|
||
loop do | ||
date = between(from: from, to: to) | ||
return date if date != excepted | ||
end | ||
end | ||
|
||
def self.birthday(min_age : Int32 = 18, max_age : Int32 = 65) : Time | ||
today = Time.utc | ||
|
||
from = birthday_date(today, max_age) | ||
to = birthday_date(today, min_age) | ||
|
||
between(from, to) | ||
end | ||
|
||
def self.forward(days : Int32 = 365) : Time | ||
from = Time.utc + 1.day | ||
to = Time.utc + days.days | ||
|
||
between(from: from, to: to) | ||
end | ||
|
||
def self.backward(days : Int32 = 365) : Time | ||
from = Time.utc - days.days | ||
to = Time.utc - 1.day | ||
|
||
between(from: from, to: to) | ||
end | ||
|
||
def self.in_date_period(month : Int32? = nil, year : Int32 = Time.utc.year) : Time | ||
from = Time.utc(year, month || 1, 1) | ||
|
||
to_month = month || 12 | ||
end_day = Time.utc(year, to_month, 1).at_end_of_month.day | ||
to = Time.utc(year, to_month, end_day) | ||
|
||
between(from: from, to: to) | ||
end | ||
|
||
private def self.parse_date(date : String) : Time | ||
Time.parse(date, "%Y-%m-%d", Time::Location::UTC) | ||
end | ||
|
||
private def self.parse_date(date : Time) : Time | ||
date | ||
end | ||
|
||
private def self.birthday_date(date : Time, age : Int32) : Time | ||
year = date.year - age | ||
day = | ||
if date.day == 29 && date.month == 2 && Time.leap_year?(year) | ||
28 | ||
else | ||
date.day | ||
end | ||
|
||
Time.utc(year, date.month, day) | ||
end | ||
end | ||
end |