Skip to content

Commit

Permalink
Reach 100% coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Oct 25, 2018
1 parent 3ec3840 commit 3360d23
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 20 deletions.
9 changes: 9 additions & 0 deletions .coveragerc
@@ -0,0 +1,9 @@
[run]
source = zope.keyreference

[report]
exclude_lines =
pragma: no cover
if __name__ == '__main__':
raise NotImplementedError
raise AssertionError
22 changes: 12 additions & 10 deletions src/zope/keyreference/interfaces.py
Expand Up @@ -27,17 +27,19 @@ class NotYet(Exception):
(e.g. at the end of the transaction).
"""


class IKeyReference(zope.interface.Interface):
"""A reference to an object (similar to a weak reference).
The references are compared by their hashes.
"""

key_type_id = DottedName(title=_('Key Type Id'),
key_type_id = DottedName(
title=_('Key Type Id'),
description=_('Key references should sort first '
'on their key type and second on any type-specific '
'information.')
)
'on their key type and second on any type-specific '
'information.')
)

def __call__():
"""Get the object this reference is linking to.
Expand All @@ -54,19 +56,19 @@ def __cmp__(ref):
"""

def __eq__(ref):
pass
"KeyReferences must be totally orderable."

def __lt__(ref):
pass
"KeyReferences must be totally orderable."

def __ne__(ref):
pass
"KeyReferences must be totally orderable."

def __gt__(ref):
pass
"KeyReferences must be totally orderable."

def __le__(ref):
pass
"KeyReferences must be totally orderable."

def __ge__(ref):
pass
"KeyReferences must be totally orderable."
10 changes: 9 additions & 1 deletion src/zope/keyreference/persistent.py
Expand Up @@ -96,8 +96,16 @@ def _get_cmp_keys(self, other):
return self.key_type_id, other.key_type_id

# Py3: For Python 2 BBB.
# If we implement all the rich comparison operations, though, this is
# never actually called.
def __cmp__(self, other):
return cmp(*self._get_cmp_keys(other))
my_keys, other_keys = self._get_cmp_keys(other)
if my_keys == other_keys:
return 0
if my_keys > other_keys:
return 1
return -1


def __eq__(self, other):
a, b = self._get_cmp_keys(other)
Expand Down
17 changes: 14 additions & 3 deletions src/zope/keyreference/testing.py
Expand Up @@ -11,16 +11,20 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Testing components
"""
Testing components.
"""

import zope.interface
import zope.component
import zope.keyreference.interfaces

@zope.component.adapter(zope.interface.Interface)
@zope.interface.implementer(zope.keyreference.interfaces.IKeyReference)
class SimpleKeyReference(object):
"""An IReference for all objects. This implementation is *not* ZODB safe.
"""An IReference for all objects. This implementation is *not*
ZODB safe.
"""

key_type_id = 'zope.app.keyreference.simple'
Expand All @@ -41,8 +45,15 @@ def _get_cmp_keys(self, other):
return self.key_type_id, other.key_type_id

# Py3: For Python 2 BBB.
# If we implement all the rich comparison operations, though, this is
# never actually called.
def __cmp__(self, other):
return cmp(*self._get_cmp_keys(other))
my_keys, other_keys = self._get_cmp_keys(other)
if my_keys == other_keys:
return 0
if my_keys > other_keys:
return 1
return -1

def __eq__(self, other):
a, b = self._get_cmp_keys(other)
Expand Down
81 changes: 76 additions & 5 deletions src/zope/keyreference/tests.py
Expand Up @@ -18,6 +18,80 @@
import unittest
from zope.testing import renormalizing


class MockDatabase(object):
database_name = ''


class MockJar(object):

def __init__(self):
self._db = MockDatabase()

def db(self):
return self._db


class MockPersistent(object):
_p_oid = 1
_p_jar = MockJar()

def __hash__(self):
# SimpleKeyReference depends on hash
return hash(self._p_oid)


class TestKeyReferenceToPersistent(unittest.TestCase):

def makeOne(self, obj):
from zope.keyreference.persistent import KeyReferenceToPersistent
return KeyReferenceToPersistent(obj)

def test__cmp__(self):

persistent = MockPersistent()

eq1 = self.makeOne(persistent)
eq2 = self.makeOne(persistent)

self.assertEqual(eq1, eq2)
self.assertEqual(0, eq1.__cmp__(eq2))
self.assertEqual(0, eq1.__cmp__(eq1))
self.assertEqual(0, eq2.__cmp__(eq1))

# But if they have different key_type_id, they
# only compare based on that.
eq2.key_type_id = 'aaa'
self.assertNotEqual(eq1, eq2)

persistent_gt = MockPersistent()
persistent_gt._p_oid = 2

gt = self.makeOne(persistent_gt)
__traceback_info__ = hash(gt), hash(eq1), hash(persistent._p_oid)
self.assertGreater(gt, eq1)
self.assertGreaterEqual(gt, eq1)
self.assertNotEqual(gt, eq1)
self.assertEqual(1, gt.__cmp__(eq1))

self.assertLess(eq1, gt)
self.assertLessEqual(eq1, gt)
self.assertEqual(-1, eq1.__cmp__(gt))


def test__call__(self):
persistent = MockPersistent()
obj = self.makeOne(persistent)

self.assertIs(persistent, obj())

class TestSimpleKeyReference(TestKeyReferenceToPersistent):

def makeOne(self, obj):
from zope.keyreference.testing import SimpleKeyReference
return SimpleKeyReference(obj)


checker = renormalizing.RENormalizing([
# Python 3 adds module name to exceptions.
(re.compile("zope.keyreference.interfaces.NotYet"),
Expand Down Expand Up @@ -61,8 +135,5 @@ def test_suite():
return unittest.TestSuite((
doctest.DocFileSuite('persistent.txt', checker=checker),
doctest.DocTestSuite(),
))

if __name__ == '__main__':
unittest.main(defaultTest='test_suite')

unittest.defaultTestLoader.loadTestsFromName(__name__),
))
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -20,7 +20,7 @@ basepython =
python3.6
commands =
coverage run -m zope.testrunner --test-path=src []
coverage report --fail-under=67
coverage report --fail-under=100
deps =
{[testenv]deps}
coverage

0 comments on commit 3360d23

Please sign in to comment.