Skip to content

Commit

Permalink
minor refactoring of debts; placeholder tests for multiple payments
Browse files Browse the repository at this point in the history
  • Loading branch information
countvajhula committed Jan 29, 2025
1 parent e2bdf2f commit 5634319
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 22 deletions.
19 changes: 12 additions & 7 deletions oldabe/money_in/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from .price import read_price
from .equity import write_attributions
from .valuation import read_valuation, write_valuation
from .debt import pay_outstanding_debts, create_debts, write_debts
from .debt import pay_outstanding_debts, create_debts, update_debts, write_debts
from .advances import draw_down_advances, advance_payments
from .equity import handle_investment

Expand Down Expand Up @@ -137,6 +137,10 @@ def distribute_payment(
return processed_debts, debt_payments, transactions, advances


# TODO: the payments within a commit are not ordered.
# It may be better to sort them chronologically, so that
# earlier payments are reflected in attributions before
# later payments are processed.
def process_payments(instruments, attributions):
"""
Process new payments by paying out instruments and then, from the amount
Expand Down Expand Up @@ -199,9 +203,11 @@ def process_payments(instruments, attributions):
valuation = handle_investment(
payment, new_itemized_payments, attributions, price, valuation
)

debts = update_debts(DebtsRepo(), new_debts, new_debt_payments)

return (
new_debts,
new_debt_payments,
debts,
new_transactions,
valuation,
new_itemized_payments,
Expand All @@ -221,8 +227,7 @@ def process_payments_and_record_updates():
assert_attributions_normalized(attributions)

(
new_debts,
debt_payments,
debts,
transactions,
posterior_valuation,
new_itemized_payments,
Expand All @@ -232,9 +237,9 @@ def process_payments_and_record_updates():
# we only write the changes to disk at the end
# so that if any errors are encountered, no
# changes are made.
write_debts(DebtsRepo(), new_debts, debt_payments)
TransactionsRepo().extend(transactions)
write_debts(debts)
write_attributions(attributions)
write_valuation(posterior_valuation)
TransactionsRepo().extend(transactions)
ItemizedPaymentsRepo().extend(new_itemized_payments)
AdvancesRepo().extend(advances)
26 changes: 11 additions & 15 deletions oldabe/money_in/debt.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ def pay_outstanding_debts(


def update_debts(existing_debts, new_debts, debt_payments):
"""
1. Build a hash of all the processed debts, generating an id for each
(based on email and payment file).
2. read the existing debts file, row by row.
3. if the debt in the row is in the "processed" hash, then write the
processed version instead of the input version and remove it from the
hash, otherwise write the input version.
"""
total_debt_payments = Tally(
(dp.debt.key(), dp.amount) for dp in debt_payments
)
Expand All @@ -87,21 +95,9 @@ def update_debts(existing_debts, new_debts, debt_payments):
]


def write_debts(existing_debts, new_debts, debt_payments):
"""
1. Build a hash of all the processed debts, generating an id for each
(based on email and payment file).
2. read the existing debts file, row by row.
3. if the debt in the row is in the "processed" hash, then write the
processed version instead of the input version and remove it from the
hash, otherwise write the input version.
4. write the debts that remain in the processed hash.
"""
replacement = update_debts(
existing_debts, new_debts, debt_payments
)

def write_debts(debts):
""" Write the debts that remain in the processed hash. """
with open(DEBTS_FILE, "w") as f:
writer = csv.writer(f)
for debt in replacement:
for debt in debts:
writer.writerow(astuple(debt))
61 changes: 61 additions & 0 deletions tests/integration/old_abe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,67 @@ def test_compiled_outstanding_balances(self, mock_git_rev, abe_fs):
) in message


class TestMultiplePayments:

# TODO: implement me
@time_machine.travel(datetime(1985, 10, 26, 1, 24), tick=False)
@patch('oldabe.models.default_commit_hash', return_value='abcd123')
def test_generates_transactions(self, mock_git_rev, abe_fs):
pass

# TODO: review me
@time_machine.travel(datetime(1985, 10, 26, 1, 24), tick=False)
@patch('oldabe.models.default_commit_hash', return_value='abcd123')
def test_dilutes_attributions(self, mock_git_rev, abe_fs):
with localcontext() as context:
context.prec = 2
amount = 10000
abe_fs.create_file(
"./abe/payments/1.txt",
contents=f"sam,036eaf6,{amount},1987-06-30 06:25:00",
)
abe_fs.create_file(
"./abe/payments/2.txt",
contents=f"sam,036eaf6,{amount},1987-06-30 06:24:00",
)
process_payments_and_record_updates()
with open('./abe/attributions.txt') as f:
assert f.read() == (
"sid,42\n" "jair,26\n" "ariana,17\n" "sam,16\n"
)

# TODO: why do sam and sri not get into attributions when the amount
# is 1000? For higher amounts like 10000 it seems OK
# TODO: add a test to check that, even though both sam and sri get
# attributed equally, sam gets paid from sri's payment,
# but sri doesn't get paid from sam's, assuming sam is first
@time_machine.travel(datetime(1985, 10, 26, 1, 24), tick=False)
@patch('oldabe.models.default_commit_hash', return_value='abcd123')
def test_equal_payment_results_in_equal_share(self, mock_git_rev, abe_fs):
with localcontext() as context:
context.prec = 2
amount = 10000
abe_fs.create_file(
"./abe/payments/1.txt",
contents=f"sam,036eaf6,{amount},1987-06-30 06:25:00",
)
abe_fs.create_file(
"./abe/payments/2.txt",
contents=f"sri,b36eaf6,{amount},1987-06-30 06:24:00",
)
process_payments_and_record_updates()
with open('./abe/attributions.txt') as f:
assert f.read() == (
"sid,42\n" "jair,26\n" "ariana,17\n" "sam,7.8\n" "sri,7.8\n"
)

# TODO: implement me
@time_machine.travel(datetime(1985, 10, 26, 1, 24), tick=False)
@patch('oldabe.models.default_commit_hash', return_value='abcd123')
def test_compiled_outstanding_balances(self, mock_git_rev, abe_fs):
pass


class TestUnpayableContributor:

def _call(self, abe_fs):
Expand Down

0 comments on commit 5634319

Please sign in to comment.