ammocpr/test/ammoprices/scraping/retailers/sg_ammo_test.exs
Graham McIntire 16a28ac01c
Add Target Sports USA scraper, product filtering, and subsonic detection
- Add subsonic boolean field to products with composite filter indexes
- Add TextDetector utility for detecting subsonic/casing from product titles
- Wire TextDetector into SgAmmo (casing + subsonic) and LuckyGunner (subsonic + fallback casing)
- Extend latest_prices_for_caliber with casing, grain_weight, and subsonic filter options
- Add distinct_grain_weights_for_caliber and distinct_casings_for_caliber queries
- Add filter UI with toggleable casing, grain weight, and subsonic buttons
- Add grain weight column and subsonic badge to product table
- Refresh filter options on PubSub price updates for real-time UI
- Implement Target Sports USA scraper with 18 caliber mappings
- Display all prices in dollar format ($0.38 instead of 38¢)
2026-03-11 16:28:04 -05:00

94 lines
2.8 KiB
Elixir

defmodule Ammoprices.Scraping.Retailers.SgAmmoTest do
use ExUnit.Case, async: true
alias Ammoprices.Scraping.Retailers.SgAmmo
@fixture_path "test/fixtures/sg_ammo/9mm.html"
describe "retailer_slug/0" do
test "returns sgammo" do
assert SgAmmo.retailer_slug() == "sgammo"
end
end
describe "category_url/1" do
test "maps 9mm-luger caliber to correct URL" do
caliber = %{slug: "9mm-luger", category: "handgun"}
assert SgAmmo.category_url(caliber) == "/catalog/pistol-ammo-for-sale/9mm-luger-ammo"
end
test "returns nil for unmapped caliber" do
caliber = %{slug: "unknown", category: "handgun"}
assert SgAmmo.category_url(caliber) == nil
end
end
describe "parse_products/1" do
setup do
html = File.read!(@fixture_path)
%{html: html}
end
test "extracts all products from HTML", %{html: html} do
products = SgAmmo.parse_products(html)
assert length(products) == 4
end
test "extracts product title", %{html: html} do
[first | _] = SgAmmo.parse_products(html)
assert first.title =~ "50 Round Box - 9mm Luger 115 Grain FMJ Ammo by Magtech"
end
test "extracts product URL", %{html: html} do
[first | _] = SgAmmo.parse_products(html)
assert first.url =~ "sgammo.com/product/9mm-luger-ammo/"
end
test "extracts price in cents", %{html: html} do
[first | _] = SgAmmo.parse_products(html)
assert first.price_cents == 1395
end
test "extracts price per round in cents", %{html: html} do
[first | _] = SgAmmo.parse_products(html)
assert first.price_per_round_cents == 28
end
test "extracts round count from title", %{html: html} do
[first | _] = SgAmmo.parse_products(html)
assert first.round_count == 50
end
test "extracts grain weight from title", %{html: html} do
[first | _] = SgAmmo.parse_products(html)
assert first.grain_weight == 115
end
test "extracts brand from title", %{html: html} do
[first | _] = SgAmmo.parse_products(html)
assert first.brand == "Magtech"
end
test "marks in-stock products", %{html: html} do
[first | _] = SgAmmo.parse_products(html)
assert first.in_stock == true
end
test "detects casing from title", %{html: html} do
products = SgAmmo.parse_products(html)
# 4th product has "Steel Case" in title
assert Enum.at(products, 3).casing == "steel"
end
test "detects subsonic from title", %{html: html} do
products = SgAmmo.parse_products(html)
# 4th product has "Subsonic" in title
assert Enum.at(products, 3).subsonic == true
end
test "non-subsonic products return false", %{html: html} do
[first | _] = SgAmmo.parse_products(html)
assert first.subsonic == false
end
end
end