def test_base_field_must_be_a_field(self): try: ListField("invalid", db_field="test") except ValueError: err = sys.exc_info()[1] expect(err) \ .to_have_an_error_message_of("The list field 'field' argument must be an instance of BaseField, not 'invalid'.") else: assert False, "Should not have gotten this far"
class User(Document): __collection__ = "AggregationUser" email = StringField(required=True) first_name = StringField(max_length=50, default=lambda: "Bernardo") last_name = StringField(max_length=50, default="Heynemann") is_admin = BooleanField(default=True) updated_at = DateTimeField(required=True, auto_now_on_insert=True, auto_now_on_update=True) number_of_documents = IntField() list_items = ListField(IntField())
class PenetrationTestDataNote(MyDocument): """ 渗透测试详情 """ __collection__ = "penetration_test_data_note" penetration_id = StringField(max_length=1024) ip = StringField(max_length=1024) details = ListField(ReferenceField(UnitPenetrationTest)) # SSHRootEmptyPassword = StringField(max_length=1024) # RedisEmptyPassword = StringField(max_length=1024) # MongoEmptyPassword = StringField(max_length=1024) organization = ReferenceField( reference_document_type=Organization) # 数据所属的组织
class User(Document): email = StringField(required=True) first_name = StringField(db_field="whatever", max_length=50, default=lambda: "Bernardo") last_name = StringField(max_length=50, default="Heynemann") is_admin = BooleanField(default=True) website = URLField(default="http://google.com/") updated_at = DateTimeField(required=True, auto_now_on_insert=True, auto_now_on_update=True) embedded = EmbeddedDocumentField(EmbeddedDocument, db_field="embedded_document") nullable = EmbeddedDocumentField(EmbeddedDocument, db_field="nullable_embedded_document") numbers = ListField(IntField()) def __repr__(self): return "%s %s <%s>" % (self.first_name, self.last_name, self.email)
class UnitTestData(MyDocument): """ 测试数据统计表--插入时没有使用,没有使用ORM """ __collection__ = "unit_test_data" pro_version = StringField(max_length=1024) # 项目版本号:1.2.3.4 was_successful = BooleanField() # 是否是成功的 total = IntField() failures = IntField() errors = IntField() skipped = IntField() run_time = FloatField() details = ListField(ReferenceField(UnitTestDataDetail)) # 数据归属 project = ReferenceField(reference_document_type=Project) # 测试数据所属的项目 project_name = StringField() # 项目名称,冗余 organization = ReferenceField( reference_document_type=Organization) # 数据所属的组织
def test_validate_propagates(self): field = ListField(StringField()) expect(field.validate(["1", "2", "3"])).to_be_true() expect(field.validate(["1", 2, "3"])).to_be_false()
def test_from_son(self): field = ListField(StringField()) expect(field.from_son([])).to_equal([]) expect(field.from_son(["1", "2", "3"])).to_equal(["1", "2", "3"])
def test_create_list_field(self): field = ListField(StringField(), db_field="test") expect(field.db_field).to_equal("test") expect(field._base_field).to_be_instance_of(StringField)
def test_validate_none(self): field = ListField(StringField()) expect(field.validate(None)).to_be_true() field = ListField(StringField(), required=True) expect(field.validate(None)).to_be_false()
class ElemMatchEmbeddedParentDocument(Document): items = ListField(EmbeddedDocumentField(ElemMatchEmbeddedDocument))
class SizeDocument(Document): items = ListField(IntField()) item_size = IntField(default=0, on_save=lambda doc, creating: len(doc.items))
class Base(Document): __collection__ = 'base' __lazy__ = False list_val = ListField(ReferenceField(reference_document_type=Ref))
class RawQueryDocument(Document): items = ListField(EmbeddedDocumentField(RawQueryEmbeddedDocument))
class Post(Document): title = StringField(required=True) body = StringField(required=True) comments = ListField(EmbeddedDocumentField(Comment))
class ElemMatchDocument(Document): items = ListField(IntField())