Files
WYGIWYH/app/apps/transactions/tests/test_changed_rows.py
T
Herculino Trotta 0244238354 feat(transactions): update saved transactions in place instead of reloading lists
Adding or editing a transaction used to fire "updated", which re-rendered the
whole list. Saves now send "transactions_changed" with the saved ids. Lists
with the transaction_mover behavior ask their own endpoint for just those rows
(?only=<id>), and the server answers with where each row goes: the row after
it and, for lists split by date, its divider. Rows it doesn't return have left
the list and are removed.

Covers the monthly overview, all transactions (a row joining or leaving the
current page still reloads it), and the installment, recurring and calendar
lists. Pages without a mover fall back to "updated".
2026-12-03 21:53:00 -03:00

134 lines
4.9 KiB
Python

import json
from datetime import date
from decimal import Decimal
from django.contrib.auth import get_user_model
from django.test import TestCase, override_settings
from django.urls import reverse
from apps.accounts.models import Account, AccountGroup
from apps.currencies.models import Currency
from apps.transactions.models import Transaction
@override_settings(
STORAGES={
"default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"
},
},
WHITENOISE_AUTOREFRESH=True,
)
class ChangedRowsTests(TestCase):
"""The lists' ?only= mode, used to move saved rows in place."""
def setUp(self):
user = get_user_model().objects.create_user(
email="test@test.com", password="testpass123"
)
self.client.login(username="test@test.com", password="testpass123")
currency = Currency.objects.create(code="USD", name="US Dollar")
self.account = Account.objects.create(
name="Account",
group=AccountGroup.objects.create(name="Group"),
currency=currency,
)
self.rows = [
Transaction.objects.create(
account=self.account,
type=Transaction.Type.EXPENSE,
is_paid=True,
date=date(2025, 12, 10),
reference_date=date(2025, 12, 1),
amount=Decimal("10"),
description=f"row {i}",
owner=user,
)
for i in range(3)
]
def _only(self, *ids):
return self.client.get(
reverse("monthly_transactions_list", kwargs={"month": 12, "year": 2025}),
{"order": "older", "only": [str(i) for i in ids]},
HTTP_HX_REQUEST="true",
)
def test_row_comes_with_the_row_after_it(self):
first, middle, last = self.rows
response = self._only(middle.pk)
self.assertContains(response, f'data-next-id="{last.pk}"')
self.assertContains(response, f'data-transaction-id="{middle.pk}"')
self.assertNotContains(response, f'data-transaction-id="{first.pk}"')
def test_last_row_has_nothing_after_it(self):
response = self._only(self.rows[-1].pk)
self.assertContains(response, 'data-next-id="" data-next-group=""')
def test_row_that_left_the_month_is_not_returned(self):
moved = self.rows[1]
moved.reference_date = date(2026, 1, 1)
moved.save()
response = self._only(moved.pk)
self.assertNotContains(response, "<template")
def test_edit_announces_the_saved_ids(self):
transaction = self.rows[0]
response = self.client.post(
reverse("transaction_edit", kwargs={"transaction_id": transaction.pk}),
{
"account": self.account.pk,
"type": Transaction.Type.EXPENSE,
"is_paid": True,
"date": "2025-12-11",
"reference_date": "2025-12",
"amount": "12",
"description": "edited",
"submit": "",
},
HTTP_HX_REQUEST="true",
)
self.assertEqual(response.status_code, 204)
trigger = json.loads(response.headers["HX-Trigger"])
self.assertEqual(trigger["transactions_changed"], {"ids": [str(transaction.pk)]})
self.assertNotIn("updated", trigger)
def test_list_without_dividers_answers_bare_rows(self):
middle = self.rows[1]
response = self.client.get(
reverse(
"calendar_transactions_list",
kwargs={"day": 10, "month": 12, "year": 2025},
),
{"only": [str(middle.pk)]},
HTTP_HX_REQUEST="true",
)
self.assertContains(response, f'data-next-id="{self.rows[2].pk}"')
self.assertContains(response, f'data-transaction-id="{middle.pk}"')
self.assertNotContains(response, "transactions-divider")
def test_paginated_list_only_looks_at_the_requested_page(self):
# 100 earlier rows fill page 1, pushing self.rows to page 2
first = self.rows[0]
for _ in range(100):
Transaction.objects.create(
account=self.account,
type=Transaction.Type.EXPENSE,
is_paid=True,
date=date(2025, 12, 1),
reference_date=date(2025, 12, 1),
amount=Decimal("1"),
owner=first.owner,
)
def only_on(page):
return self.client.get(
reverse("transactions_all_list"),
{"order": "older", "page": page, "only": [str(first.pk)]},
HTTP_HX_REQUEST="true",
)
self.assertNotContains(only_on("1"), "<template")
self.assertContains(only_on("2"), f'data-next-id="{self.rows[1].pk}"')