def __init__(self, **kwargs): self._errors = Errors() for field in self.fields: set_field_attr(self, field, kwargs.get(field)) for k, v in kwargs.iteritems(): if k not in self.fields: msg = "Ignoring unknown field for %s: %s = '%s'" logging.warn(msg, self.__class__.__name__, repr(k), repr(v)) self.changed_fields = set()
class Target(Document): f = fields.DateTimeField("my_datetime", required=True) errors = Errors()
class Order(object): f = fields.ListField("orderlines", OrderLine) errors = Errors()
class Target(Document): f = fields.EmbeddedField("address", Address) errors = Errors()
class Target(Document): errors = Errors()
class Target(Document): f = fields.StringField("my_field", choices=["type_a", "type_b"]) errors = Errors()
class Target(Document): f = fields.StringField("my_field", pattern="^This") errors = Errors()
class Target(Document): f = fields.ObjectIdField("my_field", required=True) errors = Errors()
class ErrorsTest(unittest.TestCase): def setUp(self): super(ErrorsTest, self).setUp() self.errors = Errors() def test_add_error(self): self.errors.add("email", "is required") self.assertEqual(["is required"], self.errors.get("email")) def test_add_errors_to_one_field(self): self.errors.add("email", "is required") self.errors.add("email", "must be valid") self.assertEqual( ["is required", "must be valid"], self.errors.get("email") ) def test_clear(self): self.errors.add("email", "is required") self.errors.add("email", "must be valid") self.errors.clear("email") self.assertEqual(0, len(self.errors.get("email"))) def test_count(self): self.errors.add("email", "is required") self.errors.add("email", "must be valid") self.errors.add("first_name", "is required") self.assertEqual(3, self.errors.count) def test_full_messages_for(self): self.errors.add("email", "is required") self.errors.add("email", "must be valid") self.errors.add("first_name", "is required") expected = [ "Email is required", "Email must be valid" ] self.assertEqual(expected, self.errors.full_messages_for("email")) def test_full_messages(self): self.errors.add("email", "is required") self.errors.add("email", "must be valid") self.errors.add("first_name", "is required") self.assertTrue("Email is required" in self.errors.full_messages) self.assertTrue("Email must be valid" in self.errors.full_messages) self.assertTrue("First Name is required" in self.errors.full_messages)
def setUp(self): super(ErrorsTest, self).setUp() self.errors = Errors()
class ErrorsTest(unittest.TestCase): def setUp(self): super(ErrorsTest, self).setUp() self.errors = Errors() def test_add_error(self): self.errors.add("email", "is required") self.assertEqual(["is required"], self.errors.get("email")) def test_add_errors_to_one_field(self): self.errors.add("email", "is required") self.errors.add("email", "must be valid") self.assertEqual(["is required", "must be valid"], self.errors.get("email")) def test_clear(self): self.errors.add("email", "is required") self.errors.add("email", "must be valid") self.errors.clear("email") self.assertEqual(0, len(self.errors.get("email"))) def test_count(self): self.errors.add("email", "is required") self.errors.add("email", "must be valid") self.errors.add("first_name", "is required") self.assertEqual(3, self.errors.count) def test_full_messages_for(self): self.errors.add("email", "is required") self.errors.add("email", "must be valid") self.errors.add("first_name", "is required") expected = ["Email is required", "Email must be valid"] self.assertEqual(expected, self.errors.full_messages_for("email")) def test_full_messages(self): self.errors.add("email", "is required") self.errors.add("email", "must be valid") self.errors.add("first_name", "is required") self.assertTrue("Email is required" in self.errors.full_messages) self.assertTrue("Email must be valid" in self.errors.full_messages) self.assertTrue("First Name is required" in self.errors.full_messages)
class Target(object): f = BaseField("my_field", default=1, required=True) errors = Errors()
class Target(object): afield = TestField("afield", default=-1) errors = Errors()
class Target(object): f = BaseField("my_field", persist=False) errors = Errors()
class Target(object): f = BaseField("my_field", choices=['type_a', 'type_b']) errors = Errors()
class Target(object): f = BaseField("my_field", default=1) errors = Errors()
class Target(Document): f = fields.ObjectIdField("my_field") errors = Errors()
class Target(Document): f = fields.StringField("my_field", max_length=10) errors = Errors()
class Target(Document): f = fields.FloatField("my_float") errors = Errors()
class Target(Document): f = fields.StringField("my_field", required=True, min_length=10) errors = Errors()
class Target(Document): f = fields.FloatField("my_float", max_value=0) errors = Errors()
class Target(Document): f = fields.StringField("my_field") errors = Errors()
class Target(Document): f = fields.FloatField("my_float", required=True, min_value=10) errors = Errors()
class Target(object): f = fields.EmbeddedField("address", Address) errors = Errors()
class Target(Document): f = fields.IntegerField("my_integer") errors = Errors()
class Target(Document): f = fields.BooleanField("my_boolean", default=True, required=True) errors = Errors()
class Target(Document): f = fields.IntegerField("my_integer", max_value=0) errors = Errors()
class Target(Document): f = fields.DateTimeField("my_datetime") errors = Errors()
class Target(Document): f = fields.IntegerField("my_integer", required=True, min_value=10) errors = Errors()
class Target(Document): f = fields.BooleanField("my_boolean", required=False) errors = Errors()
class Target(Document): f = fields.ArrayField("violence", validate_item=validate) errors = Errors()