forked from code-corps/code-corps-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_test.exs
More file actions
108 lines (93 loc) · 3.19 KB
/
task_test.exs
File metadata and controls
108 lines (93 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
defmodule CodeCorps.TaskTest do
use CodeCorps.ModelCase
alias CodeCorps.Task
@valid_attrs %{
title: "Test task",
task_type: "issue",
markdown: "A test task",
}
@invalid_attrs %{
task_type: "nonexistent"
}
describe "create/2" do
test "is invalid with invalid attributes" do
changeset = Task.changeset(%Task{}, @invalid_attrs)
refute changeset.valid?
end
test "only allows specific values for task_type" do
changes = Map.put(@valid_attrs, :task_type, "nonexistent")
changeset = Task.changeset(%Task{}, changes)
refute changeset.valid?
end
test "renders body html from markdown" do
user = insert(:user)
project = insert(:project)
changes = Map.merge(@valid_attrs, %{
markdown: "A **strong** body",
project_id: project.id,
user_id: user.id
})
changeset = Task.changeset(%Task{}, changes)
assert changeset.valid?
assert changeset |> get_change(:body) == "<p>A <strong>strong</strong> body</p>\n"
end
end
describe "create_changeset/2" do
test "is valid with valid attributes" do
user = insert(:user)
project = insert(:project)
changeset = Task.create_changeset(%Task{}, %{
markdown: "some content",
task_type: "issue",
title: "some content",
project_id: project.id,
user_id: user.id,
})
assert changeset.valid?
end
test "auto-sequences number, scoped to project" do
user = insert(:user)
project_a = insert(:project, title: "Project A")
project_b = insert(:project, title: "Project B")
insert(:task, project: project_a, user: user, title: "Project A Task 1")
insert(:task, project: project_a, user: user, title: "Project A Task 2")
insert(:task, project: project_b, user: user, title: "Project B Task 1")
changes = Map.merge(@valid_attrs, %{
project_id: project_a.id,
user_id: user.id
})
changeset = Task.create_changeset(%Task{}, changes)
{:ok, result} = Repo.insert(changeset)
result = Repo.get(Task, result.id)
assert result.number == 3
changes = Map.merge(@valid_attrs, %{
project_id: project_b.id,
user_id: user.id
})
changeset = Task.create_changeset(%Task{}, changes)
{:ok, result} = Repo.insert(changeset)
result = Repo.get(Task, result.id)
assert result.number == 2
end
test "sets state to 'published'" do
changeset = Task.create_changeset(%Task{}, %{})
assert changeset |> get_change(:state) == "published"
end
test "sets status to 'open'" do
changeset = Task.create_changeset(%Task{}, %{})
# open is default, so we `get_field` instead of `get_change`
assert changeset |> get_field(:status) == "open"
end
end
describe "update_changeset/2" do
test "sets state to 'edited'" do
changeset = Task.update_changeset(%Task{}, %{})
assert changeset |> get_change(:state) == "edited"
end
test "only allows specific values for status" do
changes = Map.put(@valid_attrs, :status, "nonexistent")
changeset = Task.update_changeset(%Task{}, changes)
refute changeset.valid?
end
end
end