예제 #1
0
class TestRecManip(unittest.TestCase):

    def setUp(self):
        self.path = os.path.abspath(os.path.dirname(__file__))
        self.rmanip = RecManip()
        self.rmanip.setUp(test=True)
        for rdate, recs in TEST_SAMPS.items():
            for rec in recs:
                self.rmanip.addItem(rdate, rec)

    def test_clear(self):
        res = self.rmanip.getAll()
        self.assertNotEqual(len(res), 0)
        
        self.rmanip.clear()
        res = self.rmanip.getAll()
        self.assertEqual(len(res), 0)

    def test_addItem(self):
        rdate = date(2011, 12, 28)
        brec = BaseRecord(28, (u'Food & Drinks', u'Meal'), u'Cash', u'CHY')
        self.rmanip.addItem(rdate, brec)
        
        res = self.rmanip.findDate(rdate)
        self.assertEqual(len(res[rdate]), 1)
        self.assertEqual(
            sum(rec.amount for rec in res[rdate]),
            28.0
        )

    def test_delItem(self):
        rdate = date(2012, 1, 5)
        res = self.rmanip.findDate(rdate)
        # print 'length: ', len(res)
        self.assertEqual(len(res[rdate]), 3)

        brec = BaseRecord(11, (u'Food & Drinks', u'Snacks'), u'Cash', u'CHY')
        self.rmanip.delItem(rdate, brec)
        res = self.rmanip.findDate(rdate)
        self.assertEqual(len(res[rdate]), 2)

    def test_updateItem(self):
        rdate = date(2012, 1, 4)
        res = self.rmanip.findDate(rdate)
        upd_rec = res[rdate][0]
        self.assertEqual(upd_rec.amount, 22.0)

        new_rec = BaseRecord(33, (u'Food & Drinks', u'Meal'), u'Cash', u'CHY')
        self.rmanip.updateItem(rdate, upd_rec, new_rec)
        res = self.rmanip.findDate(rdate)
        self.assertEqual(res[rdate][0].amount, 33.0)

    def test_getAll(self):
        res = self.rmanip.getAll()
        self.assertEqual(len(res), 11)
        self.assertEqual(
            sum(rec.amount for rdate in res
                               for rec in res[rdate]),
            2320.0
        )

    def test_getInfo(self):
        # TODO: need to complete the implementation of RecManip.getInfo() priorly.
        pass

    def test_date_range(self):
        bdate = date(2012, 1, 1)
        edate = date(2012, 1, 7)
        res = self.rmanip.date_range(bdate, edate)
        self.assertEqual(len(res), 5)
        self.assertIsNone(res.get(edate, None))
        self.assertEqual(
            sum(rec.amount for rdate in res
                               for rec in res[rdate]),
            294.0
        )

    def test_findDate(self):
        rdate = date(2012, 1, 13)
        res = self.rmanip.findDate(rdate)
        self.assertEqual(len(res[rdate]), 2)
        self.assertEqual(
            sum(rec.amount for rec in res[rdate]),
            1818.0
        )

    def test_findDates(self):
        rdate = date(2012, 1, 8)
        res = self.rmanip.findDates(rdate, 5)
        self.assertEqual(len(res), 4)
        self.assertIsNone(
            res.get(date(2012, 1, 13), None)
        )
        self.assertEqual(
            sum(rec.amount for keydate in res
                               for rec in res[keydate]),
            175.0
        )
    
    def test_import_export_Record(self):
        fpath = os.path.join(self.path, 'temp_rec.csv')
        self.rmanip.exportRecord(fpath)
        new_rmanip = RecManip()
        new_rmanip.setUp(test=True)
        new_rmanip.importRecord(fpath)

        ores = self.rmanip.getAll()
        nres = new_rmanip.getAll()
        
        self.assertEqual(len(ores), len(nres))
        self.assertEqual(
            sum(rec.amount for keydate in ores
                               for rec in ores[keydate]),
            sum(rec.amount for keydate in nres
                               for rec in nres[keydate])
        )
예제 #2
0
class TestRecParser(unittest.TestCase):

    def setUp(self):
        self.rmanip = RecManip()
        self.rmanip.setUp(test=True)
        for rdate, recs in TEST_SAMPS.items():
            for rec in recs:
                self.rmanip.addItem(rdate, rec)
        self.dataflow = self.rmanip.getAll()

    # analy, stst, filter
    def test_Amount_Stat(self):
        parser = RP.MainParser(self.dataflow)
        parser.append(RP.Amount_Stat())
        dummy1, statres, dummy2 = parser.parse()
        self.assertEqual(statres[0][1], 2320.0)

    def test_Count_Stat(self):
        parser = RP.MainParser(self.dataflow)
        parser.append(RP.Count_Stat())
        dummy1, statres, dummy2 = parser.parse()
        self.assertEqual(statres[0][1], 21)

    def test_Type_Filter(self):
        parser = RP.MainParser(self.dataflow)
        parser.append(RP.Type_Filter((u'Food & Drinks', u'Meal'), 'type'))
        dummy1, dummy2, fseq = parser.parse()
        self.assertEqual(
            sum(rec.amount for rdate in fseq
                               for rec in fseq[rdate]),
            227.0
        )

        parser = RP.MainParser(self.dataflow)
        parser.append(RP.Type_Filter(u'Digital devices', 'type'))
        dummy1, dummy2, fseq = parser.parse()
        self.assertEqual(
            sum(rec.amount for rdate in fseq
                               for rec in fseq[rdate]),
            1798.0
        )

        parser = RP.MainParser(self.dataflow)
        parser.append(RP.Type_Filter(u'Credit card', 'payment'))
        dummy1, dummy2, fseq = parser.parse()
        self.assertEqual(
            sum(rec.amount for rdate in fseq
                               for rec in fseq[rdate]),
            1969.0
        )

        parser = RP.MainParser(self.dataflow)
        parser.append(RP.Type_Filter(u'Like', 'tag'))
        dummy1, dummy2, fseq = parser.parse()
        self.assertEqual(
            sum(rec.amount for rdate in fseq
                               for rec in fseq[rdate]),
            154.0
        )

    def test_Money_Filter(self):
        # TODO: modify Money_Filter range that is [start, stop)
        parser = RP.MainParser(self.dataflow)
        parser.append(RP.Money_Filter(20))
        dummy1, dummy2, fseq = parser.parse()
        self.assertEqual(
            sum(rec.amount for rdate in fseq
                               for rec in fseq[rdate]),
            161.0
        )
        
        parser = RP.MainParser(self.dataflow)
        parser.append(RP.Money_Filter(30, 100))
        dummy1, dummy2, fseq = parser.parse()
        self.assertEqual(
            sum(rec.amount for rdate in fseq
                               for rec in fseq[rdate]),
            134.0
        )

    def test_DaysInWeek_Filter(self):
        parser = RP.MainParser(self.dataflow)
        parser.append(RP.DaysInWeek_Filter([5, 6]))
        dummy1, dummy2, fseq = parser.parse()
        self.assertEqual(
            sum(rec.amount for rdate in fseq
                               for rec in fseq[rdate]),
            131.0
        )

    def test_DaysInMonth_Filter(self):
        parser = RP.MainParser(self.dataflow)
        parser.append(RP.DaysInMonth_Filter([2, 4, 6, 8, 10]))
        dummy1, dummy2, fseq = parser.parse()
        self.assertEqual(
            sum(rec.amount for rdate in fseq
                               for rec in fseq[rdate]),
            164.0
        )

    def test_MthsInYear_Filter(self):
        parser = RP.MainParser(self.dataflow)
        parser.append(RP.MthsInYear_Filter([1]))
        dummy1, dummy2, fseq = parser.parse()
        self.assertEqual(
            sum(rec.amount for rdate in fseq
                               for rec in fseq[rdate]),
            2320.0
        )

        parser = RP.MainParser(self.dataflow)
        parser.append(RP.MthsInYear_Filter([2]))
        dummy1, dummy2, fseq = parser.parse()
        self.assertEqual(
            sum(rec.amount for rdate in fseq
                               for rec in fseq[rdate]),
            0.0
        )

    def test_General_Analys(self):
        parser = RP.MainParser(self.dataflow)
        parser.append(RP.General_Analys())
        analyres, dummy1, dummy2 = parser.parse()

        rdict = {}
        for key, amount, percent in analyres[0][1]:
            rdict[key] = amount
        self.assertEqual(
            rdict[u'Food & Drinks'], 322.0
        )
        self.assertEqual(
            rdict[u'Learning & Education'], 121.0
        )
        self.assertEqual(
            rdict[u'Digital devices'], 1798.0
        )
        self.assertEqual(
            rdict[u'Transport costs'], 16.0
        )
        self.assertEqual(
            rdict[u'Health care'], 13.0
        )
        self.assertEqual(
            rdict[u'Recreation'], 50.0
        )