Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
import contextlib
|
|
import io
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
|
|
from ledger import cli
|
|
|
|
SINGLE_YEAR = """date,description,amount,category
|
|
2024-01-05,Opening deposit,5000.00,3000
|
|
2024-01-10,Consulting invoice,1200.00,4000
|
|
2024-02-03,Groceries,-150.25,5100
|
|
2024-02-20,Consulting invoice,900.00,4000
|
|
"""
|
|
|
|
TWO_YEARS = """date,description,amount,category
|
|
2023-01-09,Salary,2000.00,4000
|
|
2023-12-20,Salary,2100.00,4000
|
|
2024-01-08,Salary,2200.00,4000
|
|
2024-01-20,Rent,-700.00,5000
|
|
"""
|
|
|
|
|
|
class CliTests(unittest.TestCase):
|
|
def run_cli(self, content, *args):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = os.path.join(folder, "statement.csv")
|
|
with open(path, "w", encoding="utf-8") as handle:
|
|
handle.write(content)
|
|
out, err = io.StringIO(), io.StringIO()
|
|
with contextlib.redirect_stderr(err):
|
|
code = cli.main([args[0], path, *args[1:]], out=out)
|
|
return code, out.getvalue(), err.getvalue()
|
|
|
|
def test_balance(self):
|
|
code, out, _ = self.run_cli(SINGLE_YEAR, "balance")
|
|
self.assertEqual((code, out), (0, "6949.75\n"))
|
|
|
|
def test_balance_styles(self):
|
|
_, out, _ = self.run_cli(SINGLE_YEAR, "balance", "--style", "german")
|
|
self.assertEqual(out, "6.949,75\n")
|
|
|
|
def test_monthly_within_one_year(self):
|
|
code, out, _ = self.run_cli(SINGLE_YEAR, "monthly")
|
|
self.assertEqual((code, out), (0, "2024-01 6200.00\n2024-02 749.75\n"))
|
|
|
|
def test_monthly_across_years(self):
|
|
_, out, _ = self.run_cli(TWO_YEARS, "monthly")
|
|
self.assertEqual(out.splitlines(), ["2023-01 2000.00", "2023-12 2100.00", "2024-01 1500.00"])
|
|
|
|
def test_trial_and_export(self):
|
|
_, trial, _ = self.run_cli(SINGLE_YEAR, "trial")
|
|
self.assertIn("1000,Bank,6949.75,0.00", trial.splitlines())
|
|
_, export, _ = self.run_cli(SINGLE_YEAR, "export")
|
|
self.assertEqual(len(export.splitlines()), 1 + 2 * 4)
|
|
|
|
def test_errors_return_two(self):
|
|
code, _, err = self.run_cli("date,amount\n", "balance")
|
|
self.assertEqual(code, 2)
|
|
self.assertIn("missing columns", err)
|
|
code, _, err = self.run_cli(SINGLE_YEAR.replace("5100", "9999"), "balance")
|
|
self.assertEqual(code, 2)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|