def test_related_field_with_pk(self): """Test that a related field receiving a model object with a primary key returns None. """ # Create a fake request. factory = RequestFactory() request = factory.get('/foo/') # Get the appropriate related field. fake_pk = uuid.uuid4() nm = test_models.NormalModel(id=42) cm = test_models.ChildModel(normal=nm) cs = test_serializers.ChildSerializer(context={'request': request}) rel_field = cs.get_related_field( model_field=test_models.ChildModel._meta.\ get_field_by_name('normal')[0], related_model=test_models.NormalModel, to_many=False, ) rel_field.context = {'request': request} # Get the final answer. answer = rel_field.to_native(nm) self.assertEqual( { 'api_endpoint': 'http://testserver/normal/%d/' % nm.id, 'id': 42, 'bacon': None, 'bar': None, 'baz': None, 'foo': None, }, answer)
def test_related_field_from_id_dict(self): """Test that a related field's `from_native` method, when sent a dictionary with an `id` key, returns that ID. """ # Test the case where we get a valid value back. with mock.patch.object(self.rel_field.queryset, 'get') as qs: qs.return_value = test_models.NormalModel(id=42) answer = self.rel_field.from_native({'id': 42}) qs.assert_called_with(id=42) self.assertEqual(answer, qs.return_value)
def test_direct_relationship(self): """Test that a direct relationship retrieval works as expected. """ # Get the related field from a direct relationship. s = test_serializers.ChildSerializer() rel_field = s.get_related_field( model_field=test_models.ChildModel._meta.\ get_field_by_name('normal')[0], related_model=test_models.NormalModel, to_many=False, ) self.assertIsInstance(rel_field, RelatedField) # Verify the label. self.assertEqual( rel_field.label_from_instance(test_models.NormalModel()), 'NormalModel object', ) # Verify the value. self.assertFalse(rel_field.prepare_value(test_models.NormalModel()))
def test_direct_relationship_with_explicit_fields(self): """Test that a direct relationship retreival works as expected, and that our explicit field list chains down to the related field. """ # Create our serializer. s = test_serializers.ChildSerializerII() rel_field = s.get_related_field( model_field=test_models.ChildModel._meta.\ get_field_by_name('normal')[0], related_model=test_models.NormalModel, to_many=False, ) self.assertIsInstance(rel_field, RelatedField) rel_field.context = {'request': RequestFactory().get('/foo/bar/')} # Get the serializer class. s = rel_field._get_serializer(test_models.NormalModel(bacon=42)) self.assertEqual([i for i in s.get_fields().keys()], ['id', 'bacon'])
def test_create_rel_serializer_class(self): """Establish that the `RelatedField._create_serializer_class` method works as expected. """ RelatedModel = test_models.RelatedModel # Create a bogus request object. factory = RequestFactory() request = factory.get('/foo/') # Create a serializer that would otherwise show itself # at a related level. rs = test_serializers.ReverseSerializer() # Create an instance. nm = test_models.NormalModel(bar=1, baz=2, bacon=3) rm = RelatedModel(id=42, baz=1, normal=nm) # Get the fields from the serializer and determine that we get # what we expect. fields_dict = rs.fields self.assertEqual( set([i for i in fields_dict.keys()]), {'bacon', 'bar', 'baz', 'related_model'}, ) # Pull out the related field. rel_field = fields_dict['related_model'] rel_field.context = {'request': request} # Establish that there is no serializer class on the related # field yet. self.assertFalse(hasattr(rel_field, '_serializer_class')) # Create a serializer class. ret_val = rel_field._create_serializer_class(RelatedModel) self.assertTrue(ret_val) self.assertTrue(hasattr(rel_field, '_serializer_class')) sc = rel_field._serializer_class # Establish that a followup call is a no-op. ret_val = rel_field._create_serializer_class(RelatedModel) self.assertFalse(ret_val) self.assertIs(rel_field._serializer_class, sc)
def test_reverse_related_field_serializer(self): """Establish that a related field can be specified on a serializer without incident. """ # Create a bogus request object. factory = RequestFactory() request = factory.get('/foo/') # Create a serializer that would otherwise show itself # at a related level. rs = test_serializers.ReverseSerializer() # Create an instance. nm = test_models.NormalModel(bar=1, baz=2, bacon=3) rm = test_models.RelatedModel(id=42, baz=1, normal=nm) # Get the fields from the serializer and determine that we get # what we expect. fields_dict = rs.get_default_fields() self.assertEqual( [i for i in fields_dict.keys()], [ 'id', 'api_endpoint', 'bacon', 'bar', 'baz', 'foo', 'related_model', ], ) # Pull out the related field. rel_field = fields_dict['related_model'] rel_field.context = {'request': request} # Convert our related field to native, and establish that it does not # have a normal model. native = rel_field.to_native(rm) self.assertEqual({'id': 42, 'baz': 1}, native)