def test_uses_requests_to_raise_connection_error_mp3( self, fake_post, fake_get): url = 'api.forvo.com' expected_data = { "action": "word-pronunciations", "format": "json", "id_lang_speak": "39", "id_order": "", "limit": "", "rate": "", "send": "", "username": "", "word": "fake_word" } html_response = ('<div class="intro"><pre> {"items":' ' [{"pathmp3": "mp3_url", ' '"code": "en",' '"country": "United States"}]} ' '</pre></div>') fake_post.expects_call().with_args( arg.contains(url), data=expected_data).returns(FakeRequestsResponse(html_response)) fake_get.expects_call().raises(requests.exceptions.ConnectionError) test_word = ForvoImporter('fake_word') with self.assertLogs(logger='general', level='ERROR') as general, self.assertLogs( logger='forvo_fails', level='ERROR') as forvo: test_word.import_sound() self.assertEqual( [*general.output, *forvo.output], ['ERROR:general:Connection error', 'ERROR:forvo_fails:fake_word'])
def test_11(self, fake_post, fake_get, fake_mkdir): url = 'api.forvo.com' expected_data = { "action": "word-pronunciations", "format": "json", "id_lang_speak": "39", "id_order": "", "limit": "", "rate": "", "send": "", "username": "", "word": "fake_word1" } mp3_file = b'1234567890' html_response = ('<div class="intro"><pre> {"items":' ' [{"pathmp3": "mp3_url", ' '"code": "en",' '"country": "United States"}]} ' '</pre></div>') fake_post.expects_call().with_args( arg.contains(url), data=expected_data).returns(FakeRequestsResponse(html_response)) fake_get.expects_call().returns(FakeRequestsResponse(mp3_file)) fake_mkdir.expects_call().with_args() test_word = ForvoImporter('fake_word1') test_word.import_sound()
def test_if_forvo_json_does_not_have_required_keys(self, fake_post): url = 'api.forvo.com' expected_data = { "action": "word-pronunciations", "format": "json", "id_lang_speak": "39", "id_order": "", "limit": "", "rate": "", "send": "", "username": "", "word": "fake_word" } html_response = ('<div class="intro"><pre> {"items":' ' [{"pathmp777": "mp3_url", ' '"code": "en",' '"country": "United States"}]} ' '</pre></div>') fake_post.expects_call().with_args( arg.contains(url), data=expected_data).returns(FakeRequestsResponse(html_response)) test_word = ForvoImporter('fake_word') with self.assertLogs(logger='general', level='ERROR') as general, self.assertLogs( logger='forvo_fails', level='ERROR') as forvo: test_word.import_sound() self.assertEqual([*general.output, *forvo.output], [ 'ERROR:general:JSON response from Forvo does not have required keys', 'ERROR:forvo_fails:fake_word' ])
def test_if_forvo_html_response_has_unexpected_structure(self, fake_post): url = 'api.forvo.com' expected_data = { "action": "word-pronunciations", "format": "json", "id_lang_speak": "39", "id_order": "", "limit": "", "rate": "", "send": "", "username": "", "word": "fake_word" } fake_post.expects_call().with_args( arg.contains(url), data=expected_data).returns( FakeRequestsResponse('some unexpected html code')) test_word = ForvoImporter('fake_word') with self.assertLogs(logger='general', level='ERROR') as general, self.assertLogs( logger='forvo_fails', level='ERROR') as forvo: test_word.import_sound() self.assertEqual([*general.output, *forvo.output], [ 'ERROR:general:Unexpected HTML Response from Forvo', 'ERROR:forvo_fails:fake_word' ])
def test_positive_case(self, fake_post, fake_get, fake_is_path_exist, fake_save): url = 'api.forvo.com' expected_data = { "action": "word-pronunciations", "format": "json", "id_lang_speak": "39", "id_order": "", "limit": "", "rate": "", "send": "", "username": "", "word": "fake_word" } fake_post.expects_call().with_args( arg.contains(url), data=expected_data).returns( FakeRequestsResponse( ('<html><div class="intro"><pre>' ' {"items": [' '{"code": "en", ' '"country": "United States", ' '"pathmp3": "mp3\/url\/"}' ']}' ' </pre></html>'))) fake_get.expects_call().returns(FakeRequestsResponse(b'0x11')) fake_is_path_exist.expects_call().returns(True) fake_save.expects_call().returns(None) test_word = ForvoImporter('fake_word') res = test_word.import_sound() # TODO check if file saved self.assertEqual(res, None)
def test_if_forvo_json_does_not_have_required_keys1( self, fake_get_html, fake_check): fake_check.expects_call().returns(False) fake_get_html.expects_call().returns( ('<html><div class="intro"><pre> {"items":' ' [{"pathmp3": "mp3/url/"}]} </pre></html>')) test_word = ForvoImporter('last') res = test_word.import_sound() self.assertEqual(res, None)
def test_uses_requests_to_raise_connection_error_html(self, fake_post): fake_post.expects_call().raises(requests.exceptions.ConnectionError) test_word = ForvoImporter('fake_word') with self.assertLogs(logger='general', level='ERROR') as general, self.assertLogs( logger='forvo_fails', level='ERROR') as forvo: test_word.import_sound() self.assertEqual( [*general.output, *forvo.output], ['ERROR:general:Connection error', 'ERROR:forvo_fails:fake_word'])
def test_uses_requests_to_raise_http_error(self, fake_post): http_error = 418 fake_post.expects_call().raises( requests.exceptions.HTTPError(http_error)) test_word = ForvoImporter('fake_word') with self.assertLogs(logger='general', level='ERROR') as general, self.assertLogs( logger='forvo_fails', level='ERROR') as forvo: test_word.import_sound() self.assertEqual([*general.output, *forvo.output], [ 'ERROR:general:Following http error occurred: 418', 'ERROR:forvo_fails:fake_word' ])
def test_save_proper_data_to_file(self, fake_open): class FakeFile: def write(self, *args, **kwargs): assert 'some_data' in args class FakeContextManager: def __enter__(self): return FakeFile() def __exit__(self, *args): pass fake_open.expects_call().with_args('some_path', 'wb').returns(FakeContextManager()) test_word = ForvoImporter('fifth') test_word.save_mp3('some_path', 'some_data') # Убедиться что функция 'save_result' сохраняет файлы - пофиксить # в нужную директорию pass
def test_uses_requests_to_get_html_from_forvo(self, fake_post): url = 'api.forvo.com' expected_data = { "action": "word-pronunciations", "format": "json", "id_lang_speak": "39", "id_order": "", "limit": "", "rate": "", "send": "", "username": "", "word": "fake_word" } fake_post.expects_call().with_args( arg.contains(url), data=expected_data).returns(FakeRequestsResponse('some html code')) test_word = ForvoImporter('fake_word') res = test_word.get_html_from_forvo() self.assertEqual(res, 'some html code')
def test_1_if_forvo_json_does_not_have_required_keys(self, fake_post): url = 'api.forvo.com' expected_data = { "action": "word-pronunciations", "format": "json", "id_lang_speak": "39", "id_order": "", "limit": "", "rate": "", "send": "", "username": "", "word": "fake_word" } html_response = ( '<div class="intro"><pre> {"items":' ' [{"pathmp3": "mp3_url", ' '"not_code": "en",' '"not_country1": "United States"}]} ' '</pre></div>') fake_post.expects_call().with_args( arg.contains(url), data=expected_data).returns(FakeRequestsResponse(html_response)) test_word = ForvoImporter('fake_word') test_word.import_sound()
def test_if_abs_mp3_path_is_created_correctly(self): test_word = ForvoImporter('sixth') test_path = test_word.make_mp3_abs_path('audio', 33) self.assertEqual(test_path, 'audio/sixth_34.mp3')
def test_write_permission_of_working_dir(self, fake_write_permissions): fake_write_permissions.expects_call().returns(False) test_word = ForvoImporter('seventh') res = test_word.is_there_dir_write_permissions('not/exist/mp3/path') self.assertEqual(res, False)
def test_if_dir_path_exists(self, fake_is_path_exist): fake_is_path_exist.expects_call().returns(False) test_word = ForvoImporter('seventh') res = test_word.is_path_exist('not/exist/mp3/path') self.assertEqual(res, False)