def test_10_incorrect_database(self): """ Test database insertion for a single user with incorrect database. Test case to increase the coverage. """ diet = Diet(None, self.user_id_2) s = diet.insert_in_database(self.diet_dict, date_time=self.dt1) self.assertFalse(s) d1 = date.today() + timedelta(days=1) dt1 = datetime(d1.year, d1.month, d1.day) + timedelta(hours=8) result, success = diet.get_columns_given_range(dt1, dt1 + timedelta(days=1)) self.assertFalse(success)
def getMeals(): """ Fetch all logged meals from database given a time period. Parameters ---------- token : str Unique token based on user ID. dateFrom : timestamp Start date of the data that wants to be fetched. dateTo : timestamp End date of the data that wants to be fetched. Returns ------- result : Tuple[Any, int] If successful, the first part of the tuple gives a dict containing the meal data for the given time period. Otherwise, it gives an error message. The second part of the tuple gives the HTTP status code. """ args = request.args if not args: return "Arguments needed.", 400 dates_data = check_datetimes(args) if dates_data['status_code'] != 200: return dates_data['msg'], dates_data['status_code'] dateFrom = dates_data['dateFrom'] dateTo = dates_data['dateTo'] token = check_token(args) if token['status_code'] != 200: return token['msg'], token['status_code'] token = token['token'] id = getIdFromToken(token) if id < 0: return "Invalid Token", 401 diet = Diet(DB_OBJECT, id) db_data, success = diet.get_columns_given_range(dateFrom, dateTo) if not success: if db_data == -1: return "Server Error", 500 else: return json.dumps([]), 200 else: db_data = json.dumps(db_data) return db_data, 200
def setUpClass(self): """ Set up the unit test. """ self.db = DB() self.email = '*****@*****.**' self.password = '******' self.fullname = 'ABC' self.user = User(self.db) self.user.create_new_user(self.email, self.password, self.fullname) self.user_id = self.user.check_email_match(self.email) self.email_1 = '*****@*****.**' self.password_1 = 'klmno' self.fullname_1 = 'XYZ' self.user_1 = User(self.db) self.user_1.create_new_user(self.email_1, self.password_1, self.fullname_1) self.user_id_1 = self.user_1.check_email_match(self.email_1) self.email_2 = '*****@*****.**' self.password_2 = 'qqq' self.fullname_2 = 'ASD' self.user_2 = User(self.db) self.user_2.create_new_user(self.email_2, self.password_2, self.fullname_2) self.user_id_2 = self.user_2.check_email_match(self.email_2) self.diet = Diet(self.db, self.user_id) self.diet_1 = Diet(self.db, self.user_id_1) self.diet_2 = Diet(self.db, self.user_id_2) self.diet_dict = {'Item': 'Apple',\ 'ServingSize': 1.0,\ 'Barcode': False,\ 'nutri_dict': {'Cals': 100.0,'Protein': 100.0, 'Carbs': 100.0, 'Fat': 100.0, 'Fiber': 100.0}} self.diet_dict_1 = {'Item': 'Orange',\ 'ServingSize': 2.0,\ 'Barcode': False,\ 'nutri_dict': {'Cals': 105.0,'Protein': 105.0, 'Carbs': 105.0, 'Fat': 105.0, 'Fiber': 105.0}} self.diet_dict_2 = {'Item': 'banana',\ 'ServingSize': 2.0,\ 'Barcode': False,\ 'nutri_dict': {'Cals': 101.0,'Protein': 101.0, 'Carbs': 101.0, 'Fat': 101.0, 'Fiber': 101.0}} self.dt1 = datetime.utcnow() self.unix_time = round(time.time())
class TestDiet(unittest.TestCase): """ Tests for Diet.py """ @classmethod def setUpClass(self): """ Set up the unit test. """ self.db = DB() self.email = '*****@*****.**' self.password = '******' self.fullname = 'ABC' self.user = User(self.db) self.user.create_new_user(self.email, self.password, self.fullname) self.user_id = self.user.check_email_match(self.email) self.email_1 = '*****@*****.**' self.password_1 = 'klmno' self.fullname_1 = 'XYZ' self.user_1 = User(self.db) self.user_1.create_new_user(self.email_1, self.password_1, self.fullname_1) self.user_id_1 = self.user_1.check_email_match(self.email_1) self.email_2 = '*****@*****.**' self.password_2 = 'qqq' self.fullname_2 = 'ASD' self.user_2 = User(self.db) self.user_2.create_new_user(self.email_2, self.password_2, self.fullname_2) self.user_id_2 = self.user_2.check_email_match(self.email_2) self.diet = Diet(self.db, self.user_id) self.diet_1 = Diet(self.db, self.user_id_1) self.diet_2 = Diet(self.db, self.user_id_2) self.diet_dict = {'Item': 'Apple',\ 'ServingSize': 1.0,\ 'Barcode': False,\ 'nutri_dict': {'Cals': 100.0,'Protein': 100.0, 'Carbs': 100.0, 'Fat': 100.0, 'Fiber': 100.0}} self.diet_dict_1 = {'Item': 'Orange',\ 'ServingSize': 2.0,\ 'Barcode': False,\ 'nutri_dict': {'Cals': 105.0,'Protein': 105.0, 'Carbs': 105.0, 'Fat': 105.0, 'Fiber': 105.0}} self.diet_dict_2 = {'Item': 'banana',\ 'ServingSize': 2.0,\ 'Barcode': False,\ 'nutri_dict': {'Cals': 101.0,'Protein': 101.0, 'Carbs': 101.0, 'Fat': 101.0, 'Fiber': 101.0}} self.dt1 = datetime.utcnow() self.unix_time = round(time.time()) def test_0_data_insertion(self): """ Test database insertion for a single user. """ s = self.diet.insert_in_database(self.diet_dict, date_time=self.dt1) self.assertEqual(s, True) def test_1_data_insertion_multiple_users(self): """ Test database insertion for multiple users. """ s = self.diet.insert_in_database(self.diet_dict, date_time=self.dt1) self.assertEqual(s, True) s_1 = self.diet_1.insert_in_database(self.diet_dict_1, date_time=self.dt1) self.assertEqual(s_1, True) s_2 = self.diet_2.insert_in_database(self.diet_dict_2) self.assertEqual(s_2, True) def test_2_incorrect_key(self): """ Test for incorrect keys. The function should return False if the input dictionary keys are incorrect. """ d = copy.deepcopy(self.diet_dict) del (d['Item']) d['ItemS'] = 'Apple' self.assertFalse(self.diet.insert_in_database(d)) d = copy.deepcopy(self.diet_dict) del (d['ServingSize']) d['ServinsSizeS'] = 1.0 self.assertFalse(self.diet.insert_in_database(d)) d = copy.deepcopy(self.diet_dict) del (d['Barcode']) d['BarcodeS'] = False self.assertFalse(self.diet.insert_in_database(d)) d = copy.deepcopy(self.diet_dict) del (d['nutri_dict']) d['nutri_dictS'] = { 'Cals': 100.0, 'Protein': 100.0, 'Carbs': 100.0, 'Fat': 100.0, 'Fiber': 100.0 } self.assertFalse(self.diet.insert_in_database(d)) def test_3_incorrect_key_1(self): """ Test for incorrect keys in the nutri_dict. The function should return False if the nutrient names is incorrect. """ d = copy.deepcopy(self.diet_dict) del (d['nutri_dict']['Cals']) d['nutri_dict']['CalsS'] = 100.0 self.assertFalse(self.diet.insert_in_database(d)) d = copy.deepcopy(self.diet_dict) del (d['nutri_dict']['Protein']) d['nutri_dict']['ProteinS'] = 100.0 self.assertFalse(self.diet.insert_in_database(d)) d = copy.deepcopy(self.diet_dict) del (d['nutri_dict']['Carbs']) d['nutri_dict']['CarbS'] = 100.0 self.assertFalse(self.diet.insert_in_database(d)) d = copy.deepcopy(self.diet_dict) del (d['nutri_dict']['Fat']) d['nutri_dict']['FatS'] = 100.0 self.assertFalse(self.diet.insert_in_database(d)) d = copy.deepcopy(self.diet_dict) del (d['nutri_dict']['Fiber']) d['nutri_dict']['FiberS'] = 100.0 self.assertFalse(self.diet.insert_in_database(d)) def test_4_incorrect_value(self): """ Test for incorrect value data type. """ d = copy.deepcopy(self.diet_dict) d['Item'] = 1 self.assertFalse(self.diet.insert_in_database(d)) d = copy.deepcopy(self.diet_dict) d['ServingSize'] = 'Apple' self.assertFalse(self.diet.insert_in_database(d)) d = copy.deepcopy(self.diet_dict) d['Barcode'] = 1 self.assertFalse(self.diet.insert_in_database(d)) def test_5_incorrect_value_1(self): """ Test for incorrect value data type in the nutrients dictionary. """ d = copy.deepcopy(self.diet_dict) d['nutri_dict']['Cals'] = 'Apple' self.assertFalse(self.diet.insert_in_database(d)) d = copy.deepcopy(self.diet_dict) d['nutri_dict']['Protein'] = 'Apple' self.assertFalse(self.diet.insert_in_database(d)) d = copy.deepcopy(self.diet_dict) d['nutri_dict']['Fiber'] = 'Apple' self.assertFalse(self.diet.insert_in_database(d)) d = copy.deepcopy(self.diet_dict) d['nutri_dict']['Carbs'] = 'Apple' self.assertFalse(self.diet.insert_in_database(d)) d = copy.deepcopy(self.diet_dict) d['nutri_dict']['Fat'] = 'Apple' self.assertFalse(self.diet.insert_in_database(d)) def test_6_data_fetching_unix_time_and_insertion(self): """ Test fetching diet data from database and also checks if the UNIX TIMESTAMP is correct. """ d1 = date.today() dt1 = datetime(d1.year, d1.month, d1.day) + timedelta(hours=8) result, success = self.diet.get_columns_given_range( dt1, dt1 + timedelta(days=1)) self.assertTrue(success) self.assertEqual(result[0]['Datetime'], self.unix_time) def test_7_data_fetching_values(self): """ Test fetching diet data from database for multiple users. """ d1 = date.today() dt1 = datetime(d1.year, d1.month, d1.day) + timedelta(hours=8) result, success = self.diet.get_columns_given_range( dt1, dt1 + timedelta(days=1)) self.assertTrue(success) self.assertEqual(result[0]['Item'], 'Apple') self.assertEqual(result[0]['ServingSize'], 1.0) self.assertEqual(result[0]['Cals'], 100.0) self.assertEqual(result[0]['Fiber'], 100.0) self.assertEqual(result[0]['Carbs'], 100.0) self.assertEqual(result[0]['Fat'], 100.0) self.assertEqual(result[0]['Protein'], 100.0) self.assertFalse(result[0]['Barcode']) result_1, success_1 = self.diet_1.get_columns_given_range( dt1, dt1 + timedelta(days=1)) self.assertTrue(success_1) self.assertEqual(result_1[0]['Item'], 'Orange') self.assertEqual(result_1[0]['ServingSize'], 2.0) self.assertEqual(result_1[0]['Cals'], 105.0) self.assertEqual(result_1[0]['Fiber'], 105.0) self.assertEqual(result_1[0]['Carbs'], 105.0) self.assertEqual(result_1[0]['Fat'], 105.0) self.assertEqual(result_1[0]['Protein'], 105.0) self.assertFalse(result_1[0]['Barcode']) def test_8_incorrect_data_fetching(self): """ Test fetching diet data from the database for incorrect date range. The function should return False. """ d1 = date.today() dt1 = datetime(d1.year, d1.month, d1.day) + timedelta(hours=8) result, success = self.diet.get_columns_given_range( dt1 + timedelta(days=10), dt1 + timedelta(days=11)) self.assertFalse(success) def test_9_data_fetching_multiple(self): """ Test fetching multiple diet entries from the database. """ d = {'Item': 'Apple',\ 'ServingSize': 1.0,\ 'Barcode': False,\ 'nutri_dict': {'Cals': 100.0,'Protein': 100.0, 'Carbs': 100.0, 'Fat': 100.0, 'Fiber': 100.0}} _ = self.diet.insert_in_database(d, date_time=datetime.utcnow() + timedelta(days=1) + timedelta(minutes=1)) d = {'Item': 'Orange',\ 'ServingSize': 1.0,\ 'Barcode': False,\ 'nutri_dict': {'Cals': 100.0,'Protein': 100.0, 'Carbs': 100.0, 'Fat': 100.0, 'Fiber': 100.0}} _ = self.diet.insert_in_database(d, date_time=datetime.utcnow() + timedelta(days=1) + timedelta(minutes=2)) d = {'Item': 'Banana',\ 'ServingSize': 1.0,\ 'Barcode': False,\ 'nutri_dict': {'Cals': 100.0,'Protein': 100.0, 'Carbs': 100.0, 'Fat': 100.0, 'Fiber': 100.0}} _ = self.diet.insert_in_database(d, date_time=datetime.utcnow() + timedelta(days=1) + timedelta(minutes=3)) d1 = date.today() + timedelta(days=1) dt1 = datetime(d1.year, d1.month, d1.day) + timedelta(hours=8) result, success = self.diet.get_columns_given_range( dt1, dt1 + timedelta(days=1)) self.assertTrue(success) self.assertEqual(len(result), 3) self.assertEqual(result[0]['Item'], 'Apple') self.assertEqual(result[1]['Item'], 'Orange') self.assertEqual(result[2]['Item'], 'Banana') def test_10_incorrect_database(self): """ Test database insertion for a single user with incorrect database. Test case to increase the coverage. """ diet = Diet(None, self.user_id_2) s = diet.insert_in_database(self.diet_dict, date_time=self.dt1) self.assertFalse(s) d1 = date.today() + timedelta(days=1) dt1 = datetime(d1.year, d1.month, d1.day) + timedelta(hours=8) result, success = diet.get_columns_given_range(dt1, dt1 + timedelta(days=1)) self.assertFalse(success)
def addMeal(): """ Log meal and add it to database. Parameters ---------- token : str Unique token based on user ID. item : Union[str, int] Food item name or barcode. ServingSize : float Serving size of the food consumed. barcode : Optional[bool] True if user scans barcode, False otherwise. Returns ------- result : Tuple[str, int] The first part gives HTTP error message. The second part of the tuple is the HTTP status code. """ args = request.form if not args: return "Arguments needed.", 400 if not 'item' in args: return "The 'item' is a required parameter", 400 item = args['item'] if not 'ServingSize' in args: return "The 'ServingSize' is a required parameter", 400 serving_size = args['ServingSize'] try: serving_size = float(serving_size) except ValueError: return "The 'ServingSize' must be a float or int", 400 barcode = False if 'barcode' in args and args['barcode'].upper() == "TRUE": barcode = True food_dict, success = EDAMAM_API.get_top_matches(item, barcode, 1, serving_size) if not success: return "Unable to find the food item based on given info", 400 label = food_dict[0]['Label'] food_dict = food_dict[0]['Nutrients'] token = check_token(args) if token['status_code'] != 200: return token['msg'], token['status_code'] token = token['token'] id = getIdFromToken(token) if id < 0: return "Invalid Token", 401 insert_food_dict = { 'Item': label, 'ServingSize': serving_size, 'Barcode': barcode } nutri_dict = {} for k, v in food_dict.items(): nutri_dict[k] = v insert_food_dict['nutri_dict'] = nutri_dict diet = Diet(DB_OBJECT, id) success = diet.insert_in_database(insert_food_dict) if not success: return "Server Error", 500 else: return "Successful", 200