another unittest
This commit is contained in:
parent
d9344b1b4b
commit
f2756506af
@ -87,3 +87,51 @@ class ScrapeTestCase(IsolatedAsyncioTestCase):
|
||||
mock_open.assert_called_once()
|
||||
mock_open.return_value.__enter__.return_value.write.assert_called_once_with(test_soup.prettify())
|
||||
logging.disable(logging.NOTSET)
|
||||
|
||||
@patch.object(scrape, 'soup_from_url')
|
||||
@patch.object(scrape, 'trs_from_page')
|
||||
@patch.object(scrape, 'extract_row_data')
|
||||
@patch.object(scrape, 'ClientSession')
|
||||
async def test_get_data_from_category(self, mock_session_cls, mock_extract_row_data,
|
||||
mock_trs_from_page, mock_soup_from_url):
|
||||
mock_session = MagicMock()
|
||||
mock_session_cls.return_value = mock_session
|
||||
mock_soup = MagicMock()
|
||||
mock_soup_from_url.return_value = mock_soup
|
||||
category = 'ßßß'
|
||||
|
||||
mock_trs = []
|
||||
mock_trs_from_page.return_value = mock_trs
|
||||
mock_extract_row_data.return_value = expected_output = []
|
||||
output = await scrape.get_data_from_category(category, last_page=1)
|
||||
self.assertListEqual(expected_output, output)
|
||||
mock_soup_from_url.assert_called_once_with(f'{scrape.BASE_URL}{category}', mock_session)
|
||||
mock_trs_from_page.assert_called_once_with(mock_soup)
|
||||
mock_extract_row_data.assert_not_called()
|
||||
mock_soup_from_url.reset_mock()
|
||||
mock_trs_from_page.reset_mock()
|
||||
|
||||
mock_trs = ['foo', 'bar']
|
||||
mock_trs_from_page.return_value = mock_trs
|
||||
mock_extract_row_data.return_value = expected_output = ['a', 'b']
|
||||
output = await scrape.get_data_from_category(category, last_page=1)
|
||||
self.assertListEqual(expected_output, output)
|
||||
mock_soup_from_url.assert_has_calls([
|
||||
call(f'{scrape.BASE_URL}{category}', mock_session),
|
||||
call(f'{scrape.BASE_URL}{category}/{2}', mock_session)
|
||||
])
|
||||
mock_trs_from_page.assert_has_calls([call(mock_soup), call(mock_soup)])
|
||||
mock_extract_row_data.assert_called_once_with(*mock_trs)
|
||||
mock_soup_from_url.reset_mock()
|
||||
mock_trs_from_page.reset_mock()
|
||||
mock_extract_row_data.reset_mock()
|
||||
|
||||
async with scrape.ClientSession() as session:
|
||||
output = await scrape.get_data_from_category(category, session, last_page=1)
|
||||
self.assertListEqual(expected_output, output)
|
||||
mock_soup_from_url.assert_has_calls([
|
||||
call(f'{scrape.BASE_URL}{category}', session),
|
||||
call(f'{scrape.BASE_URL}{category}/{2}', session)
|
||||
])
|
||||
mock_trs_from_page.assert_has_calls([call(mock_soup), call(mock_soup)])
|
||||
mock_extract_row_data.assert_called_once_with(*mock_trs)
|
||||
|
Loading…
Reference in New Issue
Block a user