"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/utest_item.py $
$Id: utest_item.py 29832 2007-05-29 19:19:08Z dbinger $
"""
from datetime import datetime
from dulcinea.item import Item, ItemFolder
from qp.lib.tz import Eastern
from sancho.utest import UTest

class MyItem(Item):
    pass

class TestItem (UTest):
    def _pre(self):
        self.item1 = MyItem()
        self.item1.timestamp = datetime(2003,1,1,tzinfo=Eastern)
        self.item1.set_approved(True)
        self.item1a = MyItem()
        self.item1a.timestamp = datetime(2003,1,1,tzinfo=Eastern)
        self.item2 = MyItem()
        self.item2.timestamp = datetime(2005,1,1,tzinfo=Eastern)
        self.item2.set_approved(True)
        self.item3 = MyItem()
        self.item3.timestamp = datetime(2006,1,1,tzinfo=Eastern)
        self.item3.set_approved(True)
        self.folder = ItemFolder()

    def check_basic(self):
        self.folder.add(self.item1)
        self.folder.add(self.item2)
        assert set(self.folder.get_items()) == set([self.item1, self.item2])
        assert self.folder.get_recent_items(count=2) == [self.item2, self.item1]
        self.folder.add(self.item3)
        assert self.folder.get_recent_items(count=2) == [self.item3, self.item2]
        assert self.folder.get_recent_items(count=3) == [
            self.item3, self.item2, self.item1]
        assert self.folder.get_recent_items() == [
            self.item3, self.item2, self.item1]
        self.folder.add(self.item1a)
        assert self.folder.get_recent_items(count=2) == [
            self.item3, self.item2]
        self.item3.absorb(self.item2)
        assert self.item3.timestamp == self.item2.timestamp

    def check_delete(self):
        self.folder.add(self.item1)
        self.folder.add(self.item1a)
        item = MyItem() # auto timestamp
        self.folder.add(item)
        assert set(self.folder.get_items()) == set(
            [self.item1, self.item1a, item])
        self.folder.delete(self.item1a)
        assert set(self.folder.get_items()) == set([self.item1, item])

if __name__ == "__main__":
    TestItem()
