From 8c475504865edad7a9e70be666d3be43a2fdc755 Mon Sep 17 00:00:00 2001 From: Shay Palachy Date: Tue, 29 Jan 2019 12:53:17 +0200 Subject: [PATCH] cleaned up test code, added pymongo to test dependencies --- .gitignore | 1 + Pipfile | 11 +++++++++ Pipfile.lock | 20 +++++++++++++++++ setup.py | 5 +++-- tests/conftest.py | 4 ++-- tests/speed_eval.py | 1 + tests/test_general.py | 2 -- tests/test_mongo_core.py | 47 ++++++++++++++++++++++++++++----------- tests/test_pickle_core.py | 25 ++++++++++++++------- 9 files changed, 89 insertions(+), 27 deletions(-) create mode 100644 Pipfile create mode 100644 Pipfile.lock diff --git a/.gitignore b/.gitignore index 92d59a6..e96b655 100644 --- a/.gitignore +++ b/.gitignore @@ -79,6 +79,7 @@ celerybeat-schedule .env # virtualenv +.venv/ venv/ ENV/ diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..7a9e19a --- /dev/null +++ b/Pipfile @@ -0,0 +1,11 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] + +[dev-packages] + +[requires] +python_version = "3.6" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..419c7ab --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,20 @@ +{ + "_meta": { + "hash": { + "sha256": "415dfdcb118dd9bdfef17671cb7dcd78dbd69b6ae7d4f39e8b44e71d60ca72e7" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.6" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": {}, + "develop": {} +} diff --git a/setup.py b/setup.py index cac0abe..b32d1b7 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ except ImportError: import versioneer -TEST_REQUIRES = ['pytest', 'coverage', 'pytest-cov'] +TEST_REQUIRES = ['pytest', 'coverage', 'pytest-cov', 'pymongo'] README_RST = '' with open('README.rst') as f: @@ -26,7 +26,8 @@ setup( name='cachier', version=versioneer.get_version(), cmdclass=versioneer.get_cmdclass(), - description='Persistent, stale-free, local and cross-machine caching for Python functions.', + description=('Persistent, stale-free, local and cross-machine caching for' + ' Python functions.'), long_description=README_RST, license='MIT', author='Shay Palachy', diff --git a/tests/conftest.py b/tests/conftest.py index 22085fa..7daf1d7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,10 +1,10 @@ """Configuration file for pytest.""" import pytest -import shutil +# import shutil from .test_mongo_core import _test_mongetter -from cachier.pickle_core import EXPANDED_CACHIER_DIR +# from cachier.pickle_core import EXPANDED_CACHIER_DIR def mongo_finalizer(): diff --git a/tests/speed_eval.py b/tests/speed_eval.py index dced773..f1f9238 100644 --- a/tests/speed_eval.py +++ b/tests/speed_eval.py @@ -38,5 +38,6 @@ def test_pickle_speed(): print(' - Decorated average = {:.8f}'.format( sum(times) / num_of_vals)) + if __name__ == '__main__': test_pickle_speed() diff --git a/tests/test_general.py b/tests/test_general.py index 8b07646..9105db0 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -30,5 +30,3 @@ def test_get_executor(): def test_set_max_workers(): """Just call this function for coverage.""" _set_max_workers(9) - - diff --git a/tests/test_mongo_core.py b/tests/test_mongo_core.py index b2585fe..fc68852 100644 --- a/tests/test_mongo_core.py +++ b/tests/test_mongo_core.py @@ -8,7 +8,7 @@ from time import sleep import threading try: import queue -except ImportError: # python 2 +except ImportError: # python 2 import Queue as queue import pytest @@ -37,11 +37,12 @@ def _get_cachier_db_mongo_client(): _COLLECTION_NAME = 'cachier_test{}.{}.{}'.format( sys.version_info[0], sys.version_info[1], sys.version_info[2]) + def _test_mongetter(): if not hasattr(_test_mongetter, 'client'): _test_mongetter.client = _get_cachier_db_mongo_client() db_obj = _test_mongetter.client['cachier_test'] - if not _COLLECTION_NAME in db_obj.collection_names(): + if _COLLECTION_NAME not in db_obj.list_collection_names(): db_obj.create_collection(_COLLECTION_NAME) return db_obj[_COLLECTION_NAME] @@ -53,6 +54,7 @@ def _test_mongo_caching(arg_1, arg_2): """Some function.""" return random() + arg_1 + arg_2 + def test_mongo_index_creation(): """Basic Mongo core functionality.""" collection = _test_mongetter() @@ -63,7 +65,6 @@ def test_mongo_index_creation(): assert _MongoCore._INDEX_NAME in collection.index_information() - def test_mongo_core(): """Basic Mongo core functionality.""" _test_mongo_caching.clear_cache() @@ -82,11 +83,13 @@ def test_mongo_core(): MONGO_DELTA = timedelta(seconds=3) + @cachier(mongetter=_test_mongetter, stale_after=MONGO_DELTA, next_time=False) def _stale_after_mongo(arg_1, arg_2): """Some function.""" return random() + arg_1 + arg_2 + def test_mongo_stale_after(): """Testing MongoDB core stale_after functionality.""" _stale_after_mongo.clear_cache() @@ -104,10 +107,12 @@ def _takes_time(arg_1, arg_2): sleep(3) return random() + arg_1 + arg_2 + def _calls_takes_time(res_queue): res = _takes_time(34, 82.3) res_queue.put(res) + def test_mongo_being_calculated(): """Testing MongoDB core handling of being calculated scenarios.""" _takes_time.clear_cache() @@ -144,14 +149,17 @@ class _BadMongoCollection: def update_one(self, *args, **kwargs): raise OperationFailure(Exception()) + def _bad_mongetter(): return _BadMongoCollection(_test_mongetter) + @cachier(mongetter=_bad_mongetter) def _func_w_bad_mongo(arg_1, arg_2): """Some function.""" return random() + arg_1 + arg_2 + def test_mongo_write_failure(): """Testing MongoDB core handling of writing failure scenarios.""" with pytest.raises(OperationFailure): @@ -175,13 +183,21 @@ def test_stalled_mongo_db_cache(): with pytest.raises(RecalculationNeeded): core.wait_on_entry_calc(key=None) + def test_stalled_mong_db_core(monkeypatch): + def mock_get_entry(self, args, kwargs): return "key", {'being_calculated': True} + def mock_get_entry_by_key(self, key): return "key", None - monkeypatch.setattr("cachier.mongo_core._MongoCore.get_entry", mock_get_entry ) - monkeypatch.setattr("cachier.mongo_core._MongoCore.get_entry_by_key", mock_get_entry_by_key ) + + monkeypatch.setattr( + "cachier.mongo_core._MongoCore.get_entry", mock_get_entry) + monkeypatch.setattr( + "cachier.mongo_core._MongoCore.get_entry_by_key", mock_get_entry_by_key + ) + @cachier(mongetter=_test_mongetter) def _stalled_func(): return 1 @@ -189,17 +205,22 @@ def test_stalled_mong_db_core(monkeypatch): assert res == 1 def mock_get_entry_2(self, args, kwargs): - return "key", {'being_calculated': True, - "value": 1, - "time": datetime.datetime.now() - datetime.timedelta(seconds=10)} - monkeypatch.setattr("cachier.mongo_core._MongoCore.get_entry", mock_get_entry_2 ) + entry = { + 'being_calculated': True, + "value": 1, + "time": datetime.datetime.now() - datetime.timedelta(seconds=10) + } + return "key", entry + + monkeypatch.setattr( + "cachier.mongo_core._MongoCore.get_entry", mock_get_entry_2) stale_after = datetime.timedelta(seconds=1) - @cachier(mongetter=_test_mongetter,stale_after=stale_after) + + @cachier(mongetter=_test_mongetter, stale_after=stale_after) def _stalled_func_2(): - """ Testing stalled function""" + """Testing stalled function""" return 2 + res = _stalled_func_2() assert res == 2 - - diff --git a/tests/test_pickle_core.py b/tests/test_pickle_core.py index f7e0d8e..6fdee6b 100644 --- a/tests/test_pickle_core.py +++ b/tests/test_pickle_core.py @@ -21,7 +21,7 @@ from random import random import threading try: import queue -except ImportError: # python 2 +except ImportError: # python 2 import Queue as queue from cachier import cachier @@ -40,9 +40,9 @@ def _takes_5_seconds(arg_1, arg_2): def test_pickle_core(): """Basic Pickle core functionality.""" _takes_5_seconds.clear_cache() - stringi = _takes_5_seconds('a', 'b') + _takes_5_seconds('a', 'b') start = time() - stringi = _takes_5_seconds('a', 'b', verbose_cache=True) + _takes_5_seconds('a', 'b', verbose_cache=True) end = time() assert end - start < 1 _takes_5_seconds.clear_cache() @@ -50,6 +50,7 @@ def test_pickle_core(): DELTA = timedelta(seconds=3) + @cachier(stale_after=DELTA, next_time=False) def _stale_after_seconds(arg_1, arg_2): """Some function.""" @@ -92,7 +93,6 @@ def test_stale_after_next_time(): _stale_after_next_time.clear_cache() - @cachier() def _random_num(): return random() @@ -151,16 +151,19 @@ def test_ignore_cache(): assert int4 == int1 _random_num_with_arg.clear_cache() + @cachier() def _takes_time(arg_1, arg_2): """Some function.""" sleep(2) # this has to be enough time for check_calculation to run twice return random() + arg_1 + arg_2 + def _calls_takes_time(res_queue): res = _takes_time(0.13, 0.02) res_queue.put(res) + def test_pickle_being_calculated(): """Testing pickle core handling of being calculated scenarios.""" _takes_time.clear_cache() @@ -186,10 +189,12 @@ def _being_calc_next_time(arg_1, arg_2): sleep(1) return random() + arg_1 + arg_2 + def _calls_being_calc_next_time(res_queue): res = _being_calc_next_time(0.13, 0.02) res_queue.put(res) + def test_being_calc_next_time(): """Testing pickle core handling of being calculated scenarios.""" _takes_time.clear_cache() @@ -217,10 +222,12 @@ def _bad_cache(arg_1, arg_2): sleep(1) return random() + arg_1 + arg_2 + # _BAD_CACHE_FNAME = '.__main__._bad_cache' _BAD_CACHE_FNAME = '.tests.test_pickle_core._bad_cache' _BAD_CACHE_FPATH = os.path.join(EXPANDED_CACHIER_DIR, _BAD_CACHE_FNAME) + def _calls_bad_cache(res_queue, trash_cache): try: res = _bad_cache(0.13, 0.02) @@ -232,6 +239,7 @@ def _calls_bad_cache(res_queue, trash_cache): except Exception as exc: res_queue.put(exc) + def test_bad_cache_file(): """Test pickle core handling of bad cache files.""" _bad_cache.clear_cache() @@ -260,10 +268,12 @@ def _delete_cache(arg_1, arg_2): sleep(1) return random() + arg_1 + arg_2 + # _DEL_CACHE_FNAME = '.__main__._delete_cache' _DEL_CACHE_FNAME = '.tests.test_pickle_core._delete_cache' _DEL_CACHE_FPATH = os.path.join(EXPANDED_CACHIER_DIR, _DEL_CACHE_FNAME) + def _calls_delete_cache(res_queue, del_cache): try: # print('in') @@ -278,6 +288,7 @@ def _calls_delete_cache(res_queue, del_cache): # print('found') res_queue.put(exc) + def test_delete_cache_file(): """Test pickle core handling of missing cache files.""" _delete_cache.clear_cache() @@ -302,10 +313,12 @@ def test_delete_cache_file(): # print(res2) # print(type(res2)) + def test_clear_being_calculated(): """Test pickle core clear `being calculated` functionality.""" _takes_time.clear_being_calculated() + @cachier(stale_after=timedelta(seconds=1), next_time=True) def _error_throwing_func(arg1): if not hasattr(_error_throwing_func, 'count'): @@ -322,7 +335,3 @@ def test_error_throwing_func(): sleep(1.5) res2 = _error_throwing_func(4) assert res1 == res2 - - -if __name__ == '__main__': - test_mongo_being_calculated()