Skip to content

Commit

Permalink
Replace docstring doctest with real test.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Oct 26, 2018
1 parent c0c8988 commit d1ed3a4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/zope/keyreference/persistent.txt
Expand Up @@ -130,10 +130,10 @@ We can't get the key reference for an object that hasn't been saved
yet:

>>> KeyReferenceToPersistent(PersistentMapping())
... # doctest: +ELLIPSIS
...
Traceback (most recent call last):
...
NotYet: ...
zope.keyreference.interfaces.NotYet: ...

Note that we get a NotYet error. This indicates that we might be able
to get a key reference later.
Expand Down
75 changes: 36 additions & 39 deletions src/zope/keyreference/tests.py
Expand Up @@ -14,7 +14,6 @@
"""Tests for the unique id utility.
"""
import doctest
import re
import unittest
from zope.testing import renormalizing

Expand All @@ -41,11 +40,11 @@ def __hash__(self):
return hash(self._p_oid)


class TestKeyReferenceToPersistent(unittest.TestCase):
class TestSimpleKeyReference(unittest.TestCase):

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

def test_comparisons(self):

Expand Down Expand Up @@ -81,56 +80,54 @@ def test__call__(self):

self.assertIs(persistent, obj())

class TestSimpleKeyReference(TestKeyReferenceToPersistent):
class TestKeyReferenceToPersistent(TestSimpleKeyReference):

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"),
r"NotYet"),
])


def test_multi_databases():
"""
>>> from ZODB.MappingStorage import DB
>>> import transaction
>>> from BTrees.OOBTree import OOBucket
from zope.keyreference.persistent import KeyReferenceToPersistent
return KeyReferenceToPersistent(obj)

>>> databases = {}

>>> db1 = DB(databases=databases, database_name='1')
>>> db2 = DB(databases=databases, database_name='2')
def test_multi_databases(self):
from ZODB.MappingStorage import DB
import transaction
from BTrees.OOBTree import OOBucket

>>> conn1 = db1.open()
>>> conn1.root()['ob'] = OOBucket()
databases = {}

>>> conn2 = conn1.get_connection('2')
>>> conn2.root()['ob'] = OOBucket()
db1 = DB(databases=databases, database_name='1')
db2 = DB(databases=databases, database_name='2')

>>> conn1.root()['ob']._p_oid == conn2.root()['ob']._p_oid
True
conn1 = db1.open()
conn1.root()['ob'] = OOBucket()

>>> transaction.commit()
conn2 = conn1.get_connection('2')
self.assertEqual(conn2.db(), db2)
conn2.root()['ob'] = OOBucket()

>>> from zope.keyreference.persistent import KeyReferenceToPersistent
self.assertEqual(conn1.root()['ob']._p_oid,
conn2.root()['ob']._p_oid)

>>> key1 = KeyReferenceToPersistent(conn1.root()['ob'])
>>> key2 = KeyReferenceToPersistent(conn2.root()['ob'])
transaction.commit()

>>> key1 != key2, key2 > key1, hash(key1) != hash(key2)
(True, True, True)
key1 = self.makeOne(conn1.root()['ob'])
key2 = self.makeOne(conn2.root()['ob'])

"""
self.assertNotEqual(key1, key2)
self.assertGreater(key2, key1)
self.assertNotEqual(hash(key1), hash(key2))


def test_suite():
doctest_flags = (
doctest.NORMALIZE_WHITESPACE
| doctest.ELLIPSIS
| renormalizing.IGNORE_EXCEPTION_MODULE_IN_PYTHON2
)
return unittest.TestSuite((
doctest.DocFileSuite('persistent.txt', checker=checker),
doctest.DocTestSuite(),
doctest.DocFileSuite(
'persistent.txt',
optionflags=doctest_flags,
checker=renormalizing.RENormalizing(),
),
unittest.defaultTestLoader.loadTestsFromName(__name__),
))

0 comments on commit d1ed3a4

Please sign in to comment.