def test_new_file(self): def create(path): with open(path, 'w') as f: f.write('test') creator = mock.Mock() creator.side_effect = create loader = mock.Mock() dir_path = tempfile.mkdtemp() # This file always does not exists as the directory is new. path = os.path.join(dir_path, 'cahche') try: dataset.cache_or_load_file(path, creator, loader) self.assertEqual(creator.call_count, 1) self.assertFalse(loader.called) self.assertTrue(os.path.exists(path)) with open(path) as f: self.assertEqual(f.read(), 'test') finally: shutil.rmtree(dir_path)
def test_file_exists(self): creator = mock.Mock() loader = mock.Mock() # This file always does not exists as the directory is new. path = os.path.join(self.dir_path, 'cahche') with self.assertRaises(RuntimeError): dataset.cache_or_load_file(path, creator, loader)
def test_cache_exists(self): creator = mock.Mock() loader = mock.Mock() file_desc, file_name = tempfile.mkstemp() try: dataset.cache_or_load_file(file_name, creator, loader) finally: os.close(file_desc) os.remove(file_name) self.assertFalse(creator.called) loader.assert_called_once_with(file_name)