Five blind reviewers swept the real-data-era surface; this lands the upload findings, all verified against the code and the documented site behavior before fixing. The two HIGHs shared a root: logging outcomes the browser never proved. add_game waited for an "Add To" button that an owned game's page does not have — so a second-copy add could never succeed, and worse, an add that LANDED but missed the log became an unretryable failure loop (every retry: 30s timeout, logged failed, nothing ever settles). add_game now polls for either button state: "In Collections" without second_copy returns the previously-dead already_present status (the landed-but-unlogged case heals itself on retry); with second_copy it refuses loudly (that flow is unverified — add by hand). A save whose dialog is slow to hide reloads the page and asks for ownership evidence instead of guessing "failed". update_entry no longer trusts the editor merely closing: the cell must settle on text matching the CHOSEN version, else the AJAX save failed server-side and "updated" would mark a job done forever that never touched the site. Per-copy bookkeeping: stale_jobs endorsed per game, so rejecting one of two queued editions let the rejected copy upload on the survivor's endorsement — it now counts endorsements per (bgg_id, version) and retires the game with "re-run diff" when a copy loses its backing. annotate_queue stamped every row sharing a job key with the same log status, so one success marked both vetoed duplicates done; completions are now claimed one row per done log line. Smaller findings: the version-drift note queued a doomed re-add after warning about it (now skips — the entry exists on BGG; re-adding only duplicates); the one-update-per-game deferral rested on a claim the collid-exact editor disproves (removed — same-game updates run together); the 3-identical-failures abort compared exception class only, so three unrelated problems aborted a healthy run (now compares whole messages). Also from the test seat: run_upload's stale filtering finally executes against a real matches.csv in tests; rejected credentials pin that no anonymous storage state is saved; update_entry's three guarded exits each have a test; _scrub's newline flattening is pinned. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016jXZFSTZQKzAC8fqpWSz9g
176 lines
6.0 KiB
Python
176 lines
6.0 KiB
Python
"""XML parser tests against hand-built API2 response shapes."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from bggpipe.models import (
|
|
BGGResponseError,
|
|
parse_collection,
|
|
parse_search,
|
|
parse_things,
|
|
parse_things_full,
|
|
)
|
|
|
|
SEARCH_XML = """<items total="2">
|
|
<item type="boardgame" id="13">
|
|
<name type="primary" value="CATAN"/><yearpublished value="1995"/>
|
|
</item>
|
|
<item type="boardgameexpansion" id="290448">
|
|
<name type="alternate" value="Wingspan: Europa"/>
|
|
</item>
|
|
</items>"""
|
|
|
|
THING_XML = """<items>
|
|
<item type="boardgame" id="266192">
|
|
<name type="primary" sortindex="1" value="Wingspan"/>
|
|
<name type="alternate" sortindex="1" value="Flügelschlag"/>
|
|
<yearpublished value="2019"/>
|
|
<statistics page="1"><ratings>
|
|
<owned value="123456"/>
|
|
<ranks>
|
|
<rank type="subtype" id="1" name="boardgame" value="30"/>
|
|
<rank type="family" id="5497" name="strategygames" value="25"/>
|
|
</ranks>
|
|
</ratings></statistics>
|
|
<versions>
|
|
<item type="boardgameversion" id="465063">
|
|
<name type="primary" value="English edition"/>
|
|
<yearpublished value="2019"/>
|
|
<link type="boardgamepublisher" id="23202" value="Stonemaier Games"/>
|
|
<link type="language" id="2184" value="English"/>
|
|
</item>
|
|
<item type="boardgameversion" id="465064">
|
|
<name type="primary" value="German edition"/>
|
|
<yearpublished value="2019"/>
|
|
<link type="boardgamepublisher" id="22160" value="Feuerland Spiele"/>
|
|
<link type="language" id="2188" value="German"/>
|
|
</item>
|
|
</versions>
|
|
</item>
|
|
</items>"""
|
|
|
|
COLLECTION_XML = """<items totalitems="2">
|
|
<item objecttype="thing" objectid="13" subtype="boardgame" collid="101">
|
|
<name sortindex="1">Catan</name>
|
|
<yearpublished>1995</yearpublished>
|
|
<status own="1" wanttoplay="0"/>
|
|
</item>
|
|
<item objecttype="thing" objectid="266192" subtype="boardgame" collid="102">
|
|
<name sortindex="1">Wingspan</name>
|
|
<status own="0" wishlist="1"/>
|
|
<version><item type="boardgameversion" id="465063"/></version>
|
|
</item>
|
|
</items>"""
|
|
|
|
NOT_RANKED_XML = """<items>
|
|
<item type="boardgame" id="99999">
|
|
<name type="primary" value="Obscurity"/>
|
|
<statistics><ratings>
|
|
<owned value="12"/>
|
|
<ranks><rank type="subtype" id="1" name="boardgame" value="Not Ranked"/></ranks>
|
|
</ratings></statistics>
|
|
</item>
|
|
</items>"""
|
|
|
|
ERROR_XML = (
|
|
"<errors><error><message>Invalid username specified</message></error></errors>"
|
|
)
|
|
|
|
|
|
def test_parse_search():
|
|
results = parse_search(SEARCH_XML)
|
|
assert [r.bgg_id for r in results] == [13, 290448]
|
|
assert results[0].name == "CATAN"
|
|
assert results[0].year == 1995
|
|
assert results[1].type == "boardgameexpansion"
|
|
assert results[1].name_type == "alternate"
|
|
assert results[1].year is None
|
|
|
|
|
|
def test_parse_things_with_stats_and_versions():
|
|
(thing,) = parse_things(THING_XML)
|
|
assert thing.name == "Wingspan" # primary, not alternate
|
|
assert thing.owned == 123456
|
|
assert thing.rank == 30 # boardgame rank, not the family rank
|
|
assert len(thing.versions) == 2
|
|
english = thing.versions[0]
|
|
assert english.version_id == 465063
|
|
assert english.publishers == ("Stonemaier Games",)
|
|
assert english.languages == ("English",)
|
|
|
|
|
|
def test_parse_things_not_ranked_is_none():
|
|
(thing,) = parse_things(NOT_RANKED_XML)
|
|
assert thing.rank is None
|
|
assert thing.owned == 12
|
|
|
|
|
|
def test_parse_collection():
|
|
catan, wingspan = parse_collection(COLLECTION_XML)
|
|
assert (catan.object_id, catan.coll_id, catan.own) == (13, 101, True)
|
|
assert catan.version_id is None
|
|
assert wingspan.own is False # wishlist item must not count as owned
|
|
assert wingspan.version_id == 465063
|
|
|
|
|
|
def test_error_document_raises():
|
|
with pytest.raises(BGGResponseError, match="Invalid username"):
|
|
parse_collection(ERROR_XML)
|
|
|
|
|
|
def test_search_all_items_malformed_raises():
|
|
xml = '<items total="2"><item type="boardgame"/><item type="boardgame"/></items>'
|
|
with pytest.raises(BGGResponseError):
|
|
parse_search(xml)
|
|
|
|
|
|
def test_search_partial_malformed_tolerated_with_warning():
|
|
xml = (
|
|
'<items total="2">'
|
|
'<item type="boardgame" id="13"><name type="primary" value="CATAN"/></item>'
|
|
'<item type="boardgame"/>'
|
|
"</items>"
|
|
)
|
|
with pytest.warns(UserWarning, match="unparseable"):
|
|
results = parse_search(xml)
|
|
assert [r.bgg_id for r in results] == [13]
|
|
|
|
|
|
def test_parse_search_dedupes_multitype_duplicates_preferring_specific():
|
|
"""A multi-type search lists an expansion under BOTH types; keeping the
|
|
bare-boardgame copy would erode the base-vs-expansion guard."""
|
|
xml = """<items total="2">
|
|
<item type="boardgame" id="290448">
|
|
<name type="primary" value="Wingspan: European Expansion"/>
|
|
</item>
|
|
<item type="boardgameexpansion" id="290448">
|
|
<name type="primary" value="Wingspan: European Expansion"/>
|
|
</item>
|
|
</items>"""
|
|
(result,) = parse_search(xml)
|
|
assert result.type == "boardgameexpansion"
|
|
|
|
|
|
def test_parse_things_full_reads_rpggeek_link_vocabulary():
|
|
"""RPGGeek items use their own link types; a board-game-only reader
|
|
silently returns nothing for them."""
|
|
xml = """<items>
|
|
<item type="rpgitem" id="311654">
|
|
<name type="primary" sortindex="1" value="Alice is Missing"/>
|
|
<yearpublished value="2020"/>
|
|
<link type="rpgdesigner" id="1" value="Spenser Starke"/>
|
|
<link type="rpgpublisher" id="2" value="Hunters Entertainment"/>
|
|
<link type="rpggenre" id="3" value="Modern"/>
|
|
<link type="rpgcategory" id="4" value="Core Rules (min needed to play)"/>
|
|
<link type="rpgmechanic" id="5" value="Card Play"/>
|
|
<link type="rpgproducer" id="6" value="Someone"/>
|
|
</item>
|
|
</items>"""
|
|
(game,) = parse_things_full(xml)
|
|
assert game["designers"] == ["Spenser Starke"]
|
|
assert game["publishers"] == ["Hunters Entertainment"]
|
|
assert game["categories"] == ["Modern", "Core Rules (min needed to play)"]
|
|
assert game["mechanics"] == ["Card Play"]
|
|
assert game["producers"] == ["Someone"]
|