cannery/test/cannery_web/controllers/export_controller_test.exs

147 lines
5.0 KiB
Elixir
Raw Permalink Normal View History

2022-11-09 23:33:50 -05:00
defmodule CanneryWeb.ExportControllerTest do
@moduledoc """
Tests the export function
"""
2023-04-14 23:48:50 -04:00
use CanneryWeb.ConnCase, async: true
alias Cannery.{ActivityLog, Ammo, Containers, Repo}
2022-11-09 23:33:50 -05:00
@moduletag :export_controller_test
2023-02-04 16:46:37 -05:00
setup [:register_and_log_in_user]
2022-11-09 23:33:50 -05:00
defp add_data(%{current_user: current_user}) do
2023-03-30 21:53:52 -04:00
type = type_fixture(current_user)
2022-11-09 23:33:50 -05:00
container = container_fixture(current_user)
tag = tag_fixture(current_user)
Containers.add_tag!(container, tag, current_user)
2023-03-30 21:53:52 -04:00
{1, [pack]} = pack_fixture(type, container, current_user)
2023-03-30 20:43:30 -04:00
shot_record = shot_record_fixture(current_user, pack)
2023-03-29 22:54:55 -04:00
pack = pack |> Repo.reload!()
2022-11-09 23:33:50 -05:00
%{
2023-03-30 21:53:52 -04:00
type: type,
2023-03-29 22:54:55 -04:00
pack: pack,
2022-11-09 23:33:50 -05:00
container: container,
2023-03-30 20:43:30 -04:00
shot_record: shot_record,
2022-11-09 23:33:50 -05:00
tag: tag
}
end
describe "Exports data" do
setup [:add_data]
test "in JSON", %{
conn: conn,
current_user: current_user,
container: container,
2023-03-30 21:53:52 -04:00
type: type,
2023-03-29 22:54:55 -04:00
pack: pack,
2023-03-30 20:43:30 -04:00
shot_record: shot_record,
2022-11-09 23:33:50 -05:00
tag: tag
} do
2023-04-14 23:34:11 -04:00
conn = get(conn, ~p"/export/json")
2022-11-09 23:33:50 -05:00
2023-03-29 22:54:55 -04:00
ideal_pack = %{
2023-03-30 21:53:52 -04:00
"type_id" => pack.type_id,
2023-03-29 22:54:55 -04:00
"container_id" => pack.container_id,
"count" => pack.count,
"id" => pack.id,
"notes" => pack.notes,
"price_paid" => pack.price_paid,
2023-03-30 23:35:01 -04:00
"lot_number" => pack.lot_number,
2023-03-29 22:54:55 -04:00
"staged" => pack.staged,
2023-06-05 19:03:13 -04:00
"used_count" => ActivityLog.get_used_count(current_user, pack_id: pack.id),
2023-03-29 22:54:55 -04:00
"original_count" => pack |> Ammo.get_original_count(current_user),
"cpr" => pack |> Ammo.get_cpr(current_user),
"percentage_remaining" => pack |> Ammo.get_percentage_remaining(current_user)
2022-11-09 23:33:50 -05:00
}
2023-03-30 21:53:52 -04:00
ideal_type = %{
2023-03-31 22:34:48 -04:00
"name" => type.name,
"desc" => type.desc,
"class" => to_string(type.class),
2023-03-30 21:53:52 -04:00
"bullet_type" => type.bullet_type,
2023-03-31 22:34:48 -04:00
"bullet_core" => type.bullet_core,
2023-03-30 21:53:52 -04:00
"caliber" => type.caliber,
"case_material" => type.case_material,
"powder_type" => type.powder_type,
2023-03-31 22:34:48 -04:00
"grains" => type.grains,
2023-03-30 21:53:52 -04:00
"pressure" => type.pressure,
"primer_type" => type.primer_type,
2023-03-31 22:34:48 -04:00
"firing_type" => type.firing_type,
"manufacturer" => type.manufacturer,
2023-03-30 21:53:52 -04:00
"upc" => type.upc,
2023-03-31 22:34:48 -04:00
"tracer" => type.tracer,
"incendiary" => type.incendiary,
"blank" => type.blank,
"corrosive" => type.corrosive,
"cartridge" => type.cartridge,
"jacket_type" => type.jacket_type,
"powder_grains_per_charge" => type.powder_grains_per_charge,
"muzzle_velocity" => type.muzzle_velocity,
"wadding" => type.wadding,
"shot_type" => type.shot_type,
"shot_material" => type.shot_material,
"shot_size" => type.shot_size,
"unfired_length" => type.unfired_length,
"brass_height" => type.brass_height,
"chamber_size" => type.chamber_size,
"load_grains" => type.load_grains,
"shot_charge_weight" => type.shot_charge_weight,
"dram_equivalent" => type.dram_equivalent,
"average_cost" => Ammo.get_average_cost(type, current_user),
2023-06-05 20:47:12 -04:00
"round_count" => Ammo.get_round_count(current_user, type_id: type.id),
2023-06-05 19:03:13 -04:00
"used_count" => ActivityLog.get_used_count(current_user, type_id: type.id),
2023-06-04 20:53:57 -04:00
"pack_count" => Ammo.get_packs_count(current_user, type_id: type.id),
"total_pack_count" =>
Ammo.get_packs_count(current_user, type_id: type.id, show_used: true)
2022-11-09 23:33:50 -05:00
}
ideal_container = %{
"desc" => container.desc,
"id" => container.id,
"location" => container.location,
"name" => container.name,
"tags" => [
%{
"id" => tag.id,
"name" => tag.name,
"bg_color" => tag.bg_color,
"text_color" => tag.text_color
}
],
"type" => container.type,
2023-06-04 20:53:57 -04:00
"pack_count" => Ammo.get_packs_count(current_user, container_id: container.id),
2023-06-05 20:47:12 -04:00
"round_count" => Ammo.get_round_count(current_user, container_id: container.id)
2022-11-09 23:33:50 -05:00
}
2023-03-30 20:43:30 -04:00
ideal_shot_record = %{
"pack_id" => shot_record.pack_id,
"count" => shot_record.count,
"date" => to_string(shot_record.date),
"id" => shot_record.id,
"notes" => shot_record.notes
2022-11-09 23:33:50 -05:00
}
ideal_user = %{
"confirmed_at" =>
current_user.confirmed_at |> Jason.encode!() |> String.replace(~r/\"/, ""),
"email" => current_user.email,
"id" => current_user.id,
"locale" => current_user.locale,
2023-02-04 16:46:37 -05:00
"role" => to_string(current_user.role),
"inserted_at" => current_user.inserted_at |> NaiveDateTime.to_iso8601(),
"updated_at" => current_user.updated_at |> NaiveDateTime.to_iso8601()
2022-11-09 23:33:50 -05:00
}
json_resp = conn |> json_response(200)
2023-03-29 22:54:55 -04:00
assert %{"packs" => [^ideal_pack]} = json_resp
2023-03-30 21:53:52 -04:00
assert %{"types" => [^ideal_type]} = json_resp
2022-11-09 23:33:50 -05:00
assert %{"containers" => [^ideal_container]} = json_resp
2023-03-30 20:43:30 -04:00
assert %{"shot_records" => [^ideal_shot_record]} = json_resp
2022-11-09 23:33:50 -05:00
assert %{"user" => ^ideal_user} = json_resp
end
end
end