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

zopefoundation / z3c.testing / 6447486104

18 Jan 2023 07:44AM UTC coverage: 41.841% (-0.2%) from 42.083%
6447486104

push

github

web-flow
Config with pure python template (#8)

* Drop support for Python 2.7, 3.5, 3.6.
* Add support for Python 3.11.

9 of 32 branches covered (0.0%)

Branch coverage included in aggregate %.

7 of 7 new or added lines in 4 files covered. (100.0%)

91 of 207 relevant lines covered (43.96%)

0.44 hits per line

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

8.57
/src/z3c/testing/layer.py
1
##############################################################################
2
#
3
# Copyright (c) 2006 Lovely Systems and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14
"""A test layer to use a saved database.
1✔
15

16
The testlayer creates a database using a configurator and uses the
17
database for all tests.
18
"""
19
import os
1✔
20
import shutil
1✔
21
import sys
1✔
22

23
import transaction
1✔
24
from ZODB.FileStorage import FileStorage
1✔
25
from zope.app.appsetup import database
×
26
from zope.app.publication.zopepublication import ZopePublication
×
27
from zope.app.testing import functional
×
28
from zope.site import hooks
×
29

30

31
class BufferedDatabaseTestLayer:
×
32
    """A test layer which creates a filestorage database.
33

34
    The created database is later used without the need to run through the
35
    setup again.
36
    This speeds up functional tests.
37
    """
38

39
    __name__ = "BufferedTestLayer"
×
40
    __bases__ = ()
×
41
    path = None
×
42

43
    def __init__(self, config_file=None, module=__module__,  # noqa
×
44
                 name="BufferedTestLayer", path=None, clean=False):
45
        self.config_file = config_file or functional.Functional.config_file
×
46
        self.__module__ = module
×
47
        self.__name__ = name
×
48
        self.path = path
×
49
        self.dbDir = os.path.join(self.path,
×
50
                                  'var_%s' % self.__module__)
51
        if clean and os.path.isdir(self.dbDir):
×
52
            shutil.rmtree(self.dbDir)
×
53

54
    def setUpApplication(self, app):
×
55
        # to be overridden by subclass
56
        pass
×
57

58
    def setUp(self):
×
59
        if not os.path.exists(self.dbDir):
×
60
            os.mkdir(self.dbDir)
×
61
        filename = os.path.join(self.dbDir, 'TestData.fs')
×
62
        fsetup = functional.FunctionalTestSetup(self.config_file)
×
63
        self.original = fsetup.base_storage
×
64
        if not os.path.exists(filename):
×
65

66
            # Generate a new database from scratch and fill it
67
            db = database(filename)
×
68
            connection = db.open()
×
69
            root = connection.root()
×
70
            app = root[ZopePublication.root_name]
×
71
            # store the site, because the connection gets closed
72
            site = hooks.getSite()
×
73
            self.setUpApplication(app)
×
74
            transaction.commit()
×
75
            connection.close()
×
76
            db.close()
×
77
            hooks.setSite(site)
×
78

79
        # sets up the db stuff normal
80
        fsetup.setUp()
×
81
        # replace the storage with our filestorage
82
        fsetup.base_storage = FileStorage(filename)
×
83
        # override close on this instance, so files dont get closed on
84
        # setup/teardown of functionsetup
85
        fsetup.base_storage.close = lambda: None
×
86

87
    def tearDown(self):
×
88
        fsetup = functional.FunctionalTestSetup(self.config_file)
×
89
        # close the filestorage files now by calling the original
90
        # close on our storage instance
91
        FileStorage.close(fsetup.base_storage)
×
92
        # replace the storage with the original, so functionalsetup
93
        # can do what it wants with it
94
        fsetup.base_storage = self.original
×
95
        fsetup.tearDown()
×
96
        fsetup.tearDownCompletely()
×
97

98

99
def defineLayer(name, zcml=None, appSetUp=None, clean=False):
×
100
    """Helper function for defining layers.
101

102
    Defines a new buffered database layer
103

104
    :name: the name of the layer in the module
105
    :zcml: optional zcml file relative to package dir
106
    :appSetUp: a callable which takes an application object as argument
107
    :clean: if True the database directory is deleted on init
108
    """
109
    globals = sys._getframe(1).f_globals
×
110
    if zcml is not None:
×
111
        zcml = os.path.join(os.path.split(globals['__file__'])[0], zcml)
×
112
    layer = BufferedDatabaseTestLayer(
×
113
        zcml, globals['__name__'], name,
114
        path=os.path.dirname(globals['__file__']),
115
        clean=clean)
116
    if appSetUp is not None:
×
117
        layer.setUpApplication = appSetUp
×
118
    globals[name] = layer
×
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