Example #1
0
 class Doc(Document):
     int_field = IntField()
     emb_field = EmbDocField(EmbDoc, mongo_name='emb')
     complex_emb_field = EmbDocField(ComplexEmbDoc, mongo_name='cmplx_emb')
     lst_field = ListField(EmbDocField(EmbDoc), mongo_name='lst')
     lst_int_field = ListField(IntField(), mongo_name='lst_int')
     complex_lst_emb_field = EmbDocField(ComplexListDoc, mongo_name='clef')
Example #2
0
    class DocWithMeta(Document):
        _id = StrField(required=True)
        value = IntField(required=True)
        name = SynonymField(_id)

        class Meta:
            collection = 'docs'
            indexes = [IndexModel([('value', ASCENDING)], name='value_index')]
            default_query = {'value': 1}
            write_concern = WriteConcern(w=0)
Example #3
0
 class Doc(Document):
     emb = EmbDocField('test_fields.EmbDoc')
     ref = RefField('test_fields.RefDoc')
     lst_emb = ListField(EmbDocField('test_fields.EmbDoc'))
     lst_ref = ListField(RefField('test_fields.RefDoc'))
     lst_int = ListField(IntField())
     wrong_emb = EmbDocField('xxx')
     wrong_ref = RefField('xxx')
     wrong_lst_emb = ListField(EmbDocField('xxx'))
     wrong_emb_doc = EmbDocField('test_fields.RefDoc')
     wrong_ref_doc = RefField('test_fields.EmbDoc')
Example #4
0
class Post(Document):
    """Post model."""

    title = StrField(required=True, allow_blank=False)
    views = IntField(required=True, default=0)
    created = DateTimeField(required=True, default=lambda: datetime.utcnow())
    rate = FloatField(required=True, default=0.0)
    author = RefField(User, required=True)
    comments = ListField(EmbDocField('models.Comment'),
                         required=True,
                         default=lambda: list())

    class Meta:
        """Post meta."""

        collection = 'posts'
        indexes = [
            IndexModel([('title', ASCENDING)], unique=True,
                       name='title_index'),
            IndexModel([('author', ASCENDING), ('created', DESCENDING)],
                       name='author_created_index')
        ]
Example #5
0
class Doc(Document):
    str_field = StrField(regex=r'[abc]+')
    int_field = IntField(gt=0, lt=10)
    emb_field = EmbDocField(EmbDoc)
    lst_field = ListField(RefField(RefDoc), min_length=1, max_length=1)
Example #6
0
 class Child(Parent):
     value = IntField()
Example #7
0
 class Parent1:
     value = IntField()
Example #8
0
class WrongRefDoc(Document):
    wrong = IntField(required=False)
Example #9
0
 class Child(Mixin, Document):
     value = IntField()
Example #10
0
class EmbDoc(EmbeddedDocument):
    int_field = IntField(required=True)
Example #11
0
FIELD_DEFAULT = [
    (AnyField, 'xxx'),
    (StrField, 'xxx'),
    (IntField, 13),
    (FloatField, 1.3),
    (BoolField, True),
    (DateTimeField, dt),
    (ObjectIdField, ObjectId('58ce6d537e592254b67a503d')),
    (EmailField, '*****@*****.**'),
    (DecimalField, Decimal('0.005')),
]


@pytest.mark.parametrize('field, expected', [
    (StrField(required=False), None),
    (IntField(required=False), None),
    (FloatField(required=False), None),
    (BoolField(required=False), None),
    (DateTimeField(required=False), None),
    (ObjectIdField(required=False), None),
    (EmbDocField(EmbDoc, required=False), None),
    (ListField(EmbDocField(EmbDoc), required=False), None),
    (RefField(RefDoc, required=False), None),
    (EmailField(required=False), None),
])
def test_field_not_exist_get_value(field, expected):
    class Doc(Document):
        value = field

    assert Doc().value is expected
Example #12
0
 class EmbDoc(EmbeddedDocument):
     int_field = IntField(mongo_name='intf')
Example #13
0
 class Parent(EmbeddedDocument):
     value = IntField()
Example #14
0
FIELD_DEFAULT = [
    (AnyField, 'xxx'),
    (StrField, 'xxx'),
    (IntField, 13),
    (FloatField, 1.3),
    (BoolField, True),
    (DateTimeField, dt),
    (ObjectIdField, ObjectId('58ce6d537e592254b67a503d')),
    (EmailField, '*****@*****.**'),
    (DecimalField, Decimal('0.005')),
]


@pytest.mark.parametrize('field, expected', [
    (StrField(required=False), None),
    (IntField(required=False), None),
    (FloatField(required=False), None),
    (BoolField(required=False), None),
    (DateTimeField(required=False), None),
    (ObjectIdField(required=False), None),
    (EmbDocField(EmbDoc, required=False), None),
    (ListField(EmbDocField(EmbDoc), required=False), None),
    (RefField(RefDoc, required=False), None),
    (EmailField(required=False), None),
])
def test_field_not_exist_get_value(field, expected):
    class Doc(Document):
        value = field

    assert Doc().value is expected
Example #15
0
 class Parent2(Root):
     value = IntField()
     slug = StrField()
Example #16
0
 class Root(Document):
     value = IntField()
Example #17
0
 class Parent(Document):
     value = IntField()