Skip to content

Commit

Permalink
#297 Fix test failures because of times with microseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
brianberlin committed Sep 12, 2023
1 parent 988bd3d commit 377123b
Show file tree
Hide file tree
Showing 13 changed files with 493 additions and 453 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.12.1-otp-24
erlang 24.0.2
elixir 1.14.5-otp-25
erlang 25.3.2
12 changes: 6 additions & 6 deletions lib/cocktail/schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,16 @@ defmodule Cocktail.Schedule do
...> schedule = start_time |> new() |> add_recurrence_rule(:daily)
...> schedule |> occurrences() |> Enum.take(3) |> Enum.map(&Timex.format!(&1, "{ISO:Extended}"))
["2017-01-02T10:00:00-08:00",
"2017-01-03T10:00:00-08:00",
"2017-01-04T10:00:00-08:00"]
"2017-01-03T10:00:00.000000-08:00",
"2017-01-04T10:00:00.000000-08:00"]
# using a NaiveDateTime with a duration
iex> start_time = ~N[2017-02-01 12:00:00]
iex> start_time = ~N[2017-02-01 12:00:00.000000]
...> schedule = start_time |> new(duration: 3_600) |> add_recurrence_rule(:weekly)
...> schedule |> occurrences() |> Enum.take(3)
[%Cocktail.Span{from: ~N[2017-02-01 12:00:00], until: ~N[2017-02-01 13:00:00]},
%Cocktail.Span{from: ~N[2017-02-08 12:00:00], until: ~N[2017-02-08 13:00:00]},
%Cocktail.Span{from: ~N[2017-02-15 12:00:00], until: ~N[2017-02-15 13:00:00]}]
[%Cocktail.Span{from: ~N[2017-02-01 12:00:00], until: ~N[2017-02-01 13:00:00.000000]},
%Cocktail.Span{from: ~N[2017-02-08 12:00:00.000000], until: ~N[2017-02-08 13:00:00.000000]},
%Cocktail.Span{from: ~N[2017-02-15 12:00:00.000000], until: ~N[2017-02-15 13:00:00.000000]}]
"""
@spec occurrences(t, Cocktail.time() | nil) :: Enumerable.t()
def occurrences(%__MODULE__{} = schedule, start_time \\ nil) do
Expand Down
119 changes: 60 additions & 59 deletions test/cocktail/daily_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Cocktail.DailyTest do
alias Cocktail.Schedule

import Cocktail.TestSupport.DateTimeSigil
import Cocktail.TestSupport.Assertions

test "Daily" do
times =
Expand All @@ -13,11 +14,11 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(3)

assert times == [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-02 06:00:00 America/Los_Angeles],
~Y[2017-01-03 06:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-02 06:00:00 America/Los_Angeles],
~Y[2017-01-03 06:00:00 America/Los_Angeles]
])
end

test "Every 2 days" do
Expand All @@ -28,11 +29,11 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(3)

assert times == [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-03 06:00:00 America/Los_Angeles],
~Y[2017-01-05 06:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-03 06:00:00 America/Los_Angeles],
~Y[2017-01-05 06:00:00 America/Los_Angeles]
])
end

test "Every 2 days / Every 3 days" do
Expand All @@ -44,15 +45,15 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(7)

assert times == [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-03 06:00:00 America/Los_Angeles],
~Y[2017-01-04 06:00:00 America/Los_Angeles],
~Y[2017-01-05 06:00:00 America/Los_Angeles],
~Y[2017-01-07 06:00:00 America/Los_Angeles],
~Y[2017-01-09 06:00:00 America/Los_Angeles],
~Y[2017-01-10 06:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-03 06:00:00 America/Los_Angeles],
~Y[2017-01-04 06:00:00 America/Los_Angeles],
~Y[2017-01-05 06:00:00 America/Los_Angeles],
~Y[2017-01-07 06:00:00 America/Los_Angeles],
~Y[2017-01-09 06:00:00 America/Los_Angeles],
~Y[2017-01-10 06:00:00 America/Los_Angeles]
])
end

test "Daily; overridden start time" do
Expand All @@ -63,11 +64,11 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences(~Y[2017-08-01 12:00:00 America/Los_Angeles])
|> Enum.take(3)

assert times == [
~Y[2017-08-02 06:00:00 America/Los_Angeles],
~Y[2017-08-03 06:00:00 America/Los_Angeles],
~Y[2017-08-04 06:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-08-02 06:00:00 America/Los_Angeles],
~Y[2017-08-03 06:00:00 America/Los_Angeles],
~Y[2017-08-04 06:00:00 America/Los_Angeles]
])
end

test "Daily on the 10th and 14th hours of the day" do
Expand All @@ -78,13 +79,13 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(5)

assert times == [
~Y[2017-01-01 10:00:00 America/Los_Angeles],
~Y[2017-01-01 14:00:00 America/Los_Angeles],
~Y[2017-01-02 10:00:00 America/Los_Angeles],
~Y[2017-01-02 14:00:00 America/Los_Angeles],
~Y[2017-01-03 10:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-01 10:00:00 America/Los_Angeles],
~Y[2017-01-01 14:00:00 America/Los_Angeles],
~Y[2017-01-02 10:00:00 America/Los_Angeles],
~Y[2017-01-02 14:00:00 America/Los_Angeles],
~Y[2017-01-03 10:00:00 America/Los_Angeles]
])
end

test "Every 6 days on Mondays and Fridays" do
Expand All @@ -95,16 +96,16 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(8)

assert times == [
~Y[2017-01-13 06:00:00 America/Los_Angeles],
~Y[2017-02-06 06:00:00 America/Los_Angeles],
~Y[2017-02-24 06:00:00 America/Los_Angeles],
~Y[2017-03-20 06:00:00 America/Los_Angeles],
~Y[2017-04-07 06:00:00 America/Los_Angeles],
~Y[2017-05-01 06:00:00 America/Los_Angeles],
~Y[2017-05-19 06:00:00 America/Los_Angeles],
~Y[2017-06-12 06:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-13 06:00:00 America/Los_Angeles],
~Y[2017-02-06 06:00:00 America/Los_Angeles],
~Y[2017-02-24 06:00:00 America/Los_Angeles],
~Y[2017-03-20 06:00:00 America/Los_Angeles],
~Y[2017-04-07 06:00:00 America/Los_Angeles],
~Y[2017-05-01 06:00:00 America/Los_Angeles],
~Y[2017-05-19 06:00:00 America/Los_Angeles],
~Y[2017-06-12 06:00:00 America/Los_Angeles]
])
end

test "Daily on Mondays and Fridays on the 10th and 14th hours of the day" do
Expand All @@ -115,13 +116,13 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(5)

assert times == [
~Y[2017-01-02 10:00:00 America/Los_Angeles],
~Y[2017-01-02 14:00:00 America/Los_Angeles],
~Y[2017-01-06 10:00:00 America/Los_Angeles],
~Y[2017-01-06 14:00:00 America/Los_Angeles],
~Y[2017-01-09 10:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-02 10:00:00 America/Los_Angeles],
~Y[2017-01-02 14:00:00 America/Los_Angeles],
~Y[2017-01-06 10:00:00 America/Los_Angeles],
~Y[2017-01-06 14:00:00 America/Los_Angeles],
~Y[2017-01-09 10:00:00 America/Los_Angeles]
])
end

test "Daily on Mondays and Fridays on the 10th and 14th hours of the day on the 15th and 45th minutes of the hour on the 0th and 30th seconds of the minute" do
Expand All @@ -138,17 +139,17 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(9)

assert times == [
~Y[2017-01-02 10:15:00 America/Los_Angeles],
~Y[2017-01-02 10:15:30 America/Los_Angeles],
~Y[2017-01-02 10:45:00 America/Los_Angeles],
~Y[2017-01-02 10:45:30 America/Los_Angeles],
~Y[2017-01-02 14:15:00 America/Los_Angeles],
~Y[2017-01-02 14:15:30 America/Los_Angeles],
~Y[2017-01-02 14:45:00 America/Los_Angeles],
~Y[2017-01-02 14:45:30 America/Los_Angeles],
~Y[2017-01-06 10:15:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-02 10:15:00 America/Los_Angeles],
~Y[2017-01-02 10:15:30 America/Los_Angeles],
~Y[2017-01-02 10:45:00 America/Los_Angeles],
~Y[2017-01-02 10:45:30 America/Los_Angeles],
~Y[2017-01-02 14:15:00 America/Los_Angeles],
~Y[2017-01-02 14:15:30 America/Los_Angeles],
~Y[2017-01-02 14:45:00 America/Los_Angeles],
~Y[2017-01-02 14:45:30 America/Los_Angeles],
~Y[2017-01-06 10:15:00 America/Los_Angeles]
])
end

test "generating occurrences when when add_recurrence_time is used" do
Expand Down
12 changes: 7 additions & 5 deletions test/cocktail/edge_cases_test.exs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
defmodule Cocktail.EdgeCasesTest do
use ExUnit.Case

import Cocktail.TestSupport.Assertions

alias Cocktail.Schedule

test "override start time to before the schedule's start time" do
schedule = ~N[2017-10-01 09:00:00] |> Schedule.new() |> Schedule.add_recurrence_rule(:daily)

times = schedule |> Schedule.occurrences(~N[2017-09-01 00:00:00]) |> Enum.take(3)

assert times == [
~N[2017-10-01 09:00:00],
~N[2017-10-02 09:00:00],
~N[2017-10-03 09:00:00]
]
assert_equivalent_times(times, [
~N[2017-10-01 09:00:00],
~N[2017-10-02 09:00:00],
~N[2017-10-03 09:00:00]
])
end
end
99 changes: 50 additions & 49 deletions test/cocktail/hourly_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Cocktail.HourlyTest do
alias Cocktail.Schedule

import Cocktail.TestSupport.DateTimeSigil
import Cocktail.TestSupport.Assertions

test "Hourly" do
times =
Expand All @@ -13,11 +14,11 @@ defmodule Cocktail.HourlyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(3)

assert times == [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-01 07:00:00 America/Los_Angeles],
~Y[2017-01-01 08:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-01 07:00:00 America/Los_Angeles],
~Y[2017-01-01 08:00:00 America/Los_Angeles]
])
end

test "Every 2 hours" do
Expand All @@ -28,11 +29,11 @@ defmodule Cocktail.HourlyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(3)

assert times == [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-01 08:00:00 America/Los_Angeles],
~Y[2017-01-01 10:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-01 08:00:00 America/Los_Angeles],
~Y[2017-01-01 10:00:00 America/Los_Angeles]
])
end

test "Every 2 hours / Every 3 hours" do
Expand All @@ -44,15 +45,15 @@ defmodule Cocktail.HourlyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(7)

assert times == [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-01 08:00:00 America/Los_Angeles],
~Y[2017-01-01 09:00:00 America/Los_Angeles],
~Y[2017-01-01 10:00:00 America/Los_Angeles],
~Y[2017-01-01 12:00:00 America/Los_Angeles],
~Y[2017-01-01 14:00:00 America/Los_Angeles],
~Y[2017-01-01 15:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-01 08:00:00 America/Los_Angeles],
~Y[2017-01-01 09:00:00 America/Los_Angeles],
~Y[2017-01-01 10:00:00 America/Los_Angeles],
~Y[2017-01-01 12:00:00 America/Los_Angeles],
~Y[2017-01-01 14:00:00 America/Los_Angeles],
~Y[2017-01-01 15:00:00 America/Los_Angeles]
])
end

test "Hourly; overridden start time" do
Expand All @@ -63,11 +64,11 @@ defmodule Cocktail.HourlyTest do
|> Cocktail.Schedule.occurrences(~Y[2017-08-01 12:00:00 America/Los_Angeles])
|> Enum.take(3)

assert times == [
~Y[2017-08-01 12:00:00 America/Los_Angeles],
~Y[2017-08-01 13:00:00 America/Los_Angeles],
~Y[2017-08-01 14:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-08-01 12:00:00 America/Los_Angeles],
~Y[2017-08-01 13:00:00 America/Los_Angeles],
~Y[2017-08-01 14:00:00 America/Los_Angeles]
])
end

test "Hourly on the 10th and 14th hours of the day" do
Expand All @@ -78,13 +79,13 @@ defmodule Cocktail.HourlyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(5)

assert times == [
~Y[2017-01-01 10:00:00 America/Los_Angeles],
~Y[2017-01-01 14:00:00 America/Los_Angeles],
~Y[2017-01-02 10:00:00 America/Los_Angeles],
~Y[2017-01-02 14:00:00 America/Los_Angeles],
~Y[2017-01-03 10:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-01 10:00:00 America/Los_Angeles],
~Y[2017-01-01 14:00:00 America/Los_Angeles],
~Y[2017-01-02 10:00:00 America/Los_Angeles],
~Y[2017-01-02 14:00:00 America/Los_Angeles],
~Y[2017-01-03 10:00:00 America/Los_Angeles]
])
end

test "Every 6 hours on Mondays and Fridays" do
Expand All @@ -95,17 +96,17 @@ defmodule Cocktail.HourlyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(9)

assert times == [
~Y[2017-01-02 00:00:00 America/Los_Angeles],
~Y[2017-01-02 06:00:00 America/Los_Angeles],
~Y[2017-01-02 12:00:00 America/Los_Angeles],
~Y[2017-01-02 18:00:00 America/Los_Angeles],
~Y[2017-01-06 00:00:00 America/Los_Angeles],
~Y[2017-01-06 06:00:00 America/Los_Angeles],
~Y[2017-01-06 12:00:00 America/Los_Angeles],
~Y[2017-01-06 18:00:00 America/Los_Angeles],
~Y[2017-01-09 00:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-02 00:00:00 America/Los_Angeles],
~Y[2017-01-02 06:00:00 America/Los_Angeles],
~Y[2017-01-02 12:00:00 America/Los_Angeles],
~Y[2017-01-02 18:00:00 America/Los_Angeles],
~Y[2017-01-06 00:00:00 America/Los_Angeles],
~Y[2017-01-06 06:00:00 America/Los_Angeles],
~Y[2017-01-06 12:00:00 America/Los_Angeles],
~Y[2017-01-06 18:00:00 America/Los_Angeles],
~Y[2017-01-09 00:00:00 America/Los_Angeles]
])
end

test "Hourly on Mondays and Fridays on the 10th and 14th hours of the day" do
Expand All @@ -116,12 +117,12 @@ defmodule Cocktail.HourlyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(5)

assert times == [
~Y[2017-01-02 10:00:00 America/Los_Angeles],
~Y[2017-01-02 14:00:00 America/Los_Angeles],
~Y[2017-01-06 10:00:00 America/Los_Angeles],
~Y[2017-01-06 14:00:00 America/Los_Angeles],
~Y[2017-01-09 10:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-02 10:00:00 America/Los_Angeles],
~Y[2017-01-02 14:00:00 America/Los_Angeles],
~Y[2017-01-06 10:00:00 America/Los_Angeles],
~Y[2017-01-06 14:00:00 America/Los_Angeles],
~Y[2017-01-09 10:00:00 America/Los_Angeles]
])
end
end
Loading

0 comments on commit 377123b

Please sign in to comment.