#!/www/python/bin/python
"""$URL: svn+ssh://svn/repos/trunk/dulcinea/lib/test/utest_item.py $
$Id: utest_item.py 25057 2004-09-09 18:38:39Z dbinger $
"""

from sets import Set
from datetime import datetime
from sancho.utest import UTest
from dulcinea.item import Item, ItemFolder

class MyItem(Item):
    approved = True


class TestItem (UTest):
    def _pre(self):
        self.item1 = MyItem(timestamp=datetime(2000,1,1))
        self.item1a = MyItem(timestamp=datetime(2000,1,1))
        self.item2 = MyItem(timestamp=datetime(2002,1,1))
        self.item3 = MyItem(timestamp=datetime(2003,1,1))
        self.folder = ItemFolder()

    def check_basic(self):
        self.folder.add_item(self.item1)
        self.folder.add_item(self.item2)
        assert Set(self.folder.get_items()) == Set([self.item1, self.item2])
        assert self.folder.get_recent_items(2) == [self.item2, self.item1]
        self.folder.add_item(self.item3)
        assert self.folder.get_recent_items(2) == [self.item3, self.item2]
        assert self.folder.get_recent_items(3) == [
            self.item3, self.item2, self.item1]
        assert self.folder.get_recent_items(None) == [
            self.item3, self.item2, self.item1]
        self.folder.add_item(self.item1a)
        assert self.folder.get_recent_items(2) == [
            self.item3, self.item2]

    def check_delete(self):
        self.folder.add_item(self.item1)
        self.folder.add_item(self.item1a)
        item = MyItem() # auto timestamp
        self.folder.add_item(item)
        assert Set(self.folder.get_items()) == Set(
            [self.item1, self.item1a, item])
        self.folder.delete_item(self.item1a)
        assert Set(self.folder.get_items()) == Set([self.item1, item])
 

if __name__ == "__main__":
    TestItem()
