Пример #1
0
 def get_all_foods(self):
     """returns a list of all food items in the db"""
     with self.conn:
         self.c.execute("SELECT * FROM fooditems")
         all_foods = self.c.fetchall()
     food_items = []
     for info_list in all_foods:
         food = FoodItem()
         food.set_info_from_list(info_list)
         food_items.append(food)
     return food_items
Пример #2
0
def get_update_info():
    prompts = ('name', 'ss', 'unit', 'cal', 'carb', 'fat', 'protein', 'fiber', 'sugar')
    info = []
    updated_food = FoodItem()
    answer = ''
    i = 0
    while answer != 'x' and i < 9:
        answer = input(f"Enter {prompts[i]}: ")
        if answer.lower() == 'x':
            return None
        if prompts[i] == 'name':
            food_by_name = food_dao.get_foods_by_name(answer)
            if food_by_name is None:
                print(f"\nNo results found for {answer}")
                return None
        info.append(answer)
        i += 1
    updated_food.set_info_from_list(info)
    return updated_food
Пример #3
0
class TestFoodItem(unittest.TestCase):
    def setUp(self):
        self.food_1 = FoodItem('apple', '50', 'g', '100', '20', '1', '2', '3',
                               '10')
        self.food_2 = FoodItem('oats', '40', 'g', '120', '25', '3', '5', '6',
                               '1')

    def test_set_info_from_dict(self):
        new_info = {
            'name': 'banana',
            'ss': '30',
            'unit': 'grams',
            'cal': '120',
            'carb': '25',
            'fat': '0',
            'protein': '2',
            'fiber': '5',
            'sugar': '20'
        }
        self.food_1.set_info_from_dict(new_info)
        self.assertEqual('banana', self.food_1.name)
        self.assertEqual('30', self.food_1.ss)
        self.assertEqual('grams', self.food_1.unit)
        self.assertEqual('120', self.food_1.cal)
        self.assertEqual('25', self.food_1.carb)
        self.assertEqual('0', self.food_1.fat)
        self.assertEqual('2', self.food_1.protein)
        self.assertEqual('5', self.food_1.fiber)
        self.assertEqual('20', self.food_1.sugar)

    def test_proportionalize(self):
        self.food_1.proportionalize(100)

        self.assertEqual('apple', self.food_1.name)
        self.assertEqual('100.0', self.food_1.ss)
        self.assertEqual('g', self.food_1.unit)
        self.assertEqual('200.0', self.food_1.cal)
        self.assertEqual('40.0', self.food_1.carb)
        self.assertEqual('2.0', self.food_1.fat)
        self.assertEqual('4.0', self.food_1.protein)
        self.assertEqual('6.0', self.food_1.fiber)
        self.assertEqual('20.0', self.food_1.sugar)

        self.food_2.proportionalize(125)
        self.assertEqual('oats', self.food_2.name)
        self.assertEqual('125.0', self.food_2.ss)
        self.assertEqual('g', self.food_2.unit)
        self.assertEqual('375.0', self.food_2.cal)
        self.assertEqual('78.1', self.food_2.carb)
        self.assertEqual('9.4', self.food_2.fat)
        self.assertEqual('15.6', self.food_2.protein)
        self.assertEqual('18.8', self.food_2.fiber)
        self.assertEqual('3.1', self.food_2.sugar)

    def test_set_info_from_list(self):
        new_info_list_1 = [
            'apple2', '60', 'grams', '110', '25', '2', '3', '4', '15'
        ]
        self.food_1.set_info_from_list(new_info_list_1)

        self.assertEqual('apple2', self.food_1.name)
        self.assertEqual('60', self.food_1.ss)
        self.assertEqual('grams', self.food_1.unit)
        self.assertEqual('110', self.food_1.cal)
        self.assertEqual('25', self.food_1.carb)
        self.assertEqual('2', self.food_1.fat)
        self.assertEqual('3', self.food_1.protein)
        self.assertEqual('4', self.food_1.fiber)
        self.assertEqual('15', self.food_1.sugar)

        new_info_list_2 = [
            'oats2', '100', 'grams', '200', '40', '15', '6', '2', '9'
        ]
        self.food_2.set_info_from_list(new_info_list_2)
        self.assertEqual('oats2', self.food_2.name)
        self.assertEqual('100', self.food_2.ss)
        self.assertEqual('grams', self.food_2.unit)
        self.assertEqual('200', self.food_2.cal)
        self.assertEqual('40', self.food_2.carb)
        self.assertEqual('15', self.food_2.fat)
        self.assertEqual('6', self.food_2.protein)
        self.assertEqual('2', self.food_2.fiber)
        self.assertEqual('9', self.food_2.sugar)
Пример #4
0
def insert_food(food_info):
    food = FoodItem()
    food.set_info_from_list(food_info)
    food_dao.insert_food(food)