<commit_before>"""
We want to allow users override fields and their attributes on
auto-generated embedded documents based on

We need to take into account the following fields:
 - exclude
 - read_only
 - extra_kwargs
"""

from __future__ import unicode_literals

from django.test import TestCase
from mongoengine import Document, EmbeddedDocument, fields
from rest_framework.compat import unicode_repr

from rest_framework_mongoengine.serializers import DocumentSerializer

from .utils import dedent


class ChildDocument(EmbeddedDocument):
    name = fields.StringField()
    age = fields.IntField()


class ReferencedDocument(Document):
    foo = fields.StringField()
    bar = fields.StringField()


class ParentDocument(Document):
    foo = fields.StringField()
    embedded = fields.EmbeddedDocumentField(ChildDocument)
    nested_reference = fields.ReferenceField(ReferencedDocument)


class CompoundParentDocument(Document):
    foo = fields.StringField()
    embedded_list = fields.EmbeddedDocumentListField(ChildDocument)
    embedded_map = fields.MapField(fields.EmbeddedDocumentField(ChildDocument))


class TestEmbeddedCustomizationMapping(TestCase):
    def test_fields(self):
        """Ensure `fields` is passed to embedded documents."""
        class ParentSerializer(DocumentSerializer):
            class Meta:
                model = ParentDocument
                fields = ('embedded', 'embedded.name', 'nested_reference', 'nested_reference.foo')
                depth = 1

        expected = dedent("""
            ParentSerializer():
                embedded = EmbeddedSerializer(required=False):
                    name = CharField(required=False)
                nested_reference = NestedSerializer(read_only=True):
                    foo = CharField(required=False)
        """)
        # TODO: what if parent field is not included, but child field is?
        assert unicode_repr(ParentSerializer()) == expected

    def test_exclude(self):
        """Ensure `exclude` is passed to embedded documents."""
        class ParentSerializer(DocumentSerializer):
            class Meta:
                model = ParentDocument
                exclude = ('foo', 'embedded.age')
                depth = 1

        expected = dedent("""
            ParentSerializer():
                id = ObjectIdField(read_only=True)
                nested_reference = NestedSerializer(read_only=True):
                    id = ObjectIdField(read_only=True)
                    foo = CharField(required=False)
                    bar = CharField(required=False)
                embedded = EmbeddedSerializer(required=False):
                    name = CharField(required=False)
        """)

        assert unicode_repr(ParentSerializer()) == expected

    def test_read_only(self):
        """Ensure `read_only` are passed to embedded documents."""
        class ParentSerializer(DocumentSerializer):
            class Meta:
                model = ParentDocument
                fields = ('__all__')
                read_only = ('foo', 'embedded.name')
                depth = 1

        expected = dedent("""
            ParentSerializer():
                id = ObjectIdField(read_only=True)
                foo = CharField(required=False)
                nested_reference = NestedSerializer(read_only=True):
                    id = ObjectIdField(read_only=True)
                    foo = CharField(required=False)
                    bar = CharField(required=False)
                embedded = EmbeddedSerializer(required=False):
                    name = CharField(required=False)
                    age = IntegerField(required=False)
        """)

        assert unicode_repr(ParentSerializer()) == expected

    def test_extra_field_kwargs(self):
        """Ensure `extra_kwargs` are passed to embedded documents."""
        class ParentSerializer(DocumentSerializer):
            class Meta:
                model = ParentDocument
                fields = ('__all__')
                depth = 1
                extra_kwargs = {
                    'foo': {'default': 'bar'},
                    'embedded.name': {'default': '<NAME>'}
                }

        expected = dedent("""
            ParentSerializer():
                id = ObjectIdField(read_only=True)
                foo = CharField(default='bar')
                nested_reference = NestedSerializer(read_only=True):
                    id = ObjectIdField(read_only=True)
                    foo = CharField(required=False)
                    bar = CharField(required=False)
                embedded = EmbeddedSerializer(required=False):
                    name = CharField(default='<NAME>')
                    age = IntegerField(required=False)
        """)

        assert unicode_repr(ParentSerializer()) == expected


class TestCompoundCustomizationMapping(TestCase):
    def test_fields(self):
        """Ensure `fields` is passed to embedded documents."""
        class CompoundParentSerializer(DocumentSerializer):
            class Meta:
                model = CompoundParentDocument
                fields = ('embedded_list', 'embedded_list.child.name', 'embedded_map', 'embedded_map.child.age')
                depth = 1

        expected = dedent("""
            CompoundParentSerializer():
                embedded_list = EmbeddedSerializer(many=True, required=False):
                    name = CharField(required=False)
                embedded_map = EmbeddedSerializer(many=True, required=False):
                    age = IntegerField(required=False)
        """)

        assert unicode_repr(CompoundParentSerializer()) == expected

    def test_exclude(self):
        """Ensure `exclude` is passed to embedded documents."""
        class CompoundParentSerializer(DocumentSerializer):
            class Meta:
                model = CompoundParentDocument
                exclude = ('id', 'foo', 'embedded_list.child.age', 'embedded_map.child.name')

        expected = dedent("""
            CompoundParentSerializer():
                embedded_list = EmbeddedSerializer(many=True, required=False):
                    name = CharField(required=False)
                embedded_map = EmbeddedSerializer(many=True, required=False):
                    age = IntegerField(required=False)
        """)

        assert unicode_repr(CompoundParentSerializer()) == expected

    def test_read_only(self):
        """Ensure `read_only` are passed to embedded documents."""
        class CompoundParentSerializer(DocumentSerializer):
            class Meta:
                model = CompoundParentDocument
                fields = ('__all__')
                read_only = ('foo', 'embedded_list.child.name')

        expected = dedent("""
            CompoundParentSerializer():
                id = ObjectIdField(read_only=True)
                foo = CharField(required=False, read_only=True)
                embedded_list = EmbeddedSerializer():
                    name = CharField(required=False, read_only=True)
                    age = IntegerField(required=False)
                embedded_map = EmbeddedSerializer(required=False):
                    name = CharField(required=False)
                    age = IntegerField(required=False)
        """)

        import pdb
        pdb.set_trace()
        serializer = CompoundParentSerializer()
        serializer.get_fields()

        assert unicode_repr(CompoundParentSerializer()) == expected

    def test_extra_field_kwargs(self):
        pass


class TestEmbeddedCustomizationIntegration(TestCase):
    def test_fields(self):
        pass

    def test_exclude(self):
        pass

    def test_read_only(self):
        pass<commit_msg> #215 Fixed broken tests: read_only -> read_only_fields.<commit_after>"""
We want to allow users override fields and their attributes on
auto-generated embedded documents based on

We need to take into account the following fields:
 - exclude
 - read_only
 - extra_kwargs
"""

from __future__ import unicode_literals

from django.test import TestCase
from mongoengine import Document, EmbeddedDocument, fields
from rest_framework.compat import unicode_repr

from rest_framework_mongoengine.serializers import DocumentSerializer

from .utils import dedent


class ChildDocument(EmbeddedDocument):
    name = fields.StringField()
    age = fields.IntField()


class ReferencedDocument(Document):
    foo = fields.StringField()
    bar = fields.StringField()


class ParentDocument(Document):
    foo = fields.StringField()
    embedded = fields.EmbeddedDocumentField(ChildDocument)
    nested_reference = fields.ReferenceField(ReferencedDocument)


class CompoundParentDocument(Document):
    foo = fields.StringField()
    embedded_list = fields.EmbeddedDocumentListField(ChildDocument)
    embedded_map = fields.MapField(fields.EmbeddedDocumentField(ChildDocument))


class TestEmbeddedCustomizationMapping(TestCase):
    def test_fields(self):
        """Ensure `fields` is passed to embedded documents."""
        class ParentSerializer(DocumentSerializer):
            class Meta:
                model = ParentDocument
                fields = ('embedded', 'embedded.name', 'nested_reference', 'nested_reference.foo')
                depth = 1

        expected = dedent("""
            ParentSerializer():
                embedded = EmbeddedSerializer(required=False):
                    name = CharField(required=False)
                nested_reference = NestedSerializer(read_only=True):
                    foo = CharField(required=False)
        """)
        # TODO: what if parent field is not included, but child field is?
        assert unicode_repr(ParentSerializer()) == expected

    def test_exclude(self):
        """Ensure `exclude` is passed to embedded documents."""
        class ParentSerializer(DocumentSerializer):
            class Meta:
                model = ParentDocument
                exclude = ('foo', 'embedded.age')
                depth = 1

        expected = dedent("""
            ParentSerializer():
                id = ObjectIdField(read_only=True)
                nested_reference = NestedSerializer(read_only=True):
                    id = ObjectIdField(read_only=True)
                    foo = CharField(required=False)
                    bar = CharField(required=False)
                embedded = EmbeddedSerializer(required=False):
                    name = CharField(required=False)
        """)

        assert unicode_repr(ParentSerializer()) == expected

    def test_read_only(self):
        """Ensure `read_only` are passed to embedded documents."""
        class ParentSerializer(DocumentSerializer):
            class Meta:
                model = ParentDocument
                fields = ('__all__')
                read_only_fields = ('foo', 'embedded.name')
                depth = 1

        expected = dedent("""
            ParentSerializer():
                id = ObjectIdField(read_only=True)
                foo = CharField(read_only=True)
                nested_reference = NestedSerializer(read_only=True):
                    id = ObjectIdField(read_only=True)
                    foo = CharField(required=False)
                    bar = CharField(required=False)
                embedded = EmbeddedSerializer(required=False):
                    name = CharField(read_only=True)
                    age = IntegerField(required=False)
        """)

        assert unicode_repr(ParentSerializer()) == expected

    def test_extra_field_kwargs(self):
        """Ensure `extra_kwargs` are passed to embedded documents."""
        class ParentSerializer(DocumentSerializer):
            class Meta:
                model = ParentDocument
                fields = ('__all__')
                depth = 1
                extra_kwargs = {
                    'foo': {'default': 'bar'},
                    'embedded.name': {'default': '<NAME>'}
                }

        expected = dedent("""
            ParentSerializer():
                id = ObjectIdField(read_only=True)
                foo = CharField(default='bar')
                nested_reference = NestedSerializer(read_only=True):
                    id = ObjectIdField(read_only=True)
                    foo = CharField(required=False)
                    bar = CharField(required=False)
                embedded = EmbeddedSerializer(required=False):
                    name = CharField(default='<NAME>')
                    age = IntegerField(required=False)
        """)

        assert unicode_repr(ParentSerializer()) == expected


class TestCompoundCustomizationMapping(TestCase):
    def test_fields(self):
        """Ensure `fields` is passed to embedded documents."""
        class CompoundParentSerializer(DocumentSerializer):
            class Meta:
                model = CompoundParentDocument
                fields = ('embedded_list', 'embedded_list.child.name', 'embedded_map', 'embedded_map.child.age')
                depth = 1

        expected = dedent("""
            CompoundParentSerializer():
                embedded_list = EmbeddedSerializer(many=True, required=False):
                    name = CharField(required=False)
                embedded_map = EmbeddedSerializer(many=True, required=False):
                    age = IntegerField(required=False)
        """)

        assert unicode_repr(CompoundParentSerializer()) == expected

    def test_exclude(self):
        """Ensure `exclude` is passed to embedded documents."""
        class CompoundParentSerializer(DocumentSerializer):
            class Meta:
                model = CompoundParentDocument
                exclude = ('id', 'foo', 'embedded_list.child.age', 'embedded_map.child.name')

        expected = dedent("""
            CompoundParentSerializer():
                embedded_list = EmbeddedSerializer(many=True, required=False):
                    name = CharField(required=False)
                embedded_map = EmbeddedSerializer(many=True, required=False):
                    age = IntegerField(required=False)
        """)

        assert unicode_repr(CompoundParentSerializer()) == expected

    def test_read_only(self):
        """Ensure `read_only` are passed to embedded documents."""
        class CompoundParentSerializer(DocumentSerializer):
            class Meta:
                model = CompoundParentDocument
                fields = ('__all__')
                read_only_fields = ('foo', 'embedded_list.child.name')

        expected = dedent("""
            CompoundParentSerializer():
                id = ObjectIdField(read_only=True)
                foo = CharField(read_only=True)
                embedded_list = EmbeddedSerializer(many=True, required=False):
                    name = CharField(read_only=True)
                    age = IntegerField(required=False)
                embedded_map = EmbeddedSerializer(many=True, required=False):
                    name = CharField(required=False)
                    age = IntegerField(required=False)
        """)

        # import pdb
        # pdb.set_trace()
        # serializer = CompoundParentSerializer()
        # serializer.get_fields()

        assert unicode_repr(CompoundParentSerializer()) == expected

    def test_extra_field_kwargs(self):
        pass


class TestEmbeddedCustomizationIntegration(TestCase):
    def test_fields(self):
        pass

    def test_exclude(self):
        pass

    def test_read_only(self):
        pass
{'fraction_non_alphanumeric': 0.09025574619617999, 'fraction_numerical': 0.0008416963418582065, 'mean_word_length': 2.1703612479474548, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3610418', 'n_tokens_mistral': 3876, 'n_tokens_neox': 3596, 'n_words': 823}