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_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_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_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_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_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_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_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_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_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_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 view_board(obj_id): """ Allows users to view the chosen restaurant's game board. """ username = current_user.get_id() rpm = RestaurantProfileManager("") gpm = GameBoardManager(rpm) gpm.update_board(obj_id) board = gpm.get_restaurant_board_by_id(obj_id) set_board_progress(current_user, board, obj_id) board["expiry_date"] = board["expiry_date"].strftime( "%B X%d, %Y - X%I:%M %p UTC").replace("X0", "X").replace("X", "") return render_template('view_game_board.j2', goals=board["board"], name=board["name"], rewards=board["board_reward"], cust_id=username, size=board["size"], date=board["expiry_date"])
def edit_profile(): """ Display the edit restaurant profile page. Prerequisite: User is logged in. """ profile = PublicProfileModifier(current_user).get_profile() ready_for_publish = GameBoardManager( current_user).get_bingo_board()["board"] != [] return render_template('edit_profile.j2', profile=profile, allow_public=ready_for_publish)
def test_view_board_goal_qr_code_data(client): """ Test that the data for goal QR codes are being set correctly. """ with app.app_context(): client.post("/login", data={ "username": "******", "password": "******" }) res = client.get("/restaurants/5f0df6a10bb07c8199d4405a/board", follow_redirects=True) rpm = RestaurantProfileManager("") bpm = GameBoardManager(rpm) board = bpm.get_restaurant_board_by_id("5f0df6a10bb07c8199d4405a") for i in range(len(board["board"])): assert str.encode("unittestuser+{}+{}".format( board["board"][i]["_id"], i)) in res.data
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_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 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 view_board(): """ When retrieving this route, get a restaurant profile's current bingo board. Render these items together to show current bingo board and expiration date. """ rest_id = current_user.get_restaurant_id() try: gbm = GameBoardManager(current_user) gbm.update_board(rest_id) bingo_board = gbm.get_restaurant_board_by_id(rest_id) if bingo_board["board"] == []: return redirect("/board/edit") return render_template( 'view_game_board.j2', goals=[x['goal'] for x in bingo_board["board"]], board_name=bingo_board["name"], rewards=[x['reward'] for x in bingo_board["board_reward"]], board_size=bingo_board["size"], current_expiry=str(bingo_board["expiry_date"])) except KeyError: return redirect("/board/edit")
def save(): """ When posting to this route, save a bingo board to the restaurant profile using the request body. Redirect to the bingo editor on completion. """ bingo_board = { "name": request.form["board_name"], "size": int(request.form["size"]), "expiry_date": request.form["expiry_date"], "board": request.form.getlist("board[]"), "board_reward": request.form.getlist("board_reward[]"), } GameBoardManager(current_user).set_bingo_board(bingo_board) return redirect("/board/edit")
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_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"]
def edit_board(): """ When retrieving this route, get a restaurant profile's goals, rewards and future board. Render these items together to show a bingo editor. """ rest_id = current_user.get_restaurant_id() gbm = GameBoardManager(current_user) gbm.update_board(rest_id) bingo_board = gbm.get_future_board() current_expiry = gbm.get_current_board_expiry() goals = GoalsManager(current_user).get_goals() rewards = RewardsManager(current_user).get_rewards() return render_template('edit_game_board.j2', goals=goals, board_name=bingo_board["name"], rewards=rewards, board=bingo_board["board"], board_reward=bingo_board["board_reward"], board_size=bingo_board["size"], current_expiry=current_expiry, future_expiry=bingo_board["expiry_date"])