pass activity log tests

This commit is contained in:
shibao 2022-02-16 19:46:31 -05:00
parent e5b80f0338
commit 3c8851616a

View File

@ -1,67 +1,190 @@
defmodule Cannery.ActivityLogTest do defmodule Cannery.ActivityLogTest do
use Cannery.DataCase @moduledoc """
This module tests the ActivityLog context
"""
alias Cannery.ActivityLog use Cannery.DataCase
import Cannery.Fixtures
alias Cannery.{
ActivityLog,
ActivityLog.ShotGroup,
Ammo,
Containers
}
@moduletag :activity_log_test
describe "shot_groups" do describe "shot_groups" do
alias Cannery.ActivityLog.ShotGroup setup do
current_user = user_fixture()
container = container_fixture(current_user)
ammo_type = ammo_type_fixture(current_user)
import Cannery.ActivityLogFixtures %{id: ammo_group_id} =
ammo_group = ammo_group_fixture(%{"count" => 25}, ammo_type, container, current_user)
@invalid_attrs %{count: nil, date: nil, notes: nil} shot_group =
%{"count" => 5, "date" => ~N[2022-02-13 03:17:00], "notes" => "some notes"}
|> shot_group_fixture(current_user, ammo_group)
test "list_shot_groups/0 returns all shot_groups" do ammo_group = ammo_group_id |> Ammo.get_ammo_group!(current_user)
shot_group = shot_group_fixture()
assert ActivityLog.list_shot_groups() == [shot_group] [
current_user: current_user,
container: container,
ammo_type: ammo_type,
ammo_group: ammo_group,
shot_group: shot_group
]
end end
test "get_shot_group!/1 returns the shot_group with given id" do test "list_shot_groups/0 returns all shot_groups",
shot_group = shot_group_fixture() %{shot_group: shot_group, current_user: current_user} do
assert ActivityLog.get_shot_group!(shot_group.id) == shot_group assert ActivityLog.list_shot_groups(current_user) == [shot_group]
end end
test "create_shot_group/1 with valid data creates a shot_group" do test "get_shot_group!/1 returns the shot_group with given id",
valid_attrs = %{count: 42, date: ~N[2022-02-13 03:17:00], notes: "some notes"} %{shot_group: shot_group, current_user: current_user} do
assert ActivityLog.get_shot_group!(shot_group.id, current_user) == shot_group
end
assert {:ok, %ShotGroup{} = shot_group} = ActivityLog.create_shot_group(valid_attrs) test "get_shot_group!/1 does not return a shot_group of another user",
assert shot_group.count == 42 %{shot_group: shot_group, current_user: current_user} do
assert shot_group.date == ~N[2022-02-13 03:17:00] another_user = user_fixture()
assert_raise Ecto.NoResultsError, fn ->
ActivityLog.get_shot_group!(shot_group.id, another_user)
end
end
test "create_shot_group/1 with valid data creates a shot_group",
%{current_user: current_user, ammo_group: ammo_group} do
valid_attrs = %{"count" => 10, "date" => ~D[2022-02-13], "notes" => "some notes"}
assert {:ok, %ShotGroup{} = shot_group} =
ActivityLog.create_shot_group(valid_attrs, current_user, ammo_group)
assert shot_group.count == 10
assert shot_group.date == ~D[2022-02-13]
assert shot_group.notes == "some notes" assert shot_group.notes == "some notes"
end end
test "create_shot_group/1 with invalid data returns error changeset" do test "create_shot_group/1 removes corresponding count from ammo group",
assert {:error, %Ecto.Changeset{}} = ActivityLog.create_shot_group(@invalid_attrs) %{
end current_user: current_user,
ammo_group: %{id: ammo_group_id, count: org_count} = ammo_group
test "update_shot_group/2 with valid data updates the shot_group" do } do
shot_group = shot_group_fixture() valid_attrs = %{"count" => 10, "date" => ~D[2022-02-13], "notes" => "some notes"}
update_attrs = %{count: 43, date: ~N[2022-02-14 03:17:00], notes: "some updated notes"}
assert {:ok, %ShotGroup{} = shot_group} = assert {:ok, %ShotGroup{} = shot_group} =
ActivityLog.update_shot_group(shot_group, update_attrs) ActivityLog.create_shot_group(valid_attrs, current_user, ammo_group)
assert shot_group.count == 43 %{count: new_count} = ammo_group_id |> Ammo.get_ammo_group!(current_user)
assert shot_group.date == ~N[2022-02-14 03:17:00]
assert shot_group.notes == "some updated notes" assert org_count - shot_group.count == new_count
assert new_count == 10
end end
test "update_shot_group/2 with invalid data returns error changeset" do test "create_shot_group/1 does not remove more than ammo group amount",
shot_group = shot_group_fixture() %{
current_user: current_user,
ammo_group: %{id: ammo_group_id, count: org_count} = ammo_group
} do
valid_attrs = %{"count" => 20, "date" => ~D[2022-02-13], "notes" => "some notes"}
assert {:ok, %ShotGroup{} = shot_group} =
ActivityLog.create_shot_group(valid_attrs, current_user, ammo_group)
ammo_group = ammo_group_id |> Ammo.get_ammo_group!(current_user)
assert ammo_group.count == 0
assert {:error, %Ecto.Changeset{}} = assert {:error, %Ecto.Changeset{}} =
ActivityLog.update_shot_group(shot_group, @invalid_attrs) ActivityLog.create_shot_group(%{"count" => 1}, current_user, ammo_group)
assert shot_group == ActivityLog.get_shot_group!(shot_group.id)
end end
test "delete_shot_group/1 deletes the shot_group" do test "create_shot_group/1 with invalid data returns error changeset",
shot_group = shot_group_fixture() %{current_user: current_user, ammo_group: ammo_group} do
assert {:ok, %ShotGroup{}} = ActivityLog.delete_shot_group(shot_group) invalid_params = %{"count" => nil, "date" => nil, "notes" => nil}
assert_raise Ecto.NoResultsError, fn -> ActivityLog.get_shot_group!(shot_group.id) end
assert {:error, %Ecto.Changeset{}} =
ActivityLog.create_shot_group(invalid_params, current_user, ammo_group)
end end
test "change_shot_group/1 returns a shot_group changeset" do test "update_shot_group/2 with valid data updates the shot_group and ammo_group",
shot_group = shot_group_fixture() %{
shot_group: shot_group,
ammo_group: %{id: ammo_group_id} = ammo_group,
current_user: current_user
} do
assert {:ok, %ShotGroup{} = shot_group} =
ActivityLog.update_shot_group(
shot_group,
%{
"count" => 10,
"date" => ~D[2022-02-13],
"notes" => "some updated notes"
},
current_user
)
ammo_group = ammo_group_id |> Ammo.get_ammo_group!(current_user)
assert shot_group.count == 10
assert ammo_group.count == 15
assert shot_group.date == ~D[2022-02-13]
assert shot_group.notes == "some updated notes"
assert {:ok, %ShotGroup{} = shot_group} =
ActivityLog.update_shot_group(
shot_group,
%{
"count" => 25,
"date" => ~D[2022-02-13],
"notes" => "some updated notes"
},
current_user
)
ammo_group = ammo_group_id |> Ammo.get_ammo_group!(current_user)
assert shot_group.count == 25
assert ammo_group.count == 0
end
test "update_shot_group/2 with invalid data returns error changeset",
%{shot_group: shot_group, current_user: current_user} do
assert {:error, %Ecto.Changeset{}} =
ActivityLog.update_shot_group(
shot_group,
%{"count" => 26, "date" => nil, "notes" => nil},
current_user
)
assert {:error, %Ecto.Changeset{}} =
ActivityLog.update_shot_group(
shot_group,
%{"count" => -1, "date" => nil, "notes" => nil},
current_user
)
assert shot_group == ActivityLog.get_shot_group!(shot_group.id, current_user)
end
test "delete_shot_group/1 deletes the shot_group and adds value back",
%{shot_group: shot_group, current_user: current_user, ammo_group: %{id: ammo_group_id}} do
assert {:ok, %ShotGroup{}} = ActivityLog.delete_shot_group(shot_group, current_user)
assert %{count: 25} = ammo_group_id |> Ammo.get_ammo_group!(current_user)
assert_raise Ecto.NoResultsError, fn ->
ActivityLog.get_shot_group!(shot_group.id, current_user)
end
end
test "change_shot_group/1 returns a shot_group changeset",
%{shot_group: shot_group} do
assert %Ecto.Changeset{} = ActivityLog.change_shot_group(shot_group) assert %Ecto.Changeset{} = ActivityLog.change_shot_group(shot_group)
end end
end end