1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
# -*- coding: utf-8 -*-
"""Tests for caching."""
import os
import tempfile
import unittest
from pathlib import Path
from pystow.cache import CachedPickle
EXPECTED = 5
EXPECTED_2 = 6
class TestCache(unittest.TestCase):
"""Tests for caches."""
def setUp(self) -> None:
"""Set up the test case with a temporary directory."""
self.tmpdir = tempfile.TemporaryDirectory()
self.directory = Path(self.tmpdir.name)
def tearDown(self) -> None:
"""Tear down the test case's temporary directory."""
self.tmpdir.cleanup()
def test_cache_exception(self):
"""Test that exceptions aren't swallowed."""
path = self.directory.joinpath("test.pkl")
self.assertFalse(path.is_file())
@CachedPickle(path=path)
def _f1():
raise NotImplementedError
self.assertFalse(path.is_file(), msg="function has not been called")
with self.assertRaises(NotImplementedError):
_f1()
self.assertFalse(
path.is_file(),
msg="file should not have been created if an exception was thrown by the function",
)
def test_cache_pickle(self):
"""Test caching a pickle."""
path = self.directory.joinpath("test.pkl")
self.assertFalse(
path.is_file(),
msg="the file should not exist at the beginning of the test",
)
raise_flag = True
@CachedPickle(path=path)
def _f1():
if raise_flag:
raise ValueError
return EXPECTED
self.assertFalse(path.is_file(), msg="the file should not exist until function is called")
with self.assertRaises(ValueError):
_f1()
self.assertFalse(
path.is_file(),
msg="the function should throw an exception because of the flag, and no file should be created",
)
raise_flag = False
actual = _f1()
self.assertEqual(EXPECTED, actual)
self.assertTrue(path.is_file(), msg="a file should have been created")
raise_flag = True
actual_2 = _f1() # if raises, the caching mechanism didn't work
self.assertEqual(EXPECTED, actual_2)
self.assertTrue(path.is_file())
os.unlink(path)
self.assertFalse(path.is_file())
with self.assertRaises(ValueError):
_f1()
@CachedPickle(path=path, force=True)
def _f2():
return EXPECTED_2
self.assertEqual(EXPECTED_2, _f2()) # overwrites the file
self.assertEqual(EXPECTED_2, _f1())
|