def create_app(): app = Flask(__name__) with app.app_context(): init_db() return app
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_update_expired_future_expiry_date(): """ Tests update_board will update an expired future game board to be 90 days from current time. """ with app.app_context(): rpm = RestaurantProfileManager('testuser') pm = PublicProfileModifier(rpm) gm = GameBoardManager(rpm) public_users = pm.get_public_users() expired_board = [] expected = True for user in public_users: # expired current and future board if 'expiry_date' in user['bingo_board'] and user['bingo_board'][ 'expiry_date'] < datetime.now( ) and user['future_board']['expiry_date'] < datetime.now(): expired_board.append(user) gm.update_board(user['_id']) public_users = rpm.get_public_users() for expired_user in expired_board: for user in public_users: #searches for users whose boards were updated if user['username'] == expired_user['username']: # future exp date was not updated if user['future_board']['expiry_date'] < datetime.now(): expected = False assert expected
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_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_valid_completed_reward(client): """ Test that a user can mark a valid reward as complete. """ with app.app_context(): rpm = RestaurantProfileManager("junaid") rm = RewardsManager(rpm) code = "junaid+5f03a9fd7aae4a086d810102+6".split("+") msg = rm.complete_reward(code[0], "junaid+5f03a9fd7aae4a086d810102+6") assert msg == "Code has already been redeemed!"
def test_duplicate_completed_reward(client): """ Test that a duplicate reward can't be completed again. """ with app.app_context(): rpm = RestaurantProfileManager("junaid") rm = RewardsManager(rpm) code = "junaid+5f03a9fd7aae4a086d810102+6".split("+") msg = rm.complete_reward(code[0], "junaid+5f03a9fd7aae4a086d810102+6") assert msg == "Code has already been redeemed!"
def test_invalid_reward(client): """ Test that a user can't complete an invalid reward. """ with app.app_context(): rpm = RestaurantProfileManager("junaid") rm = RewardsManager(rpm) code = "junaid+5f03a9fd7aae4a086d810102+7".split("+") msg = rm.complete_reward(code[0], "junaid+5f03a9fd7aae4a086d810102+7") assert msg == "Invalid QR code!"
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 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_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_update_current_with_future(): """ Tests that update_board replaces an expired bingo board with a future board that has an expiry date that is in the future and updates the future board to have an expiration date of 90 more days, and customer's completed goals are removed. """ with app.app_context(): rpm = RestaurantProfileManager('testuser') pm = PublicProfileModifier(rpm) gm = GameBoardManager(rpm) public_users = pm.get_public_users() expired_board_user = [] current_boards = [] actual_future = [] expected_boards = [] expected_future = [] customer_update = True for user in public_users: # expired current but not future board if 'expiry_date' in user['bingo_board'] and user['bingo_board'][ 'expiry_date'] < datetime.now( ) and user['future_board']['expiry_date'] > datetime.now(): expired_board_user.append(user['_id']) # expected current board is future board expected_boards.append(user['future_board']) to_add = copy.deepcopy(user['future_board']) to_add['expiry_date'] = user['future_board'][ 'expiry_date'] + timedelta(days=90) # expected future board has increased exp date expected_future.append(to_add) gm.update_board(user['_id']) public_users = pm.get_public_users() for user in public_users: #searches for users whose boards were updated if user['_id'] in expired_board_user: current_boards.append(user['bingo_board']) actual_future.append(user['future_board']) customers = rpm.db.query('customers') for c in customers: if 'progress' in c: for rest in c['progress']: # searches for customers whose progress was updated if rest['restaurant_id'] in expired_board_user and 'completed_goals' in rest: # completed goals were not updated if len(rest['completed_goals']) > 0: customer_update = False assert current_boards == expected_boards and actual_future == expected_future and customer_update
def test_add__duplicate_custom_goal(): """ Test that add_custom_goal() function in restaurant_profile_manager.py does not add a custom goal to the restaurant profile, given that it is in the database. """ with app.app_context(): rpm = RestaurantProfileManager("vchang") gm = GoalsManager(rpm) existing_goal_dict = gm.get_goals() existing_goal_list = [x['goal'] for x in existing_goal_dict] dupe_goal = existing_goal_list[0] gm.add_custom_goal(dupe_goal) goal_list = gm.get_goals() goal = [x for x in goal_list if x['goal'] == dupe_goal] assert len(goal) == 1
def test_remove_custom_reward_on_board(): """ Test that the remove_custom_reward() function in restaurant_profile_manager.py will not remove a goal that is on the board. """ with app.app_context(): rpm = RestaurantProfileManager("vchang") rm = RewardsManager(rpm) gm = GameBoardManager(rpm) old_rewards = rm.get_custom_rewards() board_rewards = gm.get_bingo_board()["board_reward"] for i in range(0, len(old_rewards)): if ObjectId(old_rewards[i]['_id']) in board_rewards or ObjectId(old_rewards[i]['_id']) in future_rewards: rm.remove_custom_reward(old_rewards[i]['_id']) break new_rewards = rm.get_custom_rewards() assert len(new_rewards) == len(old_rewards)
def test_add__duplicate_custom_reward(): """ Test that add_custom_reward() function in restaurant_profile_manager.py does not add a custom reward to the restaurant profile, given that it is 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] dupe_reward = existing_reward_list[0] rm.add_custom_reward(dupe_reward) reward_list = rm.get_rewards() reward = [x for x in reward_list if x['reward'] == dupe_reward] assert len(reward) == 1
def test_set_bingo_board_old_user(db): """ Test that set_bingo_board() sets only the future board for users that have previous board data. """ with app.app_context(): prev_user = db.query("restaurant_users", {"username": "******"})[0] rpm = RestaurantProfileManager("boardeditoruser") gm = GameBoardManager(rpm) gm.set_bingo_board(get_board()) user = db.query("restaurant_users", {"username": "******"})[0] assert user["future_board"] == get_database_board() assert user["bingo_board"] == prev_user["bingo_board"] # reset user's boards to pre-test db.update("restaurant_users", {"username": "******"}, {"$set": prev_user})
def test_remove_custom_goal(): """ Test that the remove_custom_goal() function in restaurant_profile_manager.py can be used to remove a user's custom goal. """ with app.app_context(): rpm = RestaurantProfileManager("vchang") gm = GoalsManager(rpm) old_goals = gm.get_custom_goals() gbm = GameBoardManager(rpm) board_goals = gbm.get_bingo_board()["board"] found = False for i in range(0, len(old_goals)): if ObjectId(old_goals[i]['_id']) not in board_goals: found = True gm.remove_custom_goal(old_goals[i]['_id']) break new_goals = gm.get_custom_goals() assert (len(new_goals) == len(old_goals) and found == False) or \ (len(new_goals) == (len(old_goals) - 1) and found == True)
def test_set_bingo_board_new_user(db): """ Test that set_bingo_board() sets both the current and future boards for users with no current board data. """ with app.app_context(): rpm = RestaurantProfileManager("newuser") gm = GameBoardManager(rpm) gm.set_bingo_board(get_board()) user = db.query("restaurant_users", {"username": "******"})[0] db_board = get_database_board() assert user["bingo_board"] == db_board db_board["expiry_date"] = datetime(2021, 8, 24, 23, 59, 59) assert user["future_board"] == db_board # reset new user's boards db.update("restaurant_users", {"username": "******"}, {"$unset": { "bingo_board": "", "future_board": "" }})
def test_get_future_board_old_user(): """ Test that get_future_board() retrieves the correct future board for a user with board data. """ with app.app_context(): rpm = RestaurantProfileManager("boardeditoruser") gm = GameBoardManager(rpm) assert gm.get_future_board() == { "name": "My 3x3", "size": 3, "expiry_date": datetime(2020, 11, 27, 23, 59, 59), "board": [ ObjectId("5ef50183ccd1e88ead4cd081"), ObjectId("5ef501f1ccd1e88ead4cd089"), ObjectId("5ef5010fccd1e88ead4cd079"), ObjectId("5ef50134ccd1e88ead4cd07c"), ObjectId("5ef50183ccd1e88ead4cd081"), ObjectId("5ef501b9ccd1e88ead4cd084"), ObjectId("5ef50155ccd1e88ead4cd07e"), ObjectId("5ef501b9ccd1e88ead4cd084"), ObjectId("5ef500edccd1e88ead4cd078") ], "board_reward": [ ObjectId("5f03a9fd7aae4a086d810102"), ObjectId("5f03aa377aae4a086d810106"), ObjectId("5f03aa507aae4a086d810108"), ObjectId("5f03a9df7aae4a086d810100"), ObjectId("5f03aa377aae4a086d810106"), ObjectId("5f03a9df7aae4a086d810100"), ObjectId("5f03aa287aae4a086d810105"), ObjectId("5f03aa507aae4a086d810108") ] }
def test_add_custom_goal(): """ Test that add_custom_goal() function in restaurant_profile_manager.py adds a custom goal to the restaurant profile, given that it is not in the database. """ with app.app_context(): rpm = RestaurantProfileManager("vchang") gm = GoalsManager(rpm) existing_goal_dict = gm.get_goals() existing_goal_list = [x['goal'] for x in existing_goal_dict] count = 2 inList = True while inList: expected_goal = "Buy " + str(count) + " desserts in one visit" count += 1 if expected_goal not in existing_goal_list: inList = False gm.add_custom_goal(expected_goal) goal_list = gm.get_goals() goal = [x for x in goal_list if x['goal'] == expected_goal] assert (expected_goal in goal[0]['goal'])
def db(client): """ Return a database instance. """ with app.app_context(): return Database.get_instance()