Skip to content

Commit

Permalink
Improve test options and fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
mlduff committed Apr 27, 2024
1 parent e955c1d commit 9caf271
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
33 changes: 28 additions & 5 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import pathlib
import unittest
from enum import Enum
from typing import Callable

from recipe_scrapers import scrape_html
Expand Down Expand Up @@ -36,7 +37,31 @@
]

OPTIONS_KEY = "_options"
OPTIONS = {"consistent_ingredient_groups": True}


class TestOptions(Enum):
CONSISTENT_INGREDIENT_GROUPS = ("consistent_ingredient_groups", True)
"""
Controls if the consistent ingredient groups test is run.
Disable if ingredient groups contain sub-quantities of the same ingredient (as the test will fail).
"""

def __new__(cls, value: str, default):
obj = object.__new__(cls)
obj._value_ = value
return obj

def __init__(self, value: str, default: str) -> None:
self.default = default


def get_options(expect):
options = {}
for option in TestOptions:
# Checks if the option has been set in the test
# Tolerates both the options node and the specific option not being defined
options[option] = expect.get(OPTIONS_KEY, {}).get(option.value, option.default)
return options


class RecipeTestCase(unittest.TestCase):
Expand Down Expand Up @@ -83,9 +108,7 @@ def test_func(self):
]
actual = scrape_html(testhtml.read_text(encoding="utf-8"), host)

options = {}
for option in options.keys():
options[option] = expect.get(OPTIONS_KEY, {}).get(option, OPTIONS[option])
options = get_options(expect)

# Mandatory tests
# If the key isn't present, check an assertion is raised
Expand Down Expand Up @@ -117,7 +140,7 @@ def test_func(self):
msg=f"The actual value for .{key}() did not match the expected value.",
)

if options.get("consistent_ingredient_groups"):
if options.get(TestOptions.CONSISTENT_INGREDIENT_GROUPS):
# Assert that the ingredients returned by the ingredient_groups() function
# are the same as the ingredients return by the ingredients() function.
grouped = []
Expand Down
2 changes: 1 addition & 1 deletion tests/test_data/ploetzblog.de/ploetzblog.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@
"total_time": 982,
"yields": "1 Stück zu (je) ca. 1050 g",
"_options": {
"consitent_ingredient_groups": false
"consistent_ingredient_groups": false
}
}

0 comments on commit 9caf271

Please sign in to comment.