Example #1
0
 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> {&quot;items&quot;:'
                      ' [{&quot;pathmp3&quot;: &quot;mp3_url&quot;, '
                      '&quot;code&quot;: &quot;en&quot;,'
                      '&quot;country&quot;: &quot;United States&quot;}]} '
                      '</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'])
Example #2
0
 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> {&quot;items&quot;:'
                      ' [{&quot;pathmp3&quot;: &quot;mp3_url&quot;, '
                      '&quot;code&quot;: &quot;en&quot;,'
                      '&quot;country&quot;: &quot;United States&quot;}]} '
                      '</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()
Example #3
0
 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> {&quot;items&quot;:'
                      ' [{&quot;pathmp777&quot;: &quot;mp3_url&quot;, '
                      '&quot;code&quot;: &quot;en&quot;,'
                      '&quot;country&quot;: &quot;United States&quot;}]} '
                      '</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'
     ])
Example #4
0
 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'
     ])
Example #5
0
 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>'
                  ' {&quot;items&quot;: ['
                  '{&quot;code&quot;: &quot;en&quot;, '
                  '&quot;country&quot;: &quot;United States&quot;, '
                  '&quot;pathmp3&quot;: &quot;mp3\/url\/&quot;}'
                  ']}'
                  ' </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)
Example #6
0
 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> {&quot;items&quot;:'
          ' [{&quot;pathmp3&quot;: &quot;mp3/url/&quot;}]} </pre></html>'))
     test_word = ForvoImporter('last')
     res = test_word.import_sound()
     self.assertEqual(res, None)
Example #7
0
 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'])
Example #8
0
 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'
     ])
Example #9
0
    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
Example #10
0
 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')
Example #11
0
 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> {&quot;items&quot;:'
         ' [{&quot;pathmp3&quot;: &quot;mp3_url&quot;, '
         '&quot;not_code&quot;: &quot;en&quot;,'
         '&quot;not_country1&quot;: &quot;United States&quot;}]} '
         '</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()
Example #12
0
 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')
Example #13
0
 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)
Example #14
0
 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)