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 report_cpfc(aim: Cpfc, real: Cpfc): """ ["", "Aim", "Real", "% Aim", "% Real"], ["kCal", "1000", "100", "", ""], ["Protein", "200", "20", "0.20", "0.20"], ["Fats", "300", "30", "0.30", "0.30"], ["Carb", "500", "50", "0.50", "0.50"] """ if aim.pfc.is_perc: aim = aim.unperc() if real.pfc.is_perc: real = real.unperc() return [ ["", "Aim", "Real", "% Aim", "% Real"], ["Calories", "%.0f" % aim.cals, "%.0f" % real.cals, "", ""], ["Protein", "%.0f" % aim.pfc.prot, "%.0f" % real.pfc.prot, "%.0f%%" % (aim.pfc.perc().prot * 100), "%.0f%%" % (real.pfc.perc().prot * 100)], ["Fats", "%.0f" % aim.pfc.fats, "%.0f" % real.pfc.fats, "%.0f%%" % (aim.pfc.perc().fats * 100), "%.0f%%" % (real.pfc.perc().fats * 100)], ["Carb", "%.0f" % aim.pfc.carb, "%.0f" % real.pfc.carb, "%.0f%%" % (aim.pfc.perc().carb * 100), "%.0f%%" % (real.pfc.perc().carb * 100)], ]
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_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 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_cpfc_zero(): dish = Dish(ingredients=[]) assert dish.cpfc() == Cpfc(cals=0, pfc=Pfc(prot=0, fats=0, carb=0))
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