Example #1
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)
Example #2
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)
Example #3
0
    def test_ignores_missing_anchors(self):

        from cocktail.schema import Schema, Member

        m1 = Member("m1", after_member="m3")
        m2 = Member("m2")

        schema = Schema()
        schema.members_order = ["m2"]
        schema.add_member(m1)
        schema.add_member(m2)

        assert schema.ordered_members() == [m2, m1]
Example #4
0
    def test_members_can_specify_relative_positions_using_base_schemas(self):

        from cocktail.schema import Schema, Member

        b1 = Member("b1")
        b2 = Member("b2")
        b3 = Member("b3")

        b = Schema()
        b.members_order = ["b2", "b3"]
        b.add_member(b1)
        b.add_member(b2)
        b.add_member(b3)

        d1 = Member("d1")
        d2 = Member("d2", before_member="b2")
        d3 = Member("d3", after_member="b1")

        d = Schema()
        d.inherit(b)
        d.add_member(d1)
        d.add_member(d2)
        d.add_member(d3)

        ordered_members = d.ordered_members()
        assert ordered_members == [d2, b2, b3, b1, d3, d1]
Example #5
0
    def get_schema(self):

        from cocktail.schema import Schema, Reference, Collection

        schema = Schema()

        schema.add_member(Reference("parent", type=schema, bidirectional=True))

        schema.add_member(
            Collection("children",
                       items=Reference(type=schema),
                       bidirectional=True))

        return schema
Example #6
0
    def test_match_one_to_one(self):

        from cocktail.schema import Schema, Reference
        from cocktail.schema.exceptions import SchemaIntegrityError

        a = Schema("a")
        b = Schema("b")

        a.add_member(Reference("rel_b", type=b, bidirectional=True))
        b.add_member(Reference("rel_a", type=a, bidirectional=True))

        self.assertTrue(a["rel_b"].related_end is b["rel_a"])
        self.assertTrue(a["rel_b"].related_type is b)
        self.assertTrue(b["rel_a"].related_end is a["rel_b"])
        self.assertTrue(b["rel_a"].related_type is a)
Example #7
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)
Example #8
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"])
Example #9
0
    def test_follows_explicit_ordering(self):

        from cocktail.schema import Schema, Member

        m1 = Member("m1")
        m2 = Member("m2")
        m3 = Member("m3")
        m4 = Member("m4")

        schema = Schema()
        schema.members_order = ["m1", "m4", "m3", "m2"]
        schema.add_member(m1)
        schema.add_member(m2)
        schema.add_member(m3)
        schema.add_member(m4)

        assert schema.ordered_members() == [m1, m4, m3, m2]
Example #10
0
    def test_no_match(self):

        from cocktail.schema import Schema, Reference
        from cocktail.schema.exceptions import SchemaIntegrityError

        a = Schema("a")
        b = Schema("b")

        a.add_member(Reference("rel_b", type=b, bidirectional=True))
        b.add_member(Reference("rel_a", type=a))

        def resolve_relation():
            print(a["rel_b"].related_end)

        self.assertRaises(SchemaIntegrityError, resolve_relation)

        self.assertTrue(b["rel_a"].related_end is None)
Example #11
0
    def test_match_many_to_many(self):

        from cocktail.schema import Schema, Reference, Collection
        from cocktail.schema.exceptions import SchemaIntegrityError

        a = Schema("a")
        b = Schema("b")

        a.add_member(
            Collection("rel_b", items=Reference(type=b), bidirectional=True))
        b.add_member(
            Collection("rel_a", items=Reference(type=a), bidirectional=True))

        self.assertTrue(a["rel_b"].related_end is b["rel_a"])
        self.assertTrue(a["rel_b"].related_type is b)
        self.assertTrue(b["rel_a"].related_end is a["rel_b"])
        self.assertTrue(b["rel_a"].related_type is a)
Example #12
0
    def test_implicitly_includes_unspecified_members(self):

        from cocktail.schema import Schema, Member

        m1 = Member("m1")
        m2 = Member("m2")
        m3 = Member("m3")
        m4 = Member("m4")

        schema = Schema()
        schema.members_order = ["m3", "m2"]
        schema.add_member(m1)
        schema.add_member(m2)
        schema.add_member(m3)
        schema.add_member(m4)

        ordered_members = schema.ordered_members()
        assert len(ordered_members) == 4
        assert ordered_members[:2] == [m3, m2]
        assert set(ordered_members[2:]) == set([m1, m4])
Example #13
0
    def test_layers_members_from_derived_schemas(self):

        from cocktail.schema import Schema, Member

        b1 = Member("b1")
        b2 = Member("b2")
        b3 = Member("b3")
        b4 = Member("b4")

        b = Schema()
        b.members_order = ["b4", "b3"]
        b.add_member(b1)
        b.add_member(b2)
        b.add_member(b3)
        b.add_member(b4)

        d1 = Member("d1")
        d2 = Member("d2")
        d3 = Member("d3")
        d4 = Member("d4")

        d = Schema()
        d.inherit(b)
        d.members_order = ["d4", "d3"]
        d.add_member(d1)
        d.add_member(d2)
        d.add_member(d3)
        d.add_member(d4)

        mlist = b.ordered_members()
        assert len(mlist) == 4
        assert mlist[:2] == [b4, b3]
        assert set(mlist[2:]) == set([b1, b2])

        mlist = d.ordered_members(False)
        assert len(mlist) == 4
        assert mlist[:2] == [d4, d3]
        assert set(mlist[2:]) == set([d1, d2])

        mlist = d.ordered_members()
        assert len(mlist) == 8
        assert mlist[:2] == [b4, b3]
        assert set(mlist[2:4]) == set([b1, b2])
        assert mlist[4:6] == [d4, d3]
        assert set(mlist[6:]) == set([d1, d2])
Example #14
0
    def test_members_can_specify_relative_positions(self):

        from cocktail.schema import Schema, Member

        m1 = Member("m1")
        m2 = Member("m2")
        m3 = Member("m3")
        m4 = Member("m4", after_member="m5")
        m5 = Member("m5", before_member="m1")
        m6 = Member("m6", before_member="m3")

        schema = Schema()
        schema.members_order = ["m3", "m2"]
        schema.add_member(m1)
        schema.add_member(m2)
        schema.add_member(m3)
        schema.add_member(m4)
        schema.add_member(m5)
        schema.add_member(m6)

        ordered_members = schema.ordered_members()
        assert ordered_members == [m6, m3, m2, m5, m4, m1]