/usr/share/pyshared/ZODB/tests/testDB.py is in python-zodb 1:3.9.7-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | ##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
from ZODB.tests.MinPO import MinPO
from zope.testing import doctest
import datetime
import os
import sys
import time
import transaction
import unittest
import ZODB
import ZODB.tests.util
# Return total number of connections across all pools in a db._pools.
def nconn(pools):
return sum([len(pool.all) for pool in pools.values()])
class DBTests(ZODB.tests.util.TestCase):
def setUp(self):
ZODB.tests.util.TestCase.setUp(self)
self.db = ZODB.DB('test.fs')
def tearDown(self):
self.db.close()
ZODB.tests.util.TestCase.tearDown(self)
def dowork(self):
c = self.db.open()
r = c.root()
o = r[time.time()] = MinPO(0)
transaction.commit()
for i in range(25):
o.value = MinPO(i)
transaction.commit()
o = o.value
serial = o._p_serial
root_serial = r._p_serial
c.close()
return serial, root_serial
# make sure the basic methods are callable
def testSets(self):
self.db.setCacheSize(15)
self.db.setHistoricalCacheSize(15)
def test_references(self):
# TODO: For now test that we're using referencesf. We really should
# have tests of referencesf.
import ZODB.serialize
self.assert_(self.db.references is ZODB.serialize.referencesf)
def test_invalidateCache():
"""The invalidateCache method invalidates a connection caches for all of
the connections attached to a database::
>>> from ZODB.tests.util import DB
>>> import transaction
>>> db = DB()
>>> tm1 = transaction.TransactionManager()
>>> c1 = db.open(transaction_manager=tm1)
>>> c1.root()['a'] = MinPO(1)
>>> tm1.commit()
>>> tm2 = transaction.TransactionManager()
>>> c2 = db.open(transaction_manager=tm2)
>>> c1.root()['a']._p_deactivate()
>>> tm3 = transaction.TransactionManager()
>>> c3 = db.open(transaction_manager=tm3)
>>> c3.root()['a'].value
1
>>> c3.close()
>>> db.invalidateCache()
>>> c1.root()['a'].value
Traceback (most recent call last):
...
ReadConflictError: database read conflict error
>>> c2.root()['a'].value
Traceback (most recent call last):
...
ReadConflictError: database read conflict error
>>> c3 is db.open(transaction_manager=tm3)
True
>>> print c3.root()['a']._p_changed
None
>>> db.close()
"""
def connectionDebugInfo():
r"""DB.connectionDebugInfo provides information about connections.
>>> import time
>>> now = 1228423244.5
>>> def faux_time():
... global now
... now += .1
... return now
>>> real_time = time.time
>>> time.time = faux_time
>>> from ZODB.tests.util import DB
>>> import transaction
>>> db = DB()
>>> c1 = db.open()
>>> c1.setDebugInfo('test info')
>>> c1.root()['a'] = MinPO(1)
>>> transaction.commit()
>>> c2 = db.open()
>>> _ = c1.root()['a']
>>> c2.close()
>>> c3 = db.open(before=c1.root()._p_serial)
>>> info = db.connectionDebugInfo()
>>> import pprint
>>> pprint.pprint(sorted(info, key=lambda i: str(i['opened'])), width=1)
[{'before': None,
'info': 'test info (2)',
'opened': '2008-12-04T20:40:44Z (1.40s)'},
{'before': '\x03zY\xd8\xc0m9\xdd',
'info': ' (0)',
'opened': '2008-12-04T20:40:45Z (0.30s)'},
{'before': None,
'info': ' (0)',
'opened': None}]
>>> time.time = real_time
"""
def passing_a_file_name_to_DB():
"""You can pass a file-storage file name to DB.
(Also note that we can access DB in ZODB.)
>>> db = ZODB.DB('data.fs')
>>> db.storage # doctest: +ELLIPSIS
<ZODB.FileStorage.FileStorage.FileStorage object at ...
>>> os.path.exists('data.fs')
True
>>> db.close()
"""
def open_convenience():
"""Often, we just want to open a single connection.
>>> conn = ZODB.connection('data.fs')
>>> conn.root()
{}
>>> conn.root()['x'] = 1
>>> transaction.commit()
>>> conn.close()
Let's make sure the database was cloased when we closed the
connection, and that the data is there.
>>> db = ZODB.DB('data.fs')
>>> conn = db.open()
>>> conn.root()
{'x': 1}
>>> db.close()
"""
if sys.version_info >= (2, 6):
def db_with_transaction():
"""Using databases with with
The transaction method returns a context manager that when entered
starts a transaction with a private transaction manager. To
illustrate this, we start a trasnaction using a regular connection
and see that it isn't automatically committed or aborted as we use
the transaction context manager.
>>> db = ZODB.tests.util.DB()
>>> conn = db.open()
>>> conn.root()['x'] = conn.root().__class__()
>>> transaction.commit()
>>> conn.root()['x']['x'] = 1
>>> with db.transaction() as conn2:
... conn2.root()['y'] = 1
>>> conn2.opened
Now, we'll open a 3rd connection a verify that
>>> conn3 = db.open()
>>> conn3.root()['x']
{}
>>> conn3.root()['y']
1
>>> conn3.close()
Let's try again, but this time, we'll have an exception:
>>> with db.transaction() as conn2:
... conn2.root()['y'] = 2
... XXX
Traceback (most recent call last):
...
NameError: name 'XXX' is not defined
>>> conn2.opened
>>> conn3 = db.open()
>>> conn3.root()['x']
{}
>>> conn3.root()['y']
1
>>> conn3.close()
>>> transaction.commit()
>>> conn3 = db.open()
>>> conn3.root()['x']
{'x': 1}
>>> db.close()
"""
def connection_allows_empty_version_for_idiots():
r"""
>>> import sys, StringIO
>>> stderr = sys.stderr
>>> sys.stderr = StringIO.StringIO()
>>> db = ZODB.DB('t.fs')
>>> c = db.open('')
>>> sys.stderr.getvalue() # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
'...: DeprecationWarning: A version string was passed to
open.\nThe first argument is a transaction manager...
>>> sys.stderr = stderr
>>> c.root()
{}
>>> db.close()
"""
def test_suite():
s = unittest.makeSuite(DBTests)
s.addTest(doctest.DocTestSuite(
setUp=ZODB.tests.util.setUp, tearDown=ZODB.tests.util.tearDown,
))
return s
|