"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/utest_partition.py $
$Id: utest_partition.py 30486 2008-01-21 22:28:33Z rmasse $
"""
from dulcinea.partition import PartKeyed, Partition
from dulcinea.base import DulcineaPersistent
from sancho.utest import UTest, raises

class Test (UTest):

    def check_basics(self):
        class B (DulcineaPersistent, PartKeyed):
            junk_is = str
            def __init__(self):
                PartKeyed.__init__(self)
                self.junk = ''
        partition = Partition()
        a = B()
        assert partition.__nonzero__() is False
        raises(AssertionError, partition.get_max_item)
        assert a.get_key() == None
        assert a.get_part_key() == None
        partition.add(a, 'current')
        raises(AssertionError, partition.add, a, 'current')
        b = B()
        b.junk = 1
        raises(TypeError, partition.add, b, 'current')
        raises(TypeError, partition.add, 1, 'current')
        assert a.get_key() == 1
        assert a.get_part_key() == 'current'
        assert list(partition.get_parts()['current'].items()) == [(1, a)]
        assert partition.get(1) == a
        assert partition.__nonzero__() is True
        assert partition.get_max_item() == (1, a)

    def check_part(self):
        class B (DulcineaPersistent, PartKeyed):
            pass
        partition = Partition(part_key_spec=('open', 'closed'))
        a = B()
        b = B()
        c = B()
        partition.add(a, 'open')
        partition.add(b, 'closed')
        raises(TypeError, partition.add, c, 'bad')
        assert list(partition.get_part('open').items()) == [(1, a)]
        assert list(partition.get_part('closed').items()) == [(2, b)]
        assert partition.get_max_item() == (2, b)
        assert partition.get(1) == a
        assert partition.get(2) == b
        assert partition.get(3) == None
        assert partition.get_part('open') is partition.get_parts()['open']

    def check_rm(self):
        class B (DulcineaPersistent, PartKeyed):
            pass
        partition = Partition(part_key_spec=('open', 'closed'))
        a = B()
        b = B()
        partition.add(a, 'open')
        partition.add(b, 'closed')
        assert partition.get(1) == a
        partition.remove(a)
        assert partition.get(1) == None
        raises(KeyError, partition.remove, a)
        assert partition.get(2) == b
        partition.discard(b)
        assert partition.get(2) == None
        partition.discard(b)
        assert partition.get(2) == None

    def check_move(self):
        class B (DulcineaPersistent, PartKeyed):
            pass
        partition = Partition(part_key_spec=('open', 'closed'))
        a = B()
        b = B()
        c = B()
        partition.add(a, 'open')
        assert partition.get(1) == a
        assert a.get_part_key() == 'open'
        assert a.get_key() in partition.get_parts()['open']
        assert 'closed' not in partition.get_parts()
        partition.move(a, 'closed')
        assert 'closed' in partition.get_parts()
        assert 'open' not in partition.get_parts()
        partition.add(c, 'open')
        assert partition.get(1) == a
        assert a.get_part_key() == 'closed'
        assert c.get_key() in partition.get_parts()['open']
        assert a.get_key() not in partition.get_parts()['open']
        assert a.get_key() in partition.get_parts()['closed']
        raises(TypeError, partition.move, a, 'bad')

    def check_iter(self):
        class B (DulcineaPersistent, PartKeyed):
            pass
        partition = Partition(part_key_spec=('open', 'closed'))
        a = B()
        b = B()
        c = B()
        partition.add(a, 'open')
        partition.add(b, 'open')
        partition.add(c, 'closed')
        assert list(partition.itervalues()) == [c, a, b]
        assert list(partition.iterkeys()) == [3, 1, 2]
        assert list(partition.iteritems()) == [(3, c), (1, a), (2, b)]

if __name__ == "__main__":
    Test()
