1 | ## $Id: test_vocabularies.py 9778 2012-12-06 15:45:03Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | import unittest |
---|
19 | from waeup.kofa.students.vocabularies import ( |
---|
20 | StudyLevelSource, levels_from_range, |
---|
21 | ) |
---|
22 | from waeup.kofa.university.certificate import Certificate |
---|
23 | |
---|
24 | class HelperTests(unittest.TestCase): |
---|
25 | |
---|
26 | def test_levels_from_range(self): |
---|
27 | # default |
---|
28 | self.assertEqual( |
---|
29 | levels_from_range(), |
---|
30 | [10, |
---|
31 | 100, 110, 120, 200, 210, 220, 300, 310, 320, 400, 410, 420, |
---|
32 | 500, 510, 520, 600, 610, 620, 700, 710, 720, 800, 810, 820, |
---|
33 | 900, 910, 920, 999,]) |
---|
34 | # edge-case: level 10 |
---|
35 | self.assertEqual( |
---|
36 | levels_from_range(10, 10), [10]) |
---|
37 | # edge-case: level 999 |
---|
38 | self.assertEqual( |
---|
39 | levels_from_range(999, 10), [999]) |
---|
40 | self.assertEqual( |
---|
41 | levels_from_range(10, 999), [999]) |
---|
42 | self.assertEqual( |
---|
43 | levels_from_range(10, 200), |
---|
44 | [10, 100, 110, 120, 200, 210, 220, 300, 310, 320]) |
---|
45 | self.assertEqual( |
---|
46 | levels_from_range(100, 300), |
---|
47 | [100, 110, 120, 200, 210, 220, 300, 310, 320, 400, 410, 420]) |
---|
48 | self.assertEqual( |
---|
49 | levels_from_range(800, 900), |
---|
50 | [800, 810, 820, 900, 910, 920]) |
---|
51 | return |
---|
52 | |
---|
53 | class FakeStudyCourse(object): |
---|
54 | |
---|
55 | def __init__(self): |
---|
56 | cert = Certificate(code="CERT1") |
---|
57 | cert.start_level = 100 |
---|
58 | cert.end_level = 500 |
---|
59 | self.certificate = cert |
---|
60 | |
---|
61 | class VocabularyTests(unittest.TestCase): |
---|
62 | |
---|
63 | def test_studylevelsource(self): |
---|
64 | studycourse = FakeStudyCourse() |
---|
65 | studylevelsource = StudyLevelSource().factory |
---|
66 | values = studylevelsource.getValues(studycourse) |
---|
67 | self.assertEqual(values, [100, 110, 120, 200, 210, 220, 300, 310, 320, |
---|
68 | 400, 410, 420, 500, 510, 520, 600, 610, 620]) |
---|
69 | # All titles do exist |
---|
70 | titles = [studylevelsource.getTitle(studycourse, value) |
---|
71 | for value in values] |
---|
72 | self.assertEqual(len(titles), 18) |
---|
73 | # Unfortunately, unittests don't know about internationalization |
---|
74 | self.assertEqual(studylevelsource.getTitle(studycourse, None), |
---|
75 | 'Error: level id ${value} out of range') |
---|
76 | self.assertEqual(studylevelsource.getTitle(studycourse, 0), |
---|
77 | 'Error: level id ${value} out of range') |
---|
78 | self.assertEqual(studylevelsource.getTitle(studycourse, 10), |
---|
79 | 'Error: level id ${value} out of range') |
---|
80 | self.assertEqual(studylevelsource.getTitle(studycourse, 100), |
---|
81 | '100 (Year 1)') |
---|
82 | self.assertEqual(studylevelsource.getTitle(studycourse, 110), |
---|
83 | '${title} on 1st probation') |
---|
84 | self.assertEqual(studylevelsource.getTitle(studycourse, 120), |
---|
85 | '${title} on 2nd probation') |
---|
86 | self.assertEqual(studylevelsource.getTitle(studycourse, 500), |
---|
87 | '500 (Year 5)') |
---|
88 | self.assertEqual(studylevelsource.getTitle(studycourse, 600), |
---|
89 | '${title} 1st spillover') |
---|
90 | self.assertEqual(studylevelsource.getTitle(studycourse, 610), |
---|
91 | '${title} 2nd spillover') |
---|
92 | self.assertEqual(studylevelsource.getTitle(studycourse, 620), |
---|
93 | '${title} 3rd spillover') |
---|
94 | self.assertEqual(studylevelsource.getTitle(studycourse, 630), |
---|
95 | 'Error: level id ${value} out of range') |
---|
96 | self.assertEqual(studylevelsource.getTitle(studycourse, 700), |
---|
97 | 'Error: level id ${value} out of range') |
---|
98 | # Now we modify the certificates to be a pure to pre-studies |
---|
99 | # course |
---|
100 | studycourse.certificate.start_level = 10 |
---|
101 | studycourse.certificate.end_level = 10 |
---|
102 | values = studylevelsource.getValues(studycourse) |
---|
103 | self.assertEqual(values, [10]) |
---|
104 | titles = [studylevelsource.getTitle(studycourse, value) |
---|
105 | for value in values] |
---|
106 | self.assertEqual(len(titles), 1) |
---|
107 | self.assertEqual(studylevelsource.getTitle(studycourse, 10), |
---|
108 | 'Pre-Studies') |
---|
109 | self.assertEqual(studylevelsource.getTitle(studycourse, 100), |
---|
110 | 'Error: level id ${value} out of range') |
---|
111 | self.assertEqual(studylevelsource.getTitle(studycourse, 200), |
---|
112 | 'Error: level id ${value} out of range') |
---|
113 | # Finally we modify the certificate to be a mixed course, |
---|
114 | # starting with pre-studies. |
---|
115 | studycourse.certificate.end_level = 200 |
---|
116 | values = studylevelsource.getValues(studycourse) |
---|
117 | self.assertEqual(values, [10, 100, 110, 120, |
---|
118 | 200, 210, 220, 300, 310, 320]) |
---|
119 | titles = [studylevelsource.getTitle(studycourse, value) |
---|
120 | for value in values] |
---|
121 | self.assertEqual(len(titles), 10) |
---|
122 | # Repeating Pre-Studies level does not exist and raises a KeyError |
---|
123 | # when trying to get its title |
---|
124 | self.assertRaises(KeyError,studylevelsource.getTitle,studycourse,20) |
---|