Skip to content

Commit

Permalink
Split up issue_comment ChangesetBuilder behavior into create and upda…
Browse files Browse the repository at this point in the history
…te cases
  • Loading branch information
begedin authored and joshsmith committed Sep 26, 2017
1 parent c2fab4e commit 836d83e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
31 changes: 20 additions & 11 deletions lib/code_corps/github/event/issue_comment/changeset_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,28 @@ defmodule CodeCorps.GitHub.Event.IssueComment.ChangesetBuilder do
webhook
"""
@spec build_changeset(Comment.t, map, Task.t, User.t) :: Changeset.t
def build_changeset(
%Comment{} = comment,
%{"comment" => comment_attrs},
%Task{id: task_id},
%User{id: user_id}) do
def build_changeset(%Comment{id: nil} = comment, %{"comment" => attrs}, %Task{} = task, %User{} = user) do
comment |> create_changeset(attrs, task, user)
end
def build_changeset(%Comment{} = comment, %{"comment" => attrs}, %Task{}, %User{}) do
comment |> update_changeset(attrs)
end

@spec create_changeset(Comment.t, map, Task.t, User.t) :: Changeset.t
defp create_changeset(%Comment{} = comment, %{} = attrs, %Task{} = task, %User{} = user) do
comment
|> Changeset.change(attrs |> CommentAdapter.from_api())
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
|> Changeset.put_assoc(:task, task)
|> Changeset.put_change(:user, user)
|> Changeset.validate_required([:markdown, :body])
end

@spec update_changeset(Comment.t, map) :: Changeset.t
defp update_changeset(%Comment{} = comment, %{} = attrs) do
comment
|> Changeset.change(comment_attrs |> CommentAdapter.from_api())
|> Changeset.change(attrs |> CommentAdapter.from_api())
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
|> Changeset.put_change(:task_id, task_id)
|> Changeset.put_change(:user_id, user_id)
|> Changeset.validate_required([:task_id, :user_id, :markdown, :body])
|> Changeset.assoc_constraint(:task)
|> Changeset.assoc_constraint(:user)
|> Changeset.validate_required([:markdown, :body])
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ defmodule CodeCorps.GitHub.Event.IssueComment.ChangesetBuilderTest do
use CodeCorps.DbAccessCase

import CodeCorps.GitHub.TestHelpers
import Ecto.Changeset

alias CodeCorps.{
GitHub.Event.IssueComment.ChangesetBuilder,
Comment
}
alias Ecto.Changeset

describe "build_changeset/3" do
test "assigns proper changes to the task" do
test "assigns proper changes to the comment, when it's new" do
payload = load_event_fixture("issue_comment_created")
comment = %Comment{}
task = insert(:task)
Expand All @@ -23,16 +23,43 @@ defmodule CodeCorps.GitHub.Event.IssueComment.ChangesetBuilderTest do
)

# adapted fields
assert get_change(changeset, :github_id) == payload["comment"]["id"]
assert get_change(changeset, :markdown) == payload["comment"]["body"]
assert Changeset.get_change(changeset, :github_id) == payload["comment"]["id"]
assert Changeset.get_change(changeset, :markdown) == payload["comment"]["body"]

# html was rendered
assert get_change(changeset, :body) ==
assert Changeset.get_change(changeset, :body) ==
Earmark.as_html!(payload["comment"]["body"], %Earmark.Options{code_class_prefix: "language-"})

# relationships are proper
assert get_change(changeset, :task_id) == task.id
assert get_change(changeset, :user_id) == user.id
assert changeset.changes.task.action == :update
assert changeset.changes.task.data == task
assert changeset.changes.user.action == :update
assert changeset.changes.user.data == user

assert changeset.valid?
end

test "assigns proper changes to the comment, when it existed previously" do
payload = load_event_fixture("issue_comment_created")
comment = insert(:comment)
task = insert(:task)
user = insert(:user)

changeset = ChangesetBuilder.build_changeset(
comment, payload, task, user
)

# adapted fields
assert Changeset.get_change(changeset, :github_id) == payload["comment"]["id"]
assert Changeset.get_change(changeset, :markdown) == payload["comment"]["body"]

# html was rendered
assert Changeset.get_change(changeset, :body) ==
Earmark.as_html!(payload["comment"]["body"], %Earmark.Options{code_class_prefix: "language-"})

# relationships are proper
refute changeset |> Changeset.get_change(:task)
refute changeset |> Changeset.get_change(:user)

assert changeset.valid?
end
Expand Down

0 comments on commit 836d83e

Please sign in to comment.