def test_listed_news_from_content(self): """Testing parsing news""" content = self._load_resource(FILE_NEWS_LIST) news_list = ListedNews.list_from_content(content) self.assertGreater(len(news_list), 0) latest_news = news_list[0] self.assertIsInstance(latest_news, ListedNews) self.assertIsInstance(latest_news.id, int) self.assertIsInstance(latest_news.category, NewsCategory) self.assertIsInstance(latest_news.type, NewsType) self.assertIsInstance(latest_news.date, datetime.date) self.assertIsNotNone(latest_news.url) self.assertEqual(latest_news.url, ListedNews.get_url(latest_news.id))
async def fetch_news_archive(self, begin_date, end_date, categories=None, types=None): """Fetches news from the archive meeting the search criteria. Parameters ---------- begin_date: :class:`datetime.date` The beginning date to search dates in. end_date: :class:`datetime.date` The end date to search dates in. categories: `list` of :class:`NewsCategory` The allowed categories to show. If left blank, all categories will be searched. types : `list` of :class:`ListedNews` The allowed news types to show. if unused, all types will be searched. Returns ------- list of :class:`ListedNews` The news meeting the search criteria. Raises ------ ValueError: If ``begin_date`` is more recent than ``end_date``. 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. """ if begin_date > end_date: raise ValueError("begin_date can't be more recent than end_date") if not categories: categories = list(NewsCategory) if not types: types = list(NewsType) data = { "filter_begin_day": begin_date.day, "filter_begin_month": begin_date.month, "filter_begin_year": begin_date.year, "filter_end_day": end_date.day, "filter_end_month": end_date.month, "filter_end_year": end_date.year, } for category in categories: key = "filter_%s" % category.value data[key] = category.value if NewsType.FEATURED_ARTICLE in types: data["filter_article"] = "article" if NewsType.NEWS in types: data["filter_news"] = "news" if NewsType.NEWS_TICKER in types: data["filter_ticker"] = "ticker" content = await self._post(News.get_list_url(), data) news = ListedNews.list_from_content(content) return news
async def test_client_handle_errors(self, mock): """Testing error handling""" mock.get(WorldOverview.get_url(), status=403) with self.assertRaises(Forbidden): await self.client.fetch_world_list() mock.get(WorldOverview.get_url(), status=404) with self.assertRaises(NetworkError): await self.client.fetch_world_list() mock.get(ListedNews.get_list_url(), exception=aiohttp.ClientError()) with self.assertRaises(NetworkError): await self.client.fetch_world_list() mock.post(ListedNews.get_list_url(), exception=aiohttp.ClientOSError()) with self.assertRaises(NetworkError): await self.client.fetch_recent_news(30)
async def test_client_fetch_recent_news(self, mock): """Testing fetching recent nows""" content = self.load_resource(FILE_NEWS_LIST) mock.post(ListedNews.get_list_url(), status=200, body=content) recent_news = await self.client.fetch_recent_news(30) self.assertIsInstance(recent_news.data, list) self.assertIsInstance(recent_news.data[0], ListedNews)
def test_listed_news_from_content_unrelated(self): """Testing parsing an unrelated section""" content = self._load_resource(self.FILE_UNRELATED_SECTION) with self.assertRaises(InvalidContent): ListedNews.list_from_content(content)
def test_listed_news_from_content_empty(self): """Testing parsing a news article that doesn't exist""" content = self._load_resource(FILE_NEWS_LIST_EMPTY) news_list = ListedNews.list_from_content(content) self.assertEqual(len(news_list), 0)