def test_list_of_references(self, models):
        student_fields = fields_for_model(models.Student)
        assert type(student_fields['courses']) is fields.List
        assert type(student_fields['courses'].container) is fields.Reference

        course_fields = fields_for_model(models.Course)
        assert type(course_fields['students']) is fields.List
        assert type(course_fields['students'].container) is fields.Reference
 def test_ReferenceField(self):
     class ReferenceDoc(me.Document):
         field = me.IntField(primary_key=True, default=42)
     class Doc(me.Document):
         id = me.StringField(primary_key=True, default='main')
         ref = me.ReferenceField(ReferenceDoc)
     fields_ = fields_for_model(Doc)
     assert type(fields_['ref']) is fields.Reference
     class DocSchema(ModelSchema):
         class Meta:
             model = Doc
     ref_doc = ReferenceDoc().save()
     doc = Doc(ref=ref_doc)
     dump = DocSchema().dump(doc)
     assert not dump.errors
     assert dump.data == {'ref': 42, 'id': 'main'}
     # Try the same with reference document type passed as string
     class DocSchemaRefAsString(Schema):
         id = fields.String()
         ref = fields.Reference('ReferenceDoc')
     dump = DocSchemaRefAsString().dump(doc)
     assert not dump.errors
     assert dump.data == {'ref': 42, 'id': 'main'}
     # Test the field loading
     load = DocSchemaRefAsString().load(dump.data)
     assert not load.errors
     assert type(load.data['ref']) == ReferenceDoc
     # Try invalid loads
     for bad_ref in (1, 'NaN', None):
         dump.data['ref'] = bad_ref
         _, errors = DocSchemaRefAsString().load(dump.data)
         assert errors, bad_ref
Ejemplo n.º 3
0
    def test_GenericEmbeddedDocumentField(self):
        class Doc(me.Document):
            id = me.StringField(primary_key=True, default="main")
            embedded = me.GenericEmbeddedDocumentField()

        class EmbeddedA(me.EmbeddedDocument):
            field_a = me.StringField(default="field_a_value")

        class EmbeddedB(me.EmbeddedDocument):
            field_b = me.IntField(default=42)

        fields_ = fields_for_model(Doc)
        assert type(fields_["embedded"]) is fields.GenericEmbeddedDocument

        class DocSchema(ModelSchema):
            class Meta:
                model = Doc

        doc = Doc(embedded=EmbeddedA())
        dump = DocSchema().dump(doc)
        assert not dump.errors
        assert dump.data == {"embedded": {"field_a": "field_a_value"}, "id": "main"}
        doc.embedded = EmbeddedB()
        doc.save()
        dump = DocSchema().dump(doc)
        assert not dump.errors
        assert dump.data == {"embedded": {"field_b": 42}, "id": "main"}
 def test_fields_for_model_types(self, models):
     fields_ = fields_for_model(models.Student)
     assert type(fields_['id']) is fields.ObjectId
     assert type(fields_['full_name']) is fields.Str
     assert type(fields_['dob']) is fields.DateTime
     assert type(fields_['current_school']) is fields.Reference
     assert type(fields_['date_created']) is fields.DateTime
Ejemplo n.º 5
0
    def test_MapField(self):
        class MappedDoc(me.EmbeddedDocument):
            field = me.StringField()

        class Doc(me.Document):
            id = me.IntField(primary_key=True, default=1)
            map = me.MapField(me.EmbeddedDocumentField(MappedDoc))
            str = me.MapField(me.StringField())

        fields_ = fields_for_model(Doc)
        assert type(fields_["map"]) is fields.Map

        class DocSchema(ModelSchema):
            class Meta:
                model = Doc

        doc = Doc(map={"a": MappedDoc(field="A"), "b": MappedDoc(field="B")}, str={"a": "aaa", "b": "bbbb"}).save()
        dump = DocSchema().dump(doc)
        assert not dump.errors
        assert dump.data == {
            "map": {"a": {"field": "A"}, "b": {"field": "B"}},
            "str": {"a": "aaa", "b": "bbbb"},
            "id": 1,
        }
        # Try the load
        load = DocSchema().load(dump.data)
        assert not load.errors
        assert load.data.map == doc.map
 def test_length_validator_set(self, models):
     fields_ = fields_for_model(models.Student)
     validator = contains_validator(fields_['full_name'], validate.Length)
     assert validator
     assert validator.max == 255
     validator = contains_validator(fields_['age'], validate.Range)
     assert validator
     assert validator.max == 99
     assert validator.min == 10
 def test_ListField(self):
     class Doc(me.Document):
         list = me.ListField(me.StringField())
     fields_ = fields_for_model(Doc)
     assert type(fields_['list']) is fields.List
     class DocSchema(ModelSchema):
         class Meta:
             model = Doc
     list_ = ['A', 'B', 'C']
     doc = Doc(list=list_)
     dump = DocSchema().dump(doc)
     assert not dump.errors
     assert dump.data == {'list': list_}
     load = DocSchema().load(dump.data)
     assert not load.errors
     assert load.data.list == list_
Ejemplo n.º 8
0
    def test_DictField(self):
        class Doc(me.Document):
            data = me.DictField()

        fields_ = fields_for_model(Doc)
        assert type(fields_["data"]) is fields.Raw

        class DocSchema(ModelSchema):
            class Meta:
                model = Doc

        data = {"int_1": 1, "nested_2": {"sub_int_1": 42, "sub_list_2": []}, "list_3": ["a", "b", "c"]}
        doc = Doc(data=data)
        dump = DocSchema().dump(doc)
        assert not dump.errors
        assert dump.data == {"data": data}
        load = DocSchema().load(dump.data)
        assert not load.errors
        assert load.data.data == data
 def test_ListSpecialField(self):
     class NestedDoc(me.EmbeddedDocument):
         field = me.StringField()
     class Doc(me.Document):
         list = me.ListField(me.EmbeddedDocumentField(NestedDoc))
     fields_ = fields_for_model(Doc)
     assert type(fields_['list']) is fields.List
     assert type(fields_['list'].container) is fields.Nested
     class DocSchema(ModelSchema):
         class Meta:
             model = Doc
     list_ = [{'field': 'A'}, {'field': 'B'}, {'field': 'C'}]
     doc = Doc(list=list_)
     dump = DocSchema().dump(doc)
     assert not dump.errors
     assert dump.data == {'list': list_}
     load = DocSchema().load(dump.data)
     assert not load.errors
     for i, elem in enumerate(list_):
         assert load.data.list[i].field == elem['field']
 def test_MapField(self):
     class MappedDoc(me.EmbeddedDocument):
         field = me.StringField()
     class Doc(me.Document):
         id = me.IntField(primary_key=True, default=1)
         map = me.MapField(me.EmbeddedDocumentField(MappedDoc))
         str = me.MapField(me.StringField())
     fields_ = fields_for_model(Doc)
     assert type(fields_['map']) is fields.Map
     class DocSchema(ModelSchema):
         class Meta:
             model = Doc
     doc = Doc(map={'a': MappedDoc(field='A'), 'b': MappedDoc(field='B')},
               str={'a': 'aaa', 'b': 'bbbb'}).save()
     dump = DocSchema().dump(doc)
     assert not dump.errors
     assert dump.data == {'map': {'a': {'field': 'A'}, 'b': {'field': 'B'}},
                          'str': {'a': 'aaa', 'b': 'bbbb'}, 'id': 1}
     # Try the load
     load = DocSchema().load(dump.data)
     assert not load.errors
     assert load.data.map == doc.map
 def test_GenericEmbeddedDocumentField(self):
     class Doc(me.Document):
         id = me.StringField(primary_key=True, default='main')
         embedded = me.GenericEmbeddedDocumentField()
     class EmbeddedA(me.EmbeddedDocument):
         field_a = me.StringField(default='field_a_value')
     class EmbeddedB(me.EmbeddedDocument):
         field_b = me.IntField(default=42)
     fields_ = fields_for_model(Doc)
     assert type(fields_['embedded']) is fields.GenericEmbeddedDocument
     class DocSchema(ModelSchema):
         class Meta:
             model = Doc
     doc = Doc(embedded=EmbeddedA())
     dump = DocSchema().dump(doc)
     assert not dump.errors
     assert dump.data == {'embedded': {'field_a': 'field_a_value'}, 'id': 'main'}
     doc.embedded = EmbeddedB()
     doc.save()
     dump = DocSchema().dump(doc)
     assert not dump.errors
     assert dump.data == {'embedded': {'field_b': 42}, 'id': 'main'}
 def test_DictField(self):
     class Doc(me.Document):
         data = me.DictField()
     fields_ = fields_for_model(Doc)
     assert type(fields_['data']) is fields.Raw
     class DocSchema(ModelSchema):
         class Meta:
             model = Doc
     data = {
         'int_1': 1,
         'nested_2': {
             'sub_int_1': 42,
             'sub_list_2': []
         },
         'list_3': ['a', 'b', 'c']
     }
     doc = Doc(data=data)
     dump = DocSchema().dump(doc)
     assert not dump.errors
     assert dump.data == {'data': data}
     load = DocSchema().load(dump.data)
     assert not load.errors
     assert load.data.data == data
 def test_dict_field(self, models):
     fields_ = fields_for_model(models.Course)
     assert type(fields_['prereqs']) is fields.Raw
 def test_fields_for_model_handles_custom_types(self, models):
     fields_ = fields_for_model(models.Course)
     assert type(fields_['grade']) is fields.Int
     assert type(fields_['id']) is fields.Int
 def test_fields_for_model_saves_doc(self, models):
     fields_ = fields_for_model(models.Student)
     assert fields_['date_created'].metadata['description'] == 'date the student was created'
 def test_dict_field(self, models):
     fields_ = fields_for_model(models.Course)
     assert type(fields_['prereqs']) is fields.Raw
 def test_embedded_document_field(self, models):
     fields_ = fields_for_model(models.School)
     assert type(fields_['headteacher']) is fields.Nested
 def test_sets_allow_none_for_nullable_fields(self, models):
     fields_ = fields_for_model(models.Student)
     assert fields_['dob'].allow_none is True
Ejemplo n.º 19
0
 def test_sets_enum_choices(self, models):
     fields_ = fields_for_model(models.Course)
     validator = contains_validator(fields_['level'], validate.OneOf)
     assert validator
     assert validator.choices == ('Primary', 'Secondary')
 def test_fields_for_model_saves_doc(self, models):
     fields_ = fields_for_model(models.Student)
     assert fields_['date_created'].metadata['description'] == 'date the student was created'
 def test_fields_for_model_handles_custom_types(self, models):
     fields_ = fields_for_model(models.Course)
     assert type(fields_['grade']) is fields.Int
     assert type(fields_['id']) is fields.Int
Ejemplo n.º 22
0
    def test_reference(self, models):
        student_fields = fields_for_model(models.Student)
        assert type(student_fields['current_school']) is fields.Reference

        course_fields = fields_for_model(models.Course)
        assert type(course_fields['id']) is fields.Int
 def test_GenericReferenceField(self):
     class Doc(me.Document):
         id = me.StringField(primary_key=True, default='main')
         generic = me.GenericReferenceField()
     class SubDocA(me.Document):
         field_a = me.StringField(primary_key=True, default='doc_a_pk')
     class SubDocB(me.Document):
         field_b = me.IntField(primary_key=True, default=42)
     fields_ = fields_for_model(Doc)
     assert type(fields_['generic']) is fields.GenericReference
     class DocSchema(ModelSchema):
         class Meta:
             model = Doc
     # Test dump
     sub_doc_a = SubDocA().save()
     sub_doc_b = SubDocB().save()
     doc = Doc(generic=sub_doc_a)
     dump = DocSchema().dump(doc)
     assert not dump.errors
     assert dump.data == {'generic': 'doc_a_pk', 'id': 'main'}
     doc.generic = sub_doc_b
     doc.save()
     dump = DocSchema().dump(doc)
     assert not dump.errors
     assert dump.data == {'generic': 42, 'id': 'main'}
     # Test load
     for bad_generic in (
             {'id': str(sub_doc_a.id)}, {'_cls': sub_doc_a._class_name},
             {'id': str(sub_doc_a.id), '_cls': sub_doc_b._class_name},
             {'id': 'not_an_id', '_cls': sub_doc_a._class_name},
             {'id': 42, '_cls': sub_doc_a._class_name},
             {'id': 'main', '_cls': sub_doc_b._class_name},
             {'id': str(sub_doc_a.id), '_cls': 'not_a_class'},
             {'id': None, '_cls': sub_doc_a._class_name},
             {'id': str(sub_doc_a.id), '_cls': None},
         ):
         load = DocSchema().load({"generic": bad_generic})
         assert 'generic' in load.errors
     load = DocSchema().load({"generic": {"id": str(sub_doc_a.id),
                                          "_cls": sub_doc_a._class_name}})
     assert not load.errors
     assert load.data['generic'] == sub_doc_a
     load = DocSchema().load({"generic": {"id": str(sub_doc_b.id),
                                          "_cls": sub_doc_b._class_name}})
     assert not load.errors
     assert load.data['generic'] == sub_doc_b
     # Teste choices param
     class DocOnlyA(me.Document):
         id = me.StringField(primary_key=True, default='main')
         generic = me.GenericReferenceField(choices=[SubDocA])
     class DocOnlyASchema(ModelSchema):
         class Meta:
             model = DocOnlyA
     load = DocOnlyASchema().load({})
     assert not load.errors
     load = DocOnlyASchema().load({"generic": {"id": str(sub_doc_a.id),
                                               "_cls": sub_doc_a._class_name}})
     assert not load.errors
     assert load.data['generic'] == sub_doc_a
     load = DocOnlyASchema().load({"generic": {"id": str(sub_doc_b.id),
                                               "_cls": sub_doc_b._class_name}})
     assert 'generic' in load.errors
 def test_sets_allow_none_for_nullable_fields(self, models):
     fields_ = fields_for_model(models.Student)
     assert fields_['dob'].allow_none is True
 def test_sets_enum_choices(self, models):
     fields_ = fields_for_model(models.Course)
     validator = contains_validator(fields_['level'], validate.OneOf)
     assert validator
     assert validator.choices == ('Primary', 'Secondary')
 def test_embedded_document_field(self, models):
     fields_ = fields_for_model(models.School)
     assert type(fields_['headteacher']) is fields.Nested
    def test_reference(self, models):
        student_fields = fields_for_model(models.Student)
        assert type(student_fields['current_school']) is fields.Reference

        course_fields = fields_for_model(models.Course)
        assert type(course_fields['id']) is fields.Int
Ejemplo n.º 28
0
    def test_GenericLazyReferenceField(self):
        class Doc(me.Document):
            id = me.StringField(primary_key=True, default='main')
            generic = me.GenericLazyReferenceField()
        class SubDocA(me.Document):
            field_a = me.StringField(primary_key=True, default='doc_a_pk')
        class SubDocB(me.Document):
            field_b = me.IntField(primary_key=True, default=42)
        fields_ = fields_for_model(Doc)
        assert type(fields_['generic']) is fields.GenericReference
        class DocSchema(ModelSchema):
            class Meta:
                model = Doc
        # Test dump
        sub_doc_a = SubDocA().save()
        sub_doc_b = SubDocB().save()
        doc = Doc(generic=sub_doc_a)
        dumped_data = DocSchema().dump(doc)
        assert dumped_data
        assert dumped_data == {'generic': 'doc_a_pk', 'id': 'main'}
        doc.generic = sub_doc_b
        doc.save()
        dumped_data = DocSchema().dump(doc)
        assert dumped_data
        assert dumped_data == {'generic': 42, 'id': 'main'}
        # Test load
        for bad_generic in (
                {'id': str(sub_doc_a.id)}, {'_cls': sub_doc_a._class_name},
                {'id': str(sub_doc_a.id), '_cls': sub_doc_b._class_name},
                {'id': 'not_an_id', '_cls': sub_doc_a._class_name},
                {'id': 42, '_cls': sub_doc_a._class_name},
                {'id': 'main', '_cls': sub_doc_b._class_name},
                {'id': str(sub_doc_a.id), '_cls': 'not_a_class'},
                {'id': None, '_cls': sub_doc_a._class_name},
                {'id': str(sub_doc_a.id), '_cls': None},
            ):
            try:
                loaded_data = DocSchema().load({"generic": bad_generic})
            except ValidationError as err:
                loaded_data = None
            assert not loaded_data

        loaded_data = DocSchema().load({"generic": {"id": str(sub_doc_a.id),
                                             "_cls": sub_doc_a._class_name}})
        assert loaded_data
        assert loaded_data['generic'] == sub_doc_a
        loaded_data = DocSchema().load({"generic": {"id": str(sub_doc_b.id),
                                             "_cls": sub_doc_b._class_name}})
        assert loaded_data
        assert loaded_data['generic'] == sub_doc_b
        # Teste choices param
        class DocOnlyA(me.Document):
            id = me.StringField(primary_key=True, default='main')
            generic = me.GenericLazyReferenceField(choices=[SubDocA])
        class DocOnlyASchema(ModelSchema):
            class Meta:
                model = DocOnlyA
        loaded_data = DocOnlyASchema().load({})
        assert loaded_data
        loaded_data = DocOnlyASchema().load({"generic": {"id": str(sub_doc_a.id),
                                                  "_cls": sub_doc_a._class_name}})
        assert loaded_data
        assert loaded_data['generic'] == sub_doc_a
        try:
            loaded_data = DocOnlyASchema().load({"generic": {"id": str(sub_doc_b.id),
                                                      "_cls": sub_doc_b._class_name}})
        except ValidationError as err:
            loaded_data = None
        assert not loaded_data