/usr/lib/python2.7/dist-packages/schooltool/lyceum/journal/tests/test_journal.py is in python-schooltool.lyceum.journal 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 | #
#
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2007 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 lyceum journal.
"""
import unittest, doctest
import datetime
from zope.component import adapter
from zope.component import provideAdapter
from zope.app.testing import setup
from zope.interface import implementer
from zope.interface import implements
from zope.interface.verify import verifyObject
from zope.keyreference.interfaces import IKeyReference
from schooltool.course.interfaces import ISection
from schooltool.requirement.testing import KeyReferenceStub
from schooltool.requirement.evaluation import Evaluations
from schooltool.requirement.interfaces import IEvaluations
from schooltool.lyceum.journal.interfaces import ISectionJournalData
from schooltool.lyceum.journal.interfaces import ISectionJournal
def stubbedGetEvaluations(context):
evals = getattr(context, '_evaluations', None)
if evals is None:
evals = Evaluations()
context._evaluations = evals
return evals
def doctest_SectionJournalData():
"""Tests for SectionJournalData
>>> from schooltool.lyceum.journal.journal import SectionJournalData
>>> journal = SectionJournalData()
>>> class SectionStub(object):
... pass
>>> section = SectionStub()
>>> @adapter(ISectionJournalData)
... @implementer(ISection)
... def getSection(jd):
... return section
>>> provideAdapter(getSection)
>>> verifyObject(ISectionJournalData, journal)
True
>>> provideAdapter(KeyReferenceStub,
... adapts=(SectionStub, ),
... provides=IKeyReference)
Grades can be added for every person/meeting pair:
>>> class PersonStub(object):
... def __init__(self, name):
... self.__name__ = name
>>> provideAdapter(stubbedGetEvaluations,
... adapts=(PersonStub, ),
... provides=IEvaluations)
>>> class CalendarStub(object):
... def __init__(self, section):
... self.__parent__ = section
>>> calendar = CalendarStub(section)
>>> class MeetingStub(object):
... __parent__ = calendar
... def __init__(self, uid, meeting_id=None,
... date=datetime.date(2011, 05, 05)):
... self.dtstart = datetime.datetime(
... date.year, date.month, date.day)
... self.unique_id = uid
... self.meeting_id = meeting_id
>>> person1 = PersonStub('john')
>>> person2 = PersonStub('pete')
>>> meeting = MeetingStub('some-unique-id')
>>> journal.setGrade(person1, meeting, "5")
And are read that way too:
>>> journal.getGrade(person1, meeting)
Decimal('5')
If there is no grade present in that position, you get None:
>>> journal.getGrade(person2, meeting) is None
True
Unless default is provided:
>>> journal.getGrade(person2, meeting, default="")
''
Absences work in a very simmilar way:
>>> journal.setAbsence(person1, meeting)
>>> journal.getAbsence(person1, meeting)
'n'
Absences are treated as unexplained by default:
>>> journal.getAbsence(person2, meeting)
''
Unless default is provided:
>>> journal.getAbsence(person2, meeting, default=True)
True
Meetings can be shared:
>>> meeting2 = MeetingStub('double-1', meeting_id='double-meeting')
>>> meeting3 = MeetingStub('double-2', meeting_id='double-meeting')
>>> journal.getGrade(person1, meeting)
Decimal('5')
>>> print journal.getGrade(person1, meeting2)
None
>>> print journal.getGrade(person1, meeting3)
None
>>> journal.setGrade(person1, meeting2, "7")
>>> journal.getGrade(person1, meeting3)
Decimal('7')
>>> journal.getGrade(person1, meeting)
Decimal('5')
"""
def doctest_getSectionJournalData():
"""Tests for getSectionJournalData
>>> from schooltool.lyceum.journal.journal import getSectionJournalData
>>> from zope.container.btree import BTreeContainer
>>> journal_container = BTreeContainer()
>>> class STAppStub(dict):
... def __init__(self, context):
... self['schooltool.lyceum.journal'] = journal_container
>>> from schooltool.app.interfaces import ISchoolToolApplication
>>> provideAdapter(STAppStub, adapts=[None], provides=ISchoolToolApplication)
>>> from zope.intid.interfaces import IIntIds
>>> from zope.component import provideUtility
>>> class FakeIntID(object):
... implements(IIntIds)
... def getId(self, object):
... return id(object)
>>> provideUtility(FakeIntID())
>>> class SectionStub(object):
... def __init__(self, name):
... self.__name__ = name
>>> section = SectionStub('some_section')
Initially the journal container is empty, but if we try to get a
journal for a section, a SectionJournalData objecgt is created:
>>> journal = getSectionJournalData(section)
>>> journal
<schooltool.lyceum.journal.journal.SectionJournalData object at ...>
>>> journal.__name__ == str(id(section))
True
>>> journal_container[str(id(section))] is journal
True
If we try to get the journal for the second time, we get the same
journal instance:
>>> getSectionJournalData(section) is journal
True
"""
def doctest_SectionJournal():
"""Tests for SectionJournal adapter:
>>> from schooltool.lyceum.journal.journal import SectionJournal
>>> section = object()
>>> sj = SectionJournal(section)
>>> sj.section is section
True
The section you pass as an argument is set as a section attribute
for the journal.
>>> class SectionDataStub(object):
... grade_data = {}
... absence_data = {}
... def setGrade(self, person, meeting, value, evaluator=None):
... self.grade_data[person, meeting] = value
... def getGrade(self, person, meeting, default):
... return self.grade_data.get((person, meeting), default)
... def setAbsence(self, person, meeting, explained=True, evaluator=None, value=None):
... self.absence_data[person, meeting] = value
... def getAbsence(self, person, meeting, default):
... return self.absence_data.get((person, meeting), default)
>>> section_data = SectionDataStub()
>>> class SectionStub(object):
... def __conform__(self, iface):
... return section_data
>>> class CalendarStub(object):
... def __init__(self, section):
... self.__parent__ = section
>>> class MeetingStub(object):
... __parent__ = CalendarStub(SectionStub())
>>> meeting = MeetingStub()
The grades are stored in the section journal data of the section
that "owns" the meeting:
>>> sj.setGrade("john", meeting, 9)
>>> sj.getGrade("john", meeting, default=0)
9
If there is no value set, the default is returned:
>>> sj.getGrade("pete", meeting, default=0)
0
Absence information belongs in the journal data of the section too:
>>> sj.setAbsence("john", meeting, True)
>>> sj.getAbsence("john", meeting)
'n'
"""
def doctest_SectionJournal_findMeeting():
"""Test for SectionJournal.findMeeting
>>> from schooltool.lyceum.journal.journal import SectionJournal
>>> from schooltool.app.interfaces import ISchoolToolCalendar
>>> class SectionStub(object):
... def __conform__(self, iface):
... if iface == ISchoolToolCalendar:
... return self.calendar
>>> class CalendarStub(object):
... events = []
... def find(self, event_id):
... if event_id in self.events:
... return "<Event uid=%s>" % event_id
... else:
... raise KeyError("Event not found!")
>>> section1 = SectionStub()
>>> section1.calendar = CalendarStub()
>>> section1.calendar.events = ["section-meeting"]
>>> sj = SectionJournal(section1)
If there is no such meeting, a key error is raised:
>>> sj.findMeeting("some-meeting-id")
Traceback (most recent call last):
...
KeyError: 'Event not found!'
But if we are looking for a meeting that belongs to the calendar
of the context section, we should get it:
>>> sj.findMeeting("section-meeting")
'<Event uid=section-meeting>'
"""
def setUp(test):
setup.placelessSetUp()
def tearDown(test):
setup.placelessTearDown()
def test_suite():
optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
return doctest.DocTestSuite(optionflags=optionflags,
setUp=setUp, tearDown=tearDown)
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')
|