Skip to content

Commit

Permalink
Add tests for time validator
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsmith committed Nov 5, 2017
1 parent 886434b commit 8aa5942
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/code_corps/github/sync/comment/comment/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ defmodule CodeCorps.GitHub.Sync.Comment.Comment.Changeset do
|> Changeset.cast(CommentAdapter.to_comment(attrs), @update_attrs)
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
|> Changeset.put_change(:modified_from, "github")
|> TimeValidator.validate_time_after(:modified_at)
|> TimeValidator.validate_time_not_before(:modified_at)
|> Changeset.validate_required([:markdown, :body])
end
end
2 changes: 1 addition & 1 deletion lib/code_corps/github/sync/issue/task/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ defmodule CodeCorps.GitHub.Sync.Issue.Task.Changeset do
|> Changeset.cast(IssueAdapter.to_task(issue_attrs), @update_attrs)
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
|> Changeset.put_change(:modified_from, "github")
|> TimeValidator.validate_time_after(:modified_at)
|> TimeValidator.validate_time_not_before(:modified_at)
|> Changeset.validate_required([:project_id, :title, :user_id])
|> Changeset.assoc_constraint(:github_repo)
|> Changeset.assoc_constraint(:project)
Expand Down
17 changes: 7 additions & 10 deletions lib/code_corps/validators/time_validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@ defmodule CodeCorps.Validators.TimeValidator do
alias Ecto.Changeset

@doc """
Validates a time after a given time.
Validates the new time is not before the previous time.
"""
def validate_time_after(%{data: data} = changeset, field) do
def validate_time_not_before(%{data: data} = changeset, field) do
previous_time = Map.get(data, field)
current_time = Changeset.get_change(changeset, field)
case current_time do
nil -> changeset
_ -> do_validate_time_after(changeset, field, previous_time, current_time)
_ -> do_validate_time_not_before(changeset, field, previous_time, current_time)
end
end

defp do_validate_time_after(changeset, field, previous_time, current_time) do
is_after = current_time |> Timex.after?(previous_time)
is_equal = current_time |> Timex.equal?(previous_time)
after_or_equal = is_after || is_equal
case after_or_equal do
true -> changeset
false -> Changeset.add_error(changeset, field, "cannot be before the last recorded time")
defp do_validate_time_not_before(changeset, field, previous_time, current_time) do
case Timex.before?(current_time, previous_time) do
true -> Changeset.add_error(changeset, field, "cannot be before the last recorded time")
false -> changeset
end
end
end
13 changes: 10 additions & 3 deletions test/lib/code_corps/validators/time_validator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ defmodule CodeCorps.Validators.TimeValidatorTest do

@previous_time DateTime.utc_now

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

test "when the time happened at the same time" do
current_time = @previous_time
changeset = cast_times(@previous_time, current_time, :modified_at)
changeset = changeset |> validate_time_not_before(:modified_at)
assert changeset.valid?
end

test "when the time happened after" do
# set the time to 1 day after the previous (recorded) time
current_time = @previous_time |> Timex.shift(days: 1)
changeset = cast_times(@previous_time, current_time, :modified_at)
changeset = changeset |> validate_time_after(:modified_at)
changeset = changeset |> validate_time_not_before(:modified_at)
assert changeset.valid?
end
end
Expand Down

0 comments on commit 8aa5942

Please sign in to comment.