|
1 | 1 | defmodule CodeCorps.ProjectTest do
|
2 | 2 | use CodeCorps.ModelCase
|
3 | 3 |
|
4 |
| - alias CodeCorps.Project |
| 4 | + import CodeCorps.Project |
| 5 | + |
| 6 | + alias CodeCorps.{Project, ProjectUser, Repo} |
5 | 7 |
|
6 | 8 | describe "changeset" do
|
7 | 9 | @valid_attrs %{title: "A title"}
|
@@ -43,42 +45,46 @@ defmodule CodeCorps.ProjectTest do
|
43 | 45 | end
|
44 | 46 | end
|
45 | 47 |
|
46 |
| - describe "create_changeset" do |
47 |
| - @valid_attrs %{title: "A title", organization_id: 1, owner_id: 1} |
48 |
| - @invalid_attrs %{} |
49 |
| - |
| 48 | + describe "create_changeset/3" do |
50 | 49 | test "with valid attributes" do
|
51 |
| - changeset = Project.create_changeset(%Project{}, @valid_attrs) |
| 50 | + organization = insert(:organization) |
| 51 | + attrs = %{title: "A title", organization_id: organization.id} |
| 52 | + changeset = create_changeset(%Project{}, attrs) |
52 | 53 | assert changeset.valid?
|
53 | 54 | end
|
54 | 55 |
|
55 | 56 | test "with invalid attributes" do
|
56 |
| - changeset = Project.create_changeset(%Project{}, @invalid_attrs) |
| 57 | + changeset = create_changeset(%Project{}, %{}) |
57 | 58 | refute changeset.valid?
|
58 | 59 | end
|
59 | 60 |
|
60 |
| - test "accepts setting of organization_id" do |
61 |
| - changeset = Project.create_changeset(%Project{}, %{organization_id: 1}) |
62 |
| - assert {:ok, 1} == changeset |> fetch_change(:organization_id) |
| 61 | + test "casts :organization_id and ensures organization exists" do |
| 62 | + attrs = %{title: "A title", organization_id: -1} |
| 63 | + changeset = create_changeset(%Project{}, attrs) |
| 64 | + |
| 65 | + assert {:error, failed_insert_changeset} = changeset |> Repo.insert() |
| 66 | + |
| 67 | + refute failed_insert_changeset.valid? |
| 68 | + assert error_message(failed_insert_changeset, :organization) == "does not exist" |
63 | 69 | end
|
64 | 70 |
|
65 |
| - test "associates the ordered default task lists to the project" do |
| 71 | + test "casts and inserts proper associated records" do |
66 | 72 | organization = insert(:organization)
|
67 |
| - user = insert(:user) |
68 |
| - changeset = Project.create_changeset( |
69 |
| - %Project{}, |
70 |
| - %{organization_id: organization.id, title: "Title", owner_id: user.id} |
71 |
| - ) |
| 73 | + attrs = %{title: "A title", organization_id: organization.id} |
| 74 | + changeset = Project.create_changeset(%Project{}, attrs) |
72 | 75 |
|
73 | 76 | {_, project} = Repo.insert(changeset)
|
74 | 77 |
|
75 | 78 | task_list_orders = for task_list <- project.task_lists, do: task_list.order
|
76 | 79 |
|
77 | 80 | assert Enum.all?(task_list_orders), "some of the orders are not set (nil)"
|
78 | 81 | assert task_list_orders == Enum.sort(task_list_orders), "task lists order does not correspond to their position"
|
79 |
| - end |
80 | 82 |
|
81 |
| - test "also inserts an owner role project_user record" |
| 83 | + project_user = Repo.one(ProjectUser) |
| 84 | + assert project_user.project_id == project.id |
| 85 | + assert project_user.user_id == organization.owner_id |
| 86 | + assert project_user.role == "owner" |
| 87 | + end |
82 | 88 | end
|
83 | 89 |
|
84 | 90 | describe "update_changeset" do
|
|
0 commit comments