From 040e981b2ea136735efa1354dc7df1d00d7463c4 Mon Sep 17 00:00:00 2001 From: Jirka Borovec <6035284+Borda@users.noreply.github.com> Date: Fri, 25 Oct 2024 18:02:24 +0200 Subject: [PATCH] fix: crash with `None` & confusing tests (#248) * ci/pytest: Treating warning as error * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix None * refactor tests * fix code * testing * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * names --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- pyproject.toml | 1 + src/cachier/cores/pickle.py | 2 +- tests/test_pickle_core.py | 42 ++++++++++++++++++++----------------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f653dec..e41367f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -148,6 +148,7 @@ addopts = [ "-r a", "-v", "-s", + "-W error", ] markers = [ "mongo: test the MongoDB core", diff --git a/src/cachier/cores/pickle.py b/src/cachier/cores/pickle.py index 4b0d269..f35be67 100644 --- a/src/cachier/cores/pickle.py +++ b/src/cachier/cores/pickle.py @@ -54,7 +54,7 @@ def _check_calculation(self) -> None: self.observer.stop() # else: # print('NOT stopping observer... :(') - except TypeError: + except AttributeError: # catching entry being None self.value = None self.observer.stop() diff --git a/tests/test_pickle_core.py b/tests/test_pickle_core.py index 7c7b2f5..07205b5 100644 --- a/tests/test_pickle_core.py +++ b/tests/test_pickle_core.py @@ -352,7 +352,7 @@ def _calls_bad_cache(bad_cache_func, res_queue, trash_cache, separate_files): res_queue.put(exc) -def _helper_bad_cache_file(sleeptime, separate_files): +def _helper_bad_cache_file(sleep_time: float, separate_files: bool): """Test pickle core handling of bad cache files.""" _bad_cache_decorated = _get_decorated_func( _bad_cache, separate_files=separate_files @@ -380,7 +380,7 @@ def _helper_bad_cache_file(sleeptime, separate_files): daemon=True, ) thread1.start() - sleep(sleeptime) + sleep(sleep_time) thread2.start() thread1.join(timeout=2) thread2.join(timeout=2) @@ -395,16 +395,17 @@ def _helper_bad_cache_file(sleeptime, separate_files): # we want this to succeed at least once @pytest.mark.pickle -@pytest.mark.xfail @pytest.mark.parametrize("separate_files", [True, False]) def test_bad_cache_file(separate_files): """Test pickle core handling of bad cache files.""" - sleeptimes = [0.1, 0.2, 0.3, 0.5, 0.6, 0.7, 0.8, 1, 1.5, 2] - sleeptimes = sleeptimes + sleeptimes - for sleeptime in sleeptimes: - if _helper_bad_cache_file(sleeptime, separate_files): - return - raise AssertionError() + sleep_times = [0.1, 0.2, 0.3, 0.5, 0.6, 0.7, 0.8, 1, 1.5, 2] + bad_file = False + for sleep_time in sleep_times * 2: + if _helper_bad_cache_file(sleep_time, separate_files): + bad_file = True + break + # it is expected that for separate_files=True files will not be bad + assert bad_file is not separate_files def _delete_cache(arg_1, arg_2): @@ -429,7 +430,9 @@ def _delete_cache(arg_1, arg_2): } -def _calls_delete_cache(del_cache_func, res_queue, del_cache, separate_files): +def _calls_delete_cache( + del_cache_func, res_queue, del_cache: bool, separate_files: bool +): try: # print('in') res = del_cache_func(0.13, 0.02) @@ -443,7 +446,7 @@ def _calls_delete_cache(del_cache_func, res_queue, del_cache, separate_files): res_queue.put(exc) -def _helper_delete_cache_file(sleeptime, separate_files): +def _helper_delete_cache_file(sleep_time: float, separate_files: bool): """Test pickle core handling of missing cache files.""" _delete_cache_decorated = _get_decorated_func( _delete_cache, separate_files=separate_files @@ -471,7 +474,7 @@ def _helper_delete_cache_file(sleeptime, separate_files): daemon=True, ) thread1.start() - sleep(sleeptime) + sleep(sleep_time) thread2.start() thread1.join(timeout=2) thread2.join(timeout=2) @@ -486,16 +489,17 @@ def _helper_delete_cache_file(sleeptime, separate_files): @pytest.mark.pickle -@pytest.mark.xfail @pytest.mark.parametrize("separate_files", [False, True]) def test_delete_cache_file(separate_files): """Test pickle core handling of missing cache files.""" - sleeptimes = [0.1, 0.2, 0.3, 0.5, 0.7, 1] - sleeptimes = sleeptimes * 4 - for sleeptime in sleeptimes: - if _helper_delete_cache_file(sleeptime, separate_files): - return - raise AssertionError() + sleep_times = [0.1, 0.2, 0.3, 0.5, 0.7, 1] + deleted = False + for sleep_time in sleep_times * 4: + if _helper_delete_cache_file(sleep_time, separate_files): + deleted = True + break + # it is expected that for separate_files=True files will not be deleted + assert deleted is not separate_files @pytest.mark.pickle