Exemplo n.º 1
0
    def test_member_added_event(self):

        from cocktail.schema import Schema, String

        foo = Schema("foo")
        spam = Schema("spam")
        spam.inherit(foo)

        events = EventLog()
        events.listen(foo.member_added, spam.member_added)

        bar = String("bar")
        foo.add_member(bar)

        scrum = String("scrum")
        foo.add_member(scrum)

        event = events.pop(0)
        self.assertEqual(event.slot, foo.member_added)
        self.assertEqual(event.member, bar)

        event = events.pop(0)
        self.assertEqual(event.slot, foo.member_added)
        self.assertEqual(event.member, scrum)

        self.assertFalse(events)
Exemplo n.º 2
0
        class TestObject(PersistentObject):

            full_text_indexed = True

            field1 = String(full_text_indexed=True)

            field2 = String()
Exemplo n.º 3
0
 class TestObject(PersistentObject):
     test_field = String(
         indexed = True
     )
     test_translated_field = String(
         indexed = True,
         translated = True
     )
Exemplo n.º 4
0
    def test_min(self):

        from cocktail.schema import String, exceptions

        self._test_validation(String(min=5),
                              [None, "hello", "strange", ", strange world!!"],
                              ["", "hulo", "hum"], exceptions.MinLengthError)
Exemplo n.º 5
0
    def test_max(self):

        from cocktail.schema import String, exceptions

        self._test_validation(String(max=5), [None, "", "hi", "hulo", "hello"],
                              ["greetings", "welcome"],
                              exceptions.MaxLengthError)
Exemplo n.º 6
0
    def test_exclusive_expression(self):

        from cocktail.schema import Schema, String, Boolean
        from cocktail.schema.exceptions import (ValueRequiredError,
                                                NoneRequiredError)

        test_schema = Schema()
        test_schema.add_member(Boolean("enabled"))
        test_schema.add_member(
            String("field", exclusive=test_schema["enabled"]))

        # Valid states
        assert test_schema.validate({"enabled": False, "field": None})
        assert test_schema.validate({"enabled": True, "field": "foo"})

        # None required error
        errors = list(
            test_schema.get_errors({
                "enabled": False,
                "field": "foo"
            }))
        assert len(errors) == 1
        error = errors[0]
        assert isinstance(error, NoneRequiredError)

        # Required error
        errors = list(test_schema.get_errors({"enabled": True, "field": None}))
        assert len(errors) == 1
        error = errors[0]
        assert isinstance(error, ValueRequiredError)
Exemplo n.º 7
0
 class TestObject(PersistentObject):
     test_field = String(
         unique = True,
         indexed = True
     )
     parent = Reference(bidirectional = True)
     children = Collection(bidirectional = True)
Exemplo n.º 8
0
    def test_format(self):

        from cocktail.schema import String, exceptions

        self._test_validation(
            String(format=r"^\d{4}-\d{2}[a-zA-Z]$"),
            [None, "4261-85M", "7508-34x"],
            ["", "Bugger numeric codes", "AAADSADS20934832498234"],
            exceptions.FormatError)
Exemplo n.º 9
0
    def test_callable(self):

        from cocktail.schema import Schema, String, Boolean
        from cocktail.schema.exceptions import ValueRequiredError

        test_schema = Schema()
        test_schema.add_member(Boolean("enabled"))
        test_schema.add_member(
            String("field", required=lambda ctx: ctx.get_value("enabled")))

        assert test_schema.validate({"enabled": False, "field": None})
        assert test_schema.validate({"enabled": True, "field": "foo"})

        errors = list(test_schema.get_errors({"enabled": True, "field": None}))
        assert len(errors) == 1
        error = errors[0]
        assert isinstance(error, ValueRequiredError)
Exemplo n.º 10
0
    def test_translated(self):

        from cocktail.schema import Schema, String, exceptions

        schema = Schema()
        schema.add_member(String("foo", translated=True, required=True))

        validable = {"foo": {"ca": "Hola", "es": "Hola", "en": "Hello"}}

        assert not list(schema.get_errors(validable))

        validable["foo"]["fr"] = None
        validable["foo"]["es"] = None

        errors = list(schema.get_errors(validable))
        assert len(errors) == 2
        assert set([error.language for error in errors]) == set(["fr", "es"])
Exemplo n.º 11
0
def get_user_schema():

    import re
    from cocktail.schema import Schema, Integer, String, Boolean

    return Schema(members = {
        "id": Integer(
            required = True,
            unique = True,
            min = 1
        ),
        "name": String(
            required = True,
            min = 4,
            max = 20,
            format = re.compile("^[a-zA-Z][a-zA-Z_0-9]*$")
        ),
        "enabled": Boolean(
            required = True,
            default = True
        )
    })
Exemplo n.º 12
0
 class TestObject(PersistentObject):
     test_member = String(indexed = True, normalized_index = True)
Exemplo n.º 13
0
 def slot(self):
     param = self.params.read(String("slot", format = r"\d+-\d+"))
     return map(int, param.split("-")) if param else None
Exemplo n.º 14
0
 def member(self):
     key = self.params.read(String("member"))
     return self.item.__class__[key] if key and self.item else None
Exemplo n.º 15
0
        class TestObject(PersistentObject):

            full_text_indexed = True

            test_field = String(translated=True, full_text_indexed=True)
Exemplo n.º 16
0
 def action(self):
     return self.params.read(String("action"))
Exemplo n.º 17
0
 def setUp(self):
     from cocktail.schema import Schema, Integer, String
     self.schema = Schema(members=[
         Integer("num", required=True, min=5, default=50),
         String("text", min=5, default="cornucopia")
     ])
Exemplo n.º 18
0
 class TestObject(PersistentObject):
     test_member = String(indexed = True)
Exemplo n.º 19
0
 class TestObject(PersistentObject):
     test_member = String(
         indexed = True,
         unique = True,
         translated = True
     )
Exemplo n.º 20
0
 class Foo(PersistentObject):
     spam = String(unique=True, indexed=True)
Exemplo n.º 21
0
 class TestObject(PersistentObject):
     test_field = String(unique=True, indexed=True)
     test_ref = Reference()
     test_collection = Collection()
     test_translated_field = String(indexed=True, translated=True)