Exemple #1
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]
Exemple #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)
    def test_base_isolation(self):

        from cocktail.schema import Schema

        base = Schema()

        derived = Schema()
        self._add_error(derived, "derived_error")
        derived.inherit(base)

        assert not list(base.get_errors({}))
    def test_single_inheritance(self):

        from cocktail.schema import Schema

        base = Schema("base")
        self._add_error(base, "base_error")

        derived = Schema("derived")
        derived.inherit(base)

        errors = list(derived.get_errors({}))
        assert len(errors) == 1
        assert errors[0].error_id == "base_error"
    def test_add_duplicated_validation_to_derived_schema(self):

        from cocktail.schema import Schema

        def a():
            pass

        s1 = Schema()
        s1.add_validation(a)

        s2 = Schema()
        s2.inherit(s1)
        v1 = list(s2.validations())
        s2.add_validation(a)
        v2 = list(s2.validations())

        assert v1 == v2
Exemple #6
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])
    def test_multiple_inheritance(self):

        from cocktail.schema import Schema

        base1 = Schema("base1")
        self._add_error(base1, "base1_error")

        base2 = Schema("base2")
        self._add_error(base2, "base2_error")

        derived = Schema("derived")
        derived.inherit(base1)
        derived.inherit(base2)

        errors = list(derived.get_errors({}))
        assert len(errors) == 2
        assert errors[0].error_id == "base1_error"
        assert errors[1].error_id == "base2_error"
Exemple #8
0
    def test_inherited_event(self):

        from cocktail.schema import Schema

        foo = Schema()
        events = EventLog()
        events.listen(foo_inherited=foo.inherited)

        # Basic inheritance
        bar = Schema()
        bar.inherit(foo)

        event = events.pop(0)
        self.assertEqual(event.slot, foo.inherited)
        self.assertEqual(event.schema, bar)

        events.listen(bar_inherited=bar.inherited)

        # Nested inheritance
        spam = Schema()
        spam.inherit(bar)

        event = events.pop(0)
        self.assertEqual(event.slot, foo.inherited)
        self.assertEqual(event.schema, spam)

        event = events.pop(0)
        self.assertEqual(event.slot, bar.inherited)
        self.assertEqual(event.schema, spam)

        # Multiple inheritance
        scrum = Schema()

        events.listen(scrum_inherited=scrum.inherited)

        snutch = Schema()
        snutch.inherit(foo, scrum)

        event = events.pop(0)
        self.assertEqual(event.slot, foo.inherited)
        self.assertEqual(event.schema, snutch)

        event = events.pop(0)
        self.assertEqual(event.slot, scrum.inherited)
        self.assertEqual(event.schema, snutch)
    def test_deep_inheritance(self):

        from cocktail.schema import Schema

        s1 = Schema()
        self._add_error(s1, "s1_error")

        s2 = Schema()
        s2.inherit(s1)
        self._add_error(s2, "s2_error")

        s3 = Schema()
        s3.inherit(s2)
        self._add_error(s3, "s3_error")

        s4 = Schema()
        s4.inherit(s3)

        errors = list(s4.get_errors({}))
        assert len(errors) == 3
        assert errors[0].error_id == "s1_error"
        assert errors[1].error_id == "s2_error"
        assert errors[2].error_id == "s3_error"