|
| 1 | +if Version.match?(System.version(), ">= 1.3.0") do |
| 2 | + defmodule DateTimeTest do |
| 3 | + use ExUnit.Case, async: true |
| 4 | + |
| 5 | + test "forward/1" do |
| 6 | + now = DateTime.utc_now() |
| 7 | + forwarded_date = Faker.DateTime.forward(10) |
| 8 | + assert %DateTime{ |
| 9 | + year: year, month: month, day: day, hour: hour, minute: minute, |
| 10 | + second: second, microsecond: microsecond |
| 11 | + } = forwarded_date |
| 12 | + assert now.year < year || now.month < month |
| 13 | + || now.hour < hour || now.minute < minute |
| 14 | + || now.day < day || now.second < second |
| 15 | + || now.microsecond < microsecond |
| 16 | + end |
| 17 | + |
| 18 | + test "backward/1" do |
| 19 | + now = DateTime.utc_now() |
| 20 | + backward_date = Faker.DateTime.backward(10) |
| 21 | + assert %DateTime{ |
| 22 | + year: year, month: month, day: day, hour: hour, minute: minute, |
| 23 | + second: second, microsecond: microsecond |
| 24 | + } = backward_date |
| 25 | + assert now.year > year || now.month > month |
| 26 | + || now.hour > hour || now.minute > minute |
| 27 | + || now.day > day || now.second > second |
| 28 | + || now.microsecond > microsecond |
| 29 | + end |
| 30 | + |
| 31 | + test "between/2 for Date.t" do |
| 32 | + from_date = DateTime.utc_now() |> DateTime.to_date |
| 33 | + to_date = Faker.DateTime.forward(50) |> DateTime.to_date |
| 34 | + between_date = Faker.DateTime.between(from_date, to_date) |
| 35 | + assert %DateTime{year: year, month: month, day: day} |
| 36 | + = between_date |
| 37 | + assert from_date.year <= year || from_date.month <= month |
| 38 | + || from_date.day <= day |
| 39 | + assert to_date.year >= year || to_date.month >= month |
| 40 | + || to_date.day >= day |
| 41 | + end |
| 42 | + |
| 43 | + test "between/2 for NaiveDateTime.t" do |
| 44 | + from_datetime = DateTime.utc_now() |
| 45 | + from_date = from_datetime |> DateTime.to_date |
| 46 | + from_time = from_datetime |> DateTime.to_time |
| 47 | + {:ok, from_naivedatetime} = NaiveDateTime.new(from_date, from_time) |
| 48 | + |
| 49 | + to_datetime = Faker.DateTime.forward(50) |
| 50 | + to_date = to_datetime |> DateTime.to_date |
| 51 | + to_time = to_datetime |> DateTime.to_time |
| 52 | + {:ok, to_naivedatetime} = NaiveDateTime.new(to_date, to_time) |
| 53 | + |
| 54 | + between_date = Faker.DateTime.between(from_naivedatetime, to_naivedatetime) |
| 55 | + assert %DateTime{ |
| 56 | + year: year, month: month, day: day, hour: hour, minute: minute, |
| 57 | + second: second, microsecond: microsecond |
| 58 | + } = between_date |
| 59 | + assert from_naivedatetime.year <= year || from_naivedatetime.month <= month |
| 60 | + || from_naivedatetime.hour <= hour || from_naivedatetime.minute <= minute |
| 61 | + || from_naivedatetime.day <= day || from_naivedatetime.second <= second |
| 62 | + || from_naivedatetime.microsecond <= microsecond |
| 63 | + assert to_naivedatetime.year >= year || to_naivedatetime.month >= month |
| 64 | + || to_naivedatetime.hour >= hour || to_naivedatetime.minute >= minute |
| 65 | + || to_naivedatetime.day >= day || to_naivedatetime.second >= second |
| 66 | + || to_naivedatetime.microsecond >= microsecond |
| 67 | + end |
| 68 | + |
| 69 | + test "between/2 for DateTime.t" do |
| 70 | + from_date = DateTime.utc_now() |
| 71 | + to_date = Faker.DateTime.forward(50) |
| 72 | + between_date = Faker.DateTime.between(from_date, to_date) |
| 73 | + assert %DateTime{ |
| 74 | + year: year, month: month, day: day, hour: hour, minute: minute, |
| 75 | + second: second, microsecond: microsecond |
| 76 | + } = between_date |
| 77 | + assert from_date.year <= year || from_date.month <= month |
| 78 | + || from_date.hour <= hour || from_date.minute <= minute |
| 79 | + || from_date.day <= day || from_date.second <= second |
| 80 | + || from_date.microsecond <= microsecond |
| 81 | + assert to_date.year >= year || to_date.month >= month |
| 82 | + || to_date.hour >= hour || to_date.minute >= minute |
| 83 | + || to_date.day >= day || to_date.second >= second |
| 84 | + || to_date.microsecond >= microsecond |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments