1
1
from nose.tools import assert_equal, raises
3
from ivle.webapp.publisher import (INF, InsufficientPathSegments, NoPath,
4
NotFound, RouteConflict, Publisher, ROOT)
3
from ivle.webapp.routing import (INF, InsufficientPathSegments, NoPath,
4
NotFound, RouteConflict, Router, ROOT)
11
10
def add_subject(self, subject):
12
11
self.subjects[subject.name] = subject
14
def add_user(self, user):
15
self.users[user.login] = user
18
def __init__(self, login):
21
13
class Subject(object):
22
14
def __init__(self, name, code):
94
83
class OfferingAddProject(View):
97
class OfferingWorksheets(View):
100
class OfferingWorksheetMarks(View):
103
class OfferingWorksheetCSVMarks(View):
106
def root_to_subject_or_user(root, name):
107
if name.startswith('~'):
108
return root.users.get(name[1:])
86
def root_to_subject(root, name):
109
87
return root.subjects.get(name)
111
89
def subject_to_offering(subject, year, semester):
140
# A user would be nice.
141
r.add_user(User('jsmith'))
143
118
# Give us some subjects...
144
119
r.add_subject(Subject('info1', '600151'))
145
120
r.add_subject(Subject('info2', '600152'))
172
147
class TestResolution(BaseTest):
174
149
super(TestResolution, self).setUp()
175
self.rtr = Publisher(root=self.r, viewset='browser')
150
self.rtr = Router(root=self.r, viewset='browser')
176
151
self.rtr.add_set_switch('api', 'api')
177
self.rtr.add_forward(Root, None, root_to_subject_or_user, 1)
152
self.rtr.add_forward(Root, None, root_to_subject, 1)
178
153
self.rtr.add_forward(Subject, None, subject_to_offering, 2)
179
154
self.rtr.add_forward(Offering, '+files', offering_to_files, 0)
180
155
self.rtr.add_forward(OfferingFiles, None, offering_files_to_file, INF)
181
156
self.rtr.add_forward(Offering, '+projects', offering_to_project, 1)
182
self.rtr.add_view(User, None, UserServeView, viewset='browser')
183
157
self.rtr.add_view(Subject, '+index', SubjectIndex, viewset='browser')
184
158
self.rtr.add_view(Subject, '+edit', SubjectEdit, viewset='browser')
185
159
self.rtr.add_view(Offering, '+index', OfferingIndex, viewset='browser')
193
167
viewset='browser')
194
168
self.rtr.add_view(Offering, ('+projects', '+index'), OfferingProjects,
195
169
viewset='browser')
196
self.rtr.add_view(Offering, ('+worksheets', '+index'),
197
OfferingWorksheets, viewset='browser')
198
self.rtr.add_view(Offering, ('+worksheets', '+marks', '+index'),
199
OfferingWorksheetMarks, viewset='browser')
200
self.rtr.add_view(Offering, ('+worksheets', '+marks', 'marks.csv'),
201
OfferingWorksheetCSVMarks, viewset='browser')
203
171
def testOneRoute(self):
204
172
assert_equal(self.rtr.resolve('/info1'),
326
294
OfferingProjects, ())
329
def testAnotherDefaultDeepView(self):
330
assert_equal(self.rtr.resolve('/info1/2009/1/+worksheets'),
331
(self.r.subjects['info1'].offerings[(2009, 1)],
332
OfferingWorksheets, ())
335
def testReallyDeepView(self):
337
self.rtr.resolve('/info1/2009/1/+worksheets/+marks/marks.csv'),
338
(self.r.subjects['info1'].offerings[(2009, 1)],
339
OfferingWorksheetCSVMarks, ())
342
def testDefaultReallyDeepView(self):
343
assert_equal(self.rtr.resolve('/info1/2009/1/+worksheets/+marks'),
344
(self.r.subjects['info1'].offerings[(2009, 1)],
345
OfferingWorksheetMarks, ())
348
297
def testNamedRouteWithDeepView(self):
349
298
assert_equal(self.rtr.resolve('/info1/2009/1/+projects/p1'),
350
299
(self.r.subjects['info1'].offerings[(2009, 1)].projects['p1'],
351
300
ProjectIndex, ())
354
def testNullPathView(self):
355
"""Verify that views can be placed immediately under an object.
357
There are some cases in which it is useful for a view with a
358
subpath to exist immediately under an object, with no name.
360
assert_equal(self.rtr.resolve('/~jsmith/foo/bar'),
361
(self.r.users['jsmith'], UserServeView, ('foo', 'bar')))
364
304
class TestGeneration(BaseTest):
366
306
super(TestGeneration, self).setUp()
367
self.rtr = Publisher(root=self.r, viewset='browser')
307
self.rtr = Router(root=self.r, viewset='browser')
368
308
self.rtr.add_set_switch('api', 'api')
369
309
self.rtr.add_reverse(Subject, subject_url)
370
310
self.rtr.add_reverse(Offering, offering_url)
475
415
class TestErrors(BaseTest):
477
417
super(TestErrors, self).setUp()
478
self.rtr = Publisher(root=self.r)
479
self.rtr.add_forward(Root, None, root_to_subject_or_user, 1)
418
self.rtr = Router(root=self.r)
419
self.rtr.add_forward(Root, None, root_to_subject, 1)
480
420
self.rtr.add_forward(Subject, '+foo', lambda s: s.name + 'foo', 0)
481
421
self.rtr.add_forward(Subject, None, subject_to_offering, 2)
482
422
self.rtr.add_reverse(Subject, subject_url)