def test_load_user(user_db_manager): # Tests that loading a user should work, as long as the user is there # Add the user first user_id = "02" + get_random_value_hex(32) user_info = UserInfo(available_slots=42, subscription_expiry=100) user_db_manager.store_user(user_id, user_info.to_dict()) # Now load it assert user_db_manager.load_user(user_id) == user_info.to_dict()
def test_store_user_wrong(user_db_manager): # Tests that trying to store wrong data will fail # Wrong pks should return False on adding user_id = "04" + get_random_value_hex(32) user_info = UserInfo(available_slots=42, subscription_expiry=100) assert user_db_manager.store_user(user_id, user_info.to_dict()) is False # Same for wrong types assert user_db_manager.store_user(42, user_info.to_dict()) is False # And for wrong type user data assert user_db_manager.store_user(user_id, 42) is False
def test_load_all_users(user_db_manager): # Tests loading all the users in the database stored_users = {} # There should be no users at the moment assert user_db_manager.load_all_users() == {} stored_users = {} # Adding some and checking we get them all for i in range(10): user_id = "02" + get_random_value_hex(32) user_info = UserInfo(available_slots=42, subscription_expiry=100) user_db_manager.store_user(user_id, user_info.to_dict()) stored_users[user_id] = user_info.to_dict() all_users = user_db_manager.load_all_users() assert all_users == stored_users
def test_store_user(user_db_manager): # Tests that users can be properly stored in the database # Store user should work as long as the user_pk is properly formatted and data is a dictionary user_id = "02" + get_random_value_hex(32) user_info = UserInfo(available_slots=42, subscription_expiry=100) assert user_db_manager.store_user(user_id, user_info.to_dict()) is True
def test_load_all_users(user_db_manager): # There should be no users at the moment assert user_db_manager.load_all_users() == {} stored_users = {} # Adding some and checking we get them all for i in range(10): user_id = "02" + get_random_value_hex(32) user_info = UserInfo(available_slots=42, subscription_expiry=100) user_db_manager.store_user(user_id, user_info.to_dict()) stored_users[user_id] = user_info.to_dict() all_users = user_db_manager.load_all_users() assert set(all_users.keys()) == set(stored_users.keys()) for k, v in all_users.items(): assert stored_users[k] == v
def test_store_user(user_db_manager): # Store user should work as long as the user_pk is properly formatted and data is a dictionary user_id = "02" + get_random_value_hex(32) user_info = UserInfo(available_slots=42, subscription_expiry=100) stored_users[user_id] = user_info.to_dict() assert user_db_manager.store_user(user_id, user_info.to_dict()) is True # Wrong pks should return False on adding user_id = "04" + get_random_value_hex(32) user_info = UserInfo(available_slots=42, subscription_expiry=100) assert user_db_manager.store_user(user_id, user_info.to_dict()) is False # Same for wrong types assert user_db_manager.store_user(42, user_info.to_dict()) is False # And for wrong type user data assert user_db_manager.store_user(user_id, 42) is False
def test_delete_user(user_db_manager): # Tests that deleting existing users should work stored_users = {} # Add some users first for _ in range(10): user_id = "02" + get_random_value_hex(32) user_info = UserInfo(available_slots=42, subscription_expiry=100) user_db_manager.store_user(user_id, user_info.to_dict()) stored_users[user_id] = user_info # Deleting existing users should work for user_id, user_data in stored_users.items(): assert user_db_manager.delete_user(user_id) is True # There should be no users anymore assert not user_db_manager.load_all_users()
def setup_users(users_db_manager, total_users): registered_users = {} for _ in range(total_users): user_id = "02" + get_random_value_hex(32) # The UserInfo params do not matter much here user_info = UserInfo(available_slots=100, subscription_expiry=0) registered_users[user_id] = user_info # Add some appointments for _ in range(random.randint(0, 10)): uuid = get_random_value_hex(16) registered_users[user_id].appointments[uuid] = 1 users_db_manager.store_user(user_id, user_info.to_dict()) return registered_users