def test_write_permissions_of_working_dir(self, fake_path_exists, fake_dir_access): fake_path_exists.expects_call().returns(True) fake_dir_access.expects_call().returns(False) test_word = ODImporter('sixth') msg = test_word.create_word_article('', '', '') self.assertEqual(msg, "Permission denied: ''")
def test_positive_case(self, fake_get, fake_open): class FakeFile: def write(self, *args, **kwargs): pass class FakeContextManager: def __enter__(self): return FakeFile() def __exit__(self, *args): pass url = 'https://od-api.oxforddictionaries.com' expected_headers = {'app_id': 'test_app_id', 'app_key': 'test_app_key'} dir_path = os.path.join(settings.BASE_DIR, 'media', 'od') fake_get.expects_call().with_args(arg.contains( url), headers=expected_headers).returns( FakeRequestsResponse('{"article": "article"}')) fake_open.expects_call().returns(FakeContextManager()) test_word = ODImporter('fifth') msg = test_word.create_word_article(dir_path, 'test_app_id', 'test_app_key') self.assertEqual(msg, 'Data successfully saved')
def test_uses_requests_to_raise_not_404_http_error(self, fake_get): http_error = 418 fake_get.expects_call().raises(requests.exceptions .HTTPError(http_error)) test_word = ODImporter('fourth') msg, res = test_word.get_article('', '') self.assertEqual(msg, 'HTTP error occurred: {}'.format( requests.exceptions.HTTPError(http_error)))
def test_uses_requests_to_raise_404_error(self, fake_get): http_error = 404 fake_get.expects_call().raises(requests.exceptions .HTTPError(http_error)) test_word = ODImporter('qwerty') msg, res = test_word.get_article('', '') self.assertEqual(msg, 'Word "qwerty" does not exist' ' in Oxford Dictionary')
def test_uses_requests_to_get_article_from_od(self, fake_get): url = 'https://od-api.oxforddictionaries.com' expected_headers = {'app_id': 'test_app_id', 'app_key': 'test_app_key'} fake_get.expects_call().with_args(arg.contains(url), headers=expected_headers).returns( FakeRequestsResponse('{}')) test_word = ODImporter('something') msg, res = test_word.get_article( settings.OXFORD_DICTIONARY_CONFIG[0]['app_id'], settings.OXFORD_DICTIONARY_CONFIG[0]['app_key']) self.assertEqual(msg, 'Data successfully saved')
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', 'w').returns( FakeContextManager()) test_word = ODImporter('fifth') test_word.save_article('some_path', 'some_data')
def test_uses_requests_to_raise_connection_error(self, fake_get): fake_get.expects_call().raises(requests.exceptions.ConnectionError) test_word = ODImporter('second') msg, res = test_word.get_article('', '') self.assertEqual(msg, 'Connection error')
def test_is_working_dir_exist(self, fake_path_exists): fake_path_exists.expects_call().returns(False) test_word = ODImporter('seventh') msg = test_word.create_word_article('', '', '') self.assertEqual(msg, "Path does not exist: ''")