class Defaults(document.Document): class Meta: connection = 'test' index = Field(int, default="12") name = Field(str) called = Field([str], default=lambda: list(range(2)))
class WithFields(document.Document): class Meta: connection = 'test' index = Field(int) name = Field(str) optional = Field(str, require=False)
class Partial(document.Document): class Meta: connection = 'test' index = Field(int) name = Field(str) type = Field(str)
class WithReference(document.Document): class Meta: connection = 'test' index = Field(int) sub = Field(SubDocument) ref = Reference(SubDocument)
class WithCollection(document.Document): class Meta: connection = 'test' collection = 'other' index = Field(int) name = Field(str)
class Bear(Document): class Meta: connection = 'example' name = Field(str) type = Field(str) height = Field(float)
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
class WithIndexes(document.Document): class Meta: connection = 'test' indexes = [ IndexModel('index'), IndexModel([('index', ASCENDING), ('name', DESCENDING)]), ] index = Field(int) name = Field(str)
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)
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)
class Parent1(document.Document): class Meta: connection = 'test' index = Field(int)
class Doc(Document): index = Field(int, require=True) name = Field(str, require=True)
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)
class Document(Document): class Meta: connection = 'test' index = Field(int) name = Field(str)
class TopDocument(document.Document): class Meta: connection = 'test' sub = Field(SubDocument)
class Modified(document.Document): class Meta: connection = 'test' index = Field(int) updated = Field(datetime, modifier=lambda v: v or now)
class MultiChild(Parent1, Parent2): class Meta: connection = 'test' name = Field(str)
class SingleChild(Parent1): class Meta: connection = 'test' name = Field(str)
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)
class Child(Document): class Meta: connection = 'test' name = Field(str)
class Doc(Document): index = Field(int) name = Field(str)
class WithDate(document.Document): class Meta: connection = 'test' index = Field(int, require=True) timestamp = Field(datetime, require=True)
class Parent(Document): class Meta: connection = 'test' name = Field(str) child = reference.Reference(Child, require=False)
class SubDocument(document.Document): index = Field(int) name = Field(str)
class Parent2(document.Document): class Meta: connection = 'test' type = Field(str)
class Subdocument(Document): index = Field(int) name = Field(str)