nti.site.testing module

Support for testing code that uses nti.site.

Most of the functionality exposed through this module uses ZODB to test persistence and transaction handling and is based on the support code from nti.testing.zodb.

New in version 2.1.0.

nti.site.testing.setHooks()[source]

Make zope.component.getSiteManager and interface adaptation respect the current site.

Most applications will want to be sure te call this early in their startup sequence. Test code that uses these APIs should also arrange to call this.

nti.site.testing.resetHooks()[source]

Reset zope.component.getSiteManager and interface adaptation to their original implementations that are unaware of the current site.

Use caution when calling this; most code will not need to call this. If code using the global API executes following this, it will most likely use the base global component registry instead of a site-specific registry it was expected. This can lead to failures in adaptation and utility lookup.

nti.site.testing.print_tree(folder, file=sys.stdout, show_unknown=repr, basic_indent=' ', details=('id', 'type', 'len', 'siteManager')) → None[source]

Print a descriptive tree of the contents of the dict-like folder to file.

Pass a subset of details to disable printing certain information when it isn’t relavent.

Changed in version 2.2.0: Add several arguments including show_unknown, details. Print the contents of site managers by default. Fix a bug not passing the basic_indent to recursive calls.

class nti.site.testing.persistent_site_trans(db=None, site_name=None)[source]

Bases: nti.testing.zodb.mock_db_trans

Context manager for a ZODB database connection and active zope.component site (usually) persisted in the database.

Changed in version 2.1.0: While there was no previous public version of this class, there was a private version in nti.site.tests. That version called install_main_application_and_sites() setting the root_alias to the main_application_folder_name. Since older versions of that function installed the root_alias to point to the main_name, this used result in no alias for the root folder in the root. Now, there will be an alias (at DEFAULT_ROOT_ALIAS).

Parameters:
main_application_folder_name = <u'nti.dataserver' is the default alias of main application folder>

The site to make active by default and when looking up a site_name. This must identify an object in the root of the database that provides IMainApplicationFolder.

on_application_and_sites_installed(folder)[source]

Called when the main application and sites have been installed. This may not be called every time an instance of this class is used, as the database may be persistent.

on_connection_opened(conn)[source]

Called when the connection to the DB has been opened.

Subclasses may override to perform initialization. The DB may have been used before, so check its state and don’t assume a complete initialization must happen.

on_main_application_folder_missing(conn)[source]

Called from on_connection_opened() when the main_application_folder_name is not found in the root of the conn.

This method calls install_main_application_and_sites(), passing main_application_folder_name as the main_alias.

class nti.site.testing.SharedConfiguringTestLayer[source]

Bases: nti.testing.zodb.ZODBLayer, nti.testing.layers.GCLayerMixin, nti.testing.layers.ConfiguringLayerMixin

A test layer that configures this package, and sets other useful test settings.

Note that the details of the test settings may change. In this version, this configures the BTreeLocalAdapterRegistry to immediately switch to BTrees instead of dicts.

current_test = None

The test (a unittest.TestCase subclass) currently executing in this layer. If there is no such test, this is None.

classmethod testSetUp(test=None)[source]

Tests that run in this layer have their db property set to the current db of this layer.

classmethod testTearDown()[source]

When a test in this layer is torn down, its db property is set to None, as is this layer’s current_test.

class nti.site.testing.SiteTestCase(methodName='runTest')[source]

Bases: unittest.case.TestCase

A test case that runs in the SharedConfiguringTestLayer.

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

layer

alias of SharedConfiguringTestLayer

nti.site.testing.uses_independent_db_site(db_factory=None, installer_factory=persistent_site_trans, installer_kwargs={}) → function[source]

A decorator or decorator factory. Creates a new database using db_factory, initializes it using installer_factory, and then runs the body of the function in a site and site manager that are disconnected from the database.

If the function is a unittest method, the unittest object’s db attribute will be set to the created db during executing. Likewise, the nti.testing.zodb.ZODBLayer db attribute (and layers that extend from it like SharedConfiguringTestLayer) will be set to this object and returned to the previous value on exit.

This can be called as given in the signature, or can be called with no arguments:

class MyTest(TestCase):

    @uses_independent_db_site
    def test_something(self):
        pass

    @uses_independent_db_site(installer_factory=MyCustomFactory)
    def test_something_else(self):
        pass

The body of the function is free to use persistent_site_trans statements. They will default to using the database established here instead of a database established by a test layer (which should be the same in most cases).

Parameters:
  • db_factory (callable) – The 0-argument factory used to create a DB to pass to the installer.
  • installer_factory (type) – The factory used to create the installer. The installer executes before the body of the wrapped function. This defaults to persistent_site_trans, but can be set to any custom subclass that accepts the db as its first argument and is a context manager that does whatever installation is needed and commits the transaction.
  • installer_kwargs (dict) – Keyword arguments to pass to the installer_factory