def test_register_adds_default_routes_from_modelname(self): class ParentView(ModelView): pass class FooView(ParentView): model = FooModel class BarView(ParentView): model = BarModel r = Router() r.register(ParentView) urls_module.urlpatterns = [url(r'^', include(r.urls))] self.assertTrue(is_valid_path('/foo_model/', urls_module)) self.assertTrue(is_valid_path('/foo_model/1/', urls_module)) self.assertTrue(is_valid_path('/bar_model/12345/', urls_module)) self.assertFalse(is_valid_path('/bar_model/lalala/', urls_module)) self.assertFalse(is_valid_path('/another_model/', urls_module))
def test_double_model_registration_triggers_error(self): class ParentView(ModelView): pass class FooView1(ParentView): model = FooModel class FooView2(ParentView): model = FooModel with self.assertRaises(ValueError): Router().register(ParentView)
def test_register_obeys_custom_route_config(self): class ParentView(ModelView): pass class FooView(ParentView): model = FooModel route = Route('foo', list_endpoint=False) class BarView(ParentView): model = BarModel route = Route('bar', detail_endpoint=False) r = Router() r.register(ParentView) urls_module.urlpatterns = [url(r'^', include(r.urls))] self.assertFalse(is_valid_path('/foo/', urls_module)) self.assertTrue(is_valid_path('/foo/1/', urls_module)) self.assertTrue(is_valid_path('/bar/', urls_module)) self.assertFalse(is_valid_path('/bar/1/', urls_module))
def test_simple_follow_related(self): """ previously, this threw the following error: File "/binder/binder/views.py", line 540, in _follow_related if '+' in related_field: # Skip missing related fields TypeError: argument of type 'NoneType' is not iterable :return: """ router = Router() router.register(AbstractBarView) view = FooView() view.router = router result = view._follow_related(['bar']) self.assertIsInstance(result[0], RelatedModel) self.assertIsNone(result[0].reverse_fieldname) self.assertEquals('bar', result[0].fieldname) self.assertEqual(BarModel, result[0].model)
def test_standard_filling_in_relation_to_existing_model(self): animal = Animal.objects.create(name='foo') caretaker = Caretaker.objects.create(name='bar') animal_view = AnimalView() class FakeUser: def has_perm(self, perm): return True class FakeRequest: user = FakeUser() GET = {} router = Router() router.register(AnimalView) animal_view.router = router animal_view._store(animal, {'caretaker': caretaker.pk}, FakeRequest()) self.assertEqual(animal.caretaker, caretaker)
def test_setting_none_existing_caretaker_gives_validation_error(self): animal = Animal.objects.create( name='foo', caretaker=Caretaker.objects.create(name='bar2')) animal_view = AnimalView() class FakeUser: def has_perm(self, perm): return True class FakeRequest: user = FakeUser() GET = {} router = Router() router.register(AnimalView) animal_view.router = router animal.caretaker with self.assertRaises(BinderValidationError): animal_view._store(animal, {'caretaker': -1}, FakeRequest())
def test_double_route_registration_triggers_error(self): class ParentView(ModelView): pass class FooView(ParentView): model = FooModel route = 'myroute' class BarView(ParentView): model = BarModel route = 'myroute' with self.assertRaises(ValueError): Router().register(ParentView)
def test_register_adds_custom_route_names(self): class ParentView(ModelView): pass class FooView(ParentView): model = FooModel route = 'foo' class BarView(ParentView): model = BarModel # Explicit Route objects should also be accepted route = Route('bar') r = Router() r.register(ParentView) urls_module.urlpatterns = [url(r'^', include(r.urls))] self.assertTrue(is_valid_path('/foo/', urls_module)) self.assertTrue(is_valid_path('/foo/1/', urls_module)) self.assertTrue(is_valid_path('/bar/12345/', urls_module)) # Default named routes should not be there self.assertFalse(is_valid_path('/foo_model/1/', urls_module)) self.assertFalse(is_valid_path('/bar_model/1/', urls_module))