def setUpClass(self):
     self.mana = UsageManager(TMPFILE)
class TestUsageManager(unittest.TestCase):
    """
    Tests for the UsageManager
    """
    def setUpClass(self):
        self.mana = UsageManager(TMPFILE)
    
    def tearDownClass(self):
        self.mana.close()
        shutil.rmtree(TMPFILE)
    
    def test_append_item(self):
        """
        Test that appending a couple of usage items works
        """
        args = [True, Time.fromDatetime(datetime.datetime.now()),
                Time.fromDatetime(datetime.datetime.now() + datetime.timedelta(minutes=59)),
                112100, 121222]
        args2 = [False, Time.fromDatetime(datetime.datetime.now() - datetime.timedelta(minutes=43)),
                Time.fromDatetime(datetime.datetime.now() + datetime.timedelta(minutes=50)),
                11244443, 12243453]
        
        item1 = self.mana.add_usage_item(*args)
        item2 = self.mana.add_usage_item(*args2)
        
        self.assertEquals(self.mana.store.getItemByID(item1.storeID), item1)
        self.assertEquals(self.mana.store.getItemByID(item2.storeID), item2)
        
        item1.deleteFromStore()
        item2.deleteFromStore()
    
    def test_get_usage_for_day(self):
        """
        Append 2 3G sessions that occurred 21/11/07 and get usage for that day
        """
        # 16:20 PM  21 Nov 2007
        now = datetime.datetime(2007, 11, 21, 16, 20)
        
        args = [True, Time.fromDatetime(now), # from now to now + 60m (60min)
                Time.fromDatetime(now + datetime.timedelta(minutes=45)),
                11212100, 12321222]
        
        args2 = [True, # from now + 80m to now + 120m  (40min)
                 Time.fromDatetime(now + datetime.timedelta(minutes=80)),
                 Time.fromDatetime(now + datetime.timedelta(minutes=120)),
                 112128, 1232121]
        
        item1 = self.mana.add_usage_item(*args)
        item2 = self.mana.add_usage_item(*args2)
        
        resp = self.mana.get_usage_for_day(now.date())
        
        self.failUnlessIn(item1, resp)
        self.failUnlessIn(item2, resp)
    
    def test_get_usage_for_month(self):
        # 16:20 PM  21 Nov 2006
        now = datetime.datetime(2006, 11, 21, 16, 20)
        
        args = [True, # from now + 180m to now + 210m  (30min)
                Time.fromDatetime(now + datetime.timedelta(minutes=180)),
                Time.fromDatetime(now + datetime.timedelta(minutes=210)),
                112128, 1232121]
        
        item3 = self.mana.add_usage_item(*args)
        
        resp = list(self.mana.get_usage_for_month(now.date()))
        self.failUnless(len(resp) == 1)
        self.failUnlessIn(item3, resp)