1
1
from nose.tools import assert_equal, raises
3
from ivle.webapp.routing import (INF, InsufficientPathSegments, NoPath,
4
NotFound, RouteConflict, Router, ROOT)
3
from ivle.webapp.publisher import (INF, InsufficientPathSegments, NoPath,
4
NotFound, RouteConflict, Publisher, ROOT)
10
11
def add_subject(self, subject):
11
12
self.subjects[subject.name] = subject
14
def add_user(self, user):
15
self.users[user.login] = user
18
def __init__(self, login):
13
21
class Subject(object):
14
22
def __init__(self, name, code):
83
94
class OfferingAddProject(View):
86
def root_to_subject(root, name):
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:])
87
109
return root.subjects.get(name)
89
111
def subject_to_offering(subject, year, semester):
140
# A user would be nice.
141
r.add_user(User('jsmith'))
118
143
# Give us some subjects...
119
144
r.add_subject(Subject('info1', '600151'))
120
145
r.add_subject(Subject('info2', '600152'))
147
172
class TestResolution(BaseTest):
149
174
super(TestResolution, self).setUp()
150
self.rtr = Router(root=self.r, viewset='browser')
175
self.rtr = Publisher(root=self.r, viewset='browser')
151
176
self.rtr.add_set_switch('api', 'api')
152
self.rtr.add_forward(Root, None, root_to_subject, 1)
177
self.rtr.add_forward(Root, None, root_to_subject_or_user, 1)
153
178
self.rtr.add_forward(Subject, None, subject_to_offering, 2)
154
179
self.rtr.add_forward(Offering, '+files', offering_to_files, 0)
155
180
self.rtr.add_forward(OfferingFiles, None, offering_files_to_file, INF)
156
181
self.rtr.add_forward(Offering, '+projects', offering_to_project, 1)
182
self.rtr.add_view(User, None, UserServeView, viewset='browser')
157
183
self.rtr.add_view(Subject, '+index', SubjectIndex, viewset='browser')
158
184
self.rtr.add_view(Subject, '+edit', SubjectEdit, viewset='browser')
159
185
self.rtr.add_view(Offering, '+index', OfferingIndex, viewset='browser')
167
193
viewset='browser')
168
194
self.rtr.add_view(Offering, ('+projects', '+index'), OfferingProjects,
169
195
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')
171
203
def testOneRoute(self):
172
204
assert_equal(self.rtr.resolve('/info1'),
294
326
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, ())
297
348
def testNamedRouteWithDeepView(self):
298
349
assert_equal(self.rtr.resolve('/info1/2009/1/+projects/p1'),
299
350
(self.r.subjects['info1'].offerings[(2009, 1)].projects['p1'],
300
351
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')))
304
364
class TestGeneration(BaseTest):
306
366
super(TestGeneration, self).setUp()
307
self.rtr = Router(root=self.r, viewset='browser')
367
self.rtr = Publisher(root=self.r, viewset='browser')
308
368
self.rtr.add_set_switch('api', 'api')
309
369
self.rtr.add_reverse(Subject, subject_url)
310
370
self.rtr.add_reverse(Offering, offering_url)
415
475
class TestErrors(BaseTest):
417
477
super(TestErrors, self).setUp()
418
self.rtr = Router(root=self.r)
419
self.rtr.add_forward(Root, None, root_to_subject, 1)
478
self.rtr = Publisher(root=self.r)
479
self.rtr.add_forward(Root, None, root_to_subject_or_user, 1)
420
480
self.rtr.add_forward(Subject, '+foo', lambda s: s.name + 'foo', 0)
421
481
self.rtr.add_forward(Subject, None, subject_to_offering, 2)
422
482
self.rtr.add_reverse(Subject, subject_url)