Skip to content

Commit

Permalink
test and debug timestamp forward
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmurilo75 committed Apr 8, 2024
1 parent 7b53623 commit 01c5dfb
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 13 deletions.
4 changes: 4 additions & 0 deletions development_notebook/2024-04-05.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Transfer are being implemented as an extended and coupled transactions.
# Today

Reimplement timestamp changes
for some reason change timestamp forward is not working. using print was able to see that it is working properly up until chain update calling the after(), which includes the newly changed self, but still for some reason self is not updating balance. next step was to clean up print statements for better understanding
for debug purpose could test on admin to move forward and raise a break point to use wergberg debuge
Solution ::
Problem was that when getting all after on another transaction , another instance of the save pk would be passed and updated the balance on it, wwhich would then be save. However, this balance would be overriden by following calls to save in the instance that started the save.
Test

Implement unsynced transfer.
Expand Down
48 changes: 35 additions & 13 deletions negligent_octopus/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,24 +153,30 @@ def chain_update_balance(self, start_transaction, *args, **kwargs):

universe = start_transaction.after(using=using)

def update_balance(transaction, previous_balance):
if transaction.update_balance(
def inner_update_balance(transaction, previous_balance):
transaction.update_balance(
previous_balance=previous_balance,
using=using,
):
transaction.save(*args, **kwargs)
commit=True,
)
return transaction.balance

previous_balance = update_balance(start_transaction, None)
previous_balance = inner_update_balance(start_transaction, None)

for transaction in universe.reverse():
previous_balance = update_balance(transaction, previous_balance)
previous_balance = inner_update_balance(
self if transaction.pk == self.pk else transaction,
previous_balance,
)
# 'universe' qs returns a new instance of this transaction
# which is saved but overriden if we later call save on this transaction.

def update_balance(
self,
*,
previous_balance: float | None = None,
skip_check: bool = False,
using=None,
commit=False,
) -> bool:
"""
Update the balance of this instance. Does not write to database.
Expand All @@ -182,6 +188,10 @@ def update_balance(
Uses the balance of this transaction to calculate balance, if provided.
skip_check (bool, optional):
Don't check if the balance has changed. Defaults to False.
commit (bool, optional):
Whether to save the updated balance. If it is an update operation,
it saves only the balance. Defaults to False.
Returns:
Returns if the balance has changed.
If skip_check is set to True, returns True.
Expand All @@ -205,11 +215,17 @@ def update_balance(
except self.__class__.DoesNotExist:
old_instance = None

return (
True
if skip_check or old_instance is None
else old_instance.balance != self.balance
)
if commit:
kwargs = {
"using": using,
"update_balance": False,
"sync_transfer": False,
}
if old_instance is not None:
kwargs["update_fields"] = ("balance",)
self.save(**kwargs)

return old_instance is None or old_instance.balance != self.balance

def sync_transfer(self, *args, **kwargs):
if self.destination_account is None:
Expand All @@ -226,6 +242,8 @@ def sync_transfer(self, *args, **kwargs):
self.transfer_transaction.category = self.category
self.transfer_transaction.destination_account = self.account

for kw in ("force_insert", "force_update", "update_fields"):
kwargs.pop(kw, None)
self.transfer_transaction.save(*args, sync_transfer=False, **kwargs)

self.transfer_transaction.transfer_transaction = self
Expand Down Expand Up @@ -266,7 +284,11 @@ def save(
old_instance = None

start_transaction = self
if old_instance is not None and self.timestamp > old_instance.timestamp:
if (
old_instance is not None
and self.timestamp > old_instance.timestamp
and self.pk != self.account.transaction_set.first()
):
start_transaction = old_instance.next()

super().save(*args, **kwargs)
Expand Down
31 changes: 31 additions & 0 deletions negligent_octopus/core/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,34 @@ def test_transaction_delete(self, account: Account):
account.transaction_set.get(pk=second_pk)

assert third_transaction.balance == account.initial_balance + 1 + 100

def test_transaction_timestamp_forward(self, account: Account):
now = timezone.now()
first_transaction = TransactionFactory(
account=account,
amount=1,
timestamp=now,
)
second_transaction = TransactionFactory(
account=account,
amount=10,
timestamp=now + timedelta(minutes=1),
)
third_transaction = TransactionFactory(
account=account,
amount=100,
timestamp=now + timedelta(minutes=2),
)
second_transaction.timestamp = now + timedelta(minutes=3)
second_transaction.save()

# Reload instances
first_transaction = account.transaction_set.get(pk=first_transaction.pk)
third_transaction = account.transaction_set.get(pk=third_transaction.pk)

assert first_transaction.balance == account.initial_balance + 1
assert third_transaction.balance == account.initial_balance + 1 + 100
assert second_transaction.balance == account.initial_balance + 1 + 10 + 100

assert second_transaction.next() is None
assert second_transaction.previous() == third_transaction

0 comments on commit 01c5dfb

Please sign in to comment.