diff --git a/cachier/mongo_core.py b/cachier/mongo_core.py index a1aaa20..2d2077d 100644 --- a/cachier/mongo_core.py +++ b/cachier/mongo_core.py @@ -7,9 +7,11 @@ # http://www.opensource.org/licenses/MIT-license # Copyright (c) 2016, Shay Palachy +import sys # to make sure that pymongo was imported import pickle # for serialization of python objects from datetime import datetime import time # to sleep when waiting on Mongo cache\ +import warnings # to warn if pymongo is missing try: from pymongo import ( @@ -20,9 +22,6 @@ try: from bson.binary import Binary # to save binary data to mongodb except ImportError: # pragma: no cover pass - # warnings.warn(( - # "Cachier warning: pymongo was not found. " - # "MongoDB cores will not work.")) from .base_core import _BaseCore @@ -39,6 +38,10 @@ class _MongoCore(_BaseCore): _INDEX_NAME = 'func_1_key_1' def __init__(self, mongetter, stale_after, next_time): + if 'pymongo' not in sys.modules: + warnings.warn(( + "Cachier warning: pymongo was not found. " + "MongoDB cores will not function.")) _BaseCore.__init__(self, stale_after, next_time) self.mongetter = mongetter self.mongo_collection = self.mongetter() diff --git a/tests/test_pickle_core.py b/tests/test_pickle_core.py index 6fdee6b..896f31b 100644 --- a/tests/test_pickle_core.py +++ b/tests/test_pickle_core.py @@ -48,7 +48,8 @@ def test_pickle_core(): _takes_5_seconds.clear_cache() -DELTA = timedelta(seconds=3) +SECONDS_IN_DELTA = 3 +DELTA = timedelta(seconds=SECONDS_IN_DELTA) @cachier(stale_after=DELTA, next_time=False) @@ -85,9 +86,10 @@ def test_stale_after_next_time(): val3 = _stale_after_next_time(1, 3) assert val1 == val2 assert val1 != val3 - sleep(3) + sleep(SECONDS_IN_DELTA + 1) val4 = _stale_after_next_time(1, 2) assert val4 == val1 + sleep(0.5) val5 = _stale_after_next_time(1, 2) assert val5 != val1 _stale_after_next_time.clear_cache()