Question

I recently switched from beaker to dogpile.cache. It works very well in live code but I am running into an issue with testing. How do I disable the caching for testing?

I am currently using

#caching.py
from dogpile.cache import make_region

region = make_region().configure(
    'dogpile.cache.redis',
    expiration_time = 3600,
    arguments = {
        'host': '127.0.0.1',
        'port': 6379
    }
)

#db.py
from .caching import region

@region.cache_on_arguments()
def fetch_from_db(item):
    return some_database.lookup(item)

How do I swap out the caching or disable it for unittests?

No correct solution

OTHER TIPS

During testing configure dogpile to use a NullBackend, which is an example of the Null Object design pattern.

from dogpile.cache import make_region

region = make_region().configure(
    'dogpile.cache.null'
)

Redefine your decorator into an identity function.

I.e.

if __debug__:
    def dont_cache():
        def noop(f):
            return f
        return noop
    class Nothing:
        pass
    region = Nothing()
    region.cache_on_arguments = dont_cache
else:
    from .caching import region
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top