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(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_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_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.assertRaisesRegex(exc, '^418 '): Bear.download_cached_file( self.teapot_url, '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_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')