Skip to content

Commit

Permalink
Utils: reject empty input in get_yields (#1314)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayaddison authored Oct 22, 2024
1 parent c1ba08b commit c0a9bb6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions recipe_scrapers/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 8 additions & 0 deletions tests/library/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
get_minutes,
get_nutrition_keys,
get_url_slug,
get_yields,
url_path_to_dict,
)

Expand Down Expand Up @@ -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("")

0 comments on commit c0a9bb6

Please sign in to comment.