def test_house_parse_status_without_bids(self): """Testing parsing the status of a house with no bids""" house = House("Name") content = self._load_resource(FILE_HOUSE_STATUS_NO_BIDS) house._parse_status(content) self.assertEqual(house.status, HouseStatus.AUCTIONED) self.assertIsNone(house.auction_end)
async def fetch_house(self, house_id, world): """Fetches a house in a specific world by its id. Parameters ---------- house_id: :class:`int` The house's internal id. world: :class:`str` The name of the world to look for. Returns ------- :class:`House` The house 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(House.get_url(house_id, world)) house = House.from_content(content) return house
def test_house_parse_status_rented(self): """Testing parsing a rented status""" house = House("Name") content = self._load_resource(FILE_HOUSE_STATUS_RENTED) house._parse_status(content) self.assertEqual(house.status, HouseStatus.RENTED) self.assertEqual(house.owner, "Thorcen") self.assertIsInstance(house.paid_until, datetime.datetime)
def test_house_parse_status_with_bids(self): """Testing parsing a house status with bids""" house = House("Name") content = self._load_resource(FILE_HOUSE_STATUS_WITH_BIDS) house._parse_status(content) self.assertEqual(house.status, HouseStatus.AUCTIONED) self.assertIsNone(house.owner) self.assertEqual(house.highest_bid, 15000) self.assertEqual(house.highest_bidder, "King of Bosnia") self.assertIsInstance(house.auction_end, datetime.datetime)
def test_house_from_content_transferred(self): """Testing parsing a house being transferred""" house = House("Name") content = self._load_resource(FILE_HOUSE_STATUS_TRANSFER) house._parse_status(content) self.assertEqual(house.status, HouseStatus.RENTED) self.assertEqual(house.owner, "Xenaris mag") self.assertEqual(house.transferee, "Ivarr Bezkosci") self.assertIsNotNone(house.transferee_url) self.assertTrue(house.transfer_accepted) self.assertEqual(house.transfer_price, 850000)
async def get_house(house_id, world, *, tries=5) -> House: """Returns a dictionary containing a house's info, a list of possible matches or None. If world is specified, it will also find the current status of the house in that world.""" if tries == 0: raise errors.NetworkError(f"get_house({house_id},{world})") try: async with aiohttp.ClientSession() as session: async with session.get(House.get_url_tibiadata(house_id, world)) as resp: content = await resp.text(encoding='ISO-8859-1') house = House.from_tibiadata(content) except aiohttp.ClientError: await asyncio.sleep(config.network_retry_delay) return await get_house(house_id, world, tries=tries - 1) return house
async def test_client_fetch_house(self, mock): """Testing fetching a house""" world = "Antica" house_id = 5236 content = self.load_resource(FILE_HOUSE_FULL) mock.get(House.get_url(house_id, world), status=200, body=content) house = await self.client.fetch_house(house_id, world) self.assertIsInstance(house.data, House)
def test_house_from_content(self): """Testing parsing a house""" content = self.load_resource(FILE_HOUSE_FULL) house = House.from_content(content) self.assertIsInstance(house, House) self.assertEqual(house.name, "Sorcerer's Avenue Labs 2e") self.assertEqual(house.beds, 1) self.assertTrue(house.rent, 715) self.assertEqual(house.status, HouseStatus.AUCTIONED) self.assertEqual(house.url, House.get_url(house.id, house.world)) self.assertIsNone(house.owner) self.assertIsNone(house.owner_url) self.assertIsNotNone(house.highest_bidder) self.assertIsNotNone(house.highest_bidder_url) self.assertEqual(house.highest_bid, 0) house_json_raw = house.to_json() house_json = json.loads(house_json_raw) self.assertEqual(house.image_url, house_json["image_url"])
def test_house_from_tibiadata(self): """Testing parsing a house from TibiaData""" content = self._load_resource(FILE_HOUSE_TIBIADATA) house = House.from_tibiadata(content) self.assertIsInstance(house, House) self.assertEqual(house.id, 32012) self.assertEqual(house.world, "Gladera") self.assertEqual(house.type, HouseType.GUILDHALL) self.assertEqual(house.size, 7) self.assertEqual(house.status, HouseStatus.AUCTIONED) self.assertIsNone(house.owner)
def test_house_from_content_unrelated(self): """Testing parsing an unrelated section""" content = self._load_resource(self.FILE_UNRELATED_SECTION) with self.assertRaises(InvalidContent): House.from_content(content)
def test_house_from_content_not_found(self): """Testing parsing a house that doesn't exist""" content = self._load_resource(FILE_HOUSE_NOT_FOUND) house = House.from_content(content) self.assertIsNone(house)
def test_house_from_tibiadata_unrelated(self): """Testing parsing an unrelated section""" content = self._load_resource(tests.tests_character.FILE_CHARACTER_TIBIADATA) with self.assertRaises(InvalidContent): House.from_tibiadata(content)
def test_house_from_tibiadata_invalid_json(self): """Testing parsing an invalid json""" with self.assertRaises(InvalidContent): House.from_tibiadata("<p>Not json</p>")