def test_download_cached_file(self): section = Section('default') uut = Bear(section, {}) mock_url = 'https://test.com' mock_text = """<html> <p> lorem ipsum dolor</p> </html>""" filename = 'test.html' file_location = join(uut.data_dir, filename) with freeze_time('2017-01-01') as frozen_datetime: with requests_mock.Mocker() as reqmock: reqmock.get(mock_url, text=mock_text) self.assertFalse(isfile(file_location)) expected_filename = file_location result_filename = uut.download_cached_file(mock_url, filename) self.assertTrue(isfile(join(file_location))) self.assertEqual(result_filename, expected_filename) expected_time = getmtime(file_location) frozen_datetime.tick(delta=datetime.timedelta(seconds=0.5)) result_filename = uut.download_cached_file(mock_url, filename) self.assertEqual(result_filename, expected_filename) result_time = getmtime(file_location) self.assertEqual(result_time, expected_time)
def test_download_cached_file_connection_timeout_mocked(self): mock_url = 'https://test.com' exc = requests.exceptions.ConnectTimeout with requests_mock.Mocker() as reqmock: reqmock.get(mock_url, exc=exc) with self.assertRaisesRegexp(exc, '^$'): Bear.download_cached_file(mock_url, 'test.html')
def test_download_cached_file(self): section = Section('default') uut = Bear(section, {}) mock_url = 'https://test.com' mock_text = """<html> <p> lorem ipsum dolor</p> </html>""" filename = 'test.html' file_location = join(uut.data_dir, filename) with requests_mock.Mocker() as reqmock: reqmock.get(mock_url, text=mock_text) self.assertFalse(isfile(file_location)) expected_filename = file_location result_filename = uut.download_cached_file(mock_url, filename) self.assertTrue(isfile(join(file_location))) self.assertEqual(result_filename, expected_filename) expected_time = getmtime(file_location) sleep(0.5) result_filename = uut.download_cached_file(mock_url, filename) self.assertEqual(result_filename, expected_filename) result_time = getmtime(file_location) self.assertEqual(result_time, expected_time)
def test_download_cached_file_connection_timeout_mocked(self): mock_url = 'https://test.com' exc = requests.exceptions.ConnectTimeout with requests_mock.Mocker() as reqmock: reqmock.get(mock_url, exc=exc) with self.assertRaisesRegexp(exc, '^$'): Bear.download_cached_file( mock_url, 'test.html')
def __init__(self, section, file_dict): """ :param section: The section object where bear settings are contained. A section passed here is considered to be immutable. :param file_dict: A dictionary containing filenames to process as keys and their contents (line-split with trailing return characters) as values. """ Bear.__init__(self, section, file_dict) self._kwargs = self.get_metadata().create_params_from_section(section)
def test_analyze(self): with self.assertRaises(NotImplementedError): Bear(Section('test-section'), {}).analyze()
def test_invalid_types_at_instantiation(self): with self.assertRaises(TypeError): Bear(Section('test-section'), 2) with self.assertRaises(TypeError): Bear(None, {})
def test_download_cached_file_status_code_error(self): exc = requests.exceptions.HTTPError with self.assertRaisesRegex(exc, '^418 '): Bear.download_cached_file( self.teapot_url, 'test.html')
def execute_task(self, args, kwargs): # To optimize performance and memory usage, we use kwargs from this # class instance instead of passing the same settings thousand times in # the tasks themselves. return Bear.execute_task(self, args, self._kwargs)
def test_download_cached_file_status_code_error(self): exc = requests.exceptions.HTTPError with self.assertRaisesRegexp(exc, '418 Client Error'): Bear.download_cached_file( 'http://httpbin.org/status/418', 'test.html')
def test_download_cached_file_status_code_error(self): exc = requests.exceptions.HTTPError with self.assertRaisesRegexp(exc, '418 Client Error'): Bear.download_cached_file('http://httpbin.org/status/418', 'test.html')
def test_new_result(self): bear = Bear(Section('test-section'), {}) result = bear.new_result('test message', '/tmp/testy') expected = Result.from_values(bear, 'test message', '/tmp/testy') self.assertEqual(result, expected)
def test_get_config_dir(self): section = Section('default') section.append(Setting('files', '**', '/path/to/dir/config')) uut = Bear(section, {}) self.assertEqual(uut.get_config_dir(), abspath('/path/to/dir'))
def test_generate_tasks(self): with self.assertRaises(NotImplementedError): Bear(Section('test-section'), {}).generate_tasks()
def __init__(self, section, file_dict, tasks_count=1): Bear.__init__(self, section, file_dict) self.tasks_count = tasks_count
def execute_task(self, args, kwargs): # To optimize performance a bit and memory usage, we use args and # kwargs from this class instance, instead of passing them via the # task. return Bear.execute_task(self, (self.file_dict,), self._kwargs)