示例#1
0
    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)
示例#2
0
 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')
示例#3
0
    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)
示例#4
0
 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')
示例#5
0
    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 __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)
示例#7
0
 def test_analyze(self):
     with self.assertRaises(NotImplementedError):
         Bear(Section('test-section'), {}).analyze()
示例#8
0
    def test_invalid_types_at_instantiation(self):
        with self.assertRaises(TypeError):
            Bear(Section('test-section'), 2)

        with self.assertRaises(TypeError):
            Bear(None, {})
示例#9
0
 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')
示例#10
0
 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)
示例#11
0
 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')
示例#12
0
 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)
示例#13
0
 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')
示例#14
0
 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')
示例#15
0
 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)
示例#16
0
 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'))
示例#17
0
 def test_generate_tasks(self):
     with self.assertRaises(NotImplementedError):
         Bear(Section('test-section'), {}).generate_tasks()
示例#18
0
 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)
示例#19
0
 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'))
示例#20
0
 def __init__(self, section, file_dict, tasks_count=1):
     Bear.__init__(self, section, file_dict)
     self.tasks_count = tasks_count
示例#21
0
 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)