From e37652f955c499547a645f6e6563571c5f7e7c5e Mon Sep 17 00:00:00 2001 From: Jirka Date: Fri, 2 Feb 2024 00:15:24 +0100 Subject: [PATCH 1/4] use `pytest.param` instead of copy whole test --- tests/test_defaults.py | 1 - tests/test_general.py | 6 ++--- tests/test_mongo_core.py | 56 +++------------------------------------- 3 files changed, 5 insertions(+), 58 deletions(-) diff --git a/tests/test_defaults.py b/tests/test_defaults.py index eec67ef3..68674440 100644 --- a/tests/test_defaults.py +++ b/tests/test_defaults.py @@ -67,7 +67,6 @@ def global_test_2(): assert global_test_2.cache_dpath() is not None -@pytest.mark.mongo def test_mongetter_default_param(): cachier.set_default_params(mongetter=_test_mongetter) diff --git a/tests/test_general.py b/tests/test_general.py index f0d110ce..0ae4e087 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -154,8 +154,7 @@ def _calls_wait_for_calc_timeout_slow(res_queue): @pytest.mark.parametrize( 'mongetter,backend', [ - # (_test_mongetter, 'mongo'), # Please do NOT test the mongo backend - # here. It is tested in test_mongo_core.py + pytest.param(_test_mongetter, 'mongo', marks=pytest.mark.mongo) (None, 'memory'), (None, 'pickle'), ] @@ -183,8 +182,7 @@ def func(arg_1, arg_2): @pytest.mark.parametrize( 'mongetter,backend', [ - # (_test_mongetter, 'mongo'), # Please do NOT test the mongo backend - # here. It is tested in test_mongo_core.py + pytest.param(_test_mongetter, 'mongo', marks=pytest.mark.mongo), (None, 'memory'), (None, 'pickle'), ] diff --git a/tests/test_mongo_core.py b/tests/test_mongo_core.py index 2174d734..a6458355 100644 --- a/tests/test_mongo_core.py +++ b/tests/test_mongo_core.py @@ -8,7 +8,7 @@ import threading from datetime import timedelta from random import random -from time import sleep, time +from time import sleep from urllib.parse import quote_plus from birch import Birch # type: ignore[import-not-found] @@ -25,6 +25,8 @@ # === Enables testing vs a real MongoDB instance === +CFG = Birch("cachier") + class CfgKey(): HOST = "TEST_HOST" @@ -34,14 +36,6 @@ class CfgKey(): TEST_VS_LIVE_MONGO = "TEST_VS_LIVE_MONGO" -CFG = Birch( - namespace="cachier", - defaults={ - CfgKey.TEST_VS_LIVE_MONGO: False, - }, -) - - URI_TEMPLATE = ( "mongodb+srv://{uname}:{pwd}@{host}/{db}?retrywrites=true&w=majority") @@ -120,50 +114,6 @@ def _test_mongo_caching(arg_1, arg_2): assert val6 == val5 -@pytest.mark.mongo -def test_mongo_precache_value(): - - @cachier(backend='mongo', mongetter=_test_mongetter) - def func(arg_1, arg_2): - """Some function.""" - return arg_1 + arg_2 - - result = func.precache_value(2, 2, value_to_cache=5) - assert result == 5 - result = func(2, 2) - assert result == 5 - func.clear_cache() - result = func(2, 2) - assert result == 4 - result = func.precache_value(2, arg_2=2, value_to_cache=5) - assert result == 5 - result = func(2, arg_2=2) - assert result == 5 - - -@pytest.mark.mongo -def test_mongo_ignore_self_in_methods(): - - class TestClass(): - @cachier(backend='mongo', mongetter=_test_mongetter) - def takes_2_seconds(self, arg_1, arg_2): - """Some function.""" - sleep(2) - return arg_1 + arg_2 - - test_object_1 = TestClass() - test_object_2 = TestClass() - test_object_1.takes_2_seconds.clear_cache() - test_object_2.takes_2_seconds.clear_cache() - result_1 = test_object_1.takes_2_seconds(1, 2) - assert result_1 == 3 - start = time() - result_2 = test_object_2.takes_2_seconds(1, 2) - end = time() - assert result_2 == 3 - assert end - start < 1 - - @pytest.mark.mongo def test_mongo_core_keywords(): """Basic Mongo core functionality with keyword arguments.""" From e81e0355dcbddcd78a6b2669cf2dc39765f82aed Mon Sep 17 00:00:00 2001 From: Jirka Date: Fri, 2 Feb 2024 00:19:37 +0100 Subject: [PATCH 2/4] revert --- tests/test_defaults.py | 1 + tests/test_mongo_core.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/test_defaults.py b/tests/test_defaults.py index 68674440..eec67ef3 100644 --- a/tests/test_defaults.py +++ b/tests/test_defaults.py @@ -67,6 +67,7 @@ def global_test_2(): assert global_test_2.cache_dpath() is not None +@pytest.mark.mongo def test_mongetter_default_param(): cachier.set_default_params(mongetter=_test_mongetter) diff --git a/tests/test_mongo_core.py b/tests/test_mongo_core.py index a6458355..4d9db84e 100644 --- a/tests/test_mongo_core.py +++ b/tests/test_mongo_core.py @@ -25,8 +25,6 @@ # === Enables testing vs a real MongoDB instance === -CFG = Birch("cachier") - class CfgKey(): HOST = "TEST_HOST" @@ -36,6 +34,14 @@ class CfgKey(): TEST_VS_LIVE_MONGO = "TEST_VS_LIVE_MONGO" +CFG = Birch( + namespace="cachier", + defaults={ + CfgKey.TEST_VS_LIVE_MONGO: False, + }, +) + + URI_TEMPLATE = ( "mongodb+srv://{uname}:{pwd}@{host}/{db}?retrywrites=true&w=majority") From 57ecc796ad3a658d2288f7e4aabf3c7c6b71d7cb Mon Sep 17 00:00:00 2001 From: Jirka Date: Fri, 2 Feb 2024 00:25:18 +0100 Subject: [PATCH 3/4] typo --- tests/test_general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_general.py b/tests/test_general.py index 0ae4e087..6ddf8510 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -154,7 +154,7 @@ def _calls_wait_for_calc_timeout_slow(res_queue): @pytest.mark.parametrize( 'mongetter,backend', [ - pytest.param(_test_mongetter, 'mongo', marks=pytest.mark.mongo) + pytest.param(_test_mongetter, 'mongo', marks=pytest.mark.mongo), (None, 'memory'), (None, 'pickle'), ] From 84b753c1b1e1ae99a40acc9afba59be34ddcc173 Mon Sep 17 00:00:00 2001 From: Jirka Date: Fri, 2 Feb 2024 10:43:24 +0100 Subject: [PATCH 4/4] color --- pytest.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/pytest.ini b/pytest.ini index 7b21133f..a0c7e9b8 100644 --- a/pytest.ini +++ b/pytest.ini @@ -8,6 +8,7 @@ markers = memory: test the memory core pickle: test the pickle core addopts = + --color=yes # --doctest-modules --cov=cachier --cov-report term