Esempio n. 1
0
class Defaults(document.Document):
    class Meta:
        connection = 'test'

    index = Field(int, default="12")
    name = Field(str)
    called = Field([str], default=lambda: list(range(2)))
Esempio n. 2
0
class WithFields(document.Document):
    class Meta:
        connection = 'test'

    index = Field(int)
    name = Field(str)
    optional = Field(str, require=False)
Esempio n. 3
0
class Partial(document.Document):
    class Meta:
        connection = 'test'

    index = Field(int)
    name = Field(str)
    type = Field(str)
Esempio n. 4
0
class WithReference(document.Document):
    class Meta:
        connection = 'test'

    index = Field(int)
    sub = Field(SubDocument)
    ref = Reference(SubDocument)
Esempio n. 5
0
class WithCollection(document.Document):
    class Meta:
        connection = 'test'
        collection = 'other'

    index = Field(int)
    name = Field(str)
Esempio n. 6
0
class Bear(Document):
    class Meta:
        connection = 'example'

    name = Field(str)
    type = Field(str)
    height = Field(float)
Esempio n. 7
0
        class InitChild(Parent1):
            class Meta:
                connection = 'test'

            name = Field(str)
            value = Field(str)

            def __init__(self, *args, **kwargs):
                super(InitChild, self).__init__(*args, **kwargs)
                self.value = "value of " + self.name
Esempio n. 8
0
class WithIndexes(document.Document):
    class Meta:
        connection = 'test'
        indexes = [
            IndexModel('index'),
            IndexModel([('index', ASCENDING), ('name', DESCENDING)]),
        ]

    index = Field(int)
    name = Field(str)
Esempio n. 9
0
def create_document(base=None, **options):
    """Return a document class with the given meta options."""
    attrs = {'Meta': type('Meta', (object, ), options)}
    if not base:
        base = document.Document
        attrs.update({
            'index': Field(int),
            'name': Field(str),
        })

    return type('CreatedDocument', (base, ), attrs)
Esempio n. 10
0
    def test_get_field(self):
        """Document._meta.get_field"""
        def test(name, want):
            field = WithReference._meta.get_field(name)
            if want is None:
                self.assertIsNone(field)
            elif isinstance(want, Reference):
                self.assertIsInstance(field, Reference)
                self.assertEqual(field.doctype, want.doctype)
            else:
                self.assertIsInstance(field, Field)
                if hasattr(field.typ, 'builtin'):
                    self.assertEqual(field.typ.builtin, want.typ.builtin)
                elif hasattr(field.typ, 'document'):
                    self.assertEqual(field.typ.document, want.typ.document)
                else:
                    self.assertEqual(field.typ.__class__, want.typ.__class__)

        test('index', Field(int))
        test('index.none', None)
        test('sub', Field(SubDocument))
        test('sub.index', Field(int))
        test('ref', Reference(SubDocument))
        test('ref.index', None)
Esempio n. 11
0
        class Parent1(document.Document):
            class Meta:
                connection = 'test'

            index = Field(int)
Esempio n. 12
0
 class Doc(Document):
     index = Field(int, require=True)
     name = Field(str, require=True)
Esempio n. 13
0
 def test_field(self):
     """Field(list)"""
     field = Field([str])
     self.assertIsInstance(field.typ, types.ListType)
     self.assertIsInstance(field.typ.typ, types.BuiltinType)
     self.assertEqual(field.typ.typ.builtin, six.text_type)
Esempio n. 14
0
 class Document(Document):
     class Meta:
         connection = 'test'
     index = Field(int)
     name = Field(str)
Esempio n. 15
0
class TopDocument(document.Document):
    class Meta:
        connection = 'test'

    sub = Field(SubDocument)
Esempio n. 16
0
        class Modified(document.Document):
            class Meta:
                connection = 'test'

            index = Field(int)
            updated = Field(datetime, modifier=lambda v: v or now)
Esempio n. 17
0
        class MultiChild(Parent1, Parent2):
            class Meta:
                connection = 'test'

            name = Field(str)
Esempio n. 18
0
        class SingleChild(Parent1):
            class Meta:
                connection = 'test'

            name = Field(str)
Esempio n. 19
0
class ForEncoders(Document):
    boolean = Field(bool)
    number = Field(int)
    text = Field(str)
    date = Field(date)
    datetime = Field(datetime)
    time = Field(time)
    intarray = Field([int])
    strarray = Field([str])
    anyarray = Field([])
    intset = Field({int})
    strset = Field({str})
    anyset = Field(set())
    sub = Field(Subdocument)
    subarray = Field([Subdocument])
    ref = Reference(Subdocument)
Esempio n. 20
0
class Child(Document):
    class Meta:
        connection = 'test'

    name = Field(str)
Esempio n. 21
0
 class Doc(Document):
     index = Field(int)
     name = Field(str)
Esempio n. 22
0
class WithDate(document.Document):
    class Meta:
        connection = 'test'

    index = Field(int, require=True)
    timestamp = Field(datetime, require=True)
Esempio n. 23
0
class Parent(Document):
    class Meta:
        connection = 'test'

    name = Field(str)
    child = reference.Reference(Child, require=False)
Esempio n. 24
0
class SubDocument(document.Document):
    index = Field(int)
    name = Field(str)
Esempio n. 25
0
        class Parent2(document.Document):
            class Meta:
                connection = 'test'

            type = Field(str)
Esempio n. 26
0
class Subdocument(Document):
    index = Field(int)
    name = Field(str)