Skip to content

Commit

Permalink
Add extra error catch to avoid data loss on extra filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
gwax committed Jan 1, 2023
1 parent d803a9f commit 2e1f517
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
12 changes: 12 additions & 0 deletions mtg_ssm/containers/counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
from mtg_ssm.containers.indexes import Oracle


class Error(Exception):
"""Base exception for this module."""


class CardNotFoundError(Error):
"""Raised when we attempt to add a card to a count but cannot find it in the oracle."""


class CountType(str, enum.Enum):
"""Enum for possible card printing types (nonfoil, foil)."""

Expand Down Expand Up @@ -39,6 +47,10 @@ def aggregate_card_counts(
if value:
counts[count_type] = value + counts.get(count_type, 0)
if counts:
if scryfall_id not in oracle.index.id_to_card:
raise CardNotFoundError(
f"Found counts for card={scryfall_id} not found scryfall data"
)
card_counts[scryfall_id] = counts
return card_counts

Expand Down
43 changes: 27 additions & 16 deletions tests/containers/test_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from mtg_ssm.containers import counts
from mtg_ssm.containers.bundles import ScryfallDataSet
from mtg_ssm.containers.counts import CountType, ScryfallCardCount
from mtg_ssm.containers.counts import CardNotFoundError, CountType, ScryfallCardCount
from mtg_ssm.containers.indexes import Oracle
from mtg_ssm.containers.legacy import NoMatchError

Expand Down Expand Up @@ -111,37 +111,48 @@ def test_diff_card_counts(
id="no id",
),
pytest.param(
[{"scryfall_id": UUID("00000000-0000-0000-0000-000000000001"), "foil": 1}],
{UUID("00000000-0000-0000-0000-000000000001"): {counts.CountType.FOIL: 1}},
[{"scryfall_id": UUID("9d26f171-5bb6-463c-8473-53b6cc27ed66"), "foil": 1}],
{UUID("9d26f171-5bb6-463c-8473-53b6cc27ed66"): {counts.CountType.FOIL: 1}},
id="id and int",
),
pytest.param(
[{"scryfall_id": "00000000-0000-0000-0000-000000000001", "foil": "1"}],
{UUID("00000000-0000-0000-0000-000000000001"): {counts.CountType.FOIL: 1}},
[{"scryfall_id": "9d26f171-5bb6-463c-8473-53b6cc27ed66", "foil": "1"}],
{UUID("9d26f171-5bb6-463c-8473-53b6cc27ed66"): {counts.CountType.FOIL: 1}},
id="text and text",
),
pytest.param(
[{"scryfall_id": "00000000-0000-0000-0000-000000000001", "nonfoil": "1"}],
{},
marks=pytest.mark.xfail(raises=CardNotFoundError),
id="count with bad id",
),
pytest.param(
[{"scryfall_id": "00000000-0000-0000-0000-000000000001", "nonfoil": ""}],
{},
id="no count with bad id",
),
pytest.param(
[
{
"scryfall_id": "00000000-0000-0000-0000-000000000001",
"scryfall_id": "9d26f171-5bb6-463c-8473-53b6cc27ed66",
"foil": "1",
"nonfoil": "",
}
],
{UUID("00000000-0000-0000-0000-000000000001"): {counts.CountType.FOIL: 1}},
{UUID("9d26f171-5bb6-463c-8473-53b6cc27ed66"): {counts.CountType.FOIL: 1}},
id="empty string",
),
pytest.param(
[
{"scryfall_id": "00000000-0000-0000-0000-000000000001", "foil": "1"},
{"scryfall_id": "00000000-0000-0000-0000-000000000002", "foil": "0"},
{"scryfall_id": "00000000-0000-0000-0000-000000000003", "nonfoil": "1"},
{"scryfall_id": "9d26f171-5bb6-463c-8473-53b6cc27ed66", "foil": "1"},
{"scryfall_id": "758abd53-6ad2-406e-8615-8e48678405b4", "foil": "0"},
{"scryfall_id": "0180d9a8-992c-4d55-8ac4-33a587786993", "nonfoil": "1"},
],
{
UUID("00000000-0000-0000-0000-000000000001"): {
UUID("9d26f171-5bb6-463c-8473-53b6cc27ed66"): {
counts.CountType.FOIL: 1
},
UUID("00000000-0000-0000-0000-000000000003"): {
UUID("0180d9a8-992c-4d55-8ac4-33a587786993"): {
counts.CountType.NONFOIL: 1
},
},
Expand All @@ -150,20 +161,20 @@ def test_diff_card_counts(
pytest.param(
[
{
"scryfall_id": UUID("00000000-0000-0000-0000-000000000001"),
"scryfall_id": UUID("9d26f171-5bb6-463c-8473-53b6cc27ed66"),
"foil": 1,
},
{
"scryfall_id": UUID("00000000-0000-0000-0000-000000000001"),
"scryfall_id": UUID("9d26f171-5bb6-463c-8473-53b6cc27ed66"),
"nonfoil": 1,
},
{
"scryfall_id": UUID("00000000-0000-0000-0000-000000000001"),
"scryfall_id": UUID("9d26f171-5bb6-463c-8473-53b6cc27ed66"),
"foil": 1,
},
],
{
UUID("00000000-0000-0000-0000-000000000001"): {
UUID("9d26f171-5bb6-463c-8473-53b6cc27ed66"): {
counts.CountType.FOIL: 2,
counts.CountType.NONFOIL: 1,
}
Expand Down

0 comments on commit 2e1f517

Please sign in to comment.