feat: add gear provenance models and browser fetcher
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
from dataclasses import asdict
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from rf4_research.community_sources import (
|
||||
CommunityParseError,
|
||||
parse_rf4db_gear_catalog,
|
||||
parse_rf4db_gear_detail,
|
||||
)
|
||||
|
||||
|
||||
FIXTURES = Path(__file__).parent / "fixtures"
|
||||
|
||||
|
||||
def test_gear_catalog_preserves_zero_level_and_unknown_total() -> None:
|
||||
html = (FIXTURES / "rf4db_gear_catalog_sample.html").read_text(encoding="utf-8")
|
||||
rows = parse_rf4db_gear_catalog(html)
|
||||
assert len(rows) == 2
|
||||
assert rows[0].category == "lure"
|
||||
assert rows[0].source_external_id == "spiker-2"
|
||||
assert rows[1].unlock_level == 0
|
||||
assert rows[1].image_url is None
|
||||
|
||||
|
||||
def test_gear_catalog_rejects_duplicate_ids() -> None:
|
||||
html = '<article class="gear-card" data-gear-id="same" data-category="bait"><h3 data-name>A</h3></article><article class="gear-card" data-gear-id="same" data-category="bait"><h3 data-name>B</h3></article>'
|
||||
with pytest.raises(CommunityParseError, match="duplicate"):
|
||||
parse_rf4db_gear_catalog(html)
|
||||
|
||||
|
||||
def test_gear_detail_distinguishes_value_not_applicable_and_missing() -> None:
|
||||
html = (FIXTURES / "rf4db_gear_detail_sample.html").read_text(encoding="utf-8")
|
||||
detail = parse_rf4db_gear_detail(html, source_url="https://rf4db.com/ru/wiki/lures/spiker-2")
|
||||
attributes = {item.key: asdict(item) for item in detail.attributes}
|
||||
assert attributes["weight"] == {"key": "weight", "state": "value", "value": 0, "source_text": "0"}
|
||||
assert attributes["floating"]["state"] == "not_applicable"
|
||||
assert attributes["floating"]["value"] is None
|
||||
assert attributes["target_fish"]["state"] == "missing"
|
||||
assert detail.variants == ("Spiker #2 01-015",)
|
||||
assert detail.rig_types == ("Spinning",)
|
||||
|
||||
|
||||
def test_gear_detail_rejects_unknown_category() -> None:
|
||||
with pytest.raises(CommunityParseError, match="category"):
|
||||
parse_rf4db_gear_detail(
|
||||
'<main data-gear-detail data-category="unknown-category"><h1>Test</h1></main>',
|
||||
source_url="https://rf4db.com/ru/wiki/gear/test",
|
||||
)
|
||||
Reference in New Issue
Block a user