From c0a9bb6343c703bc8d3210d8cdd534b90adf2bf5 Mon Sep 17 00:00:00 2001 From: James Addison <55152140+jayaddison@users.noreply.github.com> Date: Tue, 22 Oct 2024 22:55:48 +0000 Subject: [PATCH] Utils: reject empty input in `get_yields` (#1314) --- recipe_scrapers/_utils.py | 2 ++ tests/library/test_utils.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/recipe_scrapers/_utils.py b/recipe_scrapers/_utils.py index b1a0906d4..438b6f970 100644 --- a/recipe_scrapers/_utils.py +++ b/recipe_scrapers/_utils.py @@ -199,6 +199,8 @@ def get_yields(element): serve_text = element else: serve_text = element.get_text() + if not serve_text: + raise ValueError("Cannot extract yield information from empty string") if SERVE_REGEX_TO.search(serve_text): serve_text = serve_text.split(SERVE_REGEX_TO.split(serve_text, 2)[1], 2)[1] diff --git a/tests/library/test_utils.py b/tests/library/test_utils.py index 148920d86..5de17c39e 100644 --- a/tests/library/test_utils.py +++ b/tests/library/test_utils.py @@ -6,6 +6,7 @@ get_minutes, get_nutrition_keys, get_url_slug, + get_yields, url_path_to_dict, ) @@ -168,3 +169,10 @@ def test_get_nutrition_keys(self): "cholesterolContent", ] self.assertEqual((expected_order), (nutrition_keys)) + + def test_get_yields(self): + self.assertEqual("5 servings", get_yields("5")) + + def test_get_yields_empty_string(self): + with self.assertRaises(ValueError): + get_yields("")