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.
Added in version 2.1.0.
Bases:
ZODBLayer,GCLayerMixin,ConfiguringLayerMixinA 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
BTreeLocalAdapterRegistryto immediately switch to BTrees instead of dicts.The test (a
unittest.TestCasesubclass) currently executing in this layer. If there is no such test, this isNone.
Class attribute naming a sequence of package objects or strings naming packages. These will be configured, in order, using ZCML. The
configure.zcmlpackage from each package will be loaded. Instead of a package object, each item can be a tuple of (filename, package); in that case, the given file (usuallymeta.zcml) will be loaded from the given package.
Tests that run in this layer have their
dbproperty set to the currentdbof this layer.
When a test in this layer is torn down, its
dbproperty is set toNone, as is this layer’scurrent_test.
- class nti.site.testing.SiteTestCase(methodName='runTest')[source]¶
Bases:
TestCaseA 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
- class nti.site.testing.persistent_site_trans(db=None, site_name=None)[source]¶
Bases:
mock_db_transContext manager for a ZODB database connection and active
zope.componentsite (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 calledinstall_main_application_and_sites()setting the root_alias to themain_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 (atDEFAULT_ROOT_ALIAS).- Parameters:
db – See
nti.testing.zodb.mock_db_transsite_name (str) – The name of a site to be made current during execution of the body. The site is found using
get_site_for_site_names()whilemain_application_folder_nameis the current site. If not given, the site found atmain_application_folder_namewill be the current site.
- main_application_folder_name = <'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 themain_application_folder_nameis not found in the root of the conn.This method calls
install_main_application_and_sites(), passingmain_application_folder_nameas the main_alias.
- nti.site.testing.print_tree(folder, **kwargs)[source]¶
- print_tree(folder, file=sys.stdout, show_unknown=repr, basic_indent=’ ‘,
details=(‘id’, ‘type’, ‘len’, ‘siteManager’)) -> None
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.
- nti.site.testing.resetHooks()[source]¶
Reset
zope.component.getSiteManagerand 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.setHooks()[source]¶
Make
zope.component.getSiteManagerand 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.
See also
- nti.site.testing.uses_independent_db_site(*args, **kwargs)[source]¶
- uses_independent_db_site(db_factory=None, installer_factory=persistent_site_trans,
installer_kwargs={}) -> function
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
dbattribute will be set to the created db during executing. Likewise, thenti.testing.zodb.ZODBLayerdbattribute (and layers that extend from it likeSharedConfiguringTestLayer) 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_transstatements. 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