File: test_wclass.py

package info (click to toggle)
wimsapi 0.5.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 564 kB
  • sloc: python: 3,096; makefile: 16
file content (295 lines) | stat: -rw-r--r-- 10,360 bytes parent folder | download | duplicates (3)
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
import datetime
import os
import unittest
from unittest import mock

from wimsapi.api import WimsAPI
from wimsapi.exceptions import AdmRawError, InvalidItemTypeError, NotSavedError
from wimsapi.sheet import Sheet
from wimsapi.user import User
from wimsapi.wclass import Class, one_year_later


WIMS_URL = os.getenv("WIMS_URL") or "http://localhost:7777/wims/wims.cgi/"



class FakeDate(datetime.date):
    """Used to override datetime.date.today"""
    
    
    @classmethod
    def today(cls):
        """Always return a Date corresponding to 1966-11-16."""
        return datetime.date(1966, 11, 16)



class ClassTestCase(unittest.TestCase):
    
    @classmethod
    def setUpClass(cls):
        """Create an API and an User to use through the tests."""
        cls.api = WimsAPI(WIMS_URL, "myself", "toto")
        cls.user = User("supervisor", "last", "first", "pass", "mail@mail.com")
    
    
    def tearDown(self):
        """Delete classes that a test might have created."""
        self.api.delclass(999999, "myclass")
        self.api.delclass(999666, "myclass")
        self.api.delclass(999990, "myclass")
    
    
    @mock.patch('datetime.date', FakeDate)
    def test_one_year_later(self):
        self.assertEqual(one_year_later(), "19671116")
    
    
    def test_init_and_properties(self):
        c = Class.get(WIMS_URL, "myself", "toto", 9001, "myclass")
        self.assertEqual(c.url, WIMS_URL)
        self.assertEqual(c.ident, "myself")
        self.assertEqual(c.passwd, "toto")
        self.assertIn("description", c.infos)
        
        c = Class("myclass", "A class", "an institution", "mail@mail.com", "password",
                  self.user, qclass=999999)
        with self.assertRaises(NotSavedError):
            c.url
        with self.assertRaises(NotSavedError):
            c.ident
        with self.assertRaises(NotSavedError):
            c.passwd
        with self.assertRaises(NotSavedError):
            c.infos
        
        with self.assertRaises(ValueError):
            Class("myclass", "A class", "an institution", "mail@mail.com", "password",
                  self.user, qclass=999999, lang="Wrong")
        with self.assertRaises(ValueError):
            Class("myclass", "A class", "an institution", "mail@mail.com", "password",
                  self.user, qclass=999999, level="Wrong")
        with self.assertRaises(ValueError):
            Class("myclass", "A class", "an institution", "mail@mail.com", "password",
                  self.user, qclass=999999, expiration="Wrong")
    
    
    def test_append_slash(self):
        url = WIMS_URL if not WIMS_URL.endswith('/') else WIMS_URL[:-1]
        c = Class.get(url, "myself", "toto", 9001, "myclass")
        self.assertEqual(c.url, url + "/")
    
    
    def test_save_and_refresh(self):
        c = Class("myclass", "A class", "an institution", "mail@mail.com", "password",
                  self.user, qclass=999999)
        
        with self.assertRaises(NotSavedError):
            c.save()
        
        with self.assertRaises(NotSavedError):
            c.refresh()
        
        c.save(WIMS_URL, "myself", "toto")
        c = Class.get(WIMS_URL, "myself", "toto", 999999, "myclass")
        c2 = Class.get(WIMS_URL, "myself", "toto", 999999, "myclass")
        
        self.assertEqual(c.institution, "an institution")
        self.assertEqual(c2.institution, "an institution")
        
        c.institution = "modified"
        c.save()
        self.assertEqual(c.institution, "modified")
        self.assertEqual(c2.institution, "an institution")
        
        c2.refresh()
        self.assertEqual(c2.institution, "modified")
        self.api.delclass(99999999, "myclass")
    
    
    def test_save_without_qclass(self):
        c = Class("myclass", "A class", "an institution", "mail@mail.com", "password",
                  self.user)
        
        with self.assertRaises(NotSavedError):
            c.save()
        
        c.save(WIMS_URL, "myself", "toto")
        self.assertIsNotNone(c.qclass)
        c.delete()
    
    
    def test_getitem(self):
        c = Class("myclass", "A class", "an institution", "mail@mail.com", "password",
                  self.user, qclass=999999)
        
        with self.assertRaises(NotSavedError):
            c.getitem("supervisor", User)
        with self.assertRaises(InvalidItemTypeError):
            c.getitem(1, int)
        
        c.save(WIMS_URL, "myself", "toto")
        user = c.getitem("supervisor", User)
        self.assertEqual(user.firstname, self.user.firstname)
    
    
    def test_checkitem(self):
        c = Class("myclass", "A class", "an institution", "mail@mail.com", "password",
                  self.user, qclass=999999)
        unknown = User("unknown", "last", "first", "pass", "mail2@mail.com")
        
        with self.assertRaises(NotSavedError):
            c.checkitem("supervisor", User)
        with self.assertRaises(InvalidItemTypeError):
            c.checkitem(1)
        
        c.save(WIMS_URL, "myself", "toto")
        self.assertTrue(c.checkitem("supervisor", User))
        self.assertTrue(c.checkitem(self.user))
        self.assertFalse(c.checkitem("Unknown", User))
        self.assertFalse(c.checkitem(unknown))
    
    
    def test___contains__(self):
        c = Class("myclass", "A class", "an institution", "mail@mail.com", "password",
                  self.user, qclass=999999)
        unknown = User("unknown", "last", "first", "pass", "mail2@mail.com")
        
        with self.assertRaises(NotSavedError):
            unknown in c
        
        c.save(WIMS_URL, "myself", "toto")
        self.assertTrue(self.user in c)
        self.assertFalse(unknown in c)
    
    
    def test_additem(self):
        c = Class("myclass", "A class", "an institution", "mail@mail.com", "password",
                  self.user, qclass=999999)
        u = User("quser", "last", "first", "pass", "mail2@mail.com")
        
        with self.assertRaises(NotSavedError):
            c.additem(u)
        with self.assertRaises(InvalidItemTypeError):
            c.additem(int)
        
        c.save(WIMS_URL, "myself", "toto")
        c.additem(u)
        self.assertEqual(u._class.qclass, c.qclass)
        self.assertEqual(u.wclass, True)
        
        u2 = c.getitem("quser", User)
        self.assertEqual(u2.firstname, u.firstname)
    
    
    def test_delitem(self):
        c = Class("myclass", "A class", "an institution", "mail@mail.com", "password",
                  self.user, qclass=999999)
        u = User("quser", "last", "first", "pass", "mail2@mail.com")
        u2 = User("quser2", "last", "first", "pass", "mail2@mail.com")
        
        with self.assertRaises(NotSavedError):
            c.delitem(u)
        with self.assertRaises(InvalidItemTypeError):
            c.delitem(int)
        
        c.save(WIMS_URL, "myself", "toto")
        c.additem(u)
        c.additem(u2)
        
        self.assertTrue(c.checkitem("quser", User))
        c.delitem(u)
        self.assertFalse(c.checkitem("quser", User))
        
        self.assertTrue(c.checkitem("quser2", User))
        c.delitem("quser2", User)
        self.assertFalse(c.checkitem("quser2", User))
    
    
    def test_delete(self):
        c = Class("myclass", "A class", "an institution", "mail@mail.com", "password",
                  self.user, qclass=999999)
        
        with self.assertRaises(NotSavedError):
            c.delete()
        
        c.save(WIMS_URL, "myself", "toto")
        
        Class.get(WIMS_URL, "myself", "toto", c.qclass, c.rclass)  #  Ok
        c.delete()
        with self.assertRaises(AdmRawError):
            Class.get(WIMS_URL, "myself", "toto", c.qclass, c.rclass)  # Should raise the exception
    
    
    def test_list(self):
        c1 = Class("rclass", "A class", "an institution", "mail@mail.com", "password", self.user)
        c2 = Class("rclass", "A class", "an institution", "mail@mail.com", "password", self.user)
        c3 = Class("rclass", "A class", "an institution", "mail@mail.com", "password", self.user)
        
        c1.save(WIMS_URL, "myself", "toto")
        c2.save(WIMS_URL, "myself", "toto")
        c3.save(WIMS_URL, "myself", "toto")
        
        self.assertListEqual(
            sorted([c1, c2, c3], key=lambda i: i.qclass),
            sorted(Class.list(WIMS_URL, "myself", "toto", "rclass"), key=lambda i: i.qclass)
        )
        
        self.assertListEqual(
            [],
            Class.list(WIMS_URL, "myself", "toto", "unknown_rclass")
        )
        
        c1.delete()
        c2.delete()
        c3.delete()
    
    
    def test_eq(self):
        c1 = Class("rclass", "A class", "an institution", "mail@mail.com", "password", self.user)
        c2 = Class("rclass", "A class", "an institution", "mail@mail.com", "password", self.user)
        c3 = Class("rclass", "A class", "an institution", "mail@mail.com", "password", self.user)
        
        with self.assertRaises(NotSavedError):
            c1 == c3
        
        c1.save(WIMS_URL, "myself", "toto")
        c2.save(WIMS_URL, "myself", "toto")
        c3.save(WIMS_URL, "myself", "toto")
        
        self.assertEqual(c1, Class.get(WIMS_URL, "myself", "toto", c1.qclass, c1.rclass))
        self.assertNotEqual(c2, Class.get(WIMS_URL, "myself", "toto", c1.qclass, c1.rclass))
        self.assertNotEqual(c2, 1)
        
        c1.delete()
        c2.delete()
        c3.delete()
    
    
    def test_listitem(self):
        c = Class("rclass", "A class", "an institution", "mail@mail.com", "password",
                  self.user, qclass=999999)
        
        with self.assertRaises(NotSavedError):
            c.listitem(Sheet)
        with self.assertRaises(InvalidItemTypeError):
            c.listitem(int)
        
        s1 = Sheet("First", "First one")
        s2 = Sheet("Second", "Second one")
        s3 = Sheet("Third", "Third one")
        
        c.save(WIMS_URL, "myself", "toto")
        
        self.assertListEqual([], c.listitem(Sheet))
        
        c.additem(s1)
        c.additem(s2)
        c.additem(s3)
        
        self.assertListEqual(
            sorted([s1, s2, s3], key=lambda i: i.qsheet),
            sorted(c.listitem(Sheet), key=lambda i: i.qsheet))
        
        c.delete()