Пример #1
0
    def test_expired(self, mock_datetime, mock_index):
        self.build_mock_index_and_datetime_now(mock_index, mock_datetime)

        cache = Cache('test', cache_max_age=1)

        self.assertTrue(cache.expired('expired_key'))
        self.assertFalse(cache.expired('valid_key_past'))
        self.assertFalse(cache.expired('valid_key_future'))
Пример #2
0
 def test_prune(self, mock_flush, mock_remove, mock_expired, mock_datetime, mock_index):
     self.build_mock_index_and_datetime_now(mock_index, mock_datetime)
     cache = Cache('test')
     mock_flush.reset_mock()
     mock_remove.reset_mock()
     mock_expired.reset_mock()
     mock_expired.return_value = True
     cache.prune()
     self.assertEqual(1, mock_flush.call_count)
     self.assertEqual(3, mock_expired.call_count)
     self.assertEqual(3, mock_remove.call_count)
Пример #3
0
    def test_remove_data(self, mock_remove, mock_path):
        cache = Cache('test')
        mock_path.return_value = 'existing_key_path'
        cache._remove_data('existing_key')
        mock_remove.assert_called_with('existing_key_path')

        mock_remove.reset_mock()
        mock_remove.side_effect = IOError('IO Error remove')
        self.assertRaises(IOError, cache._remove_data, 'existing_key')

        mock_remove.reset_mock()
        mock_remove.side_effect = OSError('OS Error remove')
        self.assertRaises(OSError, cache._remove_data, 'existing_key')
Пример #4
0
    def test_set(self, mock_open, mock_cache_file, mock_cache_object, mock_flush_index, mock_datetime, mock_index):
        self.build_mock_index_and_datetime_now(mock_index, mock_datetime)
        cache = Cache('test')
        value = cache.set('key_obj', 'value_obj', as_fileobj=False)
        self.assertTrue(mock_flush_index.called)
        self.assertTrue(mock_cache_object.called)
        self.assertEqual(value, 'value_obj')
        self.assertEqual(cache.index['key_obj'], mock_datetime.now())

        mock_flush_index.reset_mock()
        value = cache.set('key_file', 'value_file', as_fileobj=True)
        self.assertTrue(mock_flush_index.called)
        self.assertTrue(mock_cache_file.called)
        self.assertEqual(value, 'value_file')
        self.assertEqual(cache.index['key_file'], mock_datetime.now())
Пример #5
0
    def test_fetch(self, mock_set, mock_get, mock_cached, mock_index):
        cache = Cache('test')
        mock_cached.return_value = True
        cache.fetch('cached_key', 'source')
        self.assertTrue(mock_get.called)
        self.assertFalse(mock_set.called)

        mock_get.reset_mock()
        mock_set.reset_mock()
        mock_cached.return_value = False

        def source(): return 'value'

        cache.fetch('uncached_key', source)
        self.assertTrue(mock_get.called)
        mock_set.assert_called_once_with(cache, 'uncached_key', source())
Пример #6
0
    def test_get(self, mock_open,  mock_read_file,  mock_read_object, mock_cached, mock_index):
        cache = Cache('test')
        mock_cached.return_value = False
        value = cache.get('unknown_key')

        self.assertTrue(mock_cached.called)
        self.assertIsNone(value)

        mock_cached.return_value = True
        mock_read_object.return_value = 'read_value_object'
        mock_read_file.return_value = 'read_value_file'
        mock_cached.reset_mock()
        value = cache.get('cached_key', as_fileobj=False)

        self.assertTrue(mock_cached.called)
        self.assertTrue(mock_read_object.called)
        self.assertEqual(value, 'read_value_object')

        mock_cached.reset_mock()
        value = cache.get('cached_key', as_fileobj=True)
        self.assertTrue(mock_cached.called)
        self.assertTrue(mock_read_file.called)
        self.assertEqual(value, 'read_value_file')
Пример #7
0
 def test_flush_index(self, mock_open, mock_serialize_index, mock_dump, mock_index):
     cache = Cache('test')
     mock_serialize_index.return_value = 'dict_to_flush'
     cache._flush_index()
     mock_dump.assert_called_with('dict_to_flush', mock.ANY)
Пример #8
0
 def test_read_index(self, mock_deserialize, mock_index):
     cache = Cache('test')
     cache._read_index()
     self.assertTrue(mock_deserialize.called)
Пример #9
0
 def test_deserialize_index(self, mock_index):
     cache = Cache('test')
     deserialized = cache._deserialize_index(cache.index)
     self.assertTrue(isinstance(deserialized, dict))
Пример #10
0
 def test_serialize_index(self, mock_datetime, mock_index):
     self.build_mock_index_and_datetime_now(mock_index, mock_datetime)
     cache = Cache('test', cache_max_age=1)
     dict_to_serialize = cache._serialize_index(cache.index)
     self.assertTrue(isinstance(dict_to_serialize, dict))
Пример #11
0
 def test_read_cached_file_contents(self, mock_start, mock_open, mock_tempfile):
     cache = Cache('test')
     mock_tempfile.TemporaryFile.return_value = open('.test', 'w')
     tmp_file = cache._read_cached_file_contents(['line1', 'line2', 'line3'])
     self.assertTrue(mock_tempfile.TemporaryFile.called)
     self.assertEquals(3, mock_open().write.call_count)
Пример #12
0
 def test_cache_file_object(self, mock_start, mock_open):
     cache = Cache('test')
     handle = open('handle', 'w')
     fileobj = ['line1', 'line2', 'line3']
     cache._cache_file_contents(handle, fileobj)
     self.assertEquals(3, mock_open().write.call_count)
Пример #13
0
 def test_read_cached_object(self, mock_load, mock_index):
     cache = Cache('test')
     cache._read_cached_object('cached_object')
     self.assertTrue(mock_load.called)
Пример #14
0
 def test_cache_object(self, mock_dump, mock_index):
     cache = Cache('test')
     cache._cache_object('file', 'value')
     self.assertTrue(mock_dump.called)
Пример #15
0
 def test_remove(self, mock_flush, mock_remove_data, mock_datetime, mock_index):
     self.build_mock_index_and_datetime_now(mock_index, mock_datetime)
     cache = Cache('test', cache_max_age=1)
     cache.remove('valid_key_past')
     self.assertEquals(2, mock_flush.call_count)
     self.assertEquals(2, mock_remove_data.call_count)