• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / Zope / 3956162881

pending completion
3956162881

push

github

Michael Howitz
Update to deprecation warning free releases.

4401 of 7036 branches covered (62.55%)

Branch coverage included in aggregate %.

27161 of 31488 relevant lines covered (86.26%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.81
/src/Testing/ZopeTestCase/testZODBCompat.py
1
##############################################################################
2
#
3
# Copyright (c) 2005 Zope Foundation and Contributors.
4
#
5
# This software is subject to the provisions of the Zope Public License,
6
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
7
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
# FOR A PARTICULAR PURPOSE.
11
#
12
##############################################################################
13
"""Tests ZODB behavior in ZopeTestCase
1✔
14

15
Demonstrates that cut/copy/paste/clone/rename and import/export
16
work if a savepoint is made before performing the respective operation.
17
"""
18

19
import os
1✔
20
import tempfile
1✔
21
import unittest
1✔
22

23
from AccessControl.Permissions import add_documents_images_and_files
1✔
24
from AccessControl.Permissions import delete_objects
1✔
25
from OFS.SimpleItem import SimpleItem
1✔
26
from Testing import ZopeTestCase
1✔
27
from Testing.ZopeTestCase import layer
1✔
28
from Testing.ZopeTestCase import transaction
1✔
29
from Testing.ZopeTestCase import utils
1✔
30

31

32
folder_name = ZopeTestCase.folder_name
1✔
33
cutpaste_permissions = [add_documents_images_and_files, delete_objects]
1✔
34

35

36
def make_request_response(environ=None):
1✔
37
    from io import StringIO
1✔
38

39
    from ZPublisher.HTTPRequest import HTTPRequest
1✔
40
    from ZPublisher.HTTPResponse import HTTPResponse
1✔
41

42
    if environ is None:
1!
43
        environ = {}
1✔
44

45
    stdout = StringIO()
1✔
46
    stdin = StringIO()
1✔
47
    resp = HTTPResponse(stdout=stdout)
1✔
48
    environ.setdefault('SERVER_NAME', 'foo')
1✔
49
    environ.setdefault('SERVER_PORT', '80')
1✔
50
    environ.setdefault('REQUEST_METHOD', 'GET')
1✔
51
    req = HTTPRequest(stdin, environ, resp)
1✔
52
    return req, resp
1✔
53

54

55
class DummyObject(SimpleItem):
1✔
56
    id = 'dummy'
1✔
57
    foo = None
1✔
58
    _v_foo = None
1✔
59
    _p_foo = None
1✔
60

61

62
class ZODBCompatLayer(layer.ZopeLite):
1✔
63

64
    @classmethod
1✔
65
    def setUp(cls):
1✔
66
        def setup(app):
1✔
67
            app._setObject('dummy1', DummyObject())
1✔
68
            app._setObject('dummy2', DummyObject())
1✔
69
            transaction.commit()
1✔
70
        utils.appcall(setup)
1✔
71

72
    @classmethod
1✔
73
    def tearDown(cls):
1✔
74
        def cleanup(app):
1✔
75
            app._delObject('dummy1')
1✔
76
            app._delObject('dummy2')
1✔
77
            transaction.commit()
1✔
78
        utils.appcall(cleanup)
1✔
79

80

81
class TestCopyPaste(ZopeTestCase.ZopeTestCase):
1✔
82

83
    def afterSetUp(self):
1✔
84
        self.setPermissions(cutpaste_permissions)
1✔
85
        self.folder.addDTMLMethod('doc', file='foo')
1✔
86
        # _p_oids are None until we create a savepoint
87
        self.assertEqual(self.folder._p_oid, None)
1✔
88
        transaction.savepoint(optimistic=True)
1✔
89
        self.assertNotEqual(self.folder._p_oid, None)
1✔
90

91
    def testCutPaste(self):
1✔
92
        cb = self.folder.manage_cutObjects(['doc'])
1✔
93
        self.folder.manage_pasteObjects(cb)
1✔
94
        self.assertTrue(hasattr(self.folder, 'doc'))
1✔
95
        self.assertFalse(hasattr(self.folder, 'copy_of_doc'))
1✔
96

97
    def testCopyPaste(self):
1✔
98
        cb = self.folder.manage_copyObjects(['doc'])
1✔
99
        self.folder.manage_pasteObjects(cb)
1✔
100
        self.assertTrue(hasattr(self.folder, 'doc'))
1✔
101
        self.assertTrue(hasattr(self.folder, 'copy_of_doc'))
1✔
102

103
    def testClone(self):
1✔
104
        self.folder.manage_clone(self.folder.doc, 'new_doc')
1✔
105
        self.assertTrue(hasattr(self.folder, 'doc'))
1✔
106
        self.assertTrue(hasattr(self.folder, 'new_doc'))
1✔
107

108
    def testRename(self):
1✔
109
        self.folder.manage_renameObjects(['doc'], ['new_doc'])
1✔
110
        self.assertFalse(hasattr(self.folder, 'doc'))
1✔
111
        self.assertTrue(hasattr(self.folder, 'new_doc'))
1✔
112

113

114
class TestImportExport(ZopeTestCase.ZopeTestCase):
1✔
115

116
    def afterSetUp(self):
1✔
117
        self.setupLocalEnvironment()
1✔
118
        self.folder.addDTMLMethod('doc', file='foo')
1✔
119
        # please note the usage of the turkish i
120
        self.folder.addDTMLMethod('ıq', file='foo')
1✔
121
        # _p_oids are None until we create a savepoint
122
        self.assertEqual(self.folder._p_oid, None)
1✔
123
        transaction.savepoint(optimistic=True)
1✔
124
        self.assertNotEqual(self.folder._p_oid, None)
1✔
125

126
    def testExport(self):
1✔
127
        self.folder.manage_exportObject('doc')
1✔
128
        self.assertTrue(os.path.exists(self.zexp_file))
1✔
129

130
    def testExportNonLatinFileNames(self):
1✔
131
        """Test compatibility of the export with unicode characters.
132

133
        Since Zope 4 also unicode ids can be used."""
134
        _, response = make_request_response()
1✔
135
        # please note the usage of a turkish `i`
136
        self.folder.manage_exportObject(
1✔
137
            'ıq', download=1, RESPONSE=response)
138

139
        found = False
1✔
140
        for header in response.listHeaders():
1✔
141
            if header[0] == 'Content-Disposition':
1✔
142
                # value needs to be `us-ascii` compatible
143
                assert header[1].encode("us-ascii")
1✔
144
                found = True
1✔
145
        self.assertTrue(found)
1✔
146

147
    def testImport(self):
1✔
148
        self.folder.manage_exportObject('doc')
1✔
149
        self.folder._delObject('doc')
1✔
150
        self.folder.manage_importObject('doc.zexp')
1✔
151
        self.assertTrue(hasattr(self.folder, 'doc'))
1✔
152

153
    # To make export and import happy, we have to provide a file-
154
    # system 'import' directory and adapt the configuration a bit:
155

156
    local_home = tempfile.gettempdir()
1✔
157
    import_dir = os.path.join(local_home, 'import')
1✔
158
    zexp_file = os.path.join(import_dir, 'doc.zexp')
1✔
159

160
    def setupLocalEnvironment(self):
1✔
161
        # Create the 'import' directory
162
        os.mkdir(self.import_dir)
1✔
163
        import App.config
1✔
164
        config = App.config.getConfiguration()
1✔
165
        self._ih = config.instancehome
1✔
166
        config.instancehome = self.local_home
1✔
167
        self._ch = config.clienthome
1✔
168
        config.clienthome = self.import_dir
1✔
169
        App.config.setConfiguration(config)
1✔
170

171
    def afterClear(self):
1✔
172
        # Remove external resources
173
        try:
1✔
174
            os.remove(self.zexp_file)
1✔
175
        except OSError:
1✔
176
            pass
1✔
177
        try:
1✔
178
            os.rmdir(self.import_dir)
1✔
179
        except OSError:
×
180
            pass
×
181
        import App.config
1✔
182
        config = App.config.getConfiguration()
1✔
183
        if hasattr(self, '_ih'):
1!
184
            config.instancehome = self._ih
1✔
185
        if hasattr(self, '_ch'):
1!
186
            config.clienthome = self._ch
1✔
187
        App.config.setConfiguration(config)
1✔
188

189

190
class TestAttributesOfCleanObjects(ZopeTestCase.ZopeTestCase):
1✔
191
    '''This testcase shows that _v_ and _p_ attributes are NOT bothered
192
       by transaction boundaries, if the respective object is otherwise
193
       left untouched (clean). This means that such variables will keep
194
       their values across tests.
195

196
       The only use case yet encountered in the wild is portal_memberdata's
197
       _v_temps attribute. Test authors are cautioned to watch out for
198
       occurrences of _v_ and _p_ attributes of objects that are not recreated
199
       for every test method execution, but preexist in the test ZODB.
200

201
       It is therefore deemed essential to initialize any _v_ and _p_
202
       attributes of such objects in afterSetup(), as otherwise test results
203
       will be distorted!
204

205
       Note that _v_ attributes used to be transactional in Zope < 2.6.
206

207
       This testcase exploits the fact that test methods are sorted by name.
208
    '''
209

210
    layer = ZODBCompatLayer
1✔
211

212
    def afterSetUp(self):
1✔
213
        self.dummy = self.app.dummy1  # See above
1✔
214

215
    def testNormal_01(self):
1✔
216
        # foo is always None
217
        self.assertEqual(self.dummy.foo, None)
1✔
218
        self.dummy.foo = 'foo'
1✔
219

220
    def testNormal_02(self):
1✔
221
        # foo is always None
222
        self.assertEqual(self.dummy.foo, None)
1✔
223
        self.dummy.foo = 'bar'
1✔
224

225
    def testNormal_03(self):
1✔
226
        # foo is always None
227
        self.assertEqual(self.dummy.foo, None)
1✔
228

229
    def testPersistent_01(self):
1✔
230
        # _p_foo is initially None
231
        self.assertEqual(self.dummy._p_foo, None)
1✔
232
        self.dummy._p_foo = 'foo'
1✔
233

234
    def testPersistent_02(self):
1✔
235
        # _p_foo retains value assigned by previous test
236
        self.assertEqual(self.dummy._p_foo, 'foo')
1✔
237
        self.dummy._p_foo = 'bar'
1✔
238

239
    def testPersistent_03(self):
1✔
240
        # _p_foo retains value assigned by previous test
241
        self.assertEqual(self.dummy._p_foo, 'bar')
1✔
242

243
    def testVolatile_01(self):
1✔
244
        # _v_foo is initially None
245
        self.assertEqual(self.dummy._v_foo, None)
1✔
246
        self.dummy._v_foo = 'foo'
1✔
247

248
    def testVolatile_02(self):
1✔
249
        if hasattr(self.app._p_jar, 'register'):
1!
250
            # _v_foo retains value assigned by previous test
251
            self.assertEqual(self.dummy._v_foo, 'foo')
1✔
252
        else:
253
            # XXX: _v_foo is transactional in Zope < 2.6
254
            self.assertEqual(self.dummy._v_foo, None)
×
255
        self.dummy._v_foo = 'bar'
1✔
256

257
    def testVolatile_03(self):
1✔
258
        if hasattr(self.app._p_jar, 'register'):
1!
259
            # _v_foo retains value assigned by previous test
260
            self.assertEqual(self.dummy._v_foo, 'bar')
1✔
261
        else:
262
            # XXX: _v_foo is transactional in Zope < 2.6
263
            self.assertEqual(self.dummy._v_foo, None)
×
264

265

266
class TestAttributesOfDirtyObjects(ZopeTestCase.ZopeTestCase):
1✔
267
    '''This testcase shows that _v_ and _p_ attributes of dirty objects
268
       ARE removed on abort.
269

270
       This testcase exploits the fact that test methods are sorted by name.
271
    '''
272

273
    layer = ZODBCompatLayer
1✔
274

275
    def afterSetUp(self):
1✔
276
        self.dummy = self.app.dummy2  # See above
1✔
277
        self.dummy.touchme = 1  # Tag, you're dirty
1✔
278

279
    def testDirtyNormal_01(self):
1✔
280
        # foo is always None
281
        self.assertEqual(self.dummy.foo, None)
1✔
282
        self.dummy.foo = 'foo'
1✔
283

284
    def testDirtyNormal_02(self):
1✔
285
        # foo is always None
286
        self.assertEqual(self.dummy.foo, None)
1✔
287
        self.dummy.foo = 'bar'
1✔
288

289
    def testDirtyNormal_03(self):
1✔
290
        # foo is always None
291
        self.assertEqual(self.dummy.foo, None)
1✔
292

293
    def testDirtyPersistent_01(self):
1✔
294
        # _p_foo is alwas None
295
        self.assertEqual(self.dummy._p_foo, None)
1✔
296
        self.dummy._p_foo = 'foo'
1✔
297

298
    def testDirtyPersistent_02(self):
1✔
299
        # _p_foo is alwas None
300
        self.assertEqual(self.dummy._p_foo, None)
1✔
301
        self.dummy._p_foo = 'bar'
1✔
302

303
    def testDirtyPersistent_03(self):
1✔
304
        # _p_foo is alwas None
305
        self.assertEqual(self.dummy._p_foo, None)
1✔
306

307
    def testDirtyVolatile_01(self):
1✔
308
        # _v_foo is always None
309
        self.assertEqual(self.dummy._v_foo, None)
1✔
310
        self.dummy._v_foo = 'foo'
1✔
311

312
    def testDirtyVolatile_02(self):
1✔
313
        # _v_foo is always None
314
        self.assertEqual(self.dummy._v_foo, None)
1✔
315
        self.dummy._v_foo = 'bar'
1✔
316

317
    def testDirtyVolatile_03(self):
1✔
318
        # _v_foo is always None
319
        self.assertEqual(self.dummy._v_foo, None)
1✔
320

321

322
class TestTransactionAbort(ZopeTestCase.ZopeTestCase):
1✔
323

324
    def _getfolder(self):
1✔
325
        return getattr(self.app, folder_name, None)
1✔
326

327
    def testTransactionAbort(self):
1✔
328
        folder = self._getfolder()
1✔
329
        self.assertTrue(folder is not None)
1✔
330
        self.assertTrue(folder._p_jar is None)
1✔
331
        transaction.savepoint()
1✔
332
        self.assertTrue(folder._p_jar is not None)
1✔
333
        transaction.abort()
1✔
334
        del folder
1✔
335
        folder = self._getfolder()
1✔
336
        self.assertTrue(folder is None)
1✔
337

338

339
def test_suite():
1✔
340
    return unittest.TestSuite((
1✔
341
        unittest.defaultTestLoader.loadTestsFromTestCase(TestCopyPaste),
342
        unittest.defaultTestLoader.loadTestsFromTestCase(TestImportExport),
343
        unittest.defaultTestLoader.loadTestsFromTestCase(
344
            TestAttributesOfCleanObjects),
345
        unittest.defaultTestLoader.loadTestsFromTestCase(
346
            TestAttributesOfDirtyObjects),
347
        unittest.defaultTestLoader.loadTestsFromTestCase(TestTransactionAbort),
348
    ))
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc