def test_buy_item_item_not_found(self): user = UserFactory(currency=150) self.db_session.commit() with self.assertRaises(ItemNotFound): asyncio.run(Palplatina().buy_item(user.id, 'random')) Session.remove() persisted_user = self.db_session.query(User).get(user.id) persisted_user_profile_items = persisted_user.profile_items self.assertEqual(persisted_user.currency, 150) self.assertEqual(len(persisted_user_profile_items), 0)
def test_set_user_profile_frame_color_invalid_color(self): user = UserFactory() self.test_session.commit() color = 'invalid color' with self.assertRaises(ValueError): asyncio.run(Profile().set_user_profile_frame_color(user.id, color)) Session.remove() self.assertEqual( self.test_session.query(User).get(user.id).profile_frame_color, None)
def test_give_daily_not_clear_for_new_daily(self): user = UserFactory(currency=150, daily_last_collected_at=datetime.utcnow()) self.db_session.commit() result, user_actual = asyncio.run(Palplatina().give_daily( user.id, user.name)) Session.remove() self.assertFalse(result) self.assertEqual(user_actual.currency, 150) self.assertEqual( self.db_session.query(User).get(user.id).currency, 150)
def test_buy_item_not_enough_currency(self): user = UserFactory(currency=150) profile_item = ProfileItemFactory(price=200) self.db_session.commit() with self.assertRaises(NotEnoughCredits): asyncio.run(Palplatina().buy_item(user.id, profile_item.name)) Session.remove() persisted_user = self.db_session.query(User).get(user.id) persisted_user_profile_items = persisted_user.profile_items self.assertEqual(persisted_user.currency, 150) self.assertEqual(len(persisted_user_profile_items), 0)
def test_set_user_profile_frame_color_user_does_not_exist(self): user_id = 140 color = '#f7b1d8' result = asyncio.run(Profile().set_user_profile_frame_color( user_id, color)) self.assertEqual(result.id, user_id) self.assertEqual(result.profile_frame_color, color) Session.remove() self.assertEqual( self.test_session.query(User).get(user_id).profile_frame_color, color)
def test_set_user_profile_frame_color_valid_color(self): user = UserFactory() self.test_session.commit() color = '#f7b1d8' result = asyncio.run(Profile().set_user_profile_frame_color( user.id, color)) self.assertEqual(result.id, user.id) self.assertEqual(result.profile_frame_color, color) Session.remove() self.assertEqual( self.test_session.query(User).get(user.id).profile_frame_color, color)
def test_unequip_item_user_has_item(self): user = UserFactory() profile_item = ProfileItemFactory() user.profile_items = [ UserProfileItemFactory(equipped=True, profile_item=profile_item) ] self.db_session.commit() result = asyncio.run(Palplatina().unequip_item(user.id, profile_item.name)) self.assertFalse(result.equipped) Session.remove() fetched_user_profile_item = self.db_session.query(UserProfileItem).get( (user.id, profile_item.id)) self.assertFalse(fetched_user_profile_item.equipped)
def test_update_config_updates_existing_server_config(self): server_config = ServerConfigFactory(language='en') self.test_session.commit() new_server_language = 'pt' result = asyncio.run( cache.update_config(server_config.id, language=new_server_language)) Session.remove() self.assertIsInstance(result, ServerConfig) self.assertEqual(result.id, server_config.id) self.assertEqual(result.language, new_server_language) fetched_server_config = self.test_session.query(ServerConfig).get( server_config.id) self.assertEqual(fetched_server_config.id, server_config.id) self.assertEqual(fetched_server_config.language, new_server_language)
def test_buy_item_success(self): user = UserFactory(currency=150) profile_item = ProfileItemFactory(price=100) self.db_session.commit() result = asyncio.run(Palplatina().buy_item(user.id, profile_item.name)) Session.remove() self.assertIsInstance(result, User) self.assertEqual(result.currency, 50) self.assertEqual(result.profile_items[0].profile_item.id, profile_item.id) persisted_user = self.db_session.query(User).get(user.id) persisted_user_profile_items = persisted_user.profile_items self.assertEqual(persisted_user.currency, 50) self.assertEqual(len(persisted_user_profile_items), 1) self.assertEqual(persisted_user_profile_items[0].profile_item.id, profile_item.id)
def tearDown(self): clear_data(self.db_session) self.db_session.close() Session.remove()