def test_get_form_fields_from_serializer_unmapped_field(self): class CustomField(serializers.Field): pass class MySerializer(serializers.Serializer): foo = serializers.CharField(read_only=True) bar = CustomField() with self.assertRaises(TypeError): SerializerFormMeta.get_form_fields_from_serializer( (object,), mock.Mock(serializer=MySerializer, fields=['foo', 'bar'], exclude=[], field_mapping={}) )
def test_get_form_fields_from_serializer_unmapped_field(self): class CustomField(serializers.Field): pass class MySerializer(serializers.Serializer): foo = serializers.CharField(read_only=True) bar = CustomField() with self.assertRaises(TypeError): SerializerFormMeta.get_form_fields_from_serializer( (object, ), mock.Mock(serializer=MySerializer, fields=['foo', 'bar'], exclude=[], field_mapping={}))
def test_get_form_fields_from_serializer(self): fields = SerializerFormMeta.get_form_fields_from_serializer( (object,), mock.Mock(serializer=TestSerializer, fields=['foo', 'bar'], exclude=['exclude'], field_mapping={}) ) self.assertSetEqual(set(fields.keys()), {'bar'}) self.assertIsInstance(fields['bar'], forms.IntegerField)
def test_get_form_fields_from_serializer(self): fields = SerializerFormMeta.get_form_fields_from_serializer( (object, ), mock.Mock(serializer=TestSerializer, fields=['foo', 'bar'], exclude=['exclude'], field_mapping={})) self.assertSetEqual(set(fields.keys()), {'bar'}) self.assertIsInstance(fields['bar'], forms.IntegerField)
def test_get_field_mapping(self): class Base1B(object): class _meta(object): field_mapping = { 'hello': 'mars', 'hi': 'there', 'foo': 'bar', } class Base1A(Base1B): class _meta(object): field_mapping = { 'hello': 'world', } class Base2(object): class _meta(object): field_mapping = { 'hello': 'sun', 'something': 'here', } class Options(object): field_mapping = { 'foo': 'baar', 'happy': 'rainbows', } mapping = SerializerFormMeta.get_field_mapping((Base1A, Base2), Options) self.assertDictEqual( mapping, { 'hi': 'there', 'hello': 'world', 'something': 'here', 'base': 'mapping', 'foo': 'baar', 'happy': 'rainbows', })
def test_get_field_mapping(self): class Base1B(object): class _meta(object): field_mapping = { 'hello': 'mars', 'hi': 'there', 'foo': 'bar', } class Base1A(Base1B): class _meta(object): field_mapping = { 'hello': 'world', } class Base2(object): class _meta(object): field_mapping = { 'hello': 'sun', 'something': 'here', } class Options(object): field_mapping = { 'foo': 'baar', 'happy': 'rainbows', } mapping = SerializerFormMeta.get_field_mapping((Base1A, Base2), Options) self.assertDictEqual(mapping, { 'hi': 'there', 'hello': 'world', 'something': 'here', 'base': 'mapping', 'foo': 'baar', 'happy': 'rainbows', })