class TestErrors(BaseTest): def setUp(self): super(TestErrors, self).setUp() self.rtr = Publisher(root=self.r) self.rtr.add_forward(Root, None, root_to_subject_or_user, 1) self.rtr.add_forward(Subject, '+foo', lambda s: s.name + 'foo', 0) self.rtr.add_forward(Subject, None, subject_to_offering, 2) self.rtr.add_reverse(Subject, subject_url) self.rtr.add_reverse(Offering, offering_url) self.rtr.add_view(Offering, '+index', OfferingIndex) self.rtr.add_view(Offering, '+index', OfferingAPIIndex, viewset='api') self.rtr.add_set_switch('rest', 'rest') @raises(RouteConflict) def testForwardConflict(self): self.rtr.add_forward(Subject, '+foo', object(), 2) @raises(RouteConflict) def testReverseConflict(self): self.rtr.add_reverse(Subject, object()) @raises(RouteConflict) def testViewConflict(self): self.rtr.add_view(Offering, '+index', object()) @raises(RouteConflict) def testSetSwitchForwardConflict(self): self.rtr.add_set_switch('rest', 'foo') @raises(RouteConflict) def testSetSwitchReverseConflict(self): self.rtr.add_set_switch('bar', 'rest') @raises(NoPath) def testNoPath(self): self.rtr.generate(object()) @raises(NoPath) def testNoSetSwitch(self): self.rtr.generate(self.r.subjects['info1'].offerings[(2009, 1)], OfferingAPIIndex) @raises(NoPath) def testUnregisteredView(self): self.rtr.generate(self.r.subjects['info1'], SubjectIndex) @raises(NotFound) def testNotFound(self): self.rtr.resolve('/bar') @raises(InsufficientPathSegments) def testInsufficientPathSegments(self): self.rtr.resolve('/info1/foo')
class TestGeneration(BaseTest): def setUp(self): super(TestGeneration, self).setUp() self.rtr = Publisher(root=self.r, viewset='browser') self.rtr.add_set_switch('api', 'api') self.rtr.add_reverse(Subject, subject_url) self.rtr.add_reverse(Offering, offering_url) self.rtr.add_reverse(OfferingFiles, offering_files_url) self.rtr.add_reverse(Project, project_url) self.rtr.add_view(Subject, '+index', SubjectIndex, viewset='browser') self.rtr.add_view(Subject, '+edit', SubjectEdit, viewset='browser') self.rtr.add_view(Offering, '+index', OfferingIndex, viewset='browser') self.rtr.add_view(Offering, '+index', OfferingAPIIndex, viewset='api') self.rtr.add_view(Project, '+index', ProjectIndex, viewset='browser') self.rtr.add_view(Offering, ('+projects', '+new'), OfferingAddProject, viewset='browser') self.rtr.add_view(Offering, ('+projects', '+index'), OfferingProjects, viewset='browser') def testOneLevel(self): assert_equal(self.rtr.generate(self.r.subjects['info1']), '/info1') def testTwoLevel(self): assert_equal( self.rtr.generate(self.r.subjects['info1'].offerings[(2009, 1)]), '/info1/2009/1' ) assert_equal( self.rtr.generate(self.r.subjects['info2'].offerings[(2008, 2)]), '/info2/2008/2' ) def testNamedRoute(self): assert_equal(self.rtr.generate( OfferingFiles(self.r.subjects['info1'].offerings[(2009, 1)])), '/info1/2009/1/+files' ) def testView(self): assert_equal(self.rtr.generate(self.r.subjects['info1'], SubjectEdit), '/info1/+edit' ) def testDefaultView(self): assert_equal( self.rtr.generate(self.r.subjects['info1'].offerings[(2009, 1)], OfferingIndex ), '/info1/2009/1' ) def testViewWithSubpath(self): assert_equal(self.rtr.generate(self.r.subjects['info1'], SubjectEdit, ('foo', 'bar')), '/info1/+edit/foo/bar' ) def testViewWithStringSubpath(self): assert_equal(self.rtr.generate(self.r.subjects['info1'], SubjectEdit, 'foo/bar'), '/info1/+edit/foo/bar' ) def testAlternateViewSetWithDefault(self): assert_equal( self.rtr.generate(self.r.subjects['info1'].offerings[(2009, 1)], OfferingAPIIndex ), '/api/info1/2009/1' ) def testDeepView(self): assert_equal( self.rtr.generate( self.r.subjects['info1'].offerings[(2009, 1)], OfferingAddProject ), '/info1/2009/1/+projects/+new' ) def testDefaultDeepView(self): assert_equal( self.rtr.generate( self.r.subjects['info1'].offerings[(2009, 1)], OfferingProjects ), '/info1/2009/1/+projects' ) def testDefaultDeepViewWithSubpath(self): assert_equal( self.rtr.generate( self.r.subjects['info1'].offerings[(2009, 1)], OfferingProjects, ('foo', 'bar') ), '/info1/2009/1/+projects/+index/foo/bar' ) def testNamedRouteWithDeepView(self): assert_equal( self.rtr.generate( self.r.subjects['info1'].offerings[(2009, 1)].projects['p1'], ProjectIndex ), '/info1/2009/1/+projects/p1' ) def testRoot(self): assert_equal(self.rtr.generate(self.r), '/')
class TestResolution(BaseTest): def setUp(self): super(TestResolution, self).setUp() self.rtr = Publisher(root=self.r, viewset='browser') self.rtr.add_set_switch('api', 'api') self.rtr.add_forward(Root, None, root_to_subject_or_user, 1) self.rtr.add_forward(Subject, None, subject_to_offering, 2) self.rtr.add_forward(Offering, '+files', offering_to_files, 0) self.rtr.add_forward(OfferingFiles, None, offering_files_to_file, INF) self.rtr.add_forward(Offering, '+projects', offering_to_project, 1) self.rtr.add_view(User, None, UserServeView, viewset='browser') self.rtr.add_view(Subject, '+index', SubjectIndex, viewset='browser') self.rtr.add_view(Subject, '+edit', SubjectEdit, viewset='browser') self.rtr.add_view(Offering, '+index', OfferingIndex, viewset='browser') self.rtr.add_view(Offering, '+index', OfferingAPIIndex, viewset='api') self.rtr.add_view(OfferingFiles, '+index', OfferingFilesIndex, viewset='browser') self.rtr.add_view(OfferingFile, '+index', OfferingFileIndex, viewset='browser') self.rtr.add_view(Project, '+index', ProjectIndex, viewset='browser') self.rtr.add_view(Offering, ('+projects', '+new'), OfferingAddProject, viewset='browser') self.rtr.add_view(Offering, ('+projects', '+index'), OfferingProjects, viewset='browser') self.rtr.add_view(Offering, ('+worksheets', '+index'), OfferingWorksheets, viewset='browser') self.rtr.add_view(Offering, ('+worksheets', '+marks', '+index'), OfferingWorksheetMarks, viewset='browser') self.rtr.add_view(Offering, ('+worksheets', '+marks', 'marks.csv'), OfferingWorksheetCSVMarks, viewset='browser') def testOneRoute(self): assert_equal(self.rtr.resolve('/info1'), (self.r.subjects['info1'], SubjectIndex, ()) ) assert_equal(self.rtr.resolve('/info3'), (self.r.subjects['info3'], SubjectIndex, ()) ) def testTwoRoutes(self): assert_equal(self.rtr.resolve('/info1/2009/1'), (self.r.subjects['info1'].offerings[(2009, 1)], OfferingIndex, ()) ) assert_equal(self.rtr.resolve('/info2/2008/2'), (self.r.subjects['info2'].offerings[(2008, 2)], OfferingIndex, ()) ) def testNamedRoute(self): assert_equal(type(self.rtr.resolve('/info1/2009/1/+files')[0]), OfferingFiles ) assert_equal(self.rtr.resolve('/info1/2009/1/+files')[0].offering, self.r.subjects['info1'].offerings[(2009, 1)] ) def testNonDefaultView(self): assert_equal(self.rtr.resolve('/info1/+edit'), (self.r.subjects['info1'], SubjectEdit, ()) ) def testDefaultView(self): assert_equal(self.rtr.resolve('/info1'), (self.r.subjects['info1'], SubjectIndex, ()) ) def testViewWithSubpath(self): assert_equal(self.rtr.resolve('/info1/+edit/foo/bar'), (self.r.subjects['info1'], SubjectEdit, ('foo', 'bar')) ) def testNoDefaultView(self): try: self.rtr.default = 'not+index' self.rtr.resolve('/info1') except NotFound, e: assert_equal(e.args, (self.r.subjects['info1'], '+index', ())) except: