Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Test helpers to temporarily alter zope.component registrations

Project Description

This packages allows you to replace, add or delete zope.component registrations of utilities, adapters and event handlers, and to later on undo the changes you made.

Usage

To change something in a registry, instantiate gocept.zcapatch.Patches for that registry (if no registry is given, the current one as returned by zope.component.getSiteManager() is used). It offers the following methods:

patch_utility(new, provided=None, name=u’‘, registry=None):
 

Register the given new object as a utility, instead of what is currently registered for this interface (and name).

If no provided interface is given, it is read from the object’s zope.interface.implements declaration.

If you want to delete a utility registration, pass new=None.

patch_adapter(factory, required=None, provided=None, name=u’‘, registry=None):
 

Register the given factory as an adapter, instead of what is currently registered for these required and provided interfaces (and name).

Any of the required or provided interfaces not given will be read from the object’s zope.component.adapts and zope.interface.implements declarations.

If you want to delete an adapter registration, pass factory=lambda *args: None.

patch_handler(handler, required, registry=None):
 

Register the given handler instead of what is currently registered for the required interfaces.

If no required interfaces are given, they are read from the object’s zope.component.adapts declaration.

If you want to delete a handler registration, use remove_handler.

remove_handler(handler, required=None, registry=None):
 

Delete the specified registration of handler. (Since handlers are set-valued, you cannot delete one without explicitly specifying which one you mean.)

If no required interfaces are given, they are read from the object’s zope.component.adapts declaration.

reset:

Undo all changes made via this Patches instance: Unregister all utilities, adapters and handlers added, and restore any previously present registrations that were replaced or removed.

Here’s a typical usage example:

import gocept.zcapatch
import unittest


class TestCase(unittest.TestCase):

    def setUp(self):
        self.zca = gocept.zcapatch.Patches()

    def tearDown(self):
        self.zca.reset()

    def test_something(self):
        self.zca.patch_utility(...)

Alternatives

plone.testing provides the ability to create a stack of registries, where each delegates to the previous one (plone.testing.zca.pushGlobalRegistry / popGlobalRegistry) – so you could, for example, push a new registry for each test, manipulate that at will, and throw it away afterwards. While this approach is much more elegant than the one provided by gocept.zcapatch for common use cases, do note the following limitations:

  • plone.testing only ever deals with zope.component.getGlobalSiteManager(), so you can’t stack other registries (e.g. LocalSiteManagers as implemented by zope.site).
  • plone.testing does not allow you to (temporarily) delete registrations from the registry.

Development

The mercurial repository of the source code as well as the issue tracker are available at <https://bitbucket.org/gocept/gocept.zcapatch>.

Changelog

1.0 (2011-12-01)

  • first release.
Release History

Release History

This version
History Node

1.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
gocept.zcapatch-1.0.tar.gz (5.7 kB) Copy SHA256 Checksum SHA256 Source Dec 1, 2011

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting