Exemple #1
0
 def test_using_too_many_field(self):
     from asobibi import ConstructionError
     from asobibi import schema
     Schema = schema("Schema", [("val0", {}), ("val1", {})])
     with pytest.raises(ConstructionError) as e:
         e.expected_regexp = "too many"
         self._getTarget()("Validator", [((Schema.val0, Schema.val1), lambda k, val0: True)])
Exemple #2
0
 def test_failure2(self):
     schema = self._getComplexSchema()
     target = schema({"person":
                      {"first_name": "first-name"},
                      "address":
                      {"prefecture": "tokyo"}})
     assert target.validate() is not True
Exemple #3
0
    def test_it(self):
        from asobibi.exceptions import ValidationError
        from asobibi.translations import SystemMessage, DisplayMessage
        from asobibi.construct import ErrorList

        SystemMessage("unicode", fmt="{value} is not strict unicode.", mapping={"value": "----"})
        DisplayMessage("unicode", fmt=u"入力してください {field}", mapping={"field": "----"})

        def strict_unicode(name, x):
            if x is None or x == "":
                raise ValidationError({"name": "unicode", "field": name, "value": x})

        from asobibi import schema, field
        StrictUnicode = field(converters=[strict_unicode])
        Person = schema("Person", [StrictUnicode("first_name"), StrictUnicode("last_name")])

        person = Person()
        self.assertFalse(person.validate())
        self.assertEquals(str(person.errors),
                          str(ErrorList({'first_name': ['first_name is Missing.'],
                                         'last_name': ['last_name is Missing.']})))
        self.assertEquals(unicode(person.errors),
                          unicode(ErrorList({'first_name': [u'first_name がみつかりません'],
                                             'last_name': [u'last_name がみつかりません']})))

        person = Person(first_name="test", last_name="")
        self.assertFalse(person.validate())
        self.assertEquals(str(person.errors),
                          str(ErrorList({'last_name': [' is not strict unicode.']})))

        self.assertEquals(unicode(person.errors),
                          unicode(ErrorList({"last_name": [u"入力してください last_name"]})))
Exemple #4
0
    def _get_schema(self):
        from asobibi import schema
        from asobibi import Op, Int

        def positive(k, x):
            assert x >= 0
            return x
        return schema("Point", [("x", {Op.converters: [Int]}),
                                ("y", {Op.converters: [Int, positive]}), ])
Exemple #5
0
    def test_it(self):
        schema = self._getTarget()
        Schema = schema("Schema", [("a", {}), ("b", {}), ("c", {}), ("d", {})])

        complete = Schema(a=1)
        assert complete.validate() is not True

        partial = Schema.partial(a=1)
        assert partial.validate() is True
        assert dict(partial.result) == {"a": 1}
Exemple #6
0
    def _getComplexSchema(self):
        from asobibi import schema
        from asobibi import Op, Unicode, as_converter

        def capitalize(k, string):
            return u"".join(x.capitalize() for x in string.split("-"))

        Address = schema("Address",
                         [("prefecture", {Op.converters: [Unicode, capitalize]}),
                          ("city", {Op.converters: [Unicode, capitalize]}),
                          ("address_1", {}),
                          ("address_2", {Op.required: False, Op.initial: None}),
                          ])

        Person = schema("Person",
                        [("first_name", {Op.converters: [Unicode, capitalize]}),
                         ("last_name", {Op.converters: [Unicode, capitalize]})])

        Account = schema("Account",
                         [("person", {Op.converters: [as_converter(Person)]}),
                          ("address", {Op.converters: [as_converter(Address)]})])
        return Account
Exemple #7
0
 def _get_A(self):
     from asobibi import schema
     from asobibi import Op, Unicode, as_converter
     Pair = schema("Pair", [("left", {Op.converters: [Unicode, lambda k, x: x.capitalize()]}),
                            ("right", {Op.converters: [Unicode, lambda k, x: x.capitalize()]}),
                            ])
     E = schema("E", [("f", {Op.converters: [as_converter(Pair)]})])
     D = schema("D", [("e", {Op.converters: [as_converter(E)]})])
     C = schema("C", [("d", {Op.converters: [as_converter(D)]})])
     B = schema("B", [("c", {Op.converters: [as_converter(C)]})])
     A = schema("A", [("b", {Op.converters: [as_converter(B)]})])
     return A
Exemple #8
0
 def test_it(self):
     schema = self._getComplexSchema()
     target = schema({"person":
                      {"first_name": "first-name",
                       "last_name": "last-name", },
                      "address":
                      {"prefecture": "tokyo",
                       "city": "chiyoda-ku",
                       "address_1": "chiyoda 1-1",
                       }
                      })
     assert target.validate() is True
     assert target.result ==\
         {"person":
          {"first_name": "FirstName",
           "last_name": "LastName", },
          "address":
          {"prefecture": "Tokyo",
           "city": "ChiyodaKu",
           "address_1": "chiyoda 1-1",
           "address_2": None}
         }
Exemple #9
0
 def test_failure(self):
     schema = self._getComplexSchema()
     target = schema()
     assert target.validate() is not True
Exemple #10
0
 def _get_schema(self):
     from asobibi import schema
     return schema("Schema", [("val", {})])
Exemple #11
0
        raise ValidationError(params)
    return x


@c.validation_from_condition
def not_empty(x):
    return x != ""

Unicode = field(converters=[c.Unicode, not_empty])


# schema definition

Submit = schema(
    "Submit",
    (Unicode("mail", initial="sample@mail", converters=[tiny_email]),
     Unicode("password"),
     Unicode("confirm")
     ))


submit = Submit(mail="foo", password="******", confirm="@")
assert submit.validate() == False

submit = Submit(mail="*****@*****.**", password="******", confirm="@")
assert submit.validate()
assert submit.result["mail"] == "*****@*****.**"
assert submit.password == "@"
assert submit.confirm == "@"


# validator definition