def test_01_save(self): db = Database() acc1 = Account("Root") db += acc1 acc2 = Account("Foo") acc1 += acc2 self.assertEqual(2, db.nofAccounts()) account_names = set( [a.fullname for a in db.getChildAccounts(recurse=False)]) self.assertSetEqual({"Root"}, account_names) account_names = set( [a.fullname for a in db.getChildAccounts(recurse=True)]) self.assertSetEqual({"Root", "Root/Foo"}, account_names) db.save(self.temp_path) time.sleep(1) acc3 = Account("Bar") acc1 += acc3 self.assertEqual(3, db.nofAccounts()) account_names = set( [a.fullname for a in db.getChildAccounts(recurse=False)]) self.assertSetEqual({"Root"}, account_names) account_names = set( [a.fullname for a in db.getChildAccounts(recurse=True)]) self.assertSetEqual({"Root", "Root/Foo", "Root/Bar"}, account_names) db.save(self.temp_path) time.sleep(1) acc4 = Account("Hello World") acc3 += acc4 self.assertEqual(4, db.nofAccounts()) account_names = set( [a.fullname for a in db.getChildAccounts(recurse=False)]) self.assertSetEqual({"Root"}, account_names) account_names = set( [a.fullname for a in db.getChildAccounts(recurse=True)]) self.assertSetEqual( {"Root", "Root/Foo", "Root/Bar", "Root/Bar/Hello World"}, account_names) db.save(self.temp_path) db2 = Database.load(self.temp_path) self.assertEqual(4, db2.nofAccounts()) account_names = set( [a.fullname for a in db2.getChildAccounts(recurse=False)]) self.assertSetEqual({"Root"}, account_names) account_names = set( [a.fullname for a in db2.getChildAccounts(recurse=True)]) self.assertSetEqual( {"Root", "Root/Foo", "Root/Bar", "Root/Bar/Hello World"}, account_names)
def test_balanced(self): db = Database() asset = Account("Asset") db += asset debit = Account("Debit") db += asset t = Transaction(datetime.date.today()) db += t i = Item("Hello World", 2) t += i i += asset self.assertEqual(Decimal("2.00"), t.getBalance()) self.assertFalse(t.isBalanced()) i = Item("Foo Bar", -1) t += i i += debit self.assertEqual(Decimal("1.00"), t.getBalance()) self.assertFalse(t.isBalanced()) i.setValue(-2) self.assertEqual(Decimal("0"), t.getBalance()) self.assertTrue(t.isBalanced())
def test_01_xml(self): db = Database() acc1 = Account("Root") db += acc1 acc2 = Account("Foo") acc1 += acc2 acc3 = Account("Bar") acc1 += acc3 t = Transaction(datetime.date.today(), "bar") db += t i = Item("AAA", -2) t += i i += acc1 i = Item("BBB", 2) t += i i += acc2 t = Transaction(datetime.date.today() + datetime.timedelta(days=1), "foo") db += t i = Item("CCC", 2.99) t += i i += acc1 i = Item("DDD", -2.99) i += acc3 t += i t = Transaction(datetime.date.today() - datetime.timedelta(days=3), "Flix") db += t i = Item(u"EEE <>/\\!'\"§$%&/(){}", 4) t += i i += acc1 i = Item(u"FFF öäüÖÄÜß", -4) t += i i += acc3 # remove the date/time depending string datePatchRe = re.compile("^\s*<saved\s+datetime=\".+?\"\s*/>\s*$", re.MULTILINE) xml = etree.tostring(db.toXml(), encoding="utf-8", xml_declaration=True, pretty_print=True).decode("utf-8") print(xml) xml = datePatchRe.sub("", xml) print(xml) db2 = Database.parseFromXml( etree.parse(BytesIO(xml.encode("utf-8"))).getroot()) xml2 = etree.tostring(db2.toXml(), encoding="utf-8", xml_declaration=True, pretty_print=True).decode("utf-8") print(xml2) xml2 = datePatchRe.sub("", xml2) self.assertEqual(xml, xml2, "xml differs")
def test_02_sub(self): db = Database() acc = Account("Root") db += acc sacc = Account("Sub") acc += sacc self.assertIn("Root/Sub", db) self.assertIsNotNone(db["Root/Sub"]) self.assertNotIn("Root/Foo", db) with self.assertRaises(KeyError): db["Root/Foo"]
def testFilterByAccount(self): i = Item() a1 = Account("Foo") i += a1 f = FilterAccountsAndChildren(a1) self.assertTrue(f.accepted(i)) a2 = Account("Bar") f = FilterAccountsAndChildren(a2, a1) self.assertTrue(f.accepted(i)) a3 = Account("Hello") f = FilterAccountsAndChildren(a2, a3) self.assertFalse(f.accepted(i))
def setUp(self): self._db = Database() accRoot = Account("Root") self._db += accRoot accFoo = Account("Foo") accRoot += accFoo accBar = Account("Bar") accRoot += accBar accTest = Account("Test") accRoot += accTest t = self._newTransaction(self.dates[0], "one") self._newItem("AAA", 2.5, t, accFoo) self._newItem("BBB", -2.5, t, accBar) t = self._newTransaction(self.dates[1], "two") self._newItem("CCC", -2, t, accRoot) self._newItem("DDD", 2, t, accTest) t = self._newTransaction(self.dates[2], "three") self._newItem(u"EEE", 4, t, accFoo) self._newItem(u"FFF", -4, t, accBar) t = self._newTransaction(self.dates[2], "four") self._newItem(u"GGG", 3, t, accFoo) self._newItem(u"HHH", -3, t, accBar) t = self._newTransaction(self.dates[3], "five") self._newItem(u"III", 5, t, accFoo) self._newItem(u"JJJ", -5, t, accTest) t = self._newTransaction(self.dates[4], "six") self._newItem(u"KKK", 5, t, accFoo) self._newItem(u"LLL", -5, t, accBar) t = self._newTransaction(self.dates[4], "seven") self._newItem(u"MMM", 7, t, accFoo) self._newItem(u"NNN", -7, t, accBar) t = self._newTransaction(self.dates[5], "eight") self._newItem(u"OOO", 1, t, accFoo) self._newItem(u"PPP", -1, t, accBar) t = self._newTransaction(self.dates[6], "nine") self._newItem(u"QQQ", 1, t, accTest) self._newItem(u"RRR", -1, t, accBar) t = self._newTransaction(self.dates[7], "ten") self._newItem(u"SSS", 1, t, accFoo) self._newItem(u"TTT", -1, t, accBar)
def test_01_root(self): db = Database() acc = Account("Root") db += acc self.assertIn("Root", db) self.assertIsNotNone(db["Root"]) self.assertNotIn("Foo", db) with self.assertRaises(KeyError): db["Foo"]
def test_transactions(self): today = datetime.date.today() db = Database() acc1 = Account("A") db += acc1 acc2 = Account("B") db += acc2 t0 = Transaction(today, "B") db += t0 i0 = Item("AA", 1) t0 += i0 i0 += acc1 i1 = Item("BB", -1) t0 += i1 i1 += acc2 t1 = Transaction(today + datetime.timedelta(days=1), "C") db += t1 i2 = Item("CC", -2) t1 += i2 i2 += acc1 i3 = Item("DD", -2) t1 += i3 i3 += acc2 t2 = Transaction(today - datetime.timedelta(days=5), "A") db += t2 i4 = Item("EE", -3) t2 += i4 i4 += acc1 i5 = Item("FF", -3) t2 += i5 i5 += acc2 expected_transactions = [t2, t0, t1] current_transactions = list(db.filterTransactions()) self.assertListEqual(expected_transactions, current_transactions) t1.setDate(today - datetime.timedelta(days=1)) expected_transactions = [t2, t1, t0] current_transactions = list(db.filterTransactions()) self.assertListEqual(expected_transactions, current_transactions)
def insertRow(self, row, parentIdx): "Return True when new account has been added to parent index." if parentIdx.isValid(): parentAccount = parentIdx.internalPointer() else: parentAccount = self._rootAccount parentRows = len(parentAccount.getChildAccounts()) self.beginInsertRows(parentIdx, parentRows, parentRows) newAccount = Account("untitled%d" % parentRows) parentAccount += newAccount self.endInsertRows() self.setDirty(True) return True
def test_item(self): db = Database() acc = Account("Root") db += acc t = Transaction(datetime.date.today()) db += t i = Item("Hello World", 2.99) t += i i += acc i = Item("Foo Bar", 1) t += i i += acc
def menuNewRandomDatabase(self): "Create a new and random filled database." LOGGER.info("Creating new random database...") self._clearViews() self._db = Database() self._dbPath = "unnamed" self._db._parsing = True for i in range(32): acc = Account("Account%d" % i) self._db += acc for ii in range(2): cacc = Account("Account%d" % ii) acc += cacc for iii in range(2): ccacc = Account("Account%d" % iii) cacc += ccacc accs = self._db.getChildAccounts(True) dt = datetime.datetime.now() - datetime.timedelta(seconds=60 * 60 * 500) for i in range(3000): t = Transaction(dt, "#" * random.randint(1, 32)) self._db += t v = float(random.randint(1, 1000)) / 100 i = Item("A" * random.randint(1, 32), v) t += i i += random.choice(accs) i = Item("B" * random.randint(1, 32), -v) t += i i += random.choice(accs) dt += datetime.timedelta(seconds=60 * 60) self._db._parsing = False self._accountTree.setDatabase(self._db) self._accountTree.expandAll() self.statusBar().showMessage("Created random database", 2000)
def test_03_items(self): db = Database() acc1 = Account("A") db += acc1 acc2 = Account("B") db += acc2 t0 = Transaction(datetime.date.today()) db += t0 i0 = Item("AA", 1) t0 += i0 i0 += acc1 i1 = Item("BB", -1) t0 += i1 i1 += acc2 t1 = Transaction(datetime.date.today() + datetime.timedelta(days=1)) db += t1 i2 = Item("CC", -2) t1 += i2 i2 += acc1 i3 = Item("DD", -2) t1 += i3 i3 += acc2 expected_acc_items = [i0, i2] current_acc_items = list(acc1.filterItems()) self.assertListEqual(expected_acc_items, current_acc_items) expected_acc_items = [i1, i3] current_acc_items = list(acc2.filterItems()) self.assertListEqual(expected_acc_items, current_acc_items) t1.setDate(datetime.date.today() - datetime.timedelta(days=1)) expected_acc_items = [i2, i0] current_acc_items = list(acc1.filterItems()) self.assertListEqual(expected_acc_items, current_acc_items) expected_acc_items = [i3, i1] current_acc_items = list(acc2.filterItems()) self.assertListEqual(expected_acc_items, current_acc_items)
def test_transaction(self): db = Database() acc = Account("Root") db += acc t = Transaction(datetime.date.today()) db += t