Skip to content

Commit

Permalink
testing done for Pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
epogrebnyak committed Jan 21, 2024
1 parent ad6716c commit 425777f
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 60 deletions.
87 changes: 75 additions & 12 deletions core/test_uncore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
Regular,
Side,
T,
Pipeline,
Move,
credit,
debit,
double_entry,
close,
)


Expand Down Expand Up @@ -42,7 +45,7 @@ def test_chart_dict_contra_pairs():


def test_chart_contra_pairs():
assert Chart().add(
assert Chart("isa", "re").add(
T.Income, "sales", contra_names=["refunds", "voids"]
).dict.contra_pairs(T.Income) == [
("sales", "refunds"),
Expand All @@ -52,12 +55,12 @@ def test_chart_contra_pairs():

def test_chart_creation():
chart0 = (
Chart()
Chart("income_summary_account", "retained_earnings")
.add(T.Income, "sales", contra_names=["refunds", "voids"])
.add(T.Asset, "cash")
.add(T.Capital, "equity", contra_names=["buyback"])
)
assert chart0.dict == ChartDict(
assert chart0.dict == ChartDict(
{
"cash": T.Asset,
"sales": T.Income,
Expand All @@ -72,7 +75,7 @@ def test_chart_creation():


def test_journal_creation():
assert Journal.from_chart(
assert Journal.new(
ChartDict(cash=T.Asset, contra_cash=Reference("cash")), "isa", "re"
) == Journal(
{
Expand All @@ -85,19 +88,24 @@ def test_journal_creation():


@pytest.fixture
def journal():
chart = (
Chart()
def chart():
return (
Chart(
retained_earnings_account="retained_earnings",
income_summary_account="income_summary_account",
)
.add_many(T.Asset, "cash", "ar")
.add(T.Capital, "equity", contra_names=["buyback"])
.add(T.Income, "sales", contra_names=["refunds", "voids"])
.add(T.Liability, "vat")
.add(T.Expense, "salary")
)
ledger = Journal.from_chart(
chart.dict, "income_summary_account", "retained_earnings"
)
ledger.post_many(


@pytest.fixture
def journal(chart):
j = Journal.from_chart(chart)
j.post_many(
[
double_entry("cash", "equity", 1200),
double_entry("buyback", "cash", 200),
Expand All @@ -112,7 +120,12 @@ def journal():
[debit("salary", 18), credit("cash", 18)],
]
)
return ledger
return j


def test_journal_has_retained_earnings(chart):
j = Journal.from_chart(chart)
assert j[chart.retained_earnings_account]


def test_balances(journal):
Expand Down Expand Up @@ -149,3 +162,53 @@ def test_tuples(journal):

def test_subset(journal):
assert journal.subset(T.Asset).balances == {"cash": 1012, "ar": 5}


@pytest.fixture
def pipeline(chart, journal):
return Pipeline(chart, journal)


def test_moves_income(pipeline):
assert pipeline.close_contra([T.Income]).moves == [
Move(frm="refunds", to="sales"),
Move(frm="voids", to="sales"),
]


def test_moves_capital(pipeline):
assert pipeline.close_contra([T.Capital]).moves == [
Move(frm="buyback", to="equity")
]


def test_moves_close_temp(pipeline):
assert pipeline.close_contra(
[T.Income, T.Expense]
).flush().close_temporary().moves == [
Move(frm="sales", to="income_summary_account"),
Move(frm="salary", to="income_summary_account"),
]


def test_eq(pipeline, chart):
p = pipeline.close_contra([T.Income, T.Expense]).close_temporary()
a = p.flush().close_isa().closing_entries
b = [
double_entry(
chart.income_summary_account,
chart.retained_earnings_account,
12,
)
]
assert a == b


def test_ledger_does_not_change_after_pipeline(chart, journal):
assert journal.balances["retained_earnings"] == 0
assert close(chart, journal).balances["retained_earnings"] == 12
assert journal.tuples["retained_earnings"] == (0, 0)


def test_equity_nets_out(chart, journal):
assert close(chart, journal).balances["equity"] == 1000
64 changes: 16 additions & 48 deletions core/uncore.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ def account_type(self, name) -> Contra | Regular:

@dataclass
class Chart:
retained_earnings_account: str = "retained_earnings"
income_summary_account: str = "income_summary_account"
income_summary_account: str
retained_earnings_account: str
dict: ChartDict = field(default_factory=ChartDict)

def add_many(self, t, *names: str):
Expand Down Expand Up @@ -211,20 +211,30 @@ def balance(self):

class Journal(UserDict[str, Account]):
@classmethod
def from_chart(
def new(
cls,
chart_dict: ChartDict,
income_summary_account: str,
retained_earnings_account: str,
):

ledger = cls()
for key in chart_dict.keys():
ledger[key] = Account(chart_dict.account_type(key))
ledger[retained_earnings_account] = Account(Regular(T.Capital))
ledger[income_summary_account] = Account(Intermediate(Side.Credit))
return ledger

@classmethod
def from_chart(
cls,
chart: Chart,
):
return cls.new(
chart_dict=chart.dict,
income_summary_account=chart.income_summary_account,
retained_earnings_account=chart.retained_earnings_account,
)

def post(self, entry: Entry):
if not is_balanced(entry):
raise ValueError(entry)
Expand Down Expand Up @@ -353,10 +363,10 @@ def statements(chart, ledger):
.add(T.Liability, "vat")
.add(T.Expense, "salary")
)
ledger = Journal.from_chart(
journal = Journal.from_chart(
chart.dict, "income_summary_account", "retained_earnings"
)
ledger.post_many(
journal.post_many(
[
double_entry("cash", "equity", 1200),
double_entry("buyback", "cash", 200),
Expand All @@ -371,48 +381,6 @@ def statements(chart, ledger):
[debit("salary", 18), credit("cash", 18)],
]
)
assert ledger.balances == {
"cash": 1012,
"ar": 5,
"equity": 1200,
"buyback": 200,
"sales": 60,
"refunds": 10,
"voids": 20,
"vat": 5,
"salary": 18,
"retained_earnings": 0,
"income_summary_account": 0,
}
print(Pipeline(chart, ledger).close_contra([T.Income]).moves)
print(Pipeline(chart, ledger).close_contra([T.Capital]).moves)
print(
(
p := Pipeline(chart, ledger)
.close_contra([T.Income, T.Expense])
.flush()
.close_temporary()
).moves
)
print(a := p.flush().close_isa().closing_entries[0])
print(
b := double_entry(
chart.income_summary_account,
"retained_earnings",
12,
)
)
assert a[0] == b[0]
assert a[1] == b[1]

# ledger does not change
assert ledger.balances["retained_earnings"] == 0
assert close(chart, ledger).balances["retained_earnings"] == 12
assert ledger.tuples["retained_earnings"] == (0, 0)

# netted buyback 1200-200=1000
assert close(chart, ledger).balances["equity"] == 1000

# - Test closing
# - Statement and viewers classes next
# - Company with chart and entries from experimental.py are next

0 comments on commit 425777f

Please sign in to comment.