def test_get_user_profile_user_has_badges(self): user_1 = UserFactory(name='Name') profile_badge_1 = ProfileItemFactory( file_path=os.path.join("tests", "support", "badge1.png")) profile_badge_2 = ProfileItemFactory( file_path=os.path.join("tests", "support", "badge2.png")) profile_badge_3 = ProfileItemFactory( file_path=os.path.join("tests", "support", "badge3.png")) profile_badge_4 = ProfileItemFactory( file_path=os.path.join("tests", "support", "badge1.png")) user_1.profile_items = [ UserProfileItemFactory(profile_item=profile_badge_1), UserProfileItemFactory(profile_item=profile_badge_2), UserProfileItemFactory(profile_item=profile_badge_3), UserProfileItemFactory(profile_item=profile_badge_4, equipped=False), ] self.test_session.commit() with open(os.path.join('tests', 'support', 'user_avatar.png'), 'rb') as f: user_avatar_bytes = f.read() result = asyncio.run(Profile().get_user_profile(user_1.id, user_avatar_bytes, lang='pt')) with open( os.path.join('tests', 'support', 'get_user_profile_with_badges.png'), 'rb') as f: self.assertEqual(result.getvalue(), f.read())
def test_get_item_item_exists(self): profile_item = ProfileItemFactory() self.db_session.commit() result = asyncio.run(Palplatina().get_item(profile_item.name)) self.assertEqual(result.id, profile_item.id)
def test_get_user_items_user_has_items(self): user = UserFactory() ProfileItemFactory.reset_sequence() for i in range(2): profile_item = ProfileItemFactory() user.profile_items.append( UserProfileItemFactory(profile_item=profile_item)) another_profile_item = ProfileItemFactory() self.db_session.commit() result = asyncio.run(Palplatina().get_user_items(user.id)) self.assertEqual(len(result), 2) fetched_items_names = [item.profile_item.name for item in result] self.assertIn('Profile item 0', fetched_items_names) self.assertIn('Profile item 1', fetched_items_names) self.assertNotIn(another_profile_item.name, fetched_items_names)
def test_get_available_items(self): ProfileItemFactory.reset_sequence() for i in range(12): ProfileItemFactory() for i in range(12): ProfileItemFactory(name=f'No match {i}') self.db_session.commit() result = asyncio.run(Palplatina().get_available_items('Profile item', page=0)) self.assertEqual(len(result), 2) self.assertEqual(len(result[0]), 9) self.assertEqual(result[1], 2) fetched_items_names = [item.name for item in result[0]] self.assertIn('Profile item 0', fetched_items_names) self.assertIn('Profile item 8', fetched_items_names) self.assertNotIn('Profile item 9', fetched_items_names) self.assertNotIn('Profile item 11', fetched_items_names)
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_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_buy_item_item_already_bought(self): profile_item = ProfileItemFactory() user = UserFactory(currency=150) user.profile_items = [ UserProfileItemFactory(profile_item=profile_item) ] self.db_session.commit() with self.assertRaises(AlreadyOwnsItem): asyncio.run(Palplatina().buy_item(user.id, profile_item.name)) 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), 1) self.assertEqual(persisted_user_profile_items[0].profile_item.id, profile_item.id)
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 test_get_user_profile_user_has_wallpaper(self): user_1 = UserFactory(name='Name') profile_badge = ProfileItemFactory(type='wallpaper', file_path=os.path.join( "tests", "support", "wallpaper.png")) user_1.profile_items = [ UserProfileItemFactory(profile_item=profile_badge), ] self.test_session.commit() with open(os.path.join('tests', 'support', 'user_avatar.png'), 'rb') as f: user_avatar_bytes = f.read() result = asyncio.run(Profile().get_user_profile(user_1.id, user_avatar_bytes, lang='pt')) with open( os.path.join('tests', 'support', 'get_user_profile_with_wallpaper.png'), 'rb') as f: self.assertEqual(result.getvalue(), f.read())