This file is indexed.

/usr/lib/python2.7/dist-packages/schooltool/requirement/generations/tests/test_evolve1.py is in python-schooltool.gradebook 2.6.3-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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# coding=UTF8
#
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2008 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, see <http://www.gnu.org/licenses/>.
#
"""
Unit tests for schooltool.requirement.generations.evolve1
"""

import cPickle
from pprint import pprint
import unittest, doctest

from zope.interface import Interface, implements
from zope.app.generations.utility import getRootFolder
from zope.app.testing import setup
from zope.site import LocalSiteManager

from schooltool.requirement.generations.tests import (
    ContextStub, provideAdapters, provideUtilities)
from schooltool.requirement.generations.evolve1 import evolve


class IStubUtil(Interface):
    pass


class StubUtil(object):
    implements(IStubUtil)


class IOtherStubUtil(Interface):
    pass


class OtherStubUtil(object):
    implements(IOtherStubUtil)


class ISubclassUtil(IStubUtil):
    pass


class SubclassUtil(object):
    implements(ISubclassUtil)


def doctest_ZopeHasABug():
    """This test checks that there's still a bug in Zope: if you add two or more
    utilities providing same interface to the local site manager, it is not
    possible to cleanly remove them any more.

        >>> context = ContextStub()
        >>> root = getRootFolder(context)
        >>> sm = LocalSiteManager(root)
        >>> root.setSiteManager(sm)

    So, our site manager has no utilities:

        >>> print sm.utilities._provided.get(IStubUtil)
        None

        >>> pprint(sm.utilities._v_lookup._extendors)
        {}

    Let's add one:

        >>> foo = StubUtil()
        >>> sm.registerUtility(foo, IStubUtil, name='foo')

    Note that utility is registered and a subscription is added, hence the 2:

        >>> print sm.utilities._provided.get(IStubUtil)
        2

    Lookup cache also got updated.

        >>> sm.utilities._v_lookup.__class__
        <class 'zope.interface.adapter.VerifyingAdapterLookup'>

        >>> pprint(sm.utilities._v_lookup._extendors)
        {<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>:
            [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>],
         <InterfaceClass zope.interface.Interface>:
            [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>]}

    Now, if we try to unregister:

        >>> sm.unregisterUtility(foo, IStubUtil, name='foo')
        True

    _provided and lookup are cleared:

        >>> print sm.utilities._provided.get(IStubUtil)
        None

        >>> pprint(sm.utilities._v_lookup._extendors)
        {<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>: [],
        <InterfaceClass zope.interface.Interface>: []}

    And our database is not polluted.

        >>> pickle = cPickle.dumps(sm)
        >>> 'IStubUtil' in pickle
        False

    The trouble begins when we have more than one utility.

        >>> sm.registerUtility(foo, IStubUtil, name='foo')

        >>> print sm.utilities._provided.get(IStubUtil)
        2

        >>> bar = StubUtil()
        >>> sm.registerUtility(bar, IStubUtil, name='bar')

    Note the ref count jumps to 4, because bar is not the same component and
    gets another subscription (that is OK IMHO).

        >>> print sm.utilities._provided.get(IStubUtil)
        4

        >>> pprint(sm.utilities._v_lookup._extendors)
        {<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>:
            [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>],
         <InterfaceClass zope.interface.Interface>:
            [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>]}

    Now, to the bug.  If we unregister utility, the adapter registry does not
    update the _provided correctly.

        >>> sm.unregisterUtility(foo, IStubUtil, name='foo')
        True

        >>> print sm.utilities._provided.get(IStubUtil)
        3

        >>> sm.unregisterUtility(bar, IStubUtil, name='bar')
        True

        >>> print sm.utilities._provided.get(IStubUtil)
        2

        >>> pprint(sm.utilities._v_lookup._extendors)
        {<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>:
            [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>],
         <InterfaceClass zope.interface.Interface>:
            [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>]}

    Congratulations, you are now a proud owner of a polluted database:

        >>> pickle = cPickle.dumps(sm)
        >>> 'IStubUtil' in pickle
        True

    """


def doctest_removeUtils_one():
    """This test demonstrates that removeUtils (hack) works when there is
    only one utility.

        >>> context = ContextStub()
        >>> root = getRootFolder(context)
        >>> sm = LocalSiteManager(root)
        >>> root.setSiteManager(sm)

        >>> sm.utilities.__class__
        <class 'zope.site.site._LocalAdapterRegistry'>

        >>> foo = StubUtil()
        >>> sm.registerUtility(foo, IStubUtil, name='foo')

        >>> ho = SubclassUtil()
        >>> sm.registerUtility(ho, IOtherStubUtil, name='ho')

        >>> from schooltool.requirement.generations.evolve1 import removeUtils
        >>> removeUtils(sm, IStubUtil)

        >>> print sm.utilities._provided.get(IStubUtil)
        None

        >>> print sm.utilities._provided.get(IOtherStubUtil)
        2

        >>> pprint(sm.utilities._v_lookup._extendors)
        {<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IOtherStubUtil>:
            [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IOtherStubUtil>],
         <InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>:
            [],
         <InterfaceClass zope.interface.Interface>:
            [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IOtherStubUtil>]}

        >>> pickle = cPickle.dumps(sm)
        >>> 'IStubUtil' in pickle
        False

    """


def doctest_removeUtils():
    """This test demonstrates that removeUtils (hack) can clean the persisted
    LocalAdapterRegistry from references to "provided" interface.  Other utilities
    are left intact.

        >>> context = ContextStub()
        >>> root = getRootFolder(context)
        >>> sm = LocalSiteManager(root)
        >>> root.setSiteManager(sm)

        >>> sm.utilities.__class__
        <class 'zope.site.site._LocalAdapterRegistry'>

        >>> foo = StubUtil()
        >>> sm.registerUtility(foo, IStubUtil, name='foo')

        >>> bar = StubUtil()
        >>> sm.registerUtility(bar, IStubUtil, name='bar')

        >>> ho = SubclassUtil()
        >>> sm.registerUtility(ho, IOtherStubUtil, name='ho')

        >>> from schooltool.requirement.generations.evolve1 import removeUtils
        >>> removeUtils(sm, IStubUtil)

        >>> print sm.utilities._provided.get(IStubUtil)
        None

        >>> print sm.utilities._provided.get(IOtherStubUtil)
        2

        >>> pprint(sm.utilities._v_lookup._extendors)
        {<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IOtherStubUtil>:
            [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IOtherStubUtil>],
         <InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>:
            [],
         <InterfaceClass zope.interface.Interface>:
            [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IOtherStubUtil>]}

        >>> pickle = cPickle.dumps(sm)
        >>> 'IStubUtil' in pickle
        False

    """


def doctest_removeUtils_subclass():
    """This test demonstrates that removeUtils (hack) is not designed to work
    in some cases.

        >>> context = ContextStub()
        >>> root = getRootFolder(context)
        >>> sm = LocalSiteManager(root)
        >>> root.setSiteManager(sm)

        >>> foo = StubUtil()
        >>> sm.registerUtility(foo, IStubUtil, name='foo')

        >>> bar = StubUtil()
        >>> sm.registerUtility(bar, IStubUtil, name='bar')

    As we use a subclass interface, the extendors are interesting to us:

        >>> pprint(sm.utilities._v_lookup._extendors)
        {<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>:
             [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>],
         <InterfaceClass zope.interface.Interface>:
             [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>]}

        >>> hey = SubclassUtil()
        >>> sm.registerUtility(hey, ISubclassUtil, name='hey')

        >>> ho = SubclassUtil()
        >>> sm.registerUtility(hey, ISubclassUtil, name='ho')

        >>> pprint(sm.utilities._v_lookup._extendors)
        {<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>:
              [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>,
               <InterfaceClass schooltool.requirement.generations.tests.test_evolve1.ISubclassUtil>],
         <InterfaceClass schooltool.requirement.generations.tests.test_evolve1.ISubclassUtil>:
              [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.ISubclassUtil>],
         <InterfaceClass zope.interface.Interface>:
              [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>,
              <InterfaceClass schooltool.requirement.generations.tests.test_evolve1.ISubclassUtil>]}

    Stub utils are removed, but subclass utils are still there

        >>> from schooltool.requirement.generations.evolve1 import removeUtils

        >>> removeUtils(sm, IStubUtil)

        >>> pprint(sm.utilities._v_lookup._extendors)
        {<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.IStubUtil>:
              [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.ISubclassUtil>],
         <InterfaceClass schooltool.requirement.generations.tests.test_evolve1.ISubclassUtil>:
              [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.ISubclassUtil>],
         <InterfaceClass zope.interface.Interface>:
              [<InterfaceClass schooltool.requirement.generations.tests.test_evolve1.ISubclassUtil>]}

        >>> pprint(dict(sm.getUtilitiesFor(ISubclassUtil)))
        {u'hey': <schooltool.requirement.generations.tests.test_evolve1.SubclassUtil object at ...>,
         u'ho': <schooltool.requirement.generations.tests.test_evolve1.SubclassUtil object at ...>}

        >>> pprint(dict(sm.getUtilitiesFor(IStubUtil)))
        {u'hey': <schooltool.requirement.generations.tests.test_evolve1.SubclassUtil object at ...>,
         u'ho': <schooltool.requirement.generations.tests.test_evolve1.SubclassUtil object at ...>}

    Is this acceptable?
    Are there any ICustomScoreSystem subclasses? - no

    """


def doctest_evolve1():
    """Evolution to generation 1.

    First, we'll set up the app object:

        >>> provideAdapters()
        >>> provideUtilities()
        >>> context = ContextStub()
        >>> app = getRootFolder(context)

        >>> sm = LocalSiteManager(app)
        >>> app.setSiteManager(sm)

    We'll set up our test with data that will be effected by running the
    evolve script:

        >>> from schooltool.requirement.interfaces import ICustomScoreSystem
        >>> from schooltool.requirement.scoresystem import (PassFail,
        ...     AmericanLetterScoreSystem, ExtendedAmericanLetterScoreSystem,
        ...     CustomScoreSystem)
        >>> for ss in [PassFail, AmericanLetterScoreSystem,
        ...            ExtendedAmericanLetterScoreSystem]:
        ...     custom_ss = CustomScoreSystem(ss.title, ss.description,
        ...         ss.scores, ss._bestScore, ss._minPassingScore)
        ...     app.getSiteManager().registerUtility(custom_ss,
        ...          ICustomScoreSystem, name=custom_ss.title)

    Finally, we'll run the evolve script, testing the effected values before and
    after:

        >>> from schooltool.requirement.scoresystem import (
        ...     SCORESYSTEM_CONTAINER_KEY)
        >>> SCORESYSTEM_CONTAINER_KEY in app
        False
        >>> sorted(app.getSiteManager().getUtilitiesFor(ICustomScoreSystem))
        [(u'Extended Letter Grade',
             <CustomScoreSystem u'Extended Letter Grade'>),
         (u'Letter Grade', <CustomScoreSystem u'Letter Grade'>),
         (u'Pass/Fail', <CustomScoreSystem u'Pass/Fail'>)]

        >>> evolve(context)

        >>> [ss for ss in app[SCORESYSTEM_CONTAINER_KEY].values()]
        [<CustomScoreSystem u'Letter Grade'>, <CustomScoreSystem u'Pass/Fail'>,
         <CustomScoreSystem u'Extended Letter Grade'>]
        >>> sorted(app.getSiteManager().getUtilitiesFor(ICustomScoreSystem))
        []

        >>> pickle = cPickle.dumps(sm)
        >>> 'ICustomScoreSystem' in pickle
        False

    """


def setUp(test):
    setup.placefulSetUp()
    setup.setUpTraversal()
    provideAdapters()
    provideUtilities()


def tearDown(test):
    setup.placefulTearDown()


def test_suite():
    optionflags = (doctest.ELLIPSIS |
                   doctest.NORMALIZE_WHITESPACE)
    return doctest.DocTestSuite(setUp=setUp, tearDown=tearDown,
                                optionflags=optionflags)


if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')