Reject malformed text imports before creating junk recipes
Co-authored-by: fredamn76 <fredrik.fallman@gmail.com> Signed-off-by: fredamn76 <fredrik.fallman@gmail.com>
This commit is contained in:
@@ -288,6 +288,17 @@ def import_recipe_text(text: str, source_url: str | None = None) -> dict[str, An
|
||||
roundup, a YouTube description, or a social caption. Build the text in Swedish
|
||||
with ``Titel`` / ``Portioner`` / ``Ingredienser`` / ``Gör så här`` sections.
|
||||
"""
|
||||
required = ("Titel", "Ingredienser", "Gör så här")
|
||||
missing = [
|
||||
heading
|
||||
for heading in required
|
||||
if not re.search(rf"(?im)^\s*{re.escape(heading)}\s*:", text)
|
||||
]
|
||||
if missing:
|
||||
raise ValueError(
|
||||
"Text import requires explicit Swedish section headings; missing: "
|
||||
+ ", ".join(missing)
|
||||
)
|
||||
payload: dict[str, Any] = {"data": text}
|
||||
if source_url:
|
||||
payload["url"] = source_url
|
||||
|
||||
@@ -96,6 +96,36 @@ class TestImportReport:
|
||||
assert server._import_report(recipe)["suggested_title"] is None
|
||||
|
||||
|
||||
class TestTextImport:
|
||||
def test_rejects_text_without_explicit_sections_before_writing(self, monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
server,
|
||||
"_request",
|
||||
lambda *_args, **_kwargs: pytest.fail("must not create a junk recipe"),
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="Titel"):
|
||||
server.import_recipe_text(
|
||||
"Namnlöst recept\n\nIngredienser:\n1 dl vatten\n\nGör så här:\nBlanda."
|
||||
)
|
||||
|
||||
def test_accepts_the_documented_swedish_section_format(self, monkeypatch):
|
||||
text = (
|
||||
"Titel: Testsoppa\n\nPortioner: 2\n\nIngredienser:\n1 dl vatten\n\n"
|
||||
"Gör så här:\nBlanda."
|
||||
)
|
||||
recipe = {
|
||||
"slug": "testsoppa",
|
||||
"name": "Testsoppa",
|
||||
"recipeIngredient": [{"display": "1 dl vatten"}],
|
||||
"recipeInstructions": [{"text": "Blanda."}],
|
||||
}
|
||||
monkeypatch.setattr(server, "_request", lambda *_args, **_kwargs: "testsoppa")
|
||||
monkeypatch.setattr(server, "get_recipe", lambda _slug: recipe)
|
||||
|
||||
assert server.import_recipe_text(text)["recipe"] == recipe
|
||||
|
||||
|
||||
class TestParseIngredients:
|
||||
def test_reverts_structure_when_mealie_rewords_the_swedish_line(self, mock_mealie):
|
||||
canonical = "4,7 dl basmatiris"
|
||||
|
||||
Reference in New Issue
Block a user