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, "