Skip to content

Commit 2010caa

Browse files
authored
Merge pull request #1149 from code-corps/1130-add-tests-for-time-validator
Add tests for time validator
2 parents 886434b + 8aa5942 commit 2010caa

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

lib/code_corps/github/sync/comment/comment/changeset.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ defmodule CodeCorps.GitHub.Sync.Comment.Comment.Changeset do
7676
|> Changeset.cast(CommentAdapter.to_comment(attrs), @update_attrs)
7777
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
7878
|> Changeset.put_change(:modified_from, "github")
79-
|> TimeValidator.validate_time_after(:modified_at)
79+
|> TimeValidator.validate_time_not_before(:modified_at)
8080
|> Changeset.validate_required([:markdown, :body])
8181
end
8282
end

lib/code_corps/github/sync/issue/task/changeset.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ defmodule CodeCorps.GitHub.Sync.Issue.Task.Changeset do
8989
|> Changeset.cast(IssueAdapter.to_task(issue_attrs), @update_attrs)
9090
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
9191
|> Changeset.put_change(:modified_from, "github")
92-
|> TimeValidator.validate_time_after(:modified_at)
92+
|> TimeValidator.validate_time_not_before(:modified_at)
9393
|> Changeset.validate_required([:project_id, :title, :user_id])
9494
|> Changeset.assoc_constraint(:github_repo)
9595
|> Changeset.assoc_constraint(:project)

lib/code_corps/validators/time_validator.ex

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,21 @@ defmodule CodeCorps.Validators.TimeValidator do
66
alias Ecto.Changeset
77

88
@doc """
9-
Validates a time after a given time.
9+
Validates the new time is not before the previous time.
1010
"""
11-
def validate_time_after(%{data: data} = changeset, field) do
11+
def validate_time_not_before(%{data: data} = changeset, field) do
1212
previous_time = Map.get(data, field)
1313
current_time = Changeset.get_change(changeset, field)
1414
case current_time do
1515
nil -> changeset
16-
_ -> do_validate_time_after(changeset, field, previous_time, current_time)
16+
_ -> do_validate_time_not_before(changeset, field, previous_time, current_time)
1717
end
1818
end
1919

20-
defp do_validate_time_after(changeset, field, previous_time, current_time) do
21-
is_after = current_time |> Timex.after?(previous_time)
22-
is_equal = current_time |> Timex.equal?(previous_time)
23-
after_or_equal = is_after || is_equal
24-
case after_or_equal do
25-
true -> changeset
26-
false -> Changeset.add_error(changeset, field, "cannot be before the last recorded time")
20+
defp do_validate_time_not_before(changeset, field, previous_time, current_time) do
21+
case Timex.before?(current_time, previous_time) do
22+
true -> Changeset.add_error(changeset, field, "cannot be before the last recorded time")
23+
false -> changeset
2724
end
2825
end
2926
end

test/lib/code_corps/validators/time_validator_test.exs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,27 @@ defmodule CodeCorps.Validators.TimeValidatorTest do
55

66
@previous_time DateTime.utc_now
77

8-
describe "validate_time_after/2" do
8+
describe "validate_time_not_before/2" do
99
test "when the time happened before" do
1010
# set the time to 1 day before the previous (recorded) time
1111
current_time = @previous_time |> Timex.shift(days: -1)
1212
changeset = cast_times(@previous_time, current_time, :modified_at)
13-
changeset = changeset |> validate_time_after(:modified_at)
13+
changeset = changeset |> validate_time_not_before(:modified_at)
1414
refute changeset.valid?
1515
end
1616

17+
test "when the time happened at the same time" do
18+
current_time = @previous_time
19+
changeset = cast_times(@previous_time, current_time, :modified_at)
20+
changeset = changeset |> validate_time_not_before(:modified_at)
21+
assert changeset.valid?
22+
end
23+
1724
test "when the time happened after" do
1825
# set the time to 1 day after the previous (recorded) time
1926
current_time = @previous_time |> Timex.shift(days: 1)
2027
changeset = cast_times(@previous_time, current_time, :modified_at)
21-
changeset = changeset |> validate_time_after(:modified_at)
28+
changeset = changeset |> validate_time_not_before(:modified_at)
2229
assert changeset.valid?
2330
end
2431
end

0 commit comments

Comments
 (0)