def __enter__(self): self.user = User(name=self.username, password=self.password, dust=self.dust, gold=self.gold) self.user.save() return self
def setup_sample_user(self): # Creating the user and saving it into the database user = User(name=self.SAMPLE_USER_NAME, password=self.SAMPLE_USER_PASSWORD, gold=0, dust=0) user.save()
class StandardUserContext: USERNAME = '******' PASSWORD = '******' DUST = 0 GOLD = 0 def __init__(self): self.username = self.USERNAME self.password = self.PASSWORD self.dust = self.DUST self.gold = self.GOLD # This field will later contain the user object self.user: User = None def update(self): self.user = User.get(User.name == self.username) def __enter__(self): self.user = User(name=self.username, password=self.password, dust=self.dust, gold=self.gold) self.user.save() return self def __exit__(self, exc_type, exc_val, exc_tb): self.user.delete_instance(recursive=True)
def test_database_creation_successful(self): installer = EnvironmentInstaller(self.FOLDER_PATH) installer.create_config_folder() installer.create_database_file() self.assertTrue(os.path.exists(installer.get_database_file_path())) # The Database should be connected to the models now, which one should be able to create a new model and # insert it into the database user = User(name="Jonas", password="******", gold=0, dust=0) user.save() self.assertIsInstance(user, User)
def test_database_working(self): # Creating the user and saving it into the database user = User(name=self.SAMPLE_USER_NAME, password=self.SAMPLE_USER_PASSWORD, gold=0, dust=0) user.save() # Attempting to load it user = User.get(User.name == self.SAMPLE_USER_NAME) self.assertTrue(user.password.check(self.SAMPLE_USER_PASSWORD))
def create_user(self, username: str, password: str): """ Creates a new user with the given username and password. Initializes gold and dust with 0 CHANGELOG Added 10.06.2019 :param username: :param password: :return: """ user = User(name=username, password=password, gold=0, dust=0) user.save()
def test_sqlite_database(self): database_config = self.get_sample_object() database_config.init() DATABASE_PROXY.initialize(database_config.get()) database_config.connect() database_config.create_tables() # Creating a user and saving it just to see if it works user = User( name="Jonas", password="******", dust=0, gold=0, ) user.save()
def test_standard_user_context_manager(self): with StandardUserContext() as user_context: query = User.select().where(User.name == user_context.username) self.assertEqual(len(query), 1) user = query[0] self.assertIsInstance(user_context.user, User) self.assertEqual(user.name, user_context.user.name)
def open_pack(username: str, pack_name: str): """ Given a user by its username and the name of a pack type, which the user owns, this function will open that pack for the user. This means it will evaluate the slot probabilities for all 5 slots of the pack. Based on these probabilities 5 rewards are randomly chosen from the set of available rewards. These rewards will be added to the user. The pack will be removed, as it has been used up. CHANGELOG Added 12.06.2019 :param username: :param pack_name: :return: """ user = User.get(User.name == username) pack = user.get_packs(pack_name)[0] rarity_reward_map = available_rewards_by_rarity() for i in range(1, 6, 1): slot = getattr(pack, f'slot{i}') rarity = slot.choice() reward = np.random.choice(rarity_reward_map[rarity], 1)[0] parameters_adapter = RewardParametersAdapter(reward['name'], reward) user.add_reward(parameters_adapter.parameters()) user.use_pack(pack_name) user.save()
def test_user_adding_gold_and_dust_works(self): # Setting up the database self.setup_sample_user() # Loading the user, adding to its gold and dust, saving it again and loading it again to check if the # changes have stayed user = User.get(User.name == self.SAMPLE_USER_NAME) self.assertEqual(user.gold, 0) self.assertEqual(user.dust, 0) user.gold += 100 user.dust += 100 user.save() user = User.get(User.name == self.SAMPLE_USER_NAME) self.assertEqual(user.gold, 100) self.assertEqual(user.dust, 100)
def test_creating_new_user(self): facade: Rewardify = Rewardify.instance() # Creating the user username = '******' password = '******' facade.create_user(username, password) # Querying for the user to check if it exists query = User.select().where(User.name == username) self.assertTrue(len(query) >= 1) user = query[0] self.assertTrue(user.password.check(password))
def get_user(self, username: str) -> User: """ Returns the User object for the user with the given username CHANGELOG Added 10.06.2019 :param username: :return: """ query = User.select(User).where(User.name == username) user = query[0] return user
def exists_user(self, username: str): """ Returns the boolean value of whether or not a user with the given username exists CHANGELOG Added 15.06.2019 :param username: :return: """ query = User.select().where(User.name == username) # Of course, when there is a result to the above query, a User object has been found and thus, the user exists return len(query) != 0
def test_user_join_reward_and_pack_columns(self): # Setting all up self.setup_sample_user() self.setup_sample_pack() self.setup_sample_reward() # Attempting to get a user object with all the correct back refs to the pack and reward objects users = User.select(User).where(User.name == self.SAMPLE_USER_NAME) user = users[0] self.assertEqual(len(user.rewards), 1) self.assertEqual(user.rewards[0].name, self.SAMPLE_REWARD_NAME) self.assertEqual(len(user.packs), 1) self.assertEqual(user.packs[0].name, self.SAMPLE_PACK_NAME)
def setup_sample_pack(self): # Loading the sample user to be the owner of this pack user = User.get(User.name == self.SAMPLE_USER_NAME) pack = Pack(name=self.SAMPLE_PACK_NAME, slug=self.SAMPLE_PACK_SLUG, description=self.SAMPLE_PACK_DESCRIPTION, gold_cost=self.SAMPLE_PACK_GOLD, date_obtained=self.SAMPLE_PACK_DATE, slot1=self.SAMPLE_PACK_PROBABILITY, slot2=self.SAMPLE_PACK_PROBABILITY, slot3=self.SAMPLE_PACK_PROBABILITY, slot4=self.SAMPLE_PACK_PROBABILITY, slot5=self.SAMPLE_PACK_PROBABILITY, user=user) pack.save()
def test_user_use_reward_with_effect(self): self.setup_sample() self.create_sample_user() facade: Rewardify = Rewardify.instance() facade.user_add_dust(self.SAMPLE_USER_PARAMETERS['name'], 100) facade.user_buy_reward(self.SAMPLE_USER_PARAMETERS['name'], 'Gold Reward') user = self.get_sample_user() self.assertEqual(len(user.rewards), 1) self.assertEqual(user.gold, 0) facade.user_use_reward(self.SAMPLE_USER_PARAMETERS['name'], 'Gold Reward') user = User.select().where( User.name == self.SAMPLE_USER_PARAMETERS['name'])[0] self.assertEqual(len(user.rewards), 0) print(user.gold) self.assertEqual(user.gold, 100)
def test_pack_creation_works(self): # Setting up the database self.setup_sample_user() user = User.get(User.name == self.SAMPLE_USER_NAME) pack = Pack(name='Simple Pack', slug='simple_pack', description='The most basic of rewards', gold_cost=1000, date_obtained=datetime.datetime.now(), slot1=self.SAMPLE_PACK_PROBABILITY, slot2=self.SAMPLE_PACK_PROBABILITY, slot3=self.SAMPLE_PACK_PROBABILITY, slot4=self.SAMPLE_PACK_PROBABILITY, slot5=self.SAMPLE_PACK_PROBABILITY, user=user) pack.save() # Loading the pack to see it it worked pack = Pack.get(Pack.slug == 'simple_pack') self.assertEqual(pack.name, 'Simple Pack')
def create_sample_user(self): user = User(**self.SAMPLE_USER_PARAMETERS) user.save()
def get_sample_user(self) -> User: user = User.get(User.name == self.SAMPLE_USER_PARAMETERS['name']) return user
def get_sample_user(self): return User.get(User.name == self.SAMPLE_USER_NAME)
def create_standard_user(self): user = User(**self.STANDARD_USER_PARAMETERS) user.save()
def get_standard_user(self): return User.get(User.name == self.STANDARD_USER_PARAMETERS['name'])
def test_database_modifications_do_not_transcend_individual_tests(self): users = User.select().where(User.name == self.SAMPLE_USER_NAME) # If other test cases indeed do not influence the self.assertEqual(len(users), 0)
def update(self): self.user = User.get(User.name == self.username)