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_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 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