def test_reset_board(db): """ Test that reset board resets goals on the board when the board is full. """ rpm = RestaurantProfileManager("boardtest3x3") cpm = CustomerProfileManager("tester2") bpm = GameBoardManager(rpm) board = bpm.get_bingo_board() vpm = Validator(rpm) for i in range(board['size']): vpm.complete_goal("tester", board['board'][i], str(i)) rest_id = rpm.get_restaurant_id() reset_complete_board(cpm, rest_id) rest_board = db.query("customers", { "username": "******", "progress.restaurant_id": rest_id }) user_board = [ x for x in rest_board[0]['progress'] if x['restaurant_id'] == rest_id ] assert len(user_board[0]["completed_goals"]) == 0
def test_get_restaurant_name_by_id_invalid(): """ Test that get_restaurant_name_by_id() returns "" for invalid ids. """ with app.app_context(): rpm = RestaurantProfileManager("") username = rpm.get_restaurant_name_by_id("not a real id") assert username == ""
def test_get_restaurant_name_by_id_valid(): """ Test that get_restaurant_name_by_id() returns the restaurant name of a valid user correctly. """ with app.app_context(): rpm = RestaurantProfileManager("") username = rpm.get_restaurant_name_by_id("5f15c084143cb39bfc5619b8") assert username == "KFC"
def test_get_restaurant_profiles(): """ Test that get_restaurant_profiles() function in customer_profile_manager.py retreives all restaurant profile fields that are public. """ rpm = RestaurantProfileManager("unitTestUser") ppm = PublicProfileModifier(rpm) profiles = ppm.get_public_profiles() expected_fields = ["name", "category", "image", "is_public"] has_id = True has_fields = True is_public = True for key in list(profiles.keys()): if type(key) != ObjectId: has_id = False break fields = list(profiles[key].keys()) for f in expected_fields: if f not in fields: has_fields = False break if profiles[key]["is_public"] == False: is_public = False break assert len(profiles) > 0 and has_id and has_fields and is_public
def test_get_rewards_from_board(): """ Test that the get_bingo_board() function in restaurant_profile_manager.py retrieves a user's list of rewards on their bingo board. """ with app.app_context(): rpm = RestaurantProfileManager("unittestuser") gbm = GameBoardManager(rpm) board = gbm.get_bingo_board() rewards = board["board_reward"] expected_rewards = [ ObjectId('5f03aa437aae4a086d810107'), ObjectId('5f03a9b57aae4a086d8100fe'), ObjectId('5f03a9c87aae4a086d8100ff'), ObjectId('5f03a9ec7aae4a086d810101'), ObjectId('5f03aa507aae4a086d810108'), ObjectId('5f03a9c87aae4a086d8100ff'), ObjectId('5f03aa287aae4a086d810105'), ObjectId('5f03a9967aae4a086d8100fd'), ObjectId('5f03a9fd7aae4a086d810102'), ObjectId('5f03a9fd7aae4a086d810102'), ObjectId('5f03a9fd7aae4a086d810102'), ObjectId('5f03a9ec7aae4a086d810101') ] assert rewards == expected_rewards
def test_add_custom_reward(): """ Test that add_custom_reward() function in restaurant_profile_manager.py adds a custom reward to the restaurant profile, given that it is not in the database. """ with app.app_context(): rpm = RestaurantProfileManager("vchang") rm = RewardsManager(rpm) existing_reward_dict = rm.get_rewards() existing_reward_list = [x['reward'] for x in existing_reward_dict] count = 2 in_list = True while in_list: expected_reward = "Get " + str( count) + " free desserts in one visit" count += 1 if expected_reward not in existing_reward_list: in_list = False rm.add_custom_reward(expected_reward) reward_list = rm.get_rewards() reward = [x for x in reward_list if x['reward'] == expected_reward] assert expected_reward in reward[0]['reward']
def test_get_favourite_doc(): """ Test that get_favourite_doc() returns a dictionary of favourite restaurant profiles. """ with app.app_context(): cpm = CustomerProfileManager("testuser") rpm = RestaurantProfileManager("") ppm = PublicProfileModifier(rpm) all_profiles = ppm.get_public_profiles() favourite = get_favourite(cpm) profiles = get_favourite_doc(all_profiles, favourite) expected_fields = ["name", "category", "image", "is_public"] has_id = True has_fields = True is_public = True for key in list(profiles.keys()): if type(key) != ObjectId: has_id = False break fields = list(profiles[key].keys()) for f in expected_fields: if f not in fields: has_fields = False break if profiles[key]["is_public"] == False: is_public = False break assert len(profiles) > 0 and has_id and has_fields and is_public
def get_restaurant_board_by_id(self, rest_id): """ Return a restaurant board given a restaurant database id. The board will include text info for goals and rewards. """ try: restaurant = self.rpm.db.query("restaurant_users", {"_id": ObjectId(rest_id)})[0] temp_rpm = self.rpm self.rpm = RestaurantProfileManager(restaurant["username"]) board = self.get_bingo_board() self.rpm = temp_rpm return board except (QueryFailureException, IndexError): print("Something's wrong with the query.") return {}
def test_set_board_rewards(): """ Test that the set_bingo_board() function in restaurant_profile_manager.py can be used to update a user's list of rewards. """ with app.app_context(): rpm = RestaurantProfileManager("vchang") gbm = GameBoardManager(rpm) board = gbm.get_bingo_board() old_rewards = board["board_reward"] new_rewards = [ ObjectId('5ef50127ccd1e88ead4cd07b'), ObjectId('5ef50127ccd1e88ead4cd07b'), ObjectId('5ef50127ccd1e88ead4cd07b'), ObjectId('5ef50127ccd1e88ead4cd07b'), ObjectId('5ef50127ccd1e88ead4cd07b'), ObjectId('5ef50127ccd1e88ead4cd07b'), ObjectId('5ef50127ccd1e88ead4cd07b'), ObjectId('5ef50127ccd1e88ead4cd07b'), ObjectId('5ef50127ccd1e88ead4cd07b'), ObjectId('5ef50127ccd1e88ead4cd07b'), ObjectId('5ef50127ccd1e88ead4cd07b'), ObjectId('5ef50127ccd1e88ead4cd07b') ] board["board_reward"] = new_rewards gbm.set_bingo_board(board) assert (gbm.get_bingo_board())["board_reward"] == new_rewards board["board_reward"] = old_rewards gbm.set_bingo_board(board)
def test_get_board_by_id(): """ Test that get_restaurant_board_by_id() returns the correct bingo board information. """ with app.app_context(): rpm = RestaurantProfileManager("") board = GameBoardManager(rpm).get_restaurant_board_by_id( "5f15c084143cb39bfc5619b8") assert board["name"] == "KFC Rewards" assert board["board"][0] == { "_id": ObjectId("5f15c1b3143cb39bfc5619b9"), "goal": "resignation" } assert board["board"][12] == { "_id": ObjectId("5f15c1de143cb39bfc5619c5"), "goal": "inch" } assert board["board"][24] == { "_id": ObjectId("5f15c200143cb39bfc5619d1"), "goal": "transmission" } assert board["board_reward"][0] == { "_id": ObjectId("5f03aa437aae4a086d810107"), "reward": "One free drink refill" } assert board["board_reward"][5] == { "_id": ObjectId("5f03aa0b7aae4a086d810103"), "reward": "Free appetizer" } assert board["board_reward"][11] == { "_id": ObjectId("5f03aa437aae4a086d810107"), "reward": "One free drink refill" }
def test_get_current_expiry_new_user(): """ Test that get_current_board_expiry() returns None for a new user. """ with app.app_context(): rpm = RestaurantProfileManager("newuser") gm = GameBoardManager(rpm) assert gm.get_current_board_expiry() is None
def test_get_custom_goals(): """ Test that the get_custom_goals() function in restaurant_profile_manager.py retrieves the user's custom goals """ rpm = RestaurantProfileManager("unittestuser") gm = GoalsManager(rpm) goals = gm.get_custom_goals() assert len(goals) == 2
def test_invalid_goal(client): """ Test that a user can't complete an invalid goal. """ rpm = RestaurantProfileManager("junaid") vm = Validator(rpm) code = "junaid+5ef5009bccd1e88ead4cd076+1".split("+") msg = vm.complete_goal(code[0], code[1], code[2]) assert msg == "Invalid QR code!"
def test_get_favourites(): """ Test that get_favourite() returns a list of restaurant ids. """ with app.app_context(): rpm = RestaurantProfileManager("") cpm = CustomerProfileManager("ksawyer") favourite = get_favourite(cpm) assert len(favourite) > 0
def test_get_goals(): """ Test that the get_goals() function in restaurant_profile_manager.py retrieves all the user's goals, there are 20 shared goals, then anything extra will custom goals """ rpm = RestaurantProfileManager("vchang") gm = GoalsManager(rpm) goals = gm.get_goals() assert len(goals) >= 20
def test_valid_completed_goal(client): """ Test that a user can mark a valid goal as complete. """ rpm = RestaurantProfileManager("junaid") vm = Validator(rpm) code = "junaid+5ef5009bccd1e88ead4cd076+0".split("+") msg = vm.complete_goal(code[0], code[1], code[2]) assert msg == "This goal has already been completed!"
def test_duplicate_completed_goal(client): """ Test that a duplicate goal can't be completed again. """ rpm = RestaurantProfileManager("junaid") vm = Validator(rpm) code = "junaid+5ef5009bccd1e88ead4cd076+0".split("+") msg = vm.complete_goal(code[0], code[1], code[2]) assert msg == "This goal has already been completed!"
def test_get_restaurant_users(): """ Test that the get_restaurant_users() function in customer_profile_manager.py retrieves all all restaurant users. """ with app.app_context(): user = RestaurantProfileManager("vchang") ppm = PublicProfileModifier(user) users = ppm.get_public_profiles() assert len(users) != 0
def test_get_current_expiry_old_user(): """ Test that get_current_board_expiry() returns the correct expiry date for a user with board data. """ with app.app_context(): rpm = RestaurantProfileManager("boardeditoruser") gm = GameBoardManager(rpm) assert gm.get_current_board_expiry() == datetime( 2020, 11, 23, 23, 59, 59)
def test_get_rewards_new_user(): """ Test that the get_bingo_board() function in restaurant_profile_manager.py retrieves an empty board and reward list when a new user is detected. """ with app.app_context(): rpm = RestaurantProfileManager("newuser") gbm = GameBoardManager(rpm) rewards = (gbm.get_bingo_board())["board_reward"] assert rewards == []
def view_profiles(): """ Allows users to view restaurant profiles that are set to public. """ rpm = RestaurantProfileManager("") restaurant_profiles = PublicProfileModifier(rpm).get_public_profiles() favourite = get_favourite(current_user) return render_template('view_profiles.j2', profiles=restaurant_profiles, favourite=favourite)
def view_favourites(): """ Allows users to view restaurants from "favourite" """ favourite = get_favourite(current_user) rpm = RestaurantProfileManager("") profiles = PublicProfileModifier(rpm).get_public_profiles() list_fav = get_favourite_doc(profiles, favourite) return render_template('view_favourites.j2', profiles=list_fav, favourite=favourite)
def test_get_future_board_new_user(): """ Test that get_future_board() retrieves an empty board for a new user. """ with app.app_context(): rpm = RestaurantProfileManager("newuser") gm = GameBoardManager(rpm) assert gm.get_future_board() == { "board": [], "name": "", "board_reward": [], "expiry_date": None, "size": 4 }
def test_set_board_progress_no_progress(): """ Test that set_board_progress() updates all goals on a bingo board to incomplete. """ with app.app_context(): rpm = RestaurantProfileManager("") board = GameBoardManager(rpm).get_restaurant_board_by_id( "5f15c084143cb39bfc5619b8") cpm = CustomerProfileManager("newuser") set_board_progress(cpm, board, "5f15c084143cb39bfc5619b8") for goal in board["board"]: assert not goal["is_complete"]
def get_reward_progress(cpm): """ Return the current user's reward history as a tuple of two lists: ([active rewards], [redeemed rewards]). Returns ([], []) on failure. """ try: customer = cpm.db.query("customers", {"username": cpm.id})[0] # no game progress if "progress" not in customer: return ([], []) active_rewards = [] redeemed_rewards = [] for resaurant in customer["progress"]: # no rewards completed at this restaurant if "completed_rewards" not in resaurant: continue restaurant_name = RestaurantProfileManager( "").get_restaurant_name_by_id(resaurant["restaurant_id"]) # add rewards to appropriate collection for reward in resaurant["completed_rewards"]: reward["restaurant_name"] = restaurant_name if reward["is_redeemed"]: redeemed_rewards.append(reward) else: active_rewards.append(reward) # sort redeemed rewards by date redeemed_rewards = sorted(redeemed_rewards, key=lambda x: x["redemption_date"], reverse=True) # format all dates for index, reward in enumerate(redeemed_rewards): redeemed_rewards[index]["redemption_date"] = reward[ "redemption_date"].strftime("%B X%d, %Y").replace("X0", "X").replace("X", "") return (active_rewards, redeemed_rewards) except QueryFailureException: print("Something's wrong with the query.") return ([], []) except IndexError: print("Could not find the customer") return ([], [])
def test_get_rewards_old_user(): """ Test that get_rewards() inrestaurant_profile_manager.py for an existin user retrieves a list of their available rewards. """ with app.app_context(): rpm = RestaurantProfileManager("vchang") rm = RewardsManager(rpm) rewards = rm.get_rewards() expected_rewards = [{ '_id': ObjectId('5f03a9df7aae4a086d810100'), 'reward': '$5 off a purchase of $30+' }, { '_id': ObjectId('5f03a9fd7aae4a086d810102'), 'reward': 'Free drink' }, { '_id': ObjectId('5f03aa287aae4a086d810105'), 'reward': 'Buy one entree, get another 50% off (of lesser or equal value)' }, { '_id': ObjectId('5f03aa1a7aae4a086d810104'), 'reward': 'Free dessert' }, { '_id': ObjectId('5f03a9c87aae4a086d8100ff'), 'reward': '$10 off a purchase of $50+' }, { '_id': ObjectId('5f03aa0b7aae4a086d810103'), 'reward': 'Free appetizer' }, { '_id': ObjectId('5f03a9967aae4a086d8100fd'), 'reward': '10% off a purchase' }, { '_id': ObjectId('5f03aa377aae4a086d810106'), 'reward': '$3 off any entree' }, { '_id': ObjectId('5f03aa437aae4a086d810107'), 'reward': 'One free drink refill' }, { '_id': ObjectId('5f03aa507aae4a086d810108'), 'reward': 'All desserts $6 each' }, { '_id': ObjectId('5f03a9b57aae4a086d8100fe'), 'reward': '15% off a purchase' }, { '_id': ObjectId('5f03a9ec7aae4a086d810101'), 'reward': '$5 gift voucher' }] assert rewards == expected_rewards
def test_get_custom_goals(): """ Test that get_custom_goals function in restaurant_profile_manager.py returns the user's list of custom goals. """ with app.app_context(): rpm = RestaurantProfileManager("vchang") gm = GoalsManager(rpm) goals = gm.get_custom_goals() hasFields = True for goal in goals: if goal['_id'] == None and goal['goal'] == None: hasFields = False assert len(goals) > 0 and hasFields
def test_get_custom_rewards(): """ Test that get_custom_goals function in restaurant_profile_manager.py returns the user's list of custom goals. """ with app.app_context(): rpm = RestaurantProfileManager("vchang") rm = RewardsManager(rpm) rewards = rm.get_custom_rewards() has_fields = True for reward in rewards: if reward['_id'] is None and reward['goal'] is None: has_fields = False assert len(rewards) >= 0 and has_fields
def test_get_custom_goals(): """ Test that get_custom_goals() function in restaurant_profile_manager.py retrieves the correct list of custom goals of a restaurant profile. """ rpm = RestaurantProfileManager("janedoe") gm = GoalsManager(rpm) custom_goals = gm.get_custom_goals() expected_custom_goals = [{ '_id': ObjectId("5f11c9721a52881b3573c029"), 'goal': "This is a custom goal." }, { '_id': ObjectId("5f11c99a1a52881b3573c02a"), 'goal': "This is another custom goal." }] assert custom_goals == expected_custom_goals
def test_set_board_progress_some_progress(): """ Test that set_board_progress() updates all goals on a bingo board to the correct progress info. """ with app.app_context(): rpm = RestaurantProfileManager("") board = GameBoardManager(rpm).get_restaurant_board_by_id( "5f15c084143cb39bfc5619b8") cpm = CustomerProfileManager("unittestuser") set_board_progress(cpm, board, "5f15c084143cb39bfc5619b8") for i in range(len(board["board"])): if i in [0, 8, 14, 23]: assert board["board"][i]["is_complete"] else: assert not board["board"][i]["is_complete"]