def test_to_representation_returns_none(self): class FakeModel(object): def __init__(self, pk): self.pk = pk def __str__(self): return "object.pk={}".format(self.pk) class FakeHyperlinkField(self.CustomHyperlinkedRelatedField): def is_saved_in_database(self, obj): return False def get_lookup_value(self, obj): return (1, ) router = SimpleRouter() router.register('/test_view/{pk}', FakeView, 'GET', name='test_view') url_configuration = { 'path': 'wss://127.0.0.1:8000', 'urls': router._urls, 'routes': router._routes } set_urlconf(url_configuration) instance = FakeHyperlinkField('test_view', read_only=True) self.assertIsNone(instance.to_representation(FakeModel(1)))
def test_to_representation_raises_improperly_configured(self): class FakeModel(object): def __init__(self, pk): self.pk = pk def __str__(self): return "object.pk={}".format(self.pk) class FakeHyperlinkField(self.CustomHyperlinkedRelatedField): def is_saved_in_database(self, obj): return True def get_lookup_value(self, obj): return () router = SimpleRouter() url_configuration = { 'path': 'wss://127.0.0.1:8000', 'urls': router._urls, 'routes': router._routes } set_urlconf(url_configuration) instance = FakeHyperlinkField('test_view', read_only=True) with self.assertRaises(ImproperlyConfigured): instance.to_representation(FakeModel(1))
def test_to_internal_value_raises_error_with_fixing_url(self): class FakeModel(object): def __init__(self, pk): self.pk = pk class FakeHyperlinkField(self.CustomHyperlinkedRelatedField): def get_object(self, view_name, view_args, view_kwargs): return FakeModel(1) router = SimpleRouter() router.register('/test_view/{pk}', FakeView, 'GET', name='test_view') url_configuration = { 'path': 'wss://127.0.0.1:8000', 'urls': router._urls, 'routes': router._routes } set_urlconf(url_configuration) instance = FakeHyperlinkField('test_view', read_only=True) data = {'url': 'test_view/1'} obj = instance.to_internal_value(data) self.assertIsInstance(obj, FakeModel) self.assertEqual(obj.pk, 1)
def setUpClass(cls): super(TestHyperlinkedRelatedField, cls).setUpClass() session = SESSION() user = cls.TestHyperlinkedRelatedFieldUserModel(name='admin') address = cls.TestHyperlinkedRelatedFieldAddressModel( email_address='*****@*****.**') user_composite_pk = cls.TestHyperlinkedRelatedFieldCompositePkModel( id=1, name='test_user') user.addresses.append(address) session.add(user) session.add(address) session.add(user_composite_pk) session.commit() session.close() url_parser = URLParser() cls.data = { 'urls': [ url_parser.define_route('/user/{pk}/', FakeView, [ 'GET', ], name='user-detail'), ] } set_urlconf(cls.data)
def _init_urlconf(self, factory, url, **options): """ Initialize urlconf thread variable. """ data = { 'path': url.strip('/'), 'urls': factory.router._urls, 'routes': factory.router._routes } set_urlconf(data)
def setUp(self): super(TestReverse, self).setUpClass() url_parser = URLParser() data = { 'path': 'wss://127.0.0.1:8000', 'routes': { 'user-detail': url_parser.define_route( '/user/{pk}/', FakeView, ['GET', ], name='user-detail' ) } } set_urlconf(data)
def test_to_internal_value_raises_error_for_incorrect_type(self): router = SimpleRouter() router.register('/test_view/{pk}', FakeView, 'GET', name='test_view') url_configuration = { 'path': 'wss://127.0.0.1:8000/', 'urls': router._urls, 'routes': router._routes } set_urlconf(url_configuration) instance = self.CustomHyperlinkedRelatedField( 'test_view', read_only=True ) with self.assertRaises(ValidationError): instance.to_internal_value('not a dict')
def setUp(self): super(TestReverse, self).setUpClass() url_parser = URLParser() data = { 'path': 'wss://127.0.0.1:8000', 'routes': { 'user-detail': url_parser.define_route('/user/{pk}/', FakeView, [ 'GET', ], name='user-detail') } } set_urlconf(data)
def setUpClass(cls): super(TestHyperlinkedRelatedField, cls).setUpClass() cls.publisher = cls.Publisher.objects.create(name='publisher') cls.book = cls.Book.objects.create(name="test book") cls.book.publisher = cls.publisher cls.book.save() url_parser = URLParser() cls.data = { 'urls': [ url_parser.define_route( '/book/{pk}/', FakeView, ['GET', ], name='book-detail' ), ] } set_urlconf(cls.data)
def test_to_internal_value_raises_error_for_no_primary_key(self): router = SimpleRouter() router.register('/test_view/{pk}', FakeView, 'GET', name='test_view') url_configuration = { 'path': 'wss://127.0.0.1:8000', 'urls': router._urls, 'routes': router._routes } set_urlconf(url_configuration) instance = self.CustomHyperlinkedRelatedField( 'test_view', read_only=True ) data = {'wrong_url_name': 'wss://127.0.0.1:8000/test_view/1/'} with self.assertRaises(ValidationError): instance.to_internal_value(data)
def test_to_representation_raises_exception_for_empty_string(self): class FakeHyperlinkField(self.CustomHyperlinkedRelatedField): def is_saved_in_database(self, obj): return True def get_lookup_value(self, obj): return () router = SimpleRouter() url_configuration = { 'path': 'wss://127.0.0.1:8000', 'urls': router._urls, 'routes': router._routes } set_urlconf(url_configuration) instance = FakeHyperlinkField('test_view', read_only=True) with self.assertRaises(ImproperlyConfigured): instance.to_representation('')
def test_to_representation_returns_relative_url(self): class FakeModel(object): def __init__(self, pk): self.pk = pk def __str__(self): return "object.pk={}".format(self.pk) class FakeModelSerializer(object): parent = None _context = {'relative': True} class FakeHyperlinkField(self.CustomHyperlinkedRelatedField): def is_saved_in_database(self, obj): return True def get_lookup_value(self, obj): return (1, ) router = SimpleRouter() router.register('/test_view/{pk}', FakeView, 'GET', name='test_view') url_configuration = { 'path': 'wss://127.0.0.1:8000', 'urls': router._urls, 'routes': router._routes } set_urlconf(url_configuration) instance = FakeHyperlinkField('test_view', read_only=True) instance.bind('pk', FakeModelSerializer()) hyperlink_object = instance.to_representation(FakeModel(1)) self.assertEqual(hyperlink_object, '/test_view/1/') self.assertEqual(hyperlink_object.name, 'object.pk=1')
def test_get_url_returns_url_to_an_object(self): class FakeHyperlinkField(self.CustomHyperlinkedRelatedField): def is_saved_in_database(self, obj): return True def get_lookup_value(self, obj): return (1, ) router = SimpleRouter() router.register('/test_view/{pk}', FakeView, 'GET', name='test_view') url_configuration = { 'path': 'wss://127.0.0.1:8000', 'urls': router._urls, 'routes': router._routes } set_urlconf(url_configuration) instance = FakeHyperlinkField('test_view', read_only=True) self.assertEqual( instance.get_url(object(), 'test_view'), 'wss://127.0.0.1:8000/test_view/1/' )
def test_reverse_raises_no_reverse_match_exceptions(self): set_urlconf({}) with self.assertRaises(NoReverseMatch): reverse('user-detail', args=('1',))
def test_get_urlconf(): data = {"list": [1, 2, 3]} set_urlconf(data) urlconfs = get_urlconf() assert urlconfs == data
def test_reverse_raises_no_reverse_match_exceptions(self): set_urlconf({}) with self.assertRaises(NoReverseMatch): reverse('user-detail', args=('1', ))
def test_set_urlconf(): data = {"dict": object()} set_urlconf(data) assert "data" in _urlconfs.keys() assert _urlconfs['data'] == data