ammocpr/test/support/fixtures.ex
Graham McIntire bbe5bde145
Initial implementation of ammo price tracker
Phoenix 1.8 app that scrapes ammunition retailers (Lucky Gunner,
SGAmmo) for price data and displays historical price trends.

- Data model: retailers, calibers, products, price snapshots
- Scraper infrastructure with Req, Floki, realistic browser headers
- Oban-scheduled scrape jobs (every 4h with randomized delays)
- LiveView pages: homepage with category cards, caliber detail with
  price table, Chart.js price history, and price stats banner
- 18 seeded calibers across handgun/rifle/rimfire/shotgun categories
- 77 tests
2026-03-11 15:58:12 -05:00

96 lines
2.3 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
{:ok, retailer} =
%Retailer{}
|> Retailer.changeset(
Map.merge(
%{
name: "Test Retailer",
slug: "test-retailer-#{System.unique_integer([:positive])}",
base_url: "https://example.com"
},
attrs
)
)
|> Repo.insert()
retailer
end
def caliber_fixture(attrs \\ %{}) do
{:ok, caliber} =
%Caliber{}
|> Caliber.changeset(
Map.merge(
%{
name: "9mm Luger",
slug: "9mm-luger-#{System.unique_integer([:positive])}",
category: "handgun",
aliases: ["9mm", "9x19"]
},
attrs
)
)
|> Repo.insert()
caliber
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,
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