"""
open/dulcinea/lib/test/utest_item.py
"""
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):
        assert self.item1.is_new()
        self.folder.add(self.item1, 'active')
        assert not self.item1.is_new()
        self.folder.add(self.item2, 'active')
        assert set(self.folder.get_active()) == set([self.item1, self.item2])
        assert self.folder.get_recent_items(count=2) == [self.item2, self.item1]
        self.folder.add(self.item3, 'active')
        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, 'active')
        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_partition(self):
        assert not self.folder.get_part('active')
        assert not self.folder.get_part('expired')
        assert self.item1.is_new()
        self.folder.add(self.item1, 'active')
        assert self.folder.get_active() == [self.item1]
        self.folder.add(self.item2, 'active')
        assert self.folder.get_active() == [self.item1, self.item2]
        assert self.folder.get_expired() == []
        self.folder.move(self.item1, 'expired')
        assert self.folder.get_active() == [self.item2]
        assert self.folder.get_expired() == [self.item1]
        assert self.folder.get(self.item1.key) is self.item1
        assert self.folder.get(self.item2.key) is self.item2
        self.folder.remove(self.item1)
        assert self.folder.get_expired() == []
        assert self.folder.get_active() == [self.item2]

    def check_delete(self):
        self.folder.add(self.item1, 'active')
        self.folder.add(self.item1a, 'active')
        item = MyItem() # auto timestamp
        self.folder.add(item, 'active')
        assert set(self.folder.get_active()) == set(
            [self.item1, self.item1a, item])
        self.folder.remove(self.item1a)
        assert set(self.folder.get_active()) == set([self.item1, item])

if __name__ == "__main__":
    TestItem()
