use atom keys in tests

This commit is contained in:
2023-03-28 21:57:29 -04:00
parent 8c95536ffd
commit bab2b26c13
28 changed files with 720 additions and 861 deletions

View File

@ -11,11 +11,11 @@ defmodule Cannery.InvitesTest do
@moduletag :invites_test
@valid_attrs %{
"name" => "some name"
name: "some name"
}
@invalid_attrs %{
"name" => nil,
"token" => nil
name: nil,
token: nil
}
describe "invites" do
@ -57,7 +57,7 @@ defmodule Cannery.InvitesTest do
assert {:ok, _user} =
Accounts.register_user(
%{"email" => unique_user_email(), "password" => valid_user_password()},
%{email: unique_user_email(), password: valid_user_password()},
token
)
@ -65,7 +65,7 @@ defmodule Cannery.InvitesTest do
assert {:ok, _user} =
Accounts.register_user(
%{"email" => unique_user_email(), "password" => valid_user_password()},
%{email: unique_user_email(), password: valid_user_password()},
token
)
@ -81,13 +81,13 @@ defmodule Cannery.InvitesTest do
assert {:ok, _user} =
Accounts.register_user(
%{"email" => unique_user_email(), "password" => valid_user_password()},
%{email: unique_user_email(), password: valid_user_password()},
token
)
assert {:ok, _user} =
Accounts.register_user(
%{"email" => unique_user_email(), "password" => valid_user_password()},
%{email: unique_user_email(), password: valid_user_password()},
another_token
)
@ -97,7 +97,7 @@ defmodule Cannery.InvitesTest do
assert {:ok, _user} =
Accounts.register_user(
%{"email" => unique_user_email(), "password" => valid_user_password()},
%{email: unique_user_email(), password: valid_user_password()},
token
)
@ -138,21 +138,14 @@ defmodule Cannery.InvitesTest do
test "create_invite/1 with valid data creates an unlimited invite",
%{current_user: current_user} do
assert {:ok, %Invite{} = invite} =
Invites.create_invite(current_user, %{
"name" => "some name"
})
assert {:ok, %Invite{} = invite} = Invites.create_invite(current_user, %{name: "some name"})
assert invite.name == "some name"
end
test "create_invite/1 with valid data creates a limited invite",
%{current_user: current_user} do
assert {:ok, %Invite{} = invite} =
Invites.create_invite(current_user, %{
"name" => "some name",
"uses_left" => 10
})
Invites.create_invite(current_user, %{name: "some name", uses_left: 10})
assert invite.name == "some name"
assert invite.uses_left == 10
@ -168,7 +161,7 @@ defmodule Cannery.InvitesTest do
assert {:ok, %Invite{} = new_invite} =
Invites.update_invite(
invite,
%{"name" => "some updated name", "uses_left" => 5},
%{name: "some updated name", uses_left: 5},
current_user
)
@ -178,12 +171,12 @@ defmodule Cannery.InvitesTest do
test "update_invite/2 can set an invite to be unlimited",
%{invite: invite, current_user: current_user} do
{:ok, invite} = Invites.update_invite(invite, %{"uses_left" => 5}, current_user)
{:ok, invite} = Invites.update_invite(invite, %{uses_left: 5}, current_user)
assert {:ok, %Invite{} = new_invite} =
Invites.update_invite(
invite,
%{"name" => "some updated name", "uses_left" => nil},
%{name: "some updated name", uses_left: nil},
current_user
)

View File

@ -63,8 +63,7 @@ defmodule Cannery.AccountsTest do
end
test "validates email and password when given" do
{:error, changeset} =
Accounts.register_user(%{"email" => "not valid", "password" => "not valid"})
{:error, changeset} = Accounts.register_user(%{email: "not valid", password: "not valid"})
assert %{
email: ["must have the @ sign and no spaces"],
@ -74,26 +73,25 @@ defmodule Cannery.AccountsTest do
test "validates maximum values for email and password for security" do
too_long = String.duplicate("db", 100)
{:error, changeset} = Accounts.register_user(%{"email" => too_long, "password" => too_long})
{:error, changeset} = Accounts.register_user(%{email: too_long, password: too_long})
assert "should be at most 160 character(s)" in errors_on(changeset).email
assert "should be at most 80 character(s)" in errors_on(changeset).password
end
test "validates email uniqueness" do
%{email: email} = user_fixture()
{:error, changeset} = Accounts.register_user(%{"email" => email})
{:error, changeset} = Accounts.register_user(%{email: email})
assert "has already been taken" in errors_on(changeset).email
# Now try with the upper cased email too, to check that email case is ignored.
{:error, changeset} = Accounts.register_user(%{"email" => String.upcase(email)})
{:error, changeset} = Accounts.register_user(%{email: String.upcase(email)})
assert "has already been taken" in errors_on(changeset).email
end
test "registers users with a hashed password" do
email = unique_user_email()
{:ok, user} =
Accounts.register_user(%{"email" => email, "password" => valid_user_password()})
{:ok, user} = Accounts.register_user(%{email: email, password: valid_user_password()})
assert user.email == email
assert is_binary(user.hashed_password)
@ -103,11 +101,11 @@ defmodule Cannery.AccountsTest do
test "records used invite during registration" do
{:ok, %{id: invite_id, token: token}} =
admin_fixture() |> Invites.create_invite(%{"name" => "my invite"})
admin_fixture() |> Invites.create_invite(%{name: "my invite"})
assert {:ok, %{invite_id: ^invite_id}} =
Accounts.register_user(
%{"email" => unique_user_email(), "password" => valid_user_password()},
%{email: unique_user_email(), password: valid_user_password()},
token
)
end
@ -123,7 +121,7 @@ defmodule Cannery.AccountsTest do
email = unique_user_email()
password = valid_user_password()
changeset = Accounts.change_user_registration(%{"email" => email, "password" => password})
changeset = Accounts.change_user_registration(%{email: email, password: password})
assert changeset.valid?
assert get_change(changeset, :email) == email
@ -151,7 +149,7 @@ defmodule Cannery.AccountsTest do
test "validates email", %{user: user} do
{:error, changeset} =
Accounts.apply_user_email(user, valid_user_password(), %{"email" => "not valid"})
Accounts.apply_user_email(user, valid_user_password(), %{email: "not valid"})
assert %{email: ["must have the @ sign and no spaces"]} = errors_on(changeset)
end
@ -160,7 +158,7 @@ defmodule Cannery.AccountsTest do
too_long = String.duplicate("db", 100)
{:error, changeset} =
Accounts.apply_user_email(user, valid_user_password(), %{"email" => too_long})
Accounts.apply_user_email(user, valid_user_password(), %{email: too_long})
assert "should be at most 160 character(s)" in errors_on(changeset).email
end
@ -169,21 +167,21 @@ defmodule Cannery.AccountsTest do
%{email: email} = user_fixture()
{:error, changeset} =
Accounts.apply_user_email(user, valid_user_password(), %{"email" => email})
Accounts.apply_user_email(user, valid_user_password(), %{email: email})
assert "has already been taken" in errors_on(changeset).email
end
test "validates current password", %{user: user} do
{:error, changeset} =
Accounts.apply_user_email(user, "invalid", %{"email" => unique_user_email()})
Accounts.apply_user_email(user, "invalid", %{email: unique_user_email()})
assert %{current_password: ["is not valid"]} = errors_on(changeset)
end
test "applies the email without persisting it", %{user: user} do
email = unique_user_email()
{:ok, user} = Accounts.apply_user_email(user, valid_user_password(), %{"email" => email})
{:ok, user} = Accounts.apply_user_email(user, valid_user_password(), %{email: email})
assert user.email == email
assert Accounts.get_user!(user.id).email != email
end
@ -258,11 +256,7 @@ defmodule Cannery.AccountsTest do
end
test "allows fields to be set" do
changeset =
Accounts.change_user_password(%User{}, %{
"password" => "new valid password"
})
changeset = Accounts.change_user_password(%User{}, %{password: "new valid password"})
assert changeset.valid?
assert get_change(changeset, :password) == "new valid password"
assert is_nil(get_change(changeset, :hashed_password))
@ -277,8 +271,8 @@ defmodule Cannery.AccountsTest do
test "validates password", %{user: user} do
{:error, changeset} =
Accounts.update_user_password(user, valid_user_password(), %{
"password" => "not valid",
"password_confirmation" => "another"
password: "not valid",
password_confirmation: "another"
})
assert %{
@ -291,14 +285,14 @@ defmodule Cannery.AccountsTest do
too_long = String.duplicate("db", 100)
{:error, changeset} =
Accounts.update_user_password(user, valid_user_password(), %{"password" => too_long})
Accounts.update_user_password(user, valid_user_password(), %{password: too_long})
assert "should be at most 80 character(s)" in errors_on(changeset).password
end
test "validates current password", %{user: user} do
{:error, changeset} =
Accounts.update_user_password(user, "invalid", %{"password" => valid_user_password()})
Accounts.update_user_password(user, "invalid", %{password: valid_user_password()})
assert %{current_password: ["is not valid"]} = errors_on(changeset)
end
@ -306,7 +300,7 @@ defmodule Cannery.AccountsTest do
test "updates the password", %{user: user} do
{:ok, user} =
Accounts.update_user_password(user, valid_user_password(), %{
"password" => "new valid password"
password: "new valid password"
})
assert is_nil(user.password)
@ -318,7 +312,7 @@ defmodule Cannery.AccountsTest do
{:ok, _} =
Accounts.update_user_password(user, valid_user_password(), %{
"password" => "new valid password"
password: "new valid password"
})
refute Repo.get_by(UserToken, user_id: user.id)
@ -486,8 +480,8 @@ defmodule Cannery.AccountsTest do
test "validates password", %{user: user} do
{:error, changeset} =
Accounts.reset_user_password(user, %{
"password" => "not valid",
"password_confirmation" => "another"
password: "not valid",
password_confirmation: "another"
})
assert %{
@ -498,13 +492,12 @@ defmodule Cannery.AccountsTest do
test "validates maximum values for password for security", %{user: user} do
too_long = String.duplicate("db", 100)
{:error, changeset} = Accounts.reset_user_password(user, %{"password" => too_long})
{:error, changeset} = Accounts.reset_user_password(user, %{password: too_long})
assert "should be at most 80 character(s)" in errors_on(changeset).password
end
test "updates the password", %{user: user} do
{:ok, updated_user} =
Accounts.reset_user_password(user, %{"password" => "new valid password"})
{:ok, updated_user} = Accounts.reset_user_password(user, %{password: "new valid password"})
assert is_nil(updated_user.password)
assert Accounts.get_user_by_email_and_password(user.email, "new valid password")
@ -512,7 +505,7 @@ defmodule Cannery.AccountsTest do
test "deletes all tokens for the given user", %{user: user} do
_session_token = Accounts.generate_user_session_token(user)
{:ok, _user} = Accounts.reset_user_password(user, %{"password" => "new valid password"})
{:ok, _user} = Accounts.reset_user_password(user, %{password: "new valid password"})
refute Repo.get_by(UserToken, user_id: user.id)
end
end

View File

@ -5,12 +5,7 @@ defmodule Cannery.ActivityLogTest do
use Cannery.DataCase
import Cannery.Fixtures
alias Cannery.{
ActivityLog,
ActivityLog.ShotGroup,
Ammo
}
alias Cannery.{ActivityLog, ActivityLog.ShotGroup, Ammo}
@moduletag :activity_log_test
@ -21,10 +16,10 @@ defmodule Cannery.ActivityLogTest do
ammo_type = ammo_type_fixture(current_user)
{1, [%{id: ammo_group_id} = ammo_group]} =
ammo_group_fixture(%{"count" => 25}, ammo_type, container, current_user)
ammo_group_fixture(%{count: 25}, ammo_type, container, current_user)
shot_group =
%{"count" => 5, "date" => ~N[2022-02-13 03:17:00], "notes" => "some notes"}
%{count: 5, date: ~N[2022-02-13 03:17:00], notes: "some notes"}
|> shot_group_fixture(current_user, ammo_group)
ammo_group = ammo_group_id |> Ammo.get_ammo_group!(current_user)
@ -54,7 +49,7 @@ defmodule Cannery.ActivityLogTest do
test "create_shot_group/3 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"}
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)
@ -69,7 +64,7 @@ defmodule Cannery.ActivityLogTest do
current_user: current_user,
ammo_group: %{id: ammo_group_id, count: org_count} = ammo_group
} do
valid_attrs = %{"count" => 10, "date" => ~D[2022-02-13], "notes" => "some notes"}
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)
@ -82,7 +77,7 @@ defmodule Cannery.ActivityLogTest do
test "create_shot_group/3 does not remove more than ammo group amount",
%{current_user: current_user, ammo_group: %{id: ammo_group_id} = ammo_group} do
valid_attrs = %{"count" => 20, "date" => ~D[2022-02-13], "notes" => "some notes"}
valid_attrs = %{count: 20, date: ~D[2022-02-13], notes: "some notes"}
assert {:ok, %ShotGroup{}} =
ActivityLog.create_shot_group(valid_attrs, current_user, ammo_group)
@ -92,12 +87,12 @@ defmodule Cannery.ActivityLogTest do
assert ammo_group.count == 0
assert {:error, %Ecto.Changeset{}} =
ActivityLog.create_shot_group(%{"count" => 1}, current_user, ammo_group)
ActivityLog.create_shot_group(%{count: 1}, current_user, ammo_group)
end
test "create_shot_group/3 with invalid data returns error changeset",
%{current_user: current_user, ammo_group: ammo_group} do
invalid_params = %{"count" => nil, "date" => nil, "notes" => nil}
invalid_params = %{count: nil, date: nil, notes: nil}
assert {:error, %Ecto.Changeset{}} =
ActivityLog.create_shot_group(invalid_params, current_user, ammo_group)
@ -113,9 +108,9 @@ defmodule Cannery.ActivityLogTest do
ActivityLog.update_shot_group(
shot_group,
%{
"count" => 10,
"date" => ~D[2022-02-13],
"notes" => "some updated notes"
count: 10,
date: ~D[2022-02-13],
notes: "some updated notes"
},
current_user
)
@ -131,9 +126,9 @@ defmodule Cannery.ActivityLogTest do
ActivityLog.update_shot_group(
shot_group,
%{
"count" => 25,
"date" => ~D[2022-02-13],
"notes" => "some updated notes"
count: 25,
date: ~D[2022-02-13],
notes: "some updated notes"
},
current_user
)
@ -149,14 +144,14 @@ defmodule Cannery.ActivityLogTest do
assert {:error, %Ecto.Changeset{}} =
ActivityLog.update_shot_group(
shot_group,
%{"count" => 26, "date" => nil, "notes" => nil},
%{count: 26, date: nil, notes: nil},
current_user
)
assert {:error, %Ecto.Changeset{}} =
ActivityLog.update_shot_group(
shot_group,
%{"count" => -1, "date" => nil, "notes" => nil},
%{count: -1, date: nil, notes: nil},
current_user
)
@ -184,10 +179,10 @@ defmodule Cannery.ActivityLogTest do
assert 0 = another_ammo_group |> ActivityLog.get_used_count(current_user)
assert 5 = ammo_group |> ActivityLog.get_used_count(current_user)
shot_group_fixture(%{"count" => 15}, current_user, ammo_group)
shot_group_fixture(%{count: 15}, current_user, ammo_group)
assert 20 = ammo_group |> ActivityLog.get_used_count(current_user)
shot_group_fixture(%{"count" => 10}, current_user, ammo_group)
shot_group_fixture(%{count: 10}, current_user, ammo_group)
assert 30 = ammo_group |> ActivityLog.get_used_count(current_user)
{1, [another_ammo_group]} = ammo_group_fixture(ammo_type, container, current_user)
@ -206,17 +201,17 @@ defmodule Cannery.ActivityLogTest do
assert %{ammo_group_id => 5} ==
[ammo_group, another_ammo_group] |> ActivityLog.get_used_counts(current_user)
shot_group_fixture(%{"count" => 5}, current_user, another_ammo_group)
shot_group_fixture(%{count: 5}, current_user, another_ammo_group)
used_counts = [ammo_group, another_ammo_group] |> ActivityLog.get_used_counts(current_user)
assert %{^ammo_group_id => 5} = used_counts
assert %{^another_ammo_group_id => 5} = used_counts
shot_group_fixture(%{"count" => 15}, current_user, ammo_group)
shot_group_fixture(%{count: 15}, current_user, ammo_group)
used_counts = [ammo_group, another_ammo_group] |> ActivityLog.get_used_counts(current_user)
assert %{^ammo_group_id => 20} = used_counts
assert %{^another_ammo_group_id => 5} = used_counts
shot_group_fixture(%{"count" => 10}, current_user, ammo_group)
shot_group_fixture(%{count: 10}, current_user, ammo_group)
used_counts = [ammo_group, another_ammo_group] |> ActivityLog.get_used_counts(current_user)
assert %{^ammo_group_id => 30} = used_counts
assert %{^another_ammo_group_id => 5} = used_counts
@ -233,10 +228,10 @@ defmodule Cannery.ActivityLogTest do
assert another_ammo_group |> ActivityLog.get_last_used_date(current_user) |> is_nil()
assert ^date = ammo_group |> ActivityLog.get_last_used_date(current_user)
%{date: date} = shot_group_fixture(%{"date" => ~D[2022-11-10]}, current_user, ammo_group)
%{date: date} = shot_group_fixture(%{date: ~D[2022-11-10]}, current_user, ammo_group)
assert ^date = ammo_group |> ActivityLog.get_last_used_date(current_user)
%{date: date} = shot_group_fixture(%{"date" => ~D[2022-11-11]}, current_user, ammo_group)
%{date: date} = shot_group_fixture(%{date: ~D[2022-11-11]}, current_user, ammo_group)
assert ^date = ammo_group |> ActivityLog.get_last_used_date(current_user)
end
@ -254,7 +249,7 @@ defmodule Cannery.ActivityLogTest do
assert %{ammo_group_id => date} ==
[ammo_group, another_ammo_group] |> ActivityLog.get_last_used_dates(current_user)
shot_group_fixture(%{"date" => ~D[2022-11-09]}, current_user, another_ammo_group)
shot_group_fixture(%{date: ~D[2022-11-09]}, current_user, another_ammo_group)
# setting initial date
last_used_shot_groups =
@ -264,7 +259,7 @@ defmodule Cannery.ActivityLogTest do
assert %{^another_ammo_group_id => ~D[2022-11-09]} = last_used_shot_groups
# setting another date
shot_group_fixture(%{"date" => ~D[2022-11-10]}, current_user, ammo_group)
shot_group_fixture(%{date: ~D[2022-11-10]}, current_user, ammo_group)
last_used_shot_groups =
[ammo_group, another_ammo_group] |> ActivityLog.get_last_used_dates(current_user)
@ -273,7 +268,7 @@ defmodule Cannery.ActivityLogTest do
assert %{^another_ammo_group_id => ~D[2022-11-09]} = last_used_shot_groups
# setting yet another date
shot_group_fixture(%{"date" => ~D[2022-11-11]}, current_user, ammo_group)
shot_group_fixture(%{date: ~D[2022-11-11]}, current_user, ammo_group)
last_used_shot_groups =
[ammo_group, another_ammo_group] |> ActivityLog.get_last_used_dates(current_user)
@ -288,10 +283,10 @@ defmodule Cannery.ActivityLogTest do
assert 0 = another_ammo_type |> ActivityLog.get_used_count_for_ammo_type(current_user)
assert 5 = ammo_type |> ActivityLog.get_used_count_for_ammo_type(current_user)
shot_group_fixture(%{"count" => 5}, current_user, ammo_group)
shot_group_fixture(%{count: 5}, current_user, ammo_group)
assert 10 = ammo_type |> ActivityLog.get_used_count_for_ammo_type(current_user)
shot_group_fixture(%{"count" => 1}, current_user, ammo_group)
shot_group_fixture(%{count: 1}, current_user, ammo_group)
assert 11 = ammo_type |> ActivityLog.get_used_count_for_ammo_type(current_user)
end
@ -309,7 +304,7 @@ defmodule Cannery.ActivityLogTest do
|> ActivityLog.get_used_count_for_ammo_types(current_user)
# use generated ammo group
shot_group_fixture(%{"count" => 5}, current_user, ammo_group)
shot_group_fixture(%{count: 5}, current_user, ammo_group)
used_counts =
[ammo_type, another_ammo_type] |> ActivityLog.get_used_count_for_ammo_types(current_user)
@ -318,7 +313,7 @@ defmodule Cannery.ActivityLogTest do
assert %{^another_ammo_type_id => 5} = used_counts
# use generated ammo group again
shot_group_fixture(%{"count" => 1}, current_user, ammo_group)
shot_group_fixture(%{count: 1}, current_user, ammo_group)
used_counts =
[ammo_type, another_ammo_type] |> ActivityLog.get_used_count_for_ammo_types(current_user)
@ -349,20 +344,20 @@ defmodule Cannery.ActivityLogTest do
other_container = container_fixture(other_user)
for type <- ["rifle", "shotgun", "pistol"] do
other_ammo_type = ammo_type_fixture(%{"type" => type}, other_user)
other_ammo_type = ammo_type_fixture(%{type: type}, other_user)
{1, [other_ammo_group]} = ammo_group_fixture(other_ammo_type, other_container, other_user)
shot_group_fixture(other_user, other_ammo_group)
end
rifle_ammo_type = ammo_type_fixture(%{"type" => "rifle"}, current_user)
rifle_ammo_type = ammo_type_fixture(%{type: "rifle"}, current_user)
{1, [rifle_ammo_group]} = ammo_group_fixture(rifle_ammo_type, container, current_user)
rifle_shot_group = shot_group_fixture(current_user, rifle_ammo_group)
shotgun_ammo_type = ammo_type_fixture(%{"type" => "shotgun"}, current_user)
shotgun_ammo_type = ammo_type_fixture(%{type: "shotgun"}, current_user)
{1, [shotgun_ammo_group]} = ammo_group_fixture(shotgun_ammo_type, container, current_user)
shotgun_shot_group = shot_group_fixture(current_user, shotgun_ammo_group)
pistol_ammo_type = ammo_type_fixture(%{"type" => "pistol"}, current_user)
pistol_ammo_type = ammo_type_fixture(%{type: "pistol"}, current_user)
{1, [pistol_ammo_group]} = ammo_group_fixture(pistol_ammo_type, container, current_user)
pistol_shot_group = shot_group_fixture(current_user, pistol_ammo_group)
@ -389,14 +384,14 @@ defmodule Cannery.ActivityLogTest do
container: container,
current_user: current_user
} do
shot_group_a = shot_group_fixture(%{"notes" => "amazing"}, current_user, ammo_group)
shot_group_a = shot_group_fixture(%{notes: "amazing"}, current_user, ammo_group)
{1, [another_ammo_group]} =
ammo_group_fixture(%{"notes" => "stupendous"}, ammo_type, container, current_user)
ammo_group_fixture(%{notes: "stupendous"}, ammo_type, container, current_user)
shot_group_b = shot_group_fixture(current_user, another_ammo_group)
another_ammo_type = ammo_type_fixture(%{"name" => "fabulous ammo"}, current_user)
another_ammo_type = ammo_type_fixture(%{name: "fabulous ammo"}, current_user)
{1, [yet_another_ammo_group]} =
ammo_group_fixture(another_ammo_type, container, current_user)

View File

@ -10,28 +10,28 @@ defmodule Cannery.AmmoTest do
@moduletag :ammo_test
@valid_attrs %{
"bullet_type" => "some bullet_type",
"case_material" => "some case_material",
"desc" => "some desc",
"manufacturer" => "some manufacturer",
"name" => "some name",
"grains" => 120
bullet_type: "some bullet_type",
case_material: "some case_material",
desc: "some desc",
manufacturer: "some manufacturer",
name: "some name",
grains: 120
}
@update_attrs %{
"bullet_type" => "some updated bullet_type",
"case_material" => "some updated case_material",
"desc" => "some updated desc",
"manufacturer" => "some updated manufacturer",
"name" => "some updated name",
"grains" => 456
bullet_type: "some updated bullet_type",
case_material: "some updated case_material",
desc: "some updated desc",
manufacturer: "some updated manufacturer",
name: "some updated name",
grains: 456
}
@invalid_attrs %{
"bullet_type" => nil,
"case_material" => nil,
"desc" => nil,
"manufacturer" => nil,
"name" => nil,
"grains" => nil
bullet_type: nil,
case_material: nil,
desc: nil,
manufacturer: nil,
name: nil,
grains: nil
}
describe "list_ammo_types/2" do
@ -40,34 +40,34 @@ defmodule Cannery.AmmoTest do
rifle_ammo_type =
%{
"name" => "bullets",
"type" => "rifle",
"desc" => "has some pews in it",
"grains" => 5
name: "bullets",
type: "rifle",
desc: "has some pews in it",
grains: 5
}
|> ammo_type_fixture(current_user)
shotgun_ammo_type =
%{
"name" => "hollows",
"type" => "shotgun",
"grains" => 3
name: "hollows",
type: "shotgun",
grains: 3
}
|> ammo_type_fixture(current_user)
pistol_ammo_type =
%{
"type" => "pistol",
"name" => "jackets",
"desc" => "brass shell",
"tracer" => true
type: "pistol",
name: "jackets",
desc: "brass shell",
tracer: true
}
|> ammo_type_fixture(current_user)
_shouldnt_return =
%{
"name" => "bullet",
"desc" => "pews brass shell"
name: "bullet",
desc: "pews brass shell"
}
|> ammo_type_fixture(user_fixture())
@ -228,7 +228,7 @@ defmodule Cannery.AmmoTest do
%{ammo_type: ammo_type, current_user: current_user, container: container} do
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 25.00, "count" => 1},
%{price_paid: 25.00, count: 1},
ammo_type,
container,
current_user
@ -238,7 +238,7 @@ defmodule Cannery.AmmoTest do
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 25.00, "count" => 1},
%{price_paid: 25.00, count: 1},
ammo_type,
container,
current_user
@ -248,7 +248,7 @@ defmodule Cannery.AmmoTest do
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 70.00, "count" => 1},
%{price_paid: 70.00, count: 1},
ammo_type,
container,
current_user
@ -258,7 +258,7 @@ defmodule Cannery.AmmoTest do
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 30.00, "count" => 1},
%{price_paid: 30.00, count: 1},
ammo_type,
container,
current_user
@ -282,7 +282,7 @@ defmodule Cannery.AmmoTest do
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 25.00, "count" => 1},
%{price_paid: 25.00, count: 1},
another_ammo_type,
container,
current_user
@ -294,7 +294,7 @@ defmodule Cannery.AmmoTest do
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 25.00, "count" => 1},
%{price_paid: 25.00, count: 1},
ammo_type,
container,
current_user
@ -308,7 +308,7 @@ defmodule Cannery.AmmoTest do
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 25.00, "count" => 1},
%{price_paid: 25.00, count: 1},
ammo_type,
container,
current_user
@ -322,7 +322,7 @@ defmodule Cannery.AmmoTest do
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 70.00, "count" => 1},
%{price_paid: 70.00, count: 1},
ammo_type,
container,
current_user
@ -336,7 +336,7 @@ defmodule Cannery.AmmoTest do
{1, [_ammo_group]} =
ammo_group_fixture(
%{"price_paid" => 30.00, "count" => 1},
%{price_paid: 30.00, count: 1},
ammo_type,
container,
current_user
@ -355,18 +355,18 @@ defmodule Cannery.AmmoTest do
assert 0 = Ammo.get_round_count_for_ammo_type(another_ammo_type, current_user)
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
ammo_group_fixture(%{count: 1}, ammo_type, container, current_user)
assert 1 = Ammo.get_round_count_for_ammo_type(ammo_type, current_user)
{1, [ammo_group]} = ammo_group_fixture(%{"count" => 50}, ammo_type, container, current_user)
{1, [ammo_group]} = ammo_group_fixture(%{count: 50}, ammo_type, container, current_user)
assert 51 = Ammo.get_round_count_for_ammo_type(ammo_type, current_user)
shot_group_fixture(%{"count" => 26}, current_user, ammo_group)
shot_group_fixture(%{count: 26}, current_user, ammo_group)
assert 25 = Ammo.get_round_count_for_ammo_type(ammo_type, current_user)
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
shot_group_fixture(%{count: 1}, current_user, first_ammo_group)
assert 24 = Ammo.get_round_count_for_ammo_type(ammo_type, current_user)
end
@ -376,7 +376,7 @@ defmodule Cannery.AmmoTest do
container: container
} do
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
ammo_group_fixture(%{count: 1}, ammo_type, container, current_user)
assert %{ammo_type_id => 1} ==
[ammo_type] |> Ammo.get_round_count_for_ammo_types(current_user)
@ -384,7 +384,7 @@ defmodule Cannery.AmmoTest do
%{id: another_ammo_type_id} = another_ammo_type = ammo_type_fixture(current_user)
{1, [_another_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, another_ammo_type, container, current_user)
ammo_group_fixture(%{count: 1}, another_ammo_type, container, current_user)
round_counts =
[ammo_type, another_ammo_type] |> Ammo.get_round_count_for_ammo_types(current_user)
@ -392,7 +392,7 @@ defmodule Cannery.AmmoTest do
assert %{^ammo_type_id => 1} = round_counts
assert %{^another_ammo_type_id => 1} = round_counts
{1, [ammo_group]} = ammo_group_fixture(%{"count" => 50}, ammo_type, container, current_user)
{1, [ammo_group]} = ammo_group_fixture(%{count: 50}, ammo_type, container, current_user)
round_counts =
[ammo_type, another_ammo_type] |> Ammo.get_round_count_for_ammo_types(current_user)
@ -400,7 +400,7 @@ defmodule Cannery.AmmoTest do
assert %{^ammo_type_id => 51} = round_counts
assert %{^another_ammo_type_id => 1} = round_counts
shot_group_fixture(%{"count" => 26}, current_user, ammo_group)
shot_group_fixture(%{count: 26}, current_user, ammo_group)
round_counts =
[ammo_type, another_ammo_type] |> Ammo.get_round_count_for_ammo_types(current_user)
@ -408,7 +408,7 @@ defmodule Cannery.AmmoTest do
assert %{^ammo_type_id => 25} = round_counts
assert %{^another_ammo_type_id => 1} = round_counts
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
shot_group_fixture(%{count: 1}, current_user, first_ammo_group)
round_counts =
[ammo_type, another_ammo_type] |> Ammo.get_round_count_for_ammo_types(current_user)
@ -422,18 +422,18 @@ defmodule Cannery.AmmoTest do
assert 0 = Ammo.get_historical_count_for_ammo_type(ammo_type, current_user)
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
ammo_group_fixture(%{count: 1}, ammo_type, container, current_user)
assert 1 = Ammo.get_historical_count_for_ammo_type(ammo_type, current_user)
{1, [ammo_group]} = ammo_group_fixture(%{"count" => 50}, ammo_type, container, current_user)
{1, [ammo_group]} = ammo_group_fixture(%{count: 50}, ammo_type, container, current_user)
assert 51 = Ammo.get_historical_count_for_ammo_type(ammo_type, current_user)
shot_group_fixture(%{"count" => 26}, current_user, ammo_group)
shot_group_fixture(%{count: 26}, current_user, ammo_group)
assert 51 = Ammo.get_historical_count_for_ammo_type(ammo_type, current_user)
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
shot_group_fixture(%{count: 1}, current_user, first_ammo_group)
assert 51 = Ammo.get_historical_count_for_ammo_type(ammo_type, current_user)
end
@ -446,7 +446,7 @@ defmodule Cannery.AmmoTest do
assert %{} == [ammo_type] |> Ammo.get_historical_count_for_ammo_types(current_user)
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
ammo_group_fixture(%{count: 1}, ammo_type, container, current_user)
assert %{ammo_type_id => 1} ==
[ammo_type] |> Ammo.get_historical_count_for_ammo_types(current_user)
@ -454,7 +454,7 @@ defmodule Cannery.AmmoTest do
%{id: another_ammo_type_id} = another_ammo_type = ammo_type_fixture(current_user)
{1, [_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, another_ammo_type, container, current_user)
ammo_group_fixture(%{count: 1}, another_ammo_type, container, current_user)
historical_counts =
[ammo_type, another_ammo_type] |> Ammo.get_historical_count_for_ammo_types(current_user)
@ -462,7 +462,7 @@ defmodule Cannery.AmmoTest do
assert %{^ammo_type_id => 1} = historical_counts
assert %{^another_ammo_type_id => 1} = historical_counts
{1, [ammo_group]} = ammo_group_fixture(%{"count" => 50}, ammo_type, container, current_user)
{1, [ammo_group]} = ammo_group_fixture(%{count: 50}, ammo_type, container, current_user)
historical_counts =
[ammo_type, another_ammo_type] |> Ammo.get_historical_count_for_ammo_types(current_user)
@ -470,7 +470,7 @@ defmodule Cannery.AmmoTest do
assert %{^ammo_type_id => 51} = historical_counts
assert %{^another_ammo_type_id => 1} = historical_counts
shot_group_fixture(%{"count" => 26}, current_user, ammo_group)
shot_group_fixture(%{count: 26}, current_user, ammo_group)
historical_counts =
[ammo_type, another_ammo_type] |> Ammo.get_historical_count_for_ammo_types(current_user)
@ -478,7 +478,7 @@ defmodule Cannery.AmmoTest do
assert %{^ammo_type_id => 51} = historical_counts
assert %{^another_ammo_type_id => 1} = historical_counts
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
shot_group_fixture(%{count: 1}, current_user, first_ammo_group)
historical_counts =
[ammo_type, another_ammo_type] |> Ammo.get_historical_count_for_ammo_types(current_user)
@ -492,18 +492,18 @@ defmodule Cannery.AmmoTest do
assert 0 = Ammo.get_used_ammo_groups_count_for_type(ammo_type, current_user)
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
ammo_group_fixture(%{count: 1}, ammo_type, container, current_user)
assert 0 = Ammo.get_used_ammo_groups_count_for_type(ammo_type, current_user)
{1, [ammo_group]} = ammo_group_fixture(%{"count" => 50}, ammo_type, container, current_user)
{1, [ammo_group]} = ammo_group_fixture(%{count: 50}, ammo_type, container, current_user)
assert 0 = Ammo.get_used_ammo_groups_count_for_type(ammo_type, current_user)
shot_group_fixture(%{"count" => 50}, current_user, ammo_group)
shot_group_fixture(%{count: 50}, current_user, ammo_group)
assert 1 = Ammo.get_used_ammo_groups_count_for_type(ammo_type, current_user)
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
shot_group_fixture(%{count: 1}, current_user, first_ammo_group)
assert 2 = Ammo.get_used_ammo_groups_count_for_type(ammo_type, current_user)
end
@ -525,7 +525,7 @@ defmodule Cannery.AmmoTest do
# testing ammo type with ammo group
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
ammo_group_fixture(%{count: 1}, ammo_type, container, current_user)
assert %{} ==
[ammo_type, another_ammo_type]
@ -533,17 +533,17 @@ defmodule Cannery.AmmoTest do
# testing ammo type with used ammo group
{1, [another_ammo_group]} =
ammo_group_fixture(%{"count" => 50}, another_ammo_type, container, current_user)
ammo_group_fixture(%{count: 50}, another_ammo_type, container, current_user)
shot_group_fixture(%{"count" => 50}, current_user, another_ammo_group)
shot_group_fixture(%{count: 50}, current_user, another_ammo_group)
assert %{another_ammo_type_id => 1} ==
[ammo_type, another_ammo_type]
|> Ammo.get_used_ammo_groups_count_for_types(current_user)
# testing two ammo types with zero and one used ammo groups
{1, [ammo_group]} = ammo_group_fixture(%{"count" => 50}, ammo_type, container, current_user)
shot_group_fixture(%{"count" => 50}, current_user, ammo_group)
{1, [ammo_group]} = ammo_group_fixture(%{count: 50}, ammo_type, container, current_user)
shot_group_fixture(%{count: 50}, current_user, ammo_group)
used_counts =
[ammo_type, another_ammo_type] |> Ammo.get_used_ammo_groups_count_for_types(current_user)
@ -552,7 +552,7 @@ defmodule Cannery.AmmoTest do
assert %{^another_ammo_type_id => 1} = used_counts
# testing two ammo type with one and two used ammo groups
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
shot_group_fixture(%{count: 1}, current_user, first_ammo_group)
used_counts =
[ammo_type, another_ammo_type] |> Ammo.get_used_ammo_groups_count_for_types(current_user)
@ -564,19 +564,18 @@ defmodule Cannery.AmmoTest do
test "get_ammo_groups_count_for_container!/2 gets accurate ammo count for container",
%{ammo_type: ammo_type, current_user: current_user, container: container} do
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 5}, ammo_type, container, current_user)
ammo_group_fixture(%{count: 5}, ammo_type, container, current_user)
assert 1 = Ammo.get_ammo_groups_count_for_container!(container, current_user)
{25, _ammo_groups} =
ammo_group_fixture(%{"count" => 5}, 25, ammo_type, container, current_user)
{25, _ammo_groups} = ammo_group_fixture(%{count: 5}, 25, ammo_type, container, current_user)
assert 26 = Ammo.get_ammo_groups_count_for_container!(container, current_user)
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
shot_group_fixture(%{count: 1}, current_user, first_ammo_group)
assert 26 = Ammo.get_ammo_groups_count_for_container!(container, current_user)
shot_group_fixture(%{"count" => 4}, current_user, first_ammo_group)
shot_group_fixture(%{count: 4}, current_user, first_ammo_group)
assert 25 = Ammo.get_ammo_groups_count_for_container!(container, current_user)
end
@ -588,10 +587,10 @@ defmodule Cannery.AmmoTest do
%{id: another_container_id} = another_container = container_fixture(current_user)
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 5}, ammo_type, container, current_user)
ammo_group_fixture(%{count: 5}, ammo_type, container, current_user)
{1, [_first_ammo_group]} =
ammo_group_fixture(%{"count" => 5}, ammo_type, another_container, current_user)
ammo_group_fixture(%{count: 5}, ammo_type, another_container, current_user)
ammo_groups_count =
[container, another_container]
@ -600,8 +599,7 @@ defmodule Cannery.AmmoTest do
assert %{^container_id => 1} = ammo_groups_count
assert %{^another_container_id => 1} = ammo_groups_count
{25, _ammo_groups} =
ammo_group_fixture(%{"count" => 5}, 25, ammo_type, container, current_user)
{25, _ammo_groups} = ammo_group_fixture(%{count: 5}, 25, ammo_type, container, current_user)
ammo_groups_count =
[container, another_container]
@ -610,7 +608,7 @@ defmodule Cannery.AmmoTest do
assert %{^container_id => 26} = ammo_groups_count
assert %{^another_container_id => 1} = ammo_groups_count
shot_group_fixture(%{"count" => 1}, current_user, first_ammo_group)
shot_group_fixture(%{count: 1}, current_user, first_ammo_group)
ammo_groups_count =
[container, another_container]
@ -619,7 +617,7 @@ defmodule Cannery.AmmoTest do
assert %{^container_id => 26} = ammo_groups_count
assert %{^another_container_id => 1} = ammo_groups_count
shot_group_fixture(%{"count" => 4}, current_user, first_ammo_group)
shot_group_fixture(%{count: 4}, current_user, first_ammo_group)
ammo_groups_count =
[container, another_container]
@ -632,16 +630,15 @@ defmodule Cannery.AmmoTest do
test "get_round_count_for_container!/2 gets accurate total round count for container",
%{ammo_type: ammo_type, current_user: current_user, container: container} do
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 5}, ammo_type, container, current_user)
ammo_group_fixture(%{count: 5}, ammo_type, container, current_user)
assert 5 = Ammo.get_round_count_for_container!(container, current_user)
{25, _ammo_groups} =
ammo_group_fixture(%{"count" => 5}, 25, ammo_type, container, current_user)
{25, _ammo_groups} = ammo_group_fixture(%{count: 5}, 25, ammo_type, container, current_user)
assert 130 = Ammo.get_round_count_for_container!(container, current_user)
shot_group_fixture(%{"count" => 5}, current_user, first_ammo_group)
shot_group_fixture(%{count: 5}, current_user, first_ammo_group)
assert 125 = Ammo.get_round_count_for_container!(container, current_user)
end
@ -654,10 +651,10 @@ defmodule Cannery.AmmoTest do
%{id: another_container_id} = another_container = container_fixture(current_user)
{1, [first_ammo_group]} =
ammo_group_fixture(%{"count" => 5}, ammo_type, container, current_user)
ammo_group_fixture(%{count: 5}, ammo_type, container, current_user)
{1, [_first_ammo_group]} =
ammo_group_fixture(%{"count" => 5}, ammo_type, another_container, current_user)
ammo_group_fixture(%{count: 5}, ammo_type, another_container, current_user)
round_counts =
[container, another_container] |> Ammo.get_round_count_for_containers(current_user)
@ -665,8 +662,7 @@ defmodule Cannery.AmmoTest do
assert %{^container_id => 5} = round_counts
assert %{^another_container_id => 5} = round_counts
{25, _ammo_groups} =
ammo_group_fixture(%{"count" => 5}, 25, ammo_type, container, current_user)
{25, _ammo_groups} = ammo_group_fixture(%{count: 5}, 25, ammo_type, container, current_user)
round_counts =
[container, another_container] |> Ammo.get_round_count_for_containers(current_user)
@ -674,7 +670,7 @@ defmodule Cannery.AmmoTest do
assert %{^container_id => 130} = round_counts
assert %{^another_container_id => 5} = round_counts
shot_group_fixture(%{"count" => 5}, current_user, first_ammo_group)
shot_group_fixture(%{count: 5}, current_user, first_ammo_group)
round_counts =
[container, another_container] |> Ammo.get_round_count_for_containers(current_user)
@ -686,20 +682,20 @@ defmodule Cannery.AmmoTest do
describe "ammo_groups" do
@valid_attrs %{
"count" => 42,
"notes" => "some notes",
"price_paid" => 120.5,
"purchased_on" => ~D[2022-11-19]
count: 42,
notes: "some notes",
price_paid: 120.5,
purchased_on: ~D[2022-11-19]
}
@update_attrs %{
"count" => 43,
"notes" => "some updated notes",
"price_paid" => 456.7
count: 43,
notes: "some updated notes",
price_paid: 456.7
}
@invalid_attrs %{
"count" => nil,
"notes" => nil,
"price_paid" => nil
count: nil,
notes: nil,
price_paid: nil
}
setup do
@ -708,8 +704,7 @@ defmodule Cannery.AmmoTest do
container = container_fixture(current_user)
{1, [ammo_group]} =
%{"count" => 50, "price_paid" => 36.1}
|> ammo_group_fixture(ammo_type, container, current_user)
ammo_group_fixture(%{count: 50, price_paid: 36.1}, ammo_type, container, current_user)
another_user = user_fixture()
another_ammo_type = ammo_type_fixture(another_user)
@ -744,9 +739,9 @@ defmodule Cannery.AmmoTest do
other_container = container_fixture(other_user)
{1, [another_ammo_group]} =
ammo_group_fixture(%{"count" => 30}, other_ammo_type, other_container, other_user)
ammo_group_fixture(%{count: 30}, other_ammo_type, other_container, other_user)
shot_group_fixture(%{"count" => 30}, other_user, another_ammo_group)
shot_group_fixture(%{count: 30}, other_user, another_ammo_group)
assert Ammo.get_ammo_groups_count!(other_user) == 0
assert Ammo.get_ammo_groups_count!(other_user, true) == 1
end
@ -755,11 +750,11 @@ defmodule Cannery.AmmoTest do
current_user = user_fixture()
container = container_fixture(current_user)
rifle_ammo_type = ammo_type_fixture(%{"type" => "rifle"}, current_user)
rifle_ammo_type = ammo_type_fixture(%{type: "rifle"}, current_user)
{1, [rifle_ammo_group]} = ammo_group_fixture(rifle_ammo_type, container, current_user)
shotgun_ammo_type = ammo_type_fixture(%{"type" => "shotgun"}, current_user)
shotgun_ammo_type = ammo_type_fixture(%{type: "shotgun"}, current_user)
{1, [shotgun_ammo_group]} = ammo_group_fixture(shotgun_ammo_type, container, current_user)
pistol_ammo_type = ammo_type_fixture(%{"type" => "pistol"}, current_user)
pistol_ammo_type = ammo_type_fixture(%{type: "pistol"}, current_user)
{1, [pistol_ammo_group]} = ammo_group_fixture(pistol_ammo_type, container, current_user)
assert [^rifle_ammo_group] = Ammo.list_ammo_groups(nil, :rifle, current_user, false)
@ -786,9 +781,9 @@ defmodule Cannery.AmmoTest do
current_user: current_user
} do
{1, [%{id: another_ammo_group_id} = another_ammo_group]} =
ammo_group_fixture(%{"count" => 30}, ammo_type, container, current_user)
ammo_group_fixture(%{count: 30}, ammo_type, container, current_user)
shot_group_fixture(%{"count" => 30}, current_user, another_ammo_group)
shot_group_fixture(%{count: 30}, current_user, another_ammo_group)
another_ammo_group = Ammo.get_ammo_group!(another_ammo_group_id, current_user)
assert Ammo.list_ammo_groups(nil, :all, current_user, false) == [ammo_group]
@ -806,20 +801,20 @@ defmodule Cannery.AmmoTest do
current_user: current_user
} do
{1, [another_ammo_group]} =
%{"count" => 49, "notes" => "cool ammo group"}
%{count: 49, notes: "cool ammo group"}
|> ammo_group_fixture(ammo_type, container, current_user)
another_ammo_type = ammo_type_fixture(%{"name" => "amazing ammo"}, current_user)
another_container = container_fixture(%{"name" => "fantastic container"}, current_user)
another_ammo_type = ammo_type_fixture(%{name: "amazing ammo"}, current_user)
another_container = container_fixture(%{name: "fantastic container"}, current_user)
tag = tag_fixture(%{"name" => "stupendous tag"}, current_user)
tag = tag_fixture(%{name: "stupendous tag"}, current_user)
Containers.add_tag!(another_container, tag, current_user)
{1, [amazing_ammo_group]} =
ammo_group_fixture(%{"count" => 48}, another_ammo_type, container, current_user)
ammo_group_fixture(%{count: 48}, another_ammo_type, container, current_user)
{1, [fantastic_ammo_group]} =
ammo_group_fixture(%{"count" => 47}, ammo_type, another_container, current_user)
ammo_group_fixture(%{count: 47}, ammo_type, another_container, current_user)
ammo_groups = Ammo.list_ammo_groups(nil, :all, current_user, false)
assert Enum.count(ammo_groups) == 4
@ -863,11 +858,11 @@ defmodule Cannery.AmmoTest do
current_user = user_fixture()
container = container_fixture(current_user)
rifle_ammo_type = ammo_type_fixture(%{"type" => "rifle"}, current_user)
rifle_ammo_type = ammo_type_fixture(%{type: "rifle"}, current_user)
{1, [rifle_ammo_group]} = ammo_group_fixture(rifle_ammo_type, container, current_user)
shotgun_ammo_type = ammo_type_fixture(%{"type" => "shotgun"}, current_user)
shotgun_ammo_type = ammo_type_fixture(%{type: "shotgun"}, current_user)
{1, [shotgun_ammo_group]} = ammo_group_fixture(shotgun_ammo_type, container, current_user)
pistol_ammo_type = ammo_type_fixture(%{"type" => "pistol"}, current_user)
pistol_ammo_type = ammo_type_fixture(%{type: "pistol"}, current_user)
{1, [pistol_ammo_group]} = ammo_group_fixture(pistol_ammo_type, container, current_user)
another_container = container_fixture(current_user)
@ -948,7 +943,7 @@ defmodule Cannery.AmmoTest do
current_user: current_user
} do
{1, [another_ammo_group]} =
ammo_group_fixture(%{"staged" => true}, ammo_type, container, current_user)
ammo_group_fixture(%{staged: true}, ammo_type, container, current_user)
assert Ammo.list_staged_ammo_groups(current_user) == [another_ammo_group]
end
@ -979,7 +974,7 @@ defmodule Cannery.AmmoTest do
} do
assert {:ok, {1, [%AmmoGroup{} = ammo_group]}} =
@valid_attrs
|> Map.merge(%{"ammo_type_id" => ammo_type.id, "container_id" => container.id})
|> Map.merge(%{ammo_type_id: ammo_type.id, container_id: container.id})
|> Ammo.create_ammo_groups(1, current_user)
assert ammo_group.count == 42
@ -994,7 +989,7 @@ defmodule Cannery.AmmoTest do
} do
assert {:ok, {3, ammo_groups}} =
@valid_attrs
|> Map.merge(%{"ammo_type_id" => ammo_type.id, "container_id" => container.id})
|> Map.merge(%{ammo_type_id: ammo_type.id, container_id: container.id})
|> Ammo.create_ammo_groups(3, current_user)
assert [%AmmoGroup{}, %AmmoGroup{}, %AmmoGroup{}] = ammo_groups
@ -1011,7 +1006,7 @@ defmodule Cannery.AmmoTest do
%{ammo_type: ammo_type, container: container, current_user: current_user} do
assert {:error, %Changeset{}} =
@invalid_attrs
|> Map.merge(%{"ammo_type_id" => ammo_type.id, "container_id" => container.id})
|> Map.merge(%{ammo_type_id: ammo_type.id, container_id: container.id})
|> Ammo.create_ammo_groups(1, current_user)
end
@ -1043,15 +1038,15 @@ defmodule Cannery.AmmoTest do
%{ammo_group: %{id: ammo_group_id} = ammo_group, current_user: current_user} do
assert 100 = ammo_group |> Ammo.get_percentage_remaining(current_user)
shot_group_fixture(%{"count" => 14}, current_user, ammo_group)
shot_group_fixture(%{count: 14}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
assert 72 = ammo_group |> Ammo.get_percentage_remaining(current_user)
shot_group_fixture(%{"count" => 11}, current_user, ammo_group)
shot_group_fixture(%{count: 11}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
assert 50 = ammo_group |> Ammo.get_percentage_remaining(current_user)
shot_group_fixture(%{"count" => 25}, current_user, ammo_group)
shot_group_fixture(%{count: 25}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
assert 0 = ammo_group |> Ammo.get_percentage_remaining(current_user)
end
@ -1066,7 +1061,7 @@ defmodule Cannery.AmmoTest do
[ammo_group] |> Ammo.get_percentages_remaining(current_user)
{1, [%{id: another_ammo_group_id} = another_ammo_group]} =
%{"count" => 50, "price_paid" => 36.1}
%{count: 50, price_paid: 36.1}
|> ammo_group_fixture(ammo_type, container, current_user)
percentages =
@ -1075,7 +1070,7 @@ defmodule Cannery.AmmoTest do
assert %{^ammo_group_id => 100} = percentages
assert %{^another_ammo_group_id => 100} = percentages
shot_group_fixture(%{"count" => 14}, current_user, ammo_group)
shot_group_fixture(%{count: 14}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
percentages =
@ -1084,7 +1079,7 @@ defmodule Cannery.AmmoTest do
assert %{^ammo_group_id => 72} = percentages
assert %{^another_ammo_group_id => 100} = percentages
shot_group_fixture(%{"count" => 11}, current_user, ammo_group)
shot_group_fixture(%{count: 11}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
percentages =
@ -1093,7 +1088,7 @@ defmodule Cannery.AmmoTest do
assert %{^ammo_group_id => 50} = percentages
assert %{^another_ammo_group_id => 100} = percentages
shot_group_fixture(%{"count" => 25}, current_user, ammo_group)
shot_group_fixture(%{count: 25}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
percentages =
@ -1105,12 +1100,12 @@ defmodule Cannery.AmmoTest do
test "get_cpr/2 gets accurate cpr",
%{ammo_type: ammo_type, container: container, current_user: current_user} do
{1, [ammo_group]} = ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
{1, [ammo_group]} = ammo_group_fixture(%{count: 1}, ammo_type, container, current_user)
assert ammo_group |> Ammo.get_cpr(current_user) |> is_nil()
{1, [ammo_group]} =
ammo_group_fixture(
%{"count" => 1, "price_paid" => 1.0},
%{count: 1, price_paid: 1.0},
ammo_type,
container,
current_user
@ -1120,7 +1115,7 @@ defmodule Cannery.AmmoTest do
{1, [ammo_group]} =
ammo_group_fixture(
%{"count" => 2, "price_paid" => 3.0},
%{count: 2, price_paid: 3.0},
ammo_type,
container,
current_user
@ -1130,7 +1125,7 @@ defmodule Cannery.AmmoTest do
{1, [ammo_group]} =
ammo_group_fixture(
%{"count" => 50, "price_paid" => 36.1},
%{count: 50, price_paid: 36.1},
ammo_type,
container,
current_user
@ -1139,19 +1134,19 @@ defmodule Cannery.AmmoTest do
assert 0.722 = ammo_group |> Ammo.get_cpr(current_user)
# with shot group, maintains total
shot_group_fixture(%{"count" => 14}, current_user, ammo_group)
shot_group_fixture(%{count: 14}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group.id, current_user)
assert 0.722 = ammo_group |> Ammo.get_cpr(current_user)
end
test "get_cprs/2 gets accurate cprs",
%{ammo_type: ammo_type, container: container, current_user: current_user} do
{1, [ammo_group]} = ammo_group_fixture(%{"count" => 1}, ammo_type, container, current_user)
{1, [ammo_group]} = ammo_group_fixture(%{count: 1}, ammo_type, container, current_user)
assert %{} == [ammo_group] |> Ammo.get_cprs(current_user)
{1, [%{id: ammo_group_id} = ammo_group]} =
ammo_group_fixture(
%{"count" => 1, "price_paid" => 1.0},
%{count: 1, price_paid: 1.0},
ammo_type,
container,
current_user
@ -1161,7 +1156,7 @@ defmodule Cannery.AmmoTest do
{1, [%{id: another_ammo_group_id} = another_ammo_group]} =
ammo_group_fixture(
%{"count" => 2, "price_paid" => 3.0},
%{count: 2, price_paid: 3.0},
ammo_type,
container,
current_user
@ -1173,7 +1168,7 @@ defmodule Cannery.AmmoTest do
{1, [%{id: yet_another_ammo_group_id} = yet_another_ammo_group]} =
ammo_group_fixture(
%{"count" => 50, "price_paid" => 36.1},
%{count: 50, price_paid: 36.1},
ammo_type,
container,
current_user
@ -1187,7 +1182,7 @@ defmodule Cannery.AmmoTest do
assert %{^yet_another_ammo_group_id => 0.722} = cprs
# with shot group, maintains total
shot_group_fixture(%{"count" => 14}, current_user, yet_another_ammo_group)
shot_group_fixture(%{count: 14}, current_user, yet_another_ammo_group)
yet_another_ammo_group = Ammo.get_ammo_group!(yet_another_ammo_group.id, current_user)
cprs =
@ -1202,15 +1197,15 @@ defmodule Cannery.AmmoTest do
%{ammo_group: %{id: ammo_group_id} = ammo_group, current_user: current_user} do
assert 50 = ammo_group |> Ammo.get_original_count(current_user)
shot_group_fixture(%{"count" => 14}, current_user, ammo_group)
shot_group_fixture(%{count: 14}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
assert 50 = ammo_group |> Ammo.get_original_count(current_user)
shot_group_fixture(%{"count" => 11}, current_user, ammo_group)
shot_group_fixture(%{count: 11}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
assert 50 = ammo_group |> Ammo.get_original_count(current_user)
shot_group_fixture(%{"count" => 25}, current_user, ammo_group)
shot_group_fixture(%{count: 25}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
assert 50 = ammo_group |> Ammo.get_original_count(current_user)
end
@ -1222,25 +1217,25 @@ defmodule Cannery.AmmoTest do
current_user: current_user
} do
{1, [%{id: another_ammo_group_id} = another_ammo_group]} =
ammo_group_fixture(%{"count" => 25}, ammo_type, container, current_user)
ammo_group_fixture(%{count: 25}, ammo_type, container, current_user)
original_counts = [ammo_group, another_ammo_group] |> Ammo.get_original_counts(current_user)
assert %{^ammo_group_id => 50} = original_counts
assert %{^another_ammo_group_id => 25} = original_counts
shot_group_fixture(%{"count" => 14}, current_user, ammo_group)
shot_group_fixture(%{count: 14}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
original_counts = [ammo_group, another_ammo_group] |> Ammo.get_original_counts(current_user)
assert %{^ammo_group_id => 50} = original_counts
assert %{^another_ammo_group_id => 25} = original_counts
shot_group_fixture(%{"count" => 11}, current_user, ammo_group)
shot_group_fixture(%{count: 11}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
original_counts = [ammo_group, another_ammo_group] |> Ammo.get_original_counts(current_user)
assert %{^ammo_group_id => 50} = original_counts
assert %{^another_ammo_group_id => 25} = original_counts
shot_group_fixture(%{"count" => 25}, current_user, ammo_group)
shot_group_fixture(%{count: 25}, current_user, ammo_group)
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
original_counts = [ammo_group, another_ammo_group] |> Ammo.get_original_counts(current_user)
assert %{^ammo_group_id => 50} = original_counts

View File

@ -10,37 +10,37 @@ defmodule Cannery.ContainersTest do
@moduletag :containers_test
@valid_attrs %{
"desc" => "some desc",
"location" => "some location",
"name" => "some name",
"type" => "some type"
desc: "some desc",
location: "some location",
name: "some name",
type: "some type"
}
@update_attrs %{
"desc" => "some updated desc",
"location" => "some updated location",
"name" => "some updated name",
"type" => "some updated type"
desc: "some updated desc",
location: "some updated location",
name: "some updated name",
type: "some updated type"
}
@invalid_attrs %{
"desc" => nil,
"location" => nil,
"name" => nil,
"type" => nil
desc: nil,
location: nil,
name: nil,
type: nil
}
@valid_tag_attrs %{
"bg_color" => "#100000",
"name" => "some name",
"text_color" => "#000000"
bg_color: "#100000",
name: "some name",
text_color: "#000000"
}
@update_tag_attrs %{
"bg_color" => "#100001",
"name" => "some updated name",
"text_color" => "#000001"
bg_color: "#100001",
name: "some updated name",
text_color: "#000001"
}
@invalid_tag_attrs %{
"bg_color" => nil,
"name" => nil,
"text_color" => nil
bg_color: nil,
name: nil,
text_color: nil
}
describe "containers" do
@ -57,25 +57,24 @@ defmodule Cannery.ContainersTest do
test "list_containers/2 returns relevant containers for a user",
%{current_user: current_user} do
container_a = container_fixture(%{"name" => "my cool container"}, current_user)
container_b = container_fixture(%{"desc" => "a fascinating description"}, current_user)
container_a = container_fixture(%{name: "my cool container"}, current_user)
container_b = container_fixture(%{desc: "a fascinating description"}, current_user)
%{id: container_c_id} =
container_c = container_fixture(%{"location" => "a secret place"}, current_user)
container_c = container_fixture(%{location: "a secret place"}, current_user)
tag = tag_fixture(%{"name" => "stupendous tag"}, current_user)
tag = tag_fixture(%{name: "stupendous tag"}, current_user)
Containers.add_tag!(container_c, tag, current_user)
container_c = container_c_id |> Containers.get_container!(current_user)
%{id: container_d_id} =
container_d = container_fixture(%{"type" => "musty old box"}, current_user)
container_d = container_fixture(%{type: "musty old box"}, current_user)
tag = tag_fixture(%{"name" => "amazing tag"}, current_user)
tag = tag_fixture(%{name: "amazing tag"}, current_user)
Containers.add_tag!(container_d, tag, current_user)
container_d = container_d_id |> Containers.get_container!(current_user)
_shouldnt_return =
container_fixture(%{"name" => "another person's container"}, user_fixture())
_shouldnt_return = container_fixture(%{name: "another person's container"}, user_fixture())
# attributes
assert Containers.list_containers("cool", current_user) == [container_a]
@ -109,7 +108,7 @@ defmodule Cannery.ContainersTest do
test "create_container/2 with valid data creates a container", %{current_user: current_user} do
assert {:ok, %Container{} = container} =
@valid_attrs |> Containers.create_container(current_user)
Containers.create_container(@valid_attrs, current_user)
assert container.desc == "some desc"
assert container.location == "some location"
@ -120,7 +119,7 @@ defmodule Cannery.ContainersTest do
test "create_container/2 with invalid data returns error changeset",
%{current_user: current_user} do
assert {:error, %Changeset{}} = @invalid_attrs |> Containers.create_container(current_user)
assert {:error, %Changeset{}} = Containers.create_container(@invalid_attrs, current_user)
end
test "update_container/3 with valid data updates the container",
@ -163,15 +162,10 @@ defmodule Cannery.ContainersTest do
end
test "list_tags/2 returns relevant tags for a user", %{current_user: current_user} do
tag_a = tag_fixture(%{"name" => "bullets"}, current_user)
tag_b = tag_fixture(%{"name" => "hollows"}, current_user)
tag_a = tag_fixture(%{name: "bullets"}, current_user)
tag_b = tag_fixture(%{name: "hollows"}, current_user)
_shouldnt_return =
%{
"name" => "bullet",
"desc" => "pews brass shell"
}
|> tag_fixture(user_fixture())
tag_fixture(%{name: "bullet", desc: "pews brass shell"}, user_fixture())
# name
assert Containers.list_tags("bullet", current_user) == [tag_a]