def test_serv(): assert Ingredient("a", _serv=None, _cpfc=Cpfc(100, Pfc(0, 0, 0, is_perc=False)), _price=100).serv() == 100 assert Ingredient("a", _serv=100, _cpfc=Cpfc(100, Pfc(0, 0, 0, is_perc=False)), _price=100).serv() == 100 assert Ingredient("a", _serv=50, _cpfc=Cpfc(100, Pfc(0, 0, 0, is_perc=False)), _price=100).serv() == 50
def test_unperc(): p, f, c = 3, 4, 5 cals = 4.1 * p + 9.29 * f + 4.1 * c pfc_perc = Cpfc(cals, Pfc(p, f, c)).pfc.perc() assert Cpfc(cals, pfc_perc).unperc() == Cpfc(cals, Pfc(p, f, c))
def test_scale(): assert Pfc(1, 1, 1) * 2 == Pfc(2, 2, 2)
def test_perc(): assert Pfc(100, 200, 300, is_perc=False).perc() == Pfc(1 / 6, 2 / 6, 3 / 6, is_perc=True)
def test___mul__(): assert Pfc(1, 2, 3) * 2 == Pfc(2, 4, 6)
def test_sum(): assert Pfc(0.93, 0.24, 9.58).sum() == 10.75
def test___abs__(): assert abs(Pfc(-1, -2, -3)) == Pfc(1, 2, 3) assert abs(Pfc(-1, 0, 1)) == Pfc(1, 0, 1)
def test_cpfc_zero(): dish = Dish(ingredients=[]) assert dish.cpfc() == Cpfc(cals=0, pfc=Pfc(prot=0, fats=0, carb=0))
def test___neg__(): assert -Pfc(1, 2, 3) == Pfc(-1, -2, -3) assert -Pfc(-1, -2, -3) == Pfc(1, 2, 3) assert Pfc(0, 0, 0) == -Pfc(0, 0, 0)
def cpfc(self) -> Cpfc: if len(self.ingredients) == 0: return Cpfc(0, Pfc(0, 0, 0)) else: return sum((_.cpfc() for _ in self.ingredients), start=Cpfc(0, Pfc(0, 0, 0)))
def test__score_kpfc_non_perc(): assert Matcher(ingredients, Cpfc(10, Pfc(20, 30, 50)))._score_kpfc(_dish) == 0
from Dish import Dish from data.Cpfc import Cpfc, Pfc from data.Matcher import Matcher from data.ingredients import ingredients, _ _target = Cpfc(10, Pfc(0.2, 0.3, 0.5, is_perc=True)) _dish = Dish( [_(title="apple", serv=50, cals=10, prot=2, fats=3, carb=5, price=0)] * 2) _matcher = Matcher(ingredients, _target) def test__score_kpfc(): assert _matcher._score_kpfc(_dish) == 0 def test__score_kpfc_non_perc(): assert Matcher(ingredients, Cpfc(10, Pfc(20, 30, 50)))._score_kpfc(_dish) == 0 def test__random_dish(): from random import seed seed(1) _matcher._random_dish() def test_find(): _matcher.find() def test__score_ingredients():
def test_report(): _target = Cpfc(1800, Pfc(0.35, 0.16, 0.49, is_perc=True)) from data.ingredients import _ _meal = Dish( [_("carrot", serv=50, cals=2, prot=4, fats=8, carb=16, price=0)]) report_meal(_meal)
from Dish import Dish from Reporter import report_cpfc, report_meal from data.Cpfc import Cpfc, Pfc _expected = Cpfc(1000, Pfc(200, 300, 500)) _actual = Cpfc(100, Pfc(20, 30, 50)) def test_report_cpfc(): # @formatter:off assert report_cpfc(_expected, _actual) == [ ["", "Aim", "Real", "% Aim", "% Real"], ["Calories", "1000", "100", "", ""], ["Protein", "200", "20", "20%", "20%"], ["Fats", "300", "30", "30%", "30%"], ["Carb", "500", "50", "50%", "50%"], ] # @formatter:on def test_report(): _target = Cpfc(1800, Pfc(0.35, 0.16, 0.49, is_perc=True)) from data.ingredients import _ _meal = Dish( [_("carrot", serv=50, cals=2, prot=4, fats=8, carb=16, price=0)]) report_meal(_meal)
def test___add__(): assert Pfc(1, 2, 3) + Pfc(2, 3, 4) == Pfc(3, 5, 7)
def test___sub__(): assert Pfc(1, 2, 3) - Pfc(2, 3, 4) == Pfc(-1, -1, -1)
def _(title: str, serv: Optional[float], cals: float, prot: float, fats: float, carb: float, price: float): return Ingredient(title, serv, Cpfc(cals, Pfc(prot, fats, carb)), price)
def test_cpfc_one(): dish = Dish( [_("carrot", serv=50, cals=2, prot=4, fats=8, carb=16, price=1)]) expected = Cpfc(cals=1, pfc=Pfc(prot=2, fats=4, carb=8)) assert dish.cpfc() == expected