def test_set_goal(self): """ Test setting a goal. Set all the three goals, Diet, Fitness and Sleep. """ self.assertTrue(self.goals.set_goal(self.diet_goal_dict)) self.assertTrue(self.goals.set_goal(self.fitness_goal_dict)) self.assertTrue(self.goals.set_goal(self.sleep_goal_dict)) goals = Goals(None, self.user_id) self.assertFalse(goals.set_goal(self.diet_goal_dict))
def test_get_all_goals(self): """ Test getting all the goals from the table. """ goals, status = self.goals.get_all_goals() self.assertTrue(status) goals = Goals(None, self.user_id) goal, status = goals.get_all_goals() self.assertFalse(status)
def test_get_type_goals(self): """ Test getting all the goals of a particular type. Mostly we will only be interested in the latest goal. """ goal, status = self.goals.get_type_goals('Calories') self.assertTrue(status) goals = Goals(None, self.user_id) goal, status = goals.get_type_goals('Calories') self.assertFalse(status)
def test_get_latest_goal(self): """ Test getting the latest goal of a particular type, Diet, Fitness or Sleep. """ goal, status = self.goals.get_latest_goal(Type='Calories') self.assertTrue(status) goal, status = self.goals.get_latest_goal(Type='FitnessMinutes') self.assertTrue(status) goal, status = self.goals.get_latest_goal(Type='SleepHours') self.assertTrue(status) goals = Goals(None, self.user_id) goal, status = goals.get_latest_goal(Type='Calories') self.assertFalse(status)
def test_alter_goal(self): """ Test altering a goal. Change all the goals and check for correctness. """ self.assertTrue(self.goals.alter_goal('Calories', 99.0)) self.assertTrue(self.goals.alter_goal('FitnessMinutes', 50.0)) self.assertTrue(self.goals.alter_goal('SleepHours', 1.99)) goal, status = self.goals.get_latest_goal(Type='Calories') self.assertEqual(goal[0]['Value'], 99.0) goal, status = self.goals.get_latest_goal(Type='FitnessMinutes') self.assertEqual(goal[0]['Value'], 50.0) goal, status = self.goals.get_latest_goal(Type='SleepHours') self.assertEqual(goal[0]['Value'], 1.99) goals = Goals(None, self.user_id) self.assertFalse(goals.alter_goal('Calories', 99.0))
def changeGoal(): """ Change a specific goal. Parameters ---------- token : str Unique token based on user ID. type : str Type of goal we want to change. Available options are: 'FitnessMinutes', 'Calories', 'SleepHours' value : float Value of the user's new goal. 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 goal_data = check_goal_type(args) if goal_data['status_code'] != 200: return goal_data['msg'], goal_data['status_code'] goal_type = goal_data['type'] goal_val_data = check_goal_value(args) if goal_val_data['status_code'] != 200: return goal_val_type['msg'], goal_val_type['status_code'] goal_value = goal_val_data['value'] 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 goals = Goals(DB_OBJECT, id) success = goals.alter_goal(goal_type, goal_value) if not success: return "Server Error", 500 else: return 'Successful', 200
def getTypeGoals(): """ Fetch a certain goal given the type. Parameters ---------- token : str Unique token based on user ID. type : str Type of goal we want to fetch. Available options are: 'FitnessMinutes', 'Calories', 'SleepHours' Returns ------- result : Tuple[Any, int] If successful, the first part of the tuple gives a dict containing all of the user's goals. 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 goal_data = check_goal_type(args) if goal_data['status_code'] != 200: return goal_data['msg'], goal_data['status_code'] goal_type = goal_data['type'] 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 goals = Goals(DB_OBJECT, id) db_data, success = goals.get_type_goals(goal_type) 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 = 'ABCD' 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.goals = Goals(self.db, self.user_id) self.diet_goal_dict = {'Type': 'Calories', 'Value': 1000.0} self.fitness_goal_dict = {'Type': 'FitnessMinutes', 'Value': 100.0} self.sleep_goal_dict = {'Type': 'SleepHours', 'Value': 10.0} self.goals.set_goal(self.diet_goal_dict) self.goals.set_goal(self.fitness_goal_dict) self.goals.set_goal(self.sleep_goal_dict)
def getAllGoals(): """ Fetch all of the user's set goals. Parameters ---------- token : str Unique token based on user ID. Returns ------- result : Tuple[Any, int] If successful, the first part of the tuple gives a dict containing all of the user's goals. 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 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 goals = Goals(DB_OBJECT, id) db_data, success = goals.get_all_goals() 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 register(): """ Register new user. Parameters ---------- email : str User's email. password : str User's password. fullname : str User's full name. 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 'email' in args: return "The 'email' is a required parameter", 400 email = args['email'] if not 'password' in args: return "The 'password' is a required parameter", 400 password = args['password'] if not 'fullname' in args: return "The 'fullname' is a required parameter", 400 fullname = args['fullname'] if USER.create_new_user(email, password, fullname): id = USER.check_password_match(email, password) if id < 0: "Unable to find registered user.", 500 goals = Goals(DB_OBJECT, id) diet_goal = {'Type': 'Calories', 'Value': 2000.0} fitness_goal = {'Type': 'FitnessMinutes', 'Value': 30.0} sleep_goal = {'Type': 'SleepHours', 'Value': 7.0} goals.set_goal(diet_goal) goals.set_goal(fitness_goal) goals.set_goal(sleep_goal) return "Succesfully Registered", 200 else: return "Unable to register new user, they possibly already have an account", 400
class TestGoals(unittest.TestCase): """ Test for goals.py """ @classmethod def setUpClass(self): """ Set up the unit test. """ self.db = DB() self.email = '*****@*****.**' self.password = '******' self.fullname = 'ABCD' 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.goals = Goals(self.db, self.user_id) self.diet_goal_dict = {'Type': 'Calories', 'Value': 1000.0} self.fitness_goal_dict = {'Type': 'FitnessMinutes', 'Value': 100.0} self.sleep_goal_dict = {'Type': 'SleepHours', 'Value': 10.0} self.goals.set_goal(self.diet_goal_dict) self.goals.set_goal(self.fitness_goal_dict) self.goals.set_goal(self.sleep_goal_dict) def test_alter_goal(self): """ Test altering a goal. Change all the goals and check for correctness. """ self.assertTrue(self.goals.alter_goal('Calories', 99.0)) self.assertTrue(self.goals.alter_goal('FitnessMinutes', 50.0)) self.assertTrue(self.goals.alter_goal('SleepHours', 1.99)) goal, status = self.goals.get_latest_goal(Type='Calories') self.assertEqual(goal[0]['Value'], 99.0) goal, status = self.goals.get_latest_goal(Type='FitnessMinutes') self.assertEqual(goal[0]['Value'], 50.0) goal, status = self.goals.get_latest_goal(Type='SleepHours') self.assertEqual(goal[0]['Value'], 1.99) goals = Goals(None, self.user_id) self.assertFalse(goals.alter_goal('Calories', 99.0)) def test_incorrect_alter_type_goal(self): """ Test for incorrect alter type. The function alter_goal() should return False. """ self.assertFalse(self.goals.alter_goal('CaloriesS', 99.0)) self.assertFalse(self.goals.alter_goal('FitnessMinutesS', 50.0)) self.assertFalse(self.goals.alter_goal('SleepHoursS', 1.99)) def test_incorrect_alter_value_type_goal(self): """ Test for incorrect alter value data type. """ self.assertFalse(self.goals.alter_goal('Calories', 'A')) self.assertFalse(self.goals.alter_goal('FitnessMinutes', 50)) self.assertFalse(self.goals.alter_goal('SleepHours', 'B')) def test_get_latest_goal(self): """ Test getting the latest goal of a particular type, Diet, Fitness or Sleep. """ goal, status = self.goals.get_latest_goal(Type='Calories') self.assertTrue(status) goal, status = self.goals.get_latest_goal(Type='FitnessMinutes') self.assertTrue(status) goal, status = self.goals.get_latest_goal(Type='SleepHours') self.assertTrue(status) goals = Goals(None, self.user_id) goal, status = goals.get_latest_goal(Type='Calories') self.assertFalse(status) def test_incorrect_get_latest_goal(self): """ Test incorrect goal type while getting the latest goal of a particular type. """ goal, status = self.goals.get_latest_goal(Type='A') self.assertFalse(status) def test_get_all_goals(self): """ Test getting all the goals from the table. """ goals, status = self.goals.get_all_goals() self.assertTrue(status) goals = Goals(None, self.user_id) goal, status = goals.get_all_goals() self.assertFalse(status) def test_get_type_goals(self): """ Test getting all the goals of a particular type. Mostly we will only be interested in the latest goal. """ goal, status = self.goals.get_type_goals('Calories') self.assertTrue(status) goals = Goals(None, self.user_id) goal, status = goals.get_type_goals('Calories') self.assertFalse(status) def test_incorrect_get_type_goals(self): """ Test for incorrect goal type while getting all the goals of a particular type. Mostly we will only be interested in the latest goal. """ goal, status = self.goals.get_type_goals('A') self.assertFalse(status) def test_set_goal(self): """ Test setting a goal. Set all the three goals, Diet, Fitness and Sleep. """ self.assertTrue(self.goals.set_goal(self.diet_goal_dict)) self.assertTrue(self.goals.set_goal(self.fitness_goal_dict)) self.assertTrue(self.goals.set_goal(self.sleep_goal_dict)) goals = Goals(None, self.user_id) self.assertFalse(goals.set_goal(self.diet_goal_dict)) def test_incorrect_key_set_goal(self): """ Test for incorrect keys. We send incorrect keys: Typo and Valuo. These keys are the columns in our tables. """ d = {'Typo': 'Calories', 'Valuo': 100.0} self.assertFalse(self.goals.set_goal(d)) def test_incorrect_value_type_set_goal(self): """ Test for incorrect value data types. We use int for the Value instead of float (in python) and double (in mysql). """ d = {'Type': 'Calories', 'Value': 100} self.assertFalse(self.goals.set_goal(d))