source: main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_utils.py @ 12943

Last change on this file since 12943 was 11595, checked in by Henrik Bettermann, 11 years ago

constructMatricNumber must not be private. We will be calling it in custom packages.

  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1# -*- coding: utf-8 -*-
2import unittest
3import grok
4from zope.component import getUtility, queryUtility, createObject
5from zope.catalog.interfaces import ICatalog
6from waeup.kofa.students.interfaces import IStudentsUtils
7from waeup.kofa.students.utils import formatted_text
8from waeup.kofa.students.tests.test_browser import StudentsFullSetup
9
10class FormatterTests(unittest.TestCase):
11
12    def test_formatted_text(self):
13        # we can format strings, unicode and things convertable to unicode.
14        result1 = formatted_text('sample')
15        result2 = formatted_text('ümlaut')
16        result3 = formatted_text(3)
17        result4 = formatted_text(u'unicöde')
18        result5 = formatted_text(u'sample', color='red')
19        self.assertTrue(isinstance(result1, unicode))
20        self.assertEqual(
21            result1, u'<font color="black">sample</font>')
22        self.assertEqual(
23            result2, u'<font color="black">ümlaut</font>')
24        self.assertEqual(
25            result3, u'<font color="black">3</font>')
26        self.assertEqual(
27            result4, u'<font color="black">unicöde</font>')
28        self.assertEqual(
29            result5, u'<font color="red">sample</font>')
30        return
31
32class StudentsUtilsTests(StudentsFullSetup):
33
34    def test_setReturningData(self):
35        utils = getUtility(IStudentsUtils)
36        self.student['studycourse'].current_level = 600
37        utils.setReturningData(self.student)
38        # The new level exceeds the certificates end_level.
39        # In this case current_level remains unchanged and no error is raised.
40        self.assertEqual(self.student['studycourse'].current_level, 600)
41
42    def test_setMatricNumber(self):
43        site = grok.getSite()
44        utils = getUtility(IStudentsUtils)
45        # Matric number can't be set twice.
46        msg, mnumber = utils.setMatricNumber(self.student)
47        self.assertEqual(msg, 'Matriculation number already set.')
48        self.assertEqual(mnumber, None)
49        self.assertEqual(self.student.matric_number, '234')
50        self.student.matric_number = None
51        # Matric number can't be set if next_matric_integer is 0.
52        msg, mnumber = utils.setMatricNumber(self.student)
53        self.assertEqual(msg, 'Matriculation number cannot be set.')
54        self.assertEqual(mnumber, None)
55        site['configuration'].next_matric_integer = 1
56        # Now all requirements are met and matric number can be set.
57        msg, mnumber = utils.setMatricNumber(self.student)
58        self.assertEqual(msg, None)
59        self.assertEqual(mnumber, '1')
60        self.assertEqual(self.student.matric_number, '1')
61        self.assertEqual(site['configuration'].next_matric_integer, 2)
62        # Student can be found in catalog with new matric number.
63        cat = queryUtility(ICatalog, name='students_catalog')
64        results = list(cat.searchResults(matric_number=('1', '1')))
65        self.assertEqual(self.student,results[0])
66        # Add another student.
67        another_student = createObject('waeup.Student')
68        another_student.matric_number = u'2'
69        self.app['students'].addStudent(another_student)
70        # Matric number can't be assigned twice.
71        self.student.matric_number = None
72        msg, mnumber = utils.setMatricNumber(self.student)
73        self.assertEqual(msg, 'Matriculation number exists.')
74        self.assertEqual(mnumber, None)
75        self.assertEqual(self.student.matric_number, None)
76        # Certificate must be set.
77        self.student.matric_number = None
78        another_student.matric_number = u'999'
79        self.student['studycourse'].certificate = None
80        msg, mnumber = utils.setMatricNumber(self.student)
81        self.assertEqual(msg, 'No certificate assigned.')
82        return
83
Note: See TracBrowser for help on using the repository browser.