3
# SchoolTool - common information systems platform for school administration
4
# Copyright (c) 2009 Shuttleworth Foundation
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.
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.
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
21
Unit tests for schooltool.intervention.generations.evolve7
24
from datetime import datetime
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
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
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
48
class AppStub(Folder):
49
implements(ISchoolToolApplication)
52
class ParentStub(object):
53
implements(IAttributeAnnotatable)
58
def contactName(contact):
59
return '%s %s' % (contact.first_name, contact.last_name)
62
def doctest_evolve7():
63
"""Evolution to generation 7.
67
>>> context = ContextStub()
68
>>> context.root_folder['app'] = app = AppStub()
69
>>> manager = setup.createSiteManager(app)
71
We need a teacher, a student and a parent.
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())
78
Finally we need an intervention root, schoolyear and student.
80
>>> root = app[u'schooltool.interventions'] = {}
81
>>> year = root['thisyear'] = {}
82
>>> student = year['jstudent'] = {}
83
>>> messages = student['messages'] = {}
84
>>> goals = student['goals'] = {}
86
Our first test will be with a message and goal with all good ids.
88
>>> message1 = messages['1'] = InterventionMessage('', '', '')
89
>>> message1.__dict__['sender'] = 'jteacher'
90
>>> message1.__dict__['recipients'] = ['jstudent', 'jstudent:1']
92
>>> goal1 = goals['1'] = InterventionGoal('', '', '', '', '', '', '',
94
>>> goal1.__dict__['creator'] = 'jteacher'
95
>>> goal1.__dict__['_persons_responsible'] = ['jstudent', 'jstudent:1']
96
>>> goal1.__dict__['at_one_time_responsible'] = ['jstudent', 'jstudent:1']
98
We'll run the evolve script, testing the effected values before and after:
100
>>> message1.__dict__['sender']
102
>>> message1.__dict__['recipients']
103
['jstudent', 'jstudent:1']
104
>>> goal1.__dict__['creator']
106
>>> goal1.__dict__['_persons_responsible']
107
['jstudent', 'jstudent:1']
108
>>> goal1.__dict__['at_one_time_responsible']
109
['jstudent', 'jstudent:1']
113
>>> 'sender' in message1.__dict__
115
>>> [contactName(contact) for contact in message1.sender]
117
>>> 'recipients' in message1.__dict__
119
>>> [contactName(contact) for contact in message1.recipients]
120
['Jane Student', 'Parent Name']
121
>>> 'creator' in goal1.__dict__
123
>>> [contactName(contact) for contact in goal1.creator]
125
>>> '_persons_responsible' in goal1.__dict__
127
>>> [contactName(contact) for contact in goal1._persons_responsible]
128
['Jane Student', 'Parent Name']
129
>>> 'at_one_time_responsible' in goal1.__dict__
131
>>> [contactName(contact) for contact in goal1.at_one_time_responsible]
132
['Jane Student', 'Parent Name']
134
Our second test will be with a message and goal with invalid ids.
136
>>> message2 = messages['2'] = InterventionMessage('', '', '')
137
>>> message2.__dict__['sender'] = 'invalid_id'
138
>>> message2.__dict__['recipients'] = ['invalid_id', 'jstudent:2']
140
>>> goal2 = goals['2'] = InterventionGoal('', '', '', '', '', '', '',
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']
146
We'll run the evolve script, testing the effected values before and after:
148
>>> message2.__dict__['sender']
150
>>> message2.__dict__['recipients']
151
['invalid_id', 'jstudent:2']
152
>>> goal2.__dict__['creator']
154
>>> goal2.__dict__['_persons_responsible']
155
['invalid_id', 'jstudent:2']
156
>>> goal2.__dict__['at_one_time_responsible']
157
['invalid_id', 'invalid_id:1']
161
>>> 'sender' in message2.__dict__
163
>>> [contactName(contact) for contact in message2.sender]
165
>>> 'recipients' in message2.__dict__
167
>>> [contactName(contact) for contact in message2.recipients]
169
>>> 'creator' in goal2.__dict__
171
>>> [contactName(contact) for contact in goal2.creator]
173
>>> '_persons_responsible' in goal2.__dict__
175
>>> [contactName(contact) for contact in goal2._persons_responsible]
177
>>> 'at_one_time_responsible' in goal2.__dict__
179
>>> [contactName(contact) for contact in goal2.at_one_time_responsible]
182
What if the intervention container doesn't exist yet in the
185
>>> context = ContextStub()
186
>>> context.root_folder['app'] = app = AppStub()
187
>>> manager = setup.createSiteManager(app)
194
setup.placefulSetUp()
199
setup.placefulTearDown()
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),
213
if __name__ == '__main__':
214
unittest.main(defaultTest='test_suite')