collective.redismemoize.testing#

Test layers and helpers for add-ons.

Test layers, and helpers to test an add-on against a fake Redis.

REDIS_MEMOIZE_FIXTURE is for add-on authors: put it in a layer's bases and every @ram.cache function goes through this package, backed by an in-process fakeredis server instead of a real Redis. Each test starts with an empty prefix, an empty RAM cache, a closed breaker and no learned routes. fakeredis is not a dependency of this package; add it to your test extra.

activate() and deactivate() are the same thing for a test runner without layers, such as a plain pytest fixture.

collective.redismemoize.testing.TEST_ENVIRON = {'REDIS_MEMOIZE_PREFIX': 'test.memoize', 'REDIS_MEMOIZE_URL': 'redis://fakeredis:6379/0'}#

What activate() sets unless told otherwise. The URL is never contacted: the client factory is replaced by fakeredis.

collective.redismemoize.testing.activate(server=None, **variables)[source]#

Point the package at a fakeredis server.

Parameters:
  • server (Any) -- A fakeredis.FakeServer; a new one when omitted.

  • variables (str) -- REDIS_MEMOIZE_* variables to set on top of TEST_ENVIRON.

Returns:

The server.

Return type:

Any

collective.redismemoize.testing.deactivate()[source]#

Restore the environment activate() changed, and reset the state.

Return type:

None

collective.redismemoize.testing.clean()[source]#

Empty the prefix and the RAM cache, close the breaker, forget routes.

Return type:

None

class collective.redismemoize.testing.RedisMemoizeLayer[source]#

Bases: Layer

Route plone.memoize through this package and a fake Redis.

Loads the package's overrides.zcml into a stacked component registry, so the chooser is in place even in a layer that never loaded this package's ZCML.

defaultBases = (plone.testing.zca.ZCML_DIRECTIVES,)#
setUp()[source]#

Register the chooser and activate the fake server.

Return type:

None

tearDown()[source]#

Deactivate the fake server and drop the stacked registry.

Return type:

None

testSetUp()[source]#

Start every test from an empty cache, with this package's chooser.

The registry stacked in setUp() is not always the one a test sees. A Plone site's component registry is based on the global registry of the moment the site was created, so for a layer such as PLONE_FIXTURE, set up before or after this one, the stock chooser plone.memoize registered there wins. For the duration of each test, every registry in the global chain that registers another chooser gets this package's one instead.

Return type:

None

testTearDown()[source]#

Put back the choosers testSetUp() replaced.

Return type:

None

class collective.redismemoize.testing.PackageLayer[source]#

Bases: PloneSandboxLayer

The package's ZCML, overrides included, on a Plone site.

defaultBases = (plone.app.contenttypes.testing.PLONE_APP_CONTENTTYPES_FIXTURE,)#
setUpZope(app, configurationContext)[source]#

Load the package's ZCML.

plone.autoinclude is disabled in the Plone fixture, so the overrides are loaded here the way a real startup would load them.