def test_highscores_from_tibiadata_loyalty(self): """Testing parsing loyalty highscores""" content = self._load_resource(FILE_HIGHSCORES_TIBIADATA_LOYALTY) highscores = Highscores.from_tibiadata(content) self.assertEqual(highscores.world, "Zunera") self.assertEqual(highscores.vocation, VocationFilter.ALL) self.assertEqual(highscores.category, Category.LOYALTY_POINTS) self.assertEqual(highscores.results_count, 57) for entry in highscores.entries: self.assertIsInstance(entry, LoyaltyHighscoresEntry) self.assertIsInstance(entry.name, str) self.assertIsInstance(entry.vocation, Vocation) self.assertIsInstance(entry.rank, int) self.assertIsInstance(entry.value, int) self.assertIsInstance(entry.title, str)
def test_highscores_from_tibiadata_experience(self): """Testing parsing experience highscores""" content = self._load_resource(FILE_HIGHSCORES_TIBIADATA_EXPERIENCE) highscores = Highscores.from_tibiadata(content) self.assertEqual(highscores.world, "Luminera") self.assertEqual(highscores.vocation, VocationFilter.ALL) self.assertEqual(highscores.category, Category.EXPERIENCE) self.assertEqual(highscores.results_count, 300) for entry in highscores.entries: self.assertIsInstance(entry, ExpHighscoresEntry) self.assertIsInstance(entry.name, str) self.assertIsInstance(entry.vocation, Vocation) self.assertIsInstance(entry.rank, int) self.assertIsInstance(entry.value, int) self.assertIsInstance(entry.level, int)
async def get_highscores(world, category=Category.EXPERIENCE, vocation=VocationFilter.ALL, *, tries=5) \ -> Optional[Highscores]: """Gets all the highscores entries of a world, category and vocation.""" # TODO: Add caching if tries == 0: raise errors.NetworkError(f"get_highscores_tibiadata({world},{category},{vocation})") try: async with aiohttp.ClientSession() as session: async with session.get(Highscores.get_url_tibiadata(world, category, vocation)) as resp: content = await resp.text() highscores = Highscores.from_tibiadata(content, vocation) except (aiohttp.ClientError, asyncio.TimeoutError, tibiapy.TibiapyException): await asyncio.sleep(config.network_retry_delay) return await get_highscores(world, category, vocation, tries=tries - 1) return highscores
def test_highscores_from_tibiadata(self): """Testing parsing highscores from TibiaData""" content = self._load_resource(FILE_HIGHSCORES_TIBIADATA_FULL) highscores = Highscores.from_tibiadata(content) self.assertEqual(highscores.world, "Antica") self.assertEqual(highscores.vocation, VocationFilter.ALL) self.assertEqual(highscores.category, Category.AXE_FIGHTING) self.assertEqual(highscores.results_count, 300) self.assertEqual( highscores.url_tibiadata, Highscores.get_url_tibiadata(highscores.world, highscores.category, highscores.vocation)) for entry in highscores.entries: self.assertIsInstance(entry, HighscoresEntry) self.assertIsInstance(entry.name, str) self.assertIsInstance(entry.vocation, Vocation) self.assertIsInstance(entry.rank, int) self.assertIsInstance(entry.value, int)
async def get_highscores(world, category=Category.EXPERIENCE, vocation=VocationFilter.ALL, *, tries=5) \ -> Optional[Highscores]: """Gets all the highscores entries of a world, category and vocation.""" # TODO: Add caching if tries == 0: raise errors.NetworkError( f"get_highscores_tibiadata({world},{category},{vocation})") try: async with aiohttp.ClientSession() as session: async with session.get( Highscores.get_url_tibiadata(world, category, vocation)) as resp: content = await resp.text() highscores = Highscores.from_tibiadata(content, vocation) except (aiohttp.ClientError, asyncio.TimeoutError, tibiapy.TibiapyException): await asyncio.sleep(config.network_retry_delay) return await get_highscores(world, category, vocation, tries=tries - 1) return highscores
def test_highscores_from_tibiadata_unrelated_section(self): """Testing parsing an unrelated section""" with self.assertRaises(InvalidContent): Highscores.from_tibiadata( self._load_resource( tests.tests_character.FILE_CHARACTER_TIBIADATA))
def test_highscores_from_tibiadata_empty(self): """Testing parsing empty highscores""" content = self._load_resource(FILE_HIGHSCORES_TIBIADATA_EMPTY) highscores = Highscores.from_tibiadata(content) self.assertIsNone(highscores)