1 | # Tests for schoolgrades module. |
---|
2 | import unittest |
---|
3 | from zope.component import getGlobalSiteManager |
---|
4 | from zope.interface.verify import verifyObject, verifyClass |
---|
5 | from zope.schema.interfaces import ConstraintNotSatisfied |
---|
6 | from ikobacustom.pcn.customers.schoolgrades import ( |
---|
7 | IResultEntry, IResultEntryField, |
---|
8 | ResultEntry, ResultEntryField, |
---|
9 | EXAM_SUBJECTS_DICT, EXAM_GRADES) |
---|
10 | |
---|
11 | |
---|
12 | class ResultEntryTests(unittest.TestCase): |
---|
13 | |
---|
14 | def setUp(self): |
---|
15 | self.valid_subj = EXAM_SUBJECTS_DICT.keys()[0] |
---|
16 | self.valid_grade = EXAM_GRADES[0][0] |
---|
17 | return |
---|
18 | |
---|
19 | def tearDown(self): |
---|
20 | return |
---|
21 | |
---|
22 | def test_ifaces(self): |
---|
23 | # make sure we implement the promised interfaces. |
---|
24 | obj = ResultEntry() |
---|
25 | verifyObject(IResultEntry, obj) |
---|
26 | verifyClass(IResultEntry, ResultEntry) |
---|
27 | return |
---|
28 | |
---|
29 | def test_init(self): |
---|
30 | # we can pass initial values |
---|
31 | item1 = ResultEntry() |
---|
32 | item2 = ResultEntry(self.valid_subj, self.valid_grade) |
---|
33 | self.assertTrue(item1.subject is None) |
---|
34 | self.assertTrue(item1.grade is None) |
---|
35 | self.assertEqual(item2.subject, self.valid_subj) |
---|
36 | self.assertEqual(item2.grade, self.valid_grade) |
---|
37 | return |
---|
38 | |
---|
39 | def test_illegal_value(self): |
---|
40 | item = ResultEntry() |
---|
41 | self.assertRaises( |
---|
42 | ConstraintNotSatisfied, ResultEntry, 'invalid', 'invalid') |
---|
43 | self.assertRaises( |
---|
44 | ConstraintNotSatisfied, ResultEntry, 'invalid') |
---|
45 | self.assertRaises( |
---|
46 | ConstraintNotSatisfied, setattr, item, 'subject', 'blah') |
---|
47 | self.assertRaises( |
---|
48 | ConstraintNotSatisfied, setattr, item, 'grade', 'blah') |
---|
49 | return |
---|
50 | |
---|
51 | def test_to_string(self): |
---|
52 | # the string representation is handy for export |
---|
53 | item1 = ResultEntry() |
---|
54 | item2 = ResultEntry(self.valid_subj, self.valid_grade) |
---|
55 | self.assertEqual(item1.to_string(), u"(None, None)") |
---|
56 | self.assertEqual(item2.to_string(), u"('%s', '%s')" % ( |
---|
57 | self.valid_subj, self.valid_grade)) |
---|
58 | return |
---|
59 | |
---|
60 | def test_from_string(self): |
---|
61 | # we can create new result entries based on strings |
---|
62 | myinput = u"(u'%s',u'%s')" % ( |
---|
63 | self.valid_subj, self.valid_grade) |
---|
64 | item1 = ResultEntry.from_string(myinput) |
---|
65 | item2 = ResultEntry.from_string(u"(u'',u'')") |
---|
66 | item3 = ResultEntry.from_string(u"(None, None)") |
---|
67 | self.assertEqual(item1.subject, self.valid_subj) |
---|
68 | self.assertEqual(item1.grade, self.valid_grade) |
---|
69 | self.assertTrue(item2.subject is None) |
---|
70 | self.assertTrue(item2.grade is None) |
---|
71 | self.assertTrue(item3.subject is None) |
---|
72 | self.assertTrue(item3.grade is None) |
---|
73 | return |
---|
74 | |
---|
75 | def test_eq(self): |
---|
76 | # we can compare equality of ResultEntry objects |
---|
77 | item1 = ResultEntry(self.valid_subj, self.valid_grade) |
---|
78 | item2 = ResultEntry(self.valid_subj, self.valid_grade) |
---|
79 | item3 = ResultEntry() |
---|
80 | item4 = ResultEntry() |
---|
81 | assert item1 is not item2 |
---|
82 | assert item1 == item1 |
---|
83 | assert item1 == item2 |
---|
84 | assert item3 is not item4 |
---|
85 | assert item3 == item4 |
---|
86 | assert item1.__eq__(item2) is True |
---|
87 | assert item1.__eq__(item3) is False |
---|
88 | |
---|
89 | def test_ne(self): |
---|
90 | # we can also tell, which ResultEntries are _not_ equal |
---|
91 | item1 = ResultEntry(self.valid_subj, self.valid_grade) |
---|
92 | item2 = ResultEntry() |
---|
93 | assert item1 != item2 |
---|
94 | assert item1.__ne__(item2) is True |
---|
95 | assert item1.__ne__(item1) is False |
---|
96 | |
---|
97 | |
---|
98 | class ResultEntryFieldTests(unittest.TestCase): |
---|
99 | |
---|
100 | def test_ifaces(self): |
---|
101 | # make sure we implement the promised interfaces. |
---|
102 | obj = ResultEntryField() |
---|
103 | verifyObject(IResultEntryField, obj) |
---|
104 | verifyClass(IResultEntryField, ResultEntryField) |
---|
105 | return |
---|