mealie/tests/unit_tests/test_crfpp_parser.py
Hayden 60908e5a88
Feature/CRF++ and server side locales (#731)
* add universal toast plugin

* add server side locales

* integrate CRF++ into CI/CD Pipeline

* docs(docs): 📝 add recipe parser docs

* feat(backend):  Continued work on ingredient parsers

* add new model dest

* feat(frontend):  New ingredient parser page

* formatting

Co-authored-by: Hayden <hay-kot@pm.me>
2021-10-09 13:08:23 -08:00

44 lines
1.4 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from dataclasses import dataclass
from fractions import Fraction
import pytest
from mealie.services.parser_services.crfpp.processor import CRFIngredient, convert_list_to_crf_model
@dataclass
class TestIngredient:
input: str
quantity: float
unit: str
food: str
comments: str
# TODO - add more robust test cases
test_ingredients = [
TestIngredient("½ cup all-purpose flour", 0.5, "cup", "all-purpose flour", ""),
TestIngredient("1½ teaspoons ground black pepper", 1.5, "teaspoon", "black pepper", "ground"),
TestIngredient("⅔ cup unsweetened flaked coconut", 0.7, "cup", "coconut", "unsweetened flaked"),
TestIngredient("⅓ cup panko bread crumbs", 0.3, "cup", "panko bread crumbs", ""),
]
def crf_exists() -> bool:
import shutil
return shutil.which("crf_test") is not None
@pytest.mark.skipif(not crf_exists(), reason="CRF++ not installed")
def test_nlp_parser():
models: list[CRFIngredient] = convert_list_to_crf_model([x.input for x in test_ingredients])
# Itterate over mdoels and test_ingreidnets to gether
for model, test_ingredient in zip(models, test_ingredients):
assert float(sum(Fraction(s) for s in model.qty.split())) == test_ingredient.quantity
assert model.comment == test_ingredient.comments
assert model.name == test_ingredient.food
assert model.unit == test_ingredient.unit