def test_init(self, mkdir_function, isdir_function): c = FileCache(TestFileCache.DIR) # Ensure path has been set self.assertEqual(c.path, TestFileCache.DIR) # Ensure we checked if the dir was already there args, kwargs = isdir_function.call_args self.assertEqual((TestFileCache.DIR,), args) # Ensure we called mkdir with the right args args, kwargs = mkdir_function.call_args self.assertEqual((TestFileCache.DIR, 0o700), args)
class TestFileCache(unittest.TestCase): ''' Class for testing the filecache TODO: Debug wth this test is creating an SSL connection ''' DIR = '/tmp/TestFileCache' @mock.patch('os.path.isdir') @mock.patch('os.mkdir') @mock.patch('{0}.open'.format(builtins_name)) def setUp(self, open_function, mkdir_function, isdir_function): self.c = FileCache(TestFileCache.DIR) self.c.put('key', 'value') @mock.patch('os.path.isdir', return_value=False) @mock.patch('os.mkdir') def test_init(self, mkdir_function, isdir_function): c = FileCache(TestFileCache.DIR) # Ensure path has been set self.assertEqual(c.path, TestFileCache.DIR) # Ensure we checked if the dir was already there args, kwargs = isdir_function.call_args self.assertEqual((TestFileCache.DIR,), args) # Ensure we called mkdir with the right args args, kwargs = mkdir_function.call_args self.assertEqual((TestFileCache.DIR, 0o700), args) # @unittest.skip("https://github.com/pycrest/PyCrest/issues/30") # def test_getpath(self): # self.assertEqual(self.c._getpath('key'), # os.path.join(TestFileCache.DIR, # '1140801208126482496.cache')) def test_get_uncached(self): # Check non-existant key self.assertIsNone(self.c.get('nope')) @mock.patch('builtins.open') def test_get_cached(self, open_function): self.assertEqual(self.c.get('key'), 'value') @unittest.skipIf( sys.version_info < ( 3,), 'Python 2.x uses a diffrent protocol') @mock.patch('{0}.open'.format(builtins_name), mock.mock_open( read_data=b'x\x9ck`\ne-K\xcc)M-d\xd0\x03\x00\x17\xde\x03\x99')) def test_get_cached_file_py3(self): del(self.c._cache['key']) self.assertEqual(self.c.get('key'), 'value') @unittest.skipIf( sys.version_info > ( 3,), 'Python 3.x uses a diffrent protocol') @mock.patch('{0}.open'.format(builtins_name), mock.mock_open( read_data='x\x9ck`\ne-K\xcc)M-d\xd0\x03\x00\x17\xde\x03\x99')) def test_get_cached_file_py2(self): del(self.c._cache['key']) self.assertEqual(self.c.get('key'), 'value') @mock.patch('os.unlink') def test_invalidate(self, unlink_function): # Make sure our key is here in the first place self.assertIn('key', self.c._cache) # Unset the key and ensure unlink() was called self.c.invalidate('key') self.assertTrue(unlink_function.called) # TODO: When paths are predictable check the args # See https://github.com/pycrest/PyCrest/issues/30 @mock.patch( 'os.unlink', side_effect=OSError( errno.ENOENT, 'No such file or directory')) def test_unlink_exception(self, unlink_function): self.assertIsNone(self.c.invalidate('key'))
def setUp(self, open_function, mkdir_function, isdir_function): self.c = FileCache(TestFileCache.DIR) self.c.put('key', 'value')
def test_file_cache(self, mkdir_function, isdir_function): file_cache = FileCache(path=TestFileCache.DIR) eve = EVE(cache=file_cache) self.assertEqual(file_cache.path, TestFileCache.DIR) self.assertTrue(isinstance(eve.cache, FileCache))