def test_world_from_content_full(self): """Testing parsing a world with full information""" content = self.load_resource(FILE_WORLD_FULL) world = World.from_content(content) self.assertIsInstance(world, World) self.assertEqual(world.name, "Premia") self.assertEqual(world.status, "Online") self.assertEqual(world.record_count, 531) self.assertIsInstance(world.record_date, datetime.datetime) self.assertEqual(world.creation_date, "2002-04") self.assertEqual(world.creation_year, 2002) self.assertEqual(world.creation_month, 4) self.assertEqual(world.location, WorldLocation.EUROPE) self.assertEqual(world.pvp_type, PvpType.OPEN_PVP) self.assertEqual(world.transfer_type, TransferType.REGULAR) self.assertEqual(len(world.world_quest_titles), 4) self.assertTrue(world.premium_only) self.assertTrue(world.battleye_protected) self.assertEqual(world.battleye_date, datetime.date(2017, 9, 5)) self.assertFalse(world.experimental) self.assertEqual(len(world.online_players), world.online_count) self.assertEqual(World.get_url(world.name), world.url) world_json_raw = world.to_json() world_json = json.loads(world_json_raw) self.assertEqual(len(world.online_players), len(world_json["online_players"]))
async def test_client_fetch_world(self, mock): """Testing fetching a world""" name = "Antica" content = self.load_resource(FILE_WORLD_FULL) mock.get(World.get_url(name), status=200, body=content) world = await self.client.fetch_world(name) self.assertIsInstance(world.data, World)
async def get_world(name, *, tries=5) -> Optional[World]: name = name.strip().title() if tries == 0: raise errors.NetworkError(f"get_world({name})") try: world = CACHE_WORLDS[name] return world except KeyError: pass try: async with aiohttp.ClientSession() as session: async with session.get(World.get_url_tibiadata(name)) as resp: content = await resp.text(encoding='ISO-8859-1') world = World.from_tibiadata(content) except (aiohttp.ClientError, asyncio.TimeoutError, tibiapy.TibiapyException): await asyncio.sleep(config.network_retry_delay) return await get_world(name, tries=tries - 1) CACHE_WORLDS[name] = world return world
def test_world_changes(self): content = self.load_resource( "website_changes/3_community_world_adra.txt") world = World.from_content(content) self.assertEqual("Adra", world.name) self.assertEqual("Online", world.status) self.assertEqual(TransferType.BLOCKED, world.transfer_type) self.assertEqual(PvpType.OPEN_PVP, world.pvp_type) self.assertEqual(WorldLocation.EUROPE, world.location)
def test_world_from_content_tournament(self): """Testing parsing a tournament world""" content = self.load_resource(FILE_WORLD_TOURNAMENT) world = World.from_content(content) self.assertIsInstance(world, World) self.assertIsInstance(world.tournament_world_type, TournamentWorldType) self.assertEqual(world.tournament_world_type, TournamentWorldType.REGULAR) self.assertEqual(world.record_count, 21) self.assertTrue(world.premium_only) self.assertFalse(world.world_quest_titles)
def test_world_from_content_offline(self): """Testing parsing an offline world""" content = self.load_resource(FILE_WORLD_FULL_OFFLINE, ) world = World.from_content(content) self.assertIsInstance(world, World) self.assertEqual(world.name, "Antica") self.assertEqual(world.status, "Offline") self.assertEqual(world.record_count, 1052) self.assertIsInstance(world.record_date, datetime.datetime) self.assertEqual(world.creation_date, "1997-01") self.assertEqual(world.creation_year, 1997) self.assertEqual(world.creation_month, 1) self.assertEqual(world.location, WorldLocation.EUROPE) self.assertEqual(world.pvp_type, PvpType.OPEN_PVP) self.assertEqual(world.transfer_type, TransferType.REGULAR) self.assertEqual(len(world.world_quest_titles), 5) self.assertFalse(world.premium_only) self.assertTrue(world.battleye_protected) self.assertEqual(world.battleye_date, datetime.date(2017, 8, 29)) self.assertFalse(world.experimental) self.assertEqual(len(world.online_players), world.online_count) self.assertEqual(World.get_url(world.name), world.url)
async def fetch_world(self, name): """Fetches a world from Tibia.com Parameters ---------- name: :class:`str` The name of the world. Returns ------- :class:`World` The world's information if found, ```None`` otherwise. Raises ------ Forbidden If a 403 Forbidden error was returned. This usually means that Tibia.com is rate-limiting the client because of too many requests. NetworkError If there's any connection errors during the request. """ content = await self._get(World.get_url(name)) world = World.from_content(content) return world
def test_world_from_tibiadata(self): """Testing parsing a world from TibiaData""" content = self._load_resource(FILE_WORLD_TIBIADATA) world = World.from_tibiadata(content) self.assertIsInstance(world, World) self.assertEqual(world.name, "Zuna") self.assertEqual(world.status, "Online") self.assertEqual(world.record_count, 106) self.assertIsInstance(world.record_date, datetime.datetime) self.assertEqual(world.creation_date, "2017-10") self.assertEqual(world.creation_year, 2017) self.assertEqual(world.creation_month, 10) self.assertEqual(world.location, WorldLocation.EUROPE) self.assertEqual(world.pvp_type, PvpType.HARDCORE_PVP) self.assertEqual(world.transfer_type, TransferType.LOCKED) self.assertEqual(len(world.world_quest_titles), 1) self.assertFalse(world.premium_only) self.assertFalse(world.battleye_protected) self.assertIsNone(world.battleye_date) self.assertTrue(world.experimental) self.assertEqual(len(world.online_players), world.online_count) self.assertEqual(World.get_url_tibiadata(world.name), world.url_tibiadata)
def test_world_from_content_unrelated_section(self): """Testing parsing a world using an unrelated section""" content = self.load_resource(self.FILE_UNRELATED_SECTION) with self.assertRaises(InvalidContent): World.from_content(content)
def test_world_from_content_not_found(self): """Testing parsing a world that doesn't exist""" content = self.load_resource(FILE_WORLD_NOT_FOUND) world = World.from_content(content) self.assertIsNone(world)
def test_world_tibiadata_unrelated_section(self): """Testing parsing an unrelated TibiaData section""" with self.assertRaises(InvalidContent): World.from_tibiadata( self._load_resource( tests.tests_character.FILE_CHARACTER_TIBIADATA))
def test_world_from_tibiadata_invalid_json(self): """Testing parsing an invalid json""" with self.assertRaises(InvalidContent): World.from_tibiadata("<html><b>Not a json string</b></html>")
def test_world_from_tibiadata_not_found(self): """Testing parsing a world that doesn't exist in TibiaData""" content = self._load_resource(FILE_WORLD_TIBIADATA_NOT_FOUND) world = World.from_tibiadata(content) self.assertIsNone(world)