<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from datetime import datetime <EOL> from decimal import Decimal <EOL> from uuid import UUID <EOL> from bson import ObjectId <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . test import TestCase <EOL> from mongoengine import Document , fields <EOL> from rest_framework import serializers <EOL> from rest_framework . compat import unicode_repr <EOL> from rest_framework_mongoengine . serializers import DocumentSerializer <EOL> import pytest <EOL> from . utils import dedent <EOL> class CustomField ( fields . BaseField ) : <EOL> pass <EOL> class OneFieldModel ( Document ) : <EOL> str_field = fields . StringField ( ) <EOL> class AutoFieldModel ( Document ) : <EOL> auto_field = fields . SequenceField ( primary_key = True ) <EOL> class RegularModel ( Document ) : <EOL> """<STR_LIT>""" <EOL> str_field = fields . StringField ( ) <EOL> url_field = fields . URLField ( ) <EOL> email_field = fields . EmailField ( ) <EOL> int_field = fields . IntField ( ) <EOL> long_field = fields . LongField ( ) <EOL> float_field = fields . FloatField ( ) <EOL> boolean_field = fields . BooleanField ( ) <EOL> nullboolean_field = fields . BooleanField ( null = True ) <EOL> date_field = fields . DateTimeField ( ) <EOL> complexdate_field = fields . ComplexDateTimeField ( ) <EOL> uuid_field = fields . UUIDField ( ) <EOL> id_field = fields . ObjectIdField ( ) <EOL> decimal_field = fields . DecimalField ( ) <EOL> custom_field = CustomField ( ) <EOL> def method ( self ) : <EOL> return '<STR_LIT>' <EOL> COLOR_CHOICES = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> class FieldOptionsModel ( Document ) : <EOL> required_field = fields . IntField ( required = True ) <EOL> null_field = fields . IntField ( null = True ) <EOL> choices_field = fields . StringField ( choices = COLOR_CHOICES ) <EOL> length_limit_field = fields . StringField ( min_length = <NUM_LIT:3> , max_length = <NUM_LIT:12> ) <EOL> value_limit_field = fields . IntField ( min_value = <NUM_LIT:3> , max_value = <NUM_LIT:12> ) <EOL> decimal_field = fields . DecimalField ( precision = <NUM_LIT:4> , max_value = <NUM_LIT> ) <EOL> DECIMAL_CHOICES = ( ( '<STR_LIT>' , Decimal ( '<STR_LIT>' ) ) , ( '<STR_LIT>' , Decimal ( '<STR_LIT>' ) ) , ( '<STR_LIT>' , Decimal ( '<STR_LIT>' ) ) ) <EOL> class ComplexChoicesModel ( Document ) : <EOL> choices_field_with_nonstandard_args = fields . DecimalField ( precision = <NUM_LIT:1> , choices = DECIMAL_CHOICES , verbose_name = '<STR_LIT>' ) <EOL> class TestRegularFieldMappings ( TestCase ) : <EOL> maxDiff = <NUM_LIT> <EOL> def test_regular_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = RegularModel <EOL> expected = dedent ( """<STR_LIT>""" ) <EOL> assert unicode_repr ( TestSerializer ( ) ) == expected <EOL> def test_meta_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = RegularModel <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT>' ) <EOL> expected = dedent ( """<STR_LIT>""" ) <EOL> assert unicode_repr ( TestSerializer ( ) ) == expected <EOL> def test_meta_exclude ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = RegularModel <EOL> exclude = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> expected = dedent ( """<STR_LIT>""" ) <EOL> assert unicode_repr ( TestSerializer ( ) ) == expected <EOL> def test_field_options ( self ) : <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = FieldOptionsModel <EOL> expected = dedent ( """<STR_LIT>""" ) <EOL> assert unicode_repr ( TestSerializer ( ) ) == expected <EOL> def test_method_field ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = RegularModel <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT>' ) <EOL> expected = dedent ( """<STR_LIT>""" ) <EOL> assert unicode_repr ( TestSerializer ( ) ) == expected <EOL> def test_pk_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = AutoFieldModel <EOL> fields = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> expected = dedent ( """<STR_LIT>""" ) <EOL> assert unicode_repr ( TestSerializer ( ) ) == expected <EOL> def test_id_field ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = OneFieldModel <EOL> fields = ( '<STR_LIT:id>' , ) <EOL> expected = dedent ( """<STR_LIT>""" ) <EOL> assert unicode_repr ( TestSerializer ( ) ) == expected <EOL> def test_extra_field_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = RegularModel <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT>' ) <EOL> extra_kwargs = { '<STR_LIT>' : { '<STR_LIT:default>' : '<STR_LIT>' } } <EOL> expected = dedent ( """<STR_LIT>""" ) <EOL> assert unicode_repr ( TestSerializer ( ) ) == expected <EOL> def test_invalid_field ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = RegularModel <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT>' ) <EOL> with pytest . raises ( ImproperlyConfigured ) as exc : <EOL> TestSerializer ( ) . fields <EOL> expected = '<STR_LIT>' <EOL> assert expected in str ( exc . value ) <EOL> def test_missing_field ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> missing = serializers . ReadOnlyField ( ) <EOL> class Meta : <EOL> model = RegularModel <EOL> fields = ( '<STR_LIT:id>' , ) <EOL> with pytest . raises ( AssertionError ) as exc : <EOL> TestSerializer ( ) . fields <EOL> expected = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> assert expected in str ( exc . value ) <EOL> def test_missing_superclass_field_not_included ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> missing = serializers . ReadOnlyField ( ) <EOL> class Meta : <EOL> model = RegularModel <EOL> class ChildSerializer ( TestSerializer ) : <EOL> missing = serializers . ReadOnlyField ( ) <EOL> class Meta : <EOL> model = RegularModel <EOL> fields = ( '<STR_LIT:id>' , ) <EOL> ChildSerializer ( ) . fields <EOL> def test_missing_superclass_field_excluded ( self ) : <EOL> """<STR_LIT>""" <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> missing = serializers . ReadOnlyField ( ) <EOL> class Meta : <EOL> model = RegularModel <EOL> class ChildSerializer ( TestSerializer ) : <EOL> missing = serializers . ReadOnlyField ( ) <EOL> class Meta : <EOL> model = RegularModel <EOL> exclude = ( '<STR_LIT>' , ) <EOL> ChildSerializer ( ) . fields <EOL> def test_choices_with_nonstandard_args ( self ) : <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> missing = serializers . ReadOnlyField ( ) <EOL> class Meta : <EOL> model = ComplexChoicesModel <EOL> TestSerializer ( ) . fields <EOL> def test_fields_and_exclude_behavior ( self ) : <EOL> class ImplicitFieldsSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = RegularModel <EOL> class ExplicitFieldsSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = RegularModel <EOL> fields = '<STR_LIT>' <EOL> implicit = ImplicitFieldsSerializer ( ) <EOL> explicit = ExplicitFieldsSerializer ( ) <EOL> assert implicit . data == explicit . data <EOL> class TestIntegration ( TestCase ) : <EOL> maxDiff = <NUM_LIT:0> <EOL> def tearDown ( self ) : <EOL> RegularModel . drop_collection ( ) <EOL> def test_parsing ( self ) : <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = RegularModel <EOL> input_data = { <EOL> '<STR_LIT>' : "<STR_LIT:str>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT:True>" , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> } <EOL> serializer = TestSerializer ( data = input_data ) <EOL> assert serializer . is_valid ( ) , serializer . errors <EOL> expected = { <EOL> '<STR_LIT>' : "<STR_LIT:str>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : UUID ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ObjectId ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : Decimal ( '<STR_LIT>' ) <EOL> } <EOL> assert serializer . validated_data == expected <EOL> def test_retrival ( self ) : <EOL> instance = RegularModel . objects . create ( <EOL> str_field = "<STR_LIT:str>" , <EOL> url_field = "<STR_LIT>" , <EOL> email_field = "<STR_LIT>" , <EOL> int_field = <NUM_LIT> , <EOL> long_field = <NUM_LIT> , <EOL> float_field = <NUM_LIT> , <EOL> boolean_field = True , <EOL> nullboolean_field = None , <EOL> date_field = datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> complexdate_field = datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> uuid_field = UUID ( "<STR_LIT>" ) , <EOL> id_field = ObjectId ( "<STR_LIT>" ) , <EOL> decimal_field = Decimal ( <NUM_LIT:1> ) / Decimal ( <NUM_LIT:3> ) <EOL> ) <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = RegularModel <EOL> serializer = TestSerializer ( instance ) <EOL> expected = { <EOL> '<STR_LIT:id>' : str ( instance . id ) , <EOL> '<STR_LIT>' : "<STR_LIT:str>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : None <EOL> } <EOL> assert serializer . data == expected <EOL> def test_create ( self ) : <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = RegularModel <EOL> data = { <EOL> '<STR_LIT>' : "<STR_LIT:str>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } <EOL> serializer = TestSerializer ( data = data ) <EOL> assert serializer . is_valid ( ) , serializer . errors <EOL> instance = serializer . save ( ) <EOL> assert instance . str_field == "<STR_LIT:str>" <EOL> assert instance . url_field == "<STR_LIT>" <EOL> assert instance . email_field == "<STR_LIT>" <EOL> assert instance . int_field == <NUM_LIT> <EOL> assert instance . long_field == <NUM_LIT> <EOL> assert instance . float_field == <NUM_LIT> <EOL> assert instance . boolean_field <EOL> assert instance . nullboolean_field is None <EOL> assert instance . date_field == datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> assert instance . complexdate_field == datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> assert instance . uuid_field == UUID ( "<STR_LIT>" ) <EOL> assert instance . id_field == ObjectId ( "<STR_LIT>" ) <EOL> assert instance . decimal_field == Decimal ( "<STR_LIT>" ) <EOL> expected = { <EOL> '<STR_LIT:id>' : str ( instance . id ) , <EOL> '<STR_LIT>' : "<STR_LIT:str>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : None <EOL> } <EOL> assert serializer . data == expected <EOL> def test_update ( self ) : <EOL> instance = RegularModel . objects . create ( <EOL> str_field = "<STR_LIT:str>" , <EOL> url_field = "<STR_LIT>" , <EOL> email_field = "<STR_LIT>" , <EOL> int_field = <NUM_LIT> , <EOL> long_field = <NUM_LIT> , <EOL> float_field = <NUM_LIT> , <EOL> boolean_field = True , <EOL> nullboolean_field = None , <EOL> date_field = datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> complexdate_field = datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> uuid_field = UUID ( "<STR_LIT>" ) , <EOL> id_field = ObjectId ( "<STR_LIT>" ) , <EOL> decimal_field = Decimal ( <NUM_LIT:1> ) / Decimal ( <NUM_LIT:3> ) <EOL> ) <EOL> class TestSerializer ( DocumentSerializer ) : <EOL> class Meta : <EOL> model = RegularModel <EOL> data = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } <EOL> serializer = TestSerializer ( instance , data = data ) <EOL> assert serializer . is_valid ( ) , serializer . errors <EOL> instance = serializer . save ( ) <EOL> assert instance . str_field == "<STR_LIT>" <EOL> assert instance . url_field == "<STR_LIT>" <EOL> assert instance . email_field == "<STR_LIT>" <EOL> assert instance . int_field == <NUM_LIT> <EOL> assert instance . long_field == <NUM_LIT> <EOL> assert instance . float_field == <NUM_LIT> <EOL> assert not instance . boolean_field <EOL> assert instance . nullboolean_field <EOL> assert instance . date_field == datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> assert instance . complexdate_field == datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> assert instance . uuid_field == UUID ( "<STR_LIT>" ) <EOL> assert instance . id_field == ObjectId ( "<STR_LIT>" ) <EOL> assert instance . decimal_field == Decimal ( "<STR_LIT>" ) <EOL> expected = { <EOL> '<STR_LIT:id>' : str ( instance . id ) , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : None <EOL> } <EOL> assert serializer . data == expected </s>
