~ubuntu-branches/ubuntu/raring/schooltool.intervention/raring

« back to all changes in this revision

Viewing changes to src/schooltool/intervention/generations/tests/test_evolve7.py

  • Committer: Gediminas Paulauskas
  • Date: 2011-09-19 16:56:45 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: menesis@pov.lt-20110919165645-718diuud5tc4mjsx
Tags: 0.5.0-0ubuntu1
* New upstream release.
* debian/rules: move gradebook to Suggests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# coding=UTF8
 
2
#
 
3
# SchoolTool - common information systems platform for school administration
 
4
# Copyright (c) 2009 Shuttleworth Foundation
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
#
 
20
"""
 
21
Unit tests for schooltool.intervention.generations.evolve7
 
22
"""
 
23
 
 
24
from datetime import datetime
 
25
import unittest
 
26
import doctest
 
27
 
 
28
from zope.annotation.interfaces import IAttributeAnnotatable
 
29
from zope.app.generations.utility import getRootFolder
 
30
from zope.app.testing import setup
 
31
from zope.component.hooks import getSite, setSite
 
32
from zope.interface import implements
 
33
from zope.site.folder import Folder
 
34
 
 
35
from schooltool.app.interfaces import ISchoolToolApplication
 
36
from schooltool.basicperson.person import BasicPerson
 
37
from schooltool.contact.contact import Contact
 
38
from schooltool.contact.interfaces import IContactable
 
39
from schooltool.generations.tests import ContextStub
 
40
from schooltool.person.interfaces import IPerson
 
41
 
 
42
from schooltool.intervention.generations.tests import provideAdapters
 
43
from schooltool.intervention.generations.evolve7 import evolve
 
44
from schooltool.intervention.intervention import InterventionMessage
 
45
from schooltool.intervention.intervention import InterventionGoal
 
46
 
 
47
 
 
48
class AppStub(Folder):
 
49
    implements(ISchoolToolApplication)
 
50
 
 
51
 
 
52
class ParentStub(object):
 
53
    implements(IAttributeAnnotatable)
 
54
    first_name = 'Parent'
 
55
    last_name = 'Name'
 
56
 
 
57
 
 
58
def contactName(contact):
 
59
    return '%s %s' % (contact.first_name, contact.last_name)
 
60
 
 
61
 
 
62
def doctest_evolve7():
 
63
    """Evolution to generation 7.
 
64
 
 
65
    We need an app.
 
66
 
 
67
        >>> context = ContextStub()
 
68
        >>> context.root_folder['app'] = app = AppStub()
 
69
        >>> manager = setup.createSiteManager(app)
 
70
 
 
71
    We need a teacher, a student and a parent.
 
72
 
 
73
        >>> persons = app[u'persons'] = {}
 
74
        >>> jteacher = persons['jteacher'] = BasicPerson('jteacher', 'John', 'Teacher')
 
75
        >>> jstudent = persons['jstudent'] = BasicPerson('jstudent', 'Jane', 'Student')
 
76
        >>> IContactable(jstudent).contacts.add(ParentStub())
 
77
 
 
78
    Finally we need an intervention root, schoolyear and student.
 
79
 
 
80
        >>> root = app[u'schooltool.interventions'] = {}
 
81
        >>> year = root['thisyear'] = {}
 
82
        >>> student = year['jstudent'] = {}
 
83
        >>> messages = student['messages'] = {}
 
84
        >>> goals = student['goals'] = {}
 
85
 
 
86
    Our first test will be with a message and goal with all good ids.
 
87
 
 
88
        >>> message1 = messages['1'] = InterventionMessage('', '', '')
 
89
        >>> message1.__dict__['sender'] = 'jteacher'
 
90
        >>> message1.__dict__['recipients'] = ['jstudent', 'jstudent:1']
 
91
 
 
92
        >>> goal1 = goals['1'] = InterventionGoal('', '', '', '', '', '', '', 
 
93
        ...                                       '')
 
94
        >>> goal1.__dict__['creator'] = 'jteacher'
 
95
        >>> goal1.__dict__['_persons_responsible'] = ['jstudent', 'jstudent:1']
 
96
        >>> goal1.__dict__['at_one_time_responsible'] = ['jstudent', 'jstudent:1']
 
97
 
 
98
    We'll run the evolve script, testing the effected values before and after:
 
99
 
 
100
        >>> message1.__dict__['sender']
 
101
        'jteacher'
 
102
        >>> message1.__dict__['recipients']
 
103
        ['jstudent', 'jstudent:1']
 
104
        >>> goal1.__dict__['creator']
 
105
        'jteacher'
 
106
        >>> goal1.__dict__['_persons_responsible']
 
107
        ['jstudent', 'jstudent:1']
 
108
        >>> goal1.__dict__['at_one_time_responsible']
 
109
        ['jstudent', 'jstudent:1']
 
110
 
 
111
        >>> evolve(context)
 
112
 
 
113
        >>> 'sender' in message1.__dict__
 
114
        False
 
115
        >>> [contactName(contact) for contact in message1.sender]
 
116
        ['John Teacher']
 
117
        >>> 'recipients' in message1.__dict__
 
118
        False
 
119
        >>> [contactName(contact) for contact in message1.recipients]
 
120
        ['Jane Student', 'Parent Name']
 
121
        >>> 'creator' in goal1.__dict__
 
122
        False
 
123
        >>> [contactName(contact) for contact in goal1.creator]
 
124
        ['John Teacher']
 
125
        >>> '_persons_responsible' in goal1.__dict__
 
126
        False
 
127
        >>> [contactName(contact) for contact in goal1._persons_responsible]
 
128
        ['Jane Student', 'Parent Name']
 
129
        >>> 'at_one_time_responsible' in goal1.__dict__
 
130
        False
 
131
        >>> [contactName(contact) for contact in goal1.at_one_time_responsible]
 
132
        ['Jane Student', 'Parent Name']
 
133
 
 
134
    Our second test will be with a message and goal with invalid ids.
 
135
 
 
136
        >>> message2 = messages['2'] = InterventionMessage('', '', '')
 
137
        >>> message2.__dict__['sender'] = 'invalid_id'
 
138
        >>> message2.__dict__['recipients'] = ['invalid_id', 'jstudent:2']
 
139
 
 
140
        >>> goal2 = goals['2'] = InterventionGoal('', '', '', '', '', '', '', 
 
141
        ...                                       '')
 
142
        >>> goal2.__dict__['creator'] = 'invalid_id'
 
143
        >>> goal2.__dict__['_persons_responsible'] = ['invalid_id', 'jstudent:2']
 
144
        >>> goal2.__dict__['at_one_time_responsible'] = ['invalid_id', 'invalid_id:1']
 
145
 
 
146
    We'll run the evolve script, testing the effected values before and after:
 
147
 
 
148
        >>> message2.__dict__['sender']
 
149
        'invalid_id'
 
150
        >>> message2.__dict__['recipients']
 
151
        ['invalid_id', 'jstudent:2']
 
152
        >>> goal2.__dict__['creator']
 
153
        'invalid_id'
 
154
        >>> goal2.__dict__['_persons_responsible']
 
155
        ['invalid_id', 'jstudent:2']
 
156
        >>> goal2.__dict__['at_one_time_responsible']
 
157
        ['invalid_id', 'invalid_id:1']
 
158
 
 
159
        >>> evolve(context)
 
160
 
 
161
        >>> 'sender' in message2.__dict__
 
162
        False
 
163
        >>> [contactName(contact) for contact in message2.sender]
 
164
        []
 
165
        >>> 'recipients' in message2.__dict__
 
166
        False
 
167
        >>> [contactName(contact) for contact in message2.recipients]
 
168
        []
 
169
        >>> 'creator' in goal2.__dict__
 
170
        False
 
171
        >>> [contactName(contact) for contact in goal2.creator]
 
172
        []
 
173
        >>> '_persons_responsible' in goal2.__dict__
 
174
        False
 
175
        >>> [contactName(contact) for contact in goal2._persons_responsible]
 
176
        []
 
177
        >>> 'at_one_time_responsible' in goal2.__dict__
 
178
        False
 
179
        >>> [contactName(contact) for contact in goal2.at_one_time_responsible]
 
180
        []
 
181
 
 
182
    What if the intervention container doesn't exist yet in the
 
183
    application:
 
184
 
 
185
        >>> context = ContextStub()
 
186
        >>> context.root_folder['app'] = app = AppStub()
 
187
        >>> manager = setup.createSiteManager(app)
 
188
        >>> evolve(context)
 
189
 
 
190
    """
 
191
 
 
192
 
 
193
def setUp(test):
 
194
    setup.placefulSetUp()
 
195
    provideAdapters()
 
196
    setSite()
 
197
 
 
198
def tearDown(test):
 
199
    setup.placefulTearDown()
 
200
    setSite()
 
201
 
 
202
 
 
203
def test_suite():
 
204
    return unittest.TestSuite([
 
205
        doctest.DocTestSuite(setUp=setUp, tearDown=tearDown,
 
206
                             optionflags=doctest.ELLIPSIS
 
207
                             | doctest.NORMALIZE_WHITESPACE
 
208
                             | doctest.REPORT_NDIFF
 
209
                             | doctest.REPORT_ONLY_FIRST_FAILURE),
 
210
        ])
 
211
 
 
212
 
 
213
if __name__ == '__main__':
 
214
    unittest.main(defaultTest='test_suite')
 
215