Skip to content

Commit 6dcea77

Browse files
committed
Add test to for conversation part when updated with a status of reopened
1 parent 5ff666c commit 6dcea77

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

lib/code_corps/messages/messages.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ defmodule CodeCorps.Messages do
5252
Conversation |> Repo.get(id)
5353
end
5454

55+
@doc ~S"""
56+
Updates a `CodeCorps.Conversation` record
57+
"""
5558
def update_conversation(conversation, params) do
59+
%{status: status} = params
60+
case status do
61+
"reopened" ->
62+
add_part(%{"conversation_id" => conversation.id, "body" => "reopened", "author_id" => conversation.user_id, "part_type" => "reopened"})
63+
end
5664
conversation |> Conversation.update_changeset(params) |> Repo.update
5765
end
5866

test/lib/code_corps/messages/messages_test.exs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,19 +244,33 @@ defmodule CodeCorps.MessagesTest do
244244
end
245245

246246
describe "update_conversation/2" do
247-
@reopened "reopned"
248247

249-
test "creates a conversation_part of part_type reopened when reopened" do
248+
test "creates a conversation_part of part_type reopened when a conversation is reopened" do
250249
conversation = insert(:conversation)
251250

252251
assert Repo.aggregate(ConversationPart, :count, :id) == 0
252+
conversation = Messages.get_conversation(conversation.id)
253+
{_ok, _updated} = Messages.update_conversation(conversation,%{status: "reopened"})
254+
255+
assert Repo.aggregate(ConversationPart, :count, :id) == 1
253256

254-
conversation = Messages.get_conversation(conversation.id)
255-
256-
updated = Messages.update_conversation(conversation,%{status: "reopened"})
257+
conversation_part = Repo.get_by(ConversationPart, part_type: "reopened")
258+
assert conversation_part.author_id == conversation.user_id
259+
assert conversation_part.conversation_id == conversation.id
260+
end
261+
262+
test "creates a conversation_part of part_type closed when a conversation is reopened" do
263+
conversation = insert(:conversation)
257264

265+
assert Repo.aggregate(ConversationPart, :count, :id) == 0
266+
conversation = Messages.get_conversation(conversation.id)
267+
{_ok, _updated} = Messages.update_conversation(conversation,%{status: "closed"})
268+
258269
assert Repo.aggregate(ConversationPart, :count, :id) == 1
259-
assert Repo.get_by(ConversationPart, part_type: "reopened")
270+
271+
conversation_part = Repo.get_by(ConversationPart, part_type: "closed")
272+
assert conversation_part.author_id == conversation.user_id
273+
assert conversation_part.conversation_id == conversation.id
260274
end
261275
end
262276

0 commit comments

Comments
 (0)