Skip to content

Commit

Permalink
100% coverage
Browse files Browse the repository at this point in the history
Ensure that our configure.zcml can be loaded standalone and produce a
functioning package by using it in the tests.
  • Loading branch information
jamadden committed Aug 8, 2017
1 parent 7d5cad4 commit bbfb253
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def read(*rnames):
return f.read()

TESTS_REQUIRE = [
'zope.configuration',
'zope.testing',
'zope.site[test]',
'zope.testrunner',
Expand Down
37 changes: 30 additions & 7 deletions src/zope/principalannotation/README.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Principal Annotations
=====================
=======================
Principal Annotations
=======================

This package implements annotations for zope.security principals.
To make it clear, the `principal` here is the object that provides
Expand All @@ -17,7 +18,7 @@ to IAnnotations.


PrincipalAnnotationUtility
--------------------------
==========================

The core of this package is the ``PrincipalAnnotationUtility`` class
that stores annotations for principals and allows to get them easily.
Expand Down Expand Up @@ -108,7 +109,7 @@ But we can't delete the key that is (no more) existant::


Multiple annotation utilities
-----------------------------
=============================

Imagine that your application has a root ``site`` object with its
component registry (a.k.a. site manager) and that object has a sub-site
Expand Down Expand Up @@ -142,6 +143,8 @@ Now, let's create a key in the IAnnotations, provided by root utility::

>>> annots = util.getAnnotations(nadako)
>>> annots['root.number'] = 42
>>> sorted(annots.items())
[('root.number', 42)]

The subsite utility should get the annotation successfully::

Expand All @@ -163,6 +166,10 @@ overriden::
1
>>> annots2['another.number']
42
>>> sorted(iter(annots))
['another.number', 'root.number']
>>> sorted(iter(annots2))
['another.number']

If we'll delete the key from lower-level, it will not be deleted from a
higher level utility::
Expand All @@ -173,10 +180,26 @@ higher level utility::
1
>>> annots2['another.number']
1
>>> sorted(iter(annots))
['another.number', 'root.number']

This is somewhat confusing given the way that ``in`` and boolean tests
work::

>>> 'another.number' in annots
True
>>> 'another.number' in annots2
False
>>> annots2['another.number']
1
>>> list(iter(annots2))
[]
>>> bool(annots2)
True


IPrincipal -> IAnnotations adapter
----------------------------------
==================================

Of course, the most nice feature is that we can simply adapt our
principal object to IAnnotations and get those annotations using
Expand All @@ -195,15 +218,15 @@ By default, the IAnnotation adapter uses the current site's utility::

>>> from zope.site.hooks import setSite
>>> setSite(subsite)

>>> IAnnotations(nadako) is util2.getAnnotations(nadako)
True

Howerver, we can use a binary multi-adapter to IAnnotations to specify
some context object from which to get the annotations utility::

>>> from zope.component import getMultiAdapter

>>> annots = getMultiAdapter((nadako, root), IAnnotations)
>>> annots is util.getAnnotations(nadako)
True
Expand Down
3 changes: 3 additions & 0 deletions src/zope/principalannotation/configure.zcml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<configure xmlns="http://namespaces.zope.org/zope">

<include package="zope.component" file="meta.zcml" />
<include package="zope.security" file="permissions.zcml" />

<class class=".utility.PrincipalAnnotationUtility">
<factory id="zope.app.PrincipalAnnotationUtility" />
<implements
Expand Down
10 changes: 4 additions & 6 deletions src/zope/principalannotation/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
"""
import doctest
import unittest
from zope.component import provideAdapter
from zope.interface import Interface
from zope.security.interfaces import IPrincipal

from zope.site.testing import siteSetUp, siteTearDown
from zope.configuration import xmlconfig

from zope.principalannotation.utility import annotations
import zope.principalannotation

def setUp(test):
site = siteSetUp(site=True)
test.globs['root'] = site
provideAdapter(annotations)
provideAdapter(annotations, (IPrincipal, Interface))
xmlconfig.file('configure.zcml', zope.principalannotation)

def tearDown(test):
siteTearDown()
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ basepython =
python2.7
commands =
coverage run -m zope.testrunner --test-path=src
coverage report --fail-under=97
coverage report --fail-under=100
deps =
{[testenv]deps}
coverage
Expand Down

0 comments on commit bbfb253

Please sign in to comment.