101 lines
2.5 KiB
Elixir
101 lines
2.5 KiB
Elixir
defmodule Ammoprices.Fixtures do
|
|
@moduledoc """
|
|
Factory functions for test data.
|
|
"""
|
|
|
|
alias Ammoprices.Catalog.Caliber
|
|
alias Ammoprices.Catalog.Product
|
|
alias Ammoprices.Catalog.Retailer
|
|
alias Ammoprices.Prices.PriceSnapshot
|
|
alias Ammoprices.Repo
|
|
|
|
def retailer_fixture(attrs \\ %{}) do
|
|
merged =
|
|
Map.merge(
|
|
%{
|
|
name: "Test Retailer",
|
|
slug: "test-retailer-#{System.unique_integer([:positive])}",
|
|
base_url: "https://example.com"
|
|
},
|
|
attrs
|
|
)
|
|
|
|
%Retailer{}
|
|
|> Retailer.changeset(merged)
|
|
|> Repo.insert!(
|
|
on_conflict: {:replace, [:name, :base_url, :updated_at]},
|
|
conflict_target: :slug,
|
|
returning: true
|
|
)
|
|
end
|
|
|
|
def caliber_fixture(attrs \\ %{}) do
|
|
merged =
|
|
Map.merge(
|
|
%{
|
|
name: "9mm Luger",
|
|
slug: "9mm-luger-#{System.unique_integer([:positive])}",
|
|
category: "handgun",
|
|
aliases: ["9mm", "9x19"]
|
|
},
|
|
attrs
|
|
)
|
|
|
|
%Caliber{}
|
|
|> Caliber.changeset(merged)
|
|
|> Repo.insert!(
|
|
on_conflict: {:replace, [:name, :category, :aliases, :updated_at]},
|
|
conflict_target: :slug,
|
|
returning: true
|
|
)
|
|
end
|
|
|
|
def product_fixture(attrs \\ %{}) do
|
|
retailer = Map.get_lazy(attrs, :retailer, fn -> retailer_fixture() end)
|
|
caliber = Map.get_lazy(attrs, :caliber, fn -> caliber_fixture() end)
|
|
|
|
{:ok, product} =
|
|
%Product{retailer_id: retailer.id, caliber_id: caliber.id}
|
|
|> Product.changeset(
|
|
Map.merge(
|
|
%{
|
|
title: "Test Ammo 9mm 115gr FMJ",
|
|
url: "/products/test-#{System.unique_integer([:positive])}",
|
|
brand: "TestBrand",
|
|
grain_weight: 115,
|
|
round_count: 50,
|
|
casing: "brass",
|
|
condition: "new",
|
|
in_stock: true,
|
|
subsonic: false,
|
|
last_seen_at: DateTime.truncate(DateTime.utc_now(), :second)
|
|
},
|
|
Map.drop(attrs, [:retailer, :caliber])
|
|
)
|
|
)
|
|
|> Repo.insert()
|
|
|
|
product
|
|
end
|
|
|
|
def snapshot_fixture(attrs \\ %{}) do
|
|
product = Map.get_lazy(attrs, :product, fn -> product_fixture() end)
|
|
|
|
{:ok, snapshot} =
|
|
%PriceSnapshot{product_id: product.id}
|
|
|> PriceSnapshot.changeset(
|
|
Map.merge(
|
|
%{
|
|
price_cents: 1599,
|
|
price_per_round_cents: 32,
|
|
in_stock: true,
|
|
recorded_at: DateTime.truncate(DateTime.utc_now(), :second)
|
|
},
|
|
Map.delete(attrs, :product)
|
|
)
|
|
)
|
|
|> Repo.insert()
|
|
|
|
snapshot
|
|
end
|
|
end
|