#!/www/python/bin/python
"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/utest_category.py $
$Id: utest_category.py 25054 2004-09-09 18:04:30Z dbinger $

Test for dulcinea.category.
"""
from sets import Set
from sancho.utest import UTest
from dulcinea.category import Category, Grouped, remove_ancestors


class CategoryTest (UTest):

    def check_basic (self):
        self.category = Category()
        assert str(self.category) == '*unnamed*'
        assert not self.category.check_name("ab%")
        assert not self.category.check_name("[ab]")
        assert not self.category.check_name("a\b")
        assert not self.category.check_name("a b")
        assert not self.category.check_name("a/b")
        assert not self.category.check_name(" a/b")
        assert self.category.check_name("__")
        assert self.category.check_name("34")
        assert self.category.check_name("a8")
        assert self.category.check_name("_a8")
        try:
            self.category.set_name(" ")
            assert 0
        except ValueError: pass
        self.category.set_name("boo")
        assert self.category.get_name() == "boo"
        self.category.set_label("Boo")
        assert self.category.get_label() == "Boo"
        self.category.set_description("casual")
        assert self.category.get_description() == "casual"

    def check_grouped (self):
        self.grouped = Grouped()
        self.category1 = Category()
        self.category2 = Category()
        try:
            self.grouped.add_group(None)
            assert 0
        except TypeError: pass
        try:
            self.grouped.add_group(42)
            assert 0
        except TypeError: pass
        self.grouped.add_group(self.category1)
        assert self.grouped.in_group(self.category1)
        assert not self.grouped.in_group(None)
        assert self.grouped.in_groups([self.category1])
        assert Set(self.grouped.get_groups()) == Set([self.category1])
        assert Set(self.grouped.get_descendants_of(self.category1)) == Set(
            [self.category1])
        assert Set(self.grouped.get_descendants_of(self.category2)) == Set([])
        assert not self.grouped.in_groups([self.category1, self.category2])

    def check_ancestry (self):
        self.a = Category("a")
        self.b = Category("b")
        self.c = Category("c")
        self.d = Category("d")
        assert Set(self.a.get_children()) == Set([])
        assert Set(self.b.get_parents()) == Set([])
        self.a.add_child(self.b)
        assert Set(self.a.get_children()) == Set([self.b])
        assert Set(self.b.get_parents()) == Set([self.a])
        assert Set(self.a.get_children()) == Set([self.b])
        assert Set(self.b.get_parents()) == Set([self.a])
        try:
            self.b.add_child(self.a)
            assert 0
        except ValueError: pass
        self.b.add_child(self.c)
        assert Set(self.c.get_children()) == Set([])
        assert Set(self.c.get_parents()) == Set([self.b])
        # Now we have a -> b -> c.
        assert Set(self.a.get_descendants()) == Set([self.b, self.c])
        assert Set(self.b.get_descendants()) == Set([self.c])
        assert Set(self.c.get_descendants()) == Set([])
        assert self.c.is_descendant_of(self.a)
        assert self.c.is_descendant_of(self.b)
        assert self.c.is_descendant_of(self.c)
        assert self.c.is_ancestor_of(self.c)
        assert not self.d.is_ancestor_of(self.c)
        self.a.set_children([self.b, self.c])
        # a -> b, a-> c, b-> c, d-> b, d-> c
        assert self.c.is_descendant_of(self.a)
        assert self.c.is_descendant_of(self.b)
        assert self.c.is_descendant_of(self.c)
        try:
            self.a.set_parents([self.b, self.c])
            assert 0
        except ValueError: pass
        assert self.c.is_descendant_of(self.a)
        assert self.c.is_descendant_of(self.b)
        assert self.c.is_descendant_of(self.c)
        self.a.set_parents([self.d])
        assert self.c.is_descendant_of(self.d)
        self.c.set_parents(list(self.c.get_parents()))
        assert Set(list(self.a.expand())) == Set([self.a, self.b, self.c])
        assert Set(list(self.b.expand())) == Set([self.b, self.c])
        assert Set(list(self.c.expand())) == Set([self.c])
        assert Set(self.d.expand()) == Set([self.a, self.b, self.c, self.d])
        self.a.remove_child(self.c)
        assert self.a.is_ancestor_of(self.c)
        self.b.remove_child(self.c)
        assert not self.a.is_ancestor_of(self.c)
        assert Set(list(self.b.expand())) == Set([self.b])
        assert Set(list(self.a.expand())) == Set([self.a, self.b])
        self.a.get_leaves()
        assert remove_ancestors([self.b, self.a]) == [self.b]
        assert remove_ancestors([]) == []
        assert remove_ancestors([self.a, self.b, self.c]) == [self.b, self.c]
        assert remove_ancestors([self.a, self.c]) == [self.a, self.c]

if __name__ == "__main__":
    CategoryTest()

