test: add archidekt parser tests
This commit is contained in:
@@ -0,0 +1,79 @@
|
|||||||
|
"""
|
||||||
|
Tests for archidekt.py — CSV and JSON parsing with various column name formats.
|
||||||
|
"""
|
||||||
|
import pytest
|
||||||
|
from app.services.imports.archidekt import parse
|
||||||
|
|
||||||
|
|
||||||
|
class TestArchidektCSV:
|
||||||
|
def _csv(self, header, rows):
|
||||||
|
lines = [header] + rows
|
||||||
|
return "\n".join(lines).encode("utf-8")
|
||||||
|
|
||||||
|
def test_standard_columns(self):
|
||||||
|
data = self._csv(
|
||||||
|
"Quantity,Foil Quantity,Card Name,Set Code,Collector Number",
|
||||||
|
["2,0,Sol Ring,cmm,1"]
|
||||||
|
)
|
||||||
|
result = parse(data)
|
||||||
|
assert len(result) == 1
|
||||||
|
assert result[0].card_name == "Sol Ring"
|
||||||
|
assert result[0].quantity == 2
|
||||||
|
assert result[0].foil_quantity == 0
|
||||||
|
assert result[0].set_code == "cmm"
|
||||||
|
|
||||||
|
def test_alternate_column_names(self):
|
||||||
|
data = self._csv(
|
||||||
|
"qty,foil qty,name,set,number",
|
||||||
|
["1,1,Black Lotus,lea,232"]
|
||||||
|
)
|
||||||
|
result = parse(data)
|
||||||
|
assert result[0].card_name == "Black Lotus"
|
||||||
|
assert result[0].quantity == 1
|
||||||
|
assert result[0].foil_quantity == 1
|
||||||
|
|
||||||
|
def test_skips_empty_rows(self):
|
||||||
|
data = self._csv(
|
||||||
|
"Quantity,Card Name",
|
||||||
|
["1,Sol Ring", ",", "2,Command Tower"]
|
||||||
|
)
|
||||||
|
result = parse(data)
|
||||||
|
assert len(result) == 2
|
||||||
|
|
||||||
|
def test_empty_csv_raises(self):
|
||||||
|
with pytest.raises(ValueError, match="empty"):
|
||||||
|
parse(b"")
|
||||||
|
|
||||||
|
def test_no_card_rows_raises(self):
|
||||||
|
data = self._csv("Quantity,Card Name", [])
|
||||||
|
with pytest.raises(ValueError, match="no card rows"):
|
||||||
|
parse(data)
|
||||||
|
|
||||||
|
|
||||||
|
class TestArchidektJSON:
|
||||||
|
def test_standard_json_array(self):
|
||||||
|
data = b'''[
|
||||||
|
{"quantity": 1, "foilQuantity": 0, "card": {"name": "Sol Ring", "set": {"code": "cmm"}, "collectorNumber": "1"}}
|
||||||
|
]'''
|
||||||
|
result = parse(data)
|
||||||
|
assert len(result) == 1
|
||||||
|
assert result[0].card_name == "Sol Ring"
|
||||||
|
assert result[0].set_code == "cmm"
|
||||||
|
|
||||||
|
def test_cards_wrapper(self):
|
||||||
|
data = b'{"cards": [{"quantity": 1, "card": {"name": "Sol Ring"}}]}'
|
||||||
|
result = parse(data)
|
||||||
|
assert result[0].card_name == "Sol Ring"
|
||||||
|
|
||||||
|
def test_foil_quantity(self):
|
||||||
|
data = b'[{"quantity": 0, "foilQuantity": 2, "card": {"name": "Sol Ring"}}]'
|
||||||
|
result = parse(data)
|
||||||
|
assert result[0].foil_quantity == 2
|
||||||
|
|
||||||
|
def test_empty_array_raises(self):
|
||||||
|
with pytest.raises(ValueError, match="no card entries"):
|
||||||
|
parse(b"[]")
|
||||||
|
|
||||||
|
def test_malformed_json_raises(self):
|
||||||
|
with pytest.raises(ValueError, match="malformed"):
|
||||||
|
parse(b"{broken json")
|
||||||
Reference in New Issue
Block a user