/usr/share/pyshared/schooltool/testing/functional.py is in python-schooltool 1:2.1.0-0ubuntu1.
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 | #
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2005 Shuttleworth Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Functional Testing Utilities
"""
import os
import unittest
import doctest
from zope.testbrowser.testing import Browser
from zope.testing.server import startServer
from zope.app.testing.functional import HTTPCaller
from zope.app.testing.functional import ZCMLLayer as _ZCMLLayer
from zope.app.testing.functional import FunctionalDocFileSuite
from zope.app.appsetup.interfaces import IDatabaseOpenedEvent
import zope.event
from schooltool.testing.analyze import queryHTML
from schooltool.testing import analyze
def find_ftesting_zcml():
"""Find ftesting.zcml to be used for SchoolTool functional tests."""
dir = os.path.abspath(os.path.dirname(__file__))
while True:
filename = os.path.join(dir, 'etc', 'ftesting.zcml')
if os.path.exists(filename):
return filename # We are testing in an instance
dir = os.path.dirname(dir)
if dir == os.path.dirname(dir): # we're looping at the filesystem root
raise RuntimeError("I can't find ftesting.zcml!")
class ZCMLLayer(_ZCMLLayer):
def __init__(self, *args, **kwargs):
self.school_type = kwargs.pop("school_type", "")
kwargs['allow_teardown'] = True
_ZCMLLayer.__init__(self, *args, **kwargs)
def setUp(self):
# SchoolTool needs to bootstrap the database first, before the Zope 3
# IDatabaseOpenedEvent gets a chance to create its own root folder and
# stuff. Unfortunatelly, we cannot install a IDatabaseOpenedEvent
# subscriber via ftesting.zcml and ensure it will get called first.
# Instead we place our own subscriber directly into zope.event.subscribers,
# where it gets a chance to intercept IDatabaseOpenedEvent before the
# Zope 3 event dispatcher sees it.
def install_db_bootstrap_hook():
"""Install schooltool_db_setup into zope.event.subscribers."""
zope.event.subscribers.insert(0, schooltool_db_setup)
def uninstall_db_bootstrap_hook():
"""Remove schooltool_db_setup from zope.event.subscribers."""
zope.event.subscribers.remove(schooltool_db_setup)
def schooltool_db_setup(event, school_type=""):
"""IDatabaseOpenedEvent handler that bootstraps SchoolTool."""
if IDatabaseOpenedEvent.providedBy(event):
import schooltool.app.main
server = schooltool.app.main.StandaloneServer()
server.bootstrapSchoolTool(event.database, self.school_type)
install_db_bootstrap_hook()
try:
_ZCMLLayer.setUp(self)
finally:
uninstall_db_bootstrap_hook()
class TestBrowser(Browser):
username = None
password = None
def __init__(self, username=None, password=None, url='http://localhost/'):
super(TestBrowser, self).__init__()
self.username = username
self.password = password
if username and password:
self.addHeader('Authorization',
'Basic %s:%s' % (self.username, self.password))
self.handleErrors = False
self.open(url)
def serve(self, url=None, port=8000):
if url is None:
url = self.url
startServer(HTTPCaller(), url, self.username, self.password, port=port)
def queryHTML(self, query):
return queryHTML(query, self.contents)
def printQuery(self, query, skip_inner_blank=False):
for item in queryHTML(query, self.contents):
if item.strip():
if skip_inner_blank:
result = str(item.strip()).splitlines()
for line in result:
line = line.strip()
if line:
print line
else:
print item.strip()
def collect_txt_ftests(package=None, level=None, layer=None, filenames=None,
suite_factory=None):
"""Collect all functional doctest files in a given package.
If `package` is None, looks up the call stack for the right module.
Returns a unittest.TestSuite.
"""
testdir = os.path.dirname(package.__file__)
if filenames is None:
filenames = [fn for fn in os.listdir(testdir)
if fn.endswith('.txt') and not fn.startswith('.')]
suites = []
for filename in filenames:
suite = suite_factory(
filename, package=package)
if level is not None:
suite.level = level
if layer is None:
raise ValueError("ftests must specify an ftesting.zcml.")
suite.layer = layer
suites.append(suite)
return unittest.TestSuite(suites)
def collect_ftests(package=None, level=None, layer=None, filenames=None):
package = doctest._normalize_module(package)
def make_suite(filename, package=None):
optionflags = (doctest.ELLIPSIS | doctest.REPORT_NDIFF |
doctest.NORMALIZE_WHITESPACE |
doctest.REPORT_ONLY_FIRST_FAILURE)
suite = FunctionalDocFileSuite(filename, package=package,
optionflags=optionflags,
globs={'analyze': analyze,
'Browser': TestBrowser})
return suite
return collect_txt_ftests(package=package, level=level,
layer=layer, filenames=filenames,
suite_factory=make_suite)
|