我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用mongoengine.StringField()。
def test_add_operators_to_field_reference_field(): from mongoengine import ReferenceField, Document, StringField from graphene_mongo.operators import gen_operators_of_field, allowed_operators from graphene_mongo.fields import respective_special_fields class Other(Document): name = StringField() class Test(Document): test = ReferenceField(Other) field = Test.test r_graphene = respective_special_fields[type(field)] applied_operators = gen_operators_of_field('test', field, r_graphene('test', field), allowed_operators(field)) assert sorted(list(applied_operators.keys())) == format_fields(['in', 'nin', 'ne']) assert isinstance(applied_operators['test__in'], graphene.List) assert isinstance(applied_operators['test__nin'], graphene.List) assert isinstance(applied_operators['test__ne'], graphene.ID)
def test_validator_wrong(): from mongoengine import Document, StringField class Test(Document): parent = StringField() with pytest.raises(Exception) as e_info: Options('TestSchema', {'model': Test, 'validator': True}) assert str(e_info.value) == "'validator' attribute must be callable." with pytest.raises(Exception) as e_info: Options('TestSchema', {'model': Test, 'validator': lambda x: x}) assert str(e_info.value) == ("The 'validator' attribute must be a callable that accepts four arguments: " "model, fields, query, special_params")
def setUp(self): """Setup.""" class ExclusionModel(gj.Document): to_json_exclude = db.StringField(exclude_to_json=True) from_json_exclude = db.IntField(exclude_from_json=True) json_exclude = db.StringField(exclude_json=True) required = db.StringField(required=True) self.cls = ExclusionModel self.test_data = [] for counter in range(3): data_el = { "to_json_exclude": ("To JSON Exclusion {}").format(counter), "from_json_exclude": counter, "json_exclude": ("JSON Exclusion {}").format(counter), "required": ("Required {}").format(counter) } model = self.cls(**data_el) model.save() self.test_data.append(data_el)
def setUp(self): """Setup.""" class ExclusionModel(gj.Document): to_json_exclude = db.StringField(exclude_to_json=True) from_json_exclude = db.IntField(exclude_from_json=True) json_exclude = db.StringField(exclude_json=True) required = db.StringField(required=True) self.cls = ExclusionModel self.data = { "to_json_exclude": "Hello", "from_json_exclude": 10234, "json_exclude": "Hi", "required": "World" } self.model = self.cls(**self.data)
def setUp(self): """Setup.""" self.maxDiff = None class ModelCls(gj.Document): name = db.StringField() ref = gj.FollowReferenceField("self") self.model_cls = ModelCls self.model = self.model_cls(name="test") self.model.save() self.model.ref = self.model self.model.save() self.data = {u"id": str(self.model.id), u"name": "test"} self.data["ref"] = self.data.copy() self.data["ref"]["ref"] = self.data["ref"].copy() self.data["ref"]["ref"]["ref"] = self.data["ref"]["ref"].copy() self.data["ref"]["ref"]["ref"]["ref"] = self.data["id"]
def setUp(self): """Setup the class.""" self.dict_model = [ {u"test": u"Hello %s" % count} for count in range(5) ] QuerySet.as_pymongo = MagicMock(return_value=self.dict_model) class TestModel(Document): test = db.StringField() self.as_pymongo = TestModel.objects.as_pymongo self.model_cls = TestModel self.model = [ self.model_cls(test="Hello %s" % count) for count in range(5) ]
def setUp(self): """Setup.""" self.maxDiff = None class User(gj.Document): name = db.StringField() email = db.EmailField() class TestDocument(gj.Document): value = db.StringField() author = gj.FollowReferenceField(User) contributors = db.ListField(gj.FollowReferenceField(User)) self.user_model_cls = User self.test_model_cls = TestDocument self.users_data = [ { "id": ObjectId(), "name": ("Test Example {}").format(counter), "email": ("test{}@example.com").format(counter) } for counter in range(3) ] self.tests_data = [ { "id": ObjectId(), "value": ("test{}").format(counter), "author": self.users_data[counter]["id"], "contributors": [ user["id"] for (index, user) in enumerate(self.users_data) if index != counter ] } for counter in range(3) ]
def setUp(self): """Setup the class.""" import json class TestDocument(Document): title = db.StringField() self.model_cls = TestDocument self.data = json.dumps({"title": "Test"})