"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/qp/lib/test/utest_keep.py $
$Id: utest_keep.py 29487 2007-02-23 22:46:20Z dbinger $
"""
from durus.btree import BTree
from durus.persistent import Persistent
from qp.lib.keep import Keyed, Keep, Stamped, stamp_sorted, reverse_stamp_sorted
from sancho.utest import UTest, raises

class Test(UTest):

    def check_keep(self):
        class PKeep(Keep, Persistent):
            pass
        class PKeyed(Keyed, Persistent):
            pass            
        x = PKeep(value_spec=Keyed)
        y = PKeyed()
        z = PKeyed()
        x.add(y)
        x.add(z)
        assert list(x.iterkeys()) == [y.get_key(), z.get_key()]
        assert list(x.itervalues()) == [y, z]
        assert list(x.iteritems()) == zip(x.iterkeys(), x.itervalues())
        assert isinstance(x.get_mapping(), BTree)
        assert x.get(y.get_key()) is y
        assert x.get(None, 23) == 23
        raises(TypeError, x.add, None)
        bad = PKeyed()
        bad.badattr = 1
        raises(TypeError, x.add, bad)

    def check_stamped(self):
        class PStamped(Persistent, Stamped):
            pass
        x = PStamped()
        y = PStamped()
        assert stamp_sorted([x, y]) == [x, y]
        assert reverse_stamp_sorted([x, y]) == [y, x]
        assert x.get_stamp() < y.get_stamp()


if __name__ == '__main__':
    Test()
