async def test_run_returns_empty_payload_when_no_results( mock_initialize, mock_get_page, mock_get_tokens, mock_get_terms, mock_upload_to_bucket, ): browser = asynctest.CoroutineMock() browser.close.return_value = asyncio.Future() browser.close.return_value.set_result(None) mock_initialize = utils.set_async_result(mock_initialize, browser) mock_get_page = utils.set_async_result(mock_get_page, None) mock_get_tokens.return_value = asyncio.Future() mock_get_tokens.return_value.set_result(["foo", "abcdef1234567890"]) mock_get_terms.return_value = [] mock_upload_to_bucket().return_value = None payload = await main.run() mock_initialize.assert_called mock_get_page.assert_called mock_get_tokens.assert_called mock_get_terms.assert_called mock_upload_to_bucket.assert_called assert payload == {}
async def test_run_returns_payload( mock_initialize, mock_get_page, mock_get_tokens, mock_get_terms, mock_get_subjects, mock_get_subjects_json, mock_upload_to_bucket, ): browser = asynctest.CoroutineMock() browser.close.return_value = asyncio.Future() browser.close.return_value.set_result(None) mock_initialize = utils.set_async_result(mock_initialize, browser) mock_get_page = utils.set_async_result(mock_get_page, None) mock_get_tokens.return_value = asyncio.Future() mock_get_tokens.return_value.set_result(["foo", "abcdef1234567890"]) mock_get_terms.return_value = [ {"code": "201904", "description": "Fall 2019 Quarter"} ] mock_get_subjects_json = utils.set_async_result( mock_get_subjects_json, data.subjects_json ) mock_upload_to_bucket().return_value = None payload = await main.run() mock_initialize.assert_called mock_get_page.assert_called mock_get_tokens.assert_called mock_get_terms.assert_called mock_get_subjects_json.assert_called mock_upload_to_bucket.assert_called assert payload["201904"][0]["crn"] == 10883
async def test_initialize_browser_returns_browser(mock_launch): mock_launch = utils.set_async_result(mock_launch, "mock-browser") browser = await pyppeteer.initialize() mock_launch.assert_called assert browser == "mock-browser"
async def test_get_subjects_json_returns_data(mock_get_unique_session_id, mock_get_schedule_json, mock_authenticate): subjects = [ { "code": "ACTG", "description": "Accounting" }, { "code": "ACTG", "description": "Accounting" }, ] term = {"code": "201904", "description": "Fall 2019 Quarter"} cookies = {"cookie": "jar"} unique_session_id = "abcdef1234567890" mock_get_unique_session_id = asynctest.CoroutineMock() mock_get_unique_session_id = utils.set_async_result( mock_get_unique_session_id, unique_session_id) mock_get_schedule_json.return_value = {"data": "foo"} subjects_json = await main.get_subjects_json(subjects, term, cookies, None) assert "foo" in subjects_json assert len(subjects_json) == 2
async def test_get_tokens_returns_session_and_unique_ids( mock_page, mock_jsession_id, mock_unique_id ): page = asynctest.CoroutineMock() page.goto.return_value = asyncio.Future() page.goto.return_value.set_result(None) browser = asynctest.CoroutineMock() browser.newPage.return_value = asyncio.Future() browser.newPage.return_value.set_result(page) mock_jsession_id = utils.set_async_result(mock_jsession_id, "test-jsession-id") mock_unique_id = utils.set_async_result(mock_unique_id, "test-unique-id") session_id, unique_session_id = await pyppeteer.get_tokens(browser) mock_jsession_id.assert_called mock_unique_id.assert_called assert session_id == "test-jsession-id" assert unique_session_id == "test-unique-id"