Skip to content

Commit

Permalink
Fix updating recurrences when no transactions are available
Browse files Browse the repository at this point in the history
  • Loading branch information
simhnna committed Feb 11, 2019
1 parent 81cabf6 commit 9d1dd96
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
10 changes: 6 additions & 4 deletions silverstrike/tests/views/test_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.test import TestCase
from django.urls import reverse

from silverstrike.models import Account, Transaction, Category
from silverstrike.models import Account, Category, Transaction
from silverstrike.tests import create_transaction


Expand All @@ -24,16 +24,18 @@ def test_context_CategoryIndex_with_no_categories(self):
self.assertEqual(context['categories'], [])

def test_context_CategoryIndex_with_category(self):
category = Category.objects.create(name='Some name')
Category.objects.create(name='Some name')
response = self.client.get(reverse('category_by_month'))
self.assertEqual(response.status_code, 200)
categories = response.context['categories']
self.assertEqual(categories, [])

def test_context_CategoryIndex_with_category_with_transactions(self):
category = Category.objects.create(name='expenses')
create_transaction('Deposit', self.revenue, self.account, 1500, Transaction.DEPOSIT, category=category)
create_transaction('Withdraw', self.account, self.expense, 500, Transaction.WITHDRAW, category=category)
create_transaction('Deposit', self.revenue, self.account, 1500,
Transaction.DEPOSIT, category=category)
create_transaction('Withdraw', self.account, self.expense, 500,
Transaction.WITHDRAW, category=category)
response = self.client.get(reverse('category_by_month'))
self.assertEqual(response.status_code, 200)
categories = response.context['categories']
Expand Down
51 changes: 51 additions & 0 deletions silverstrike/tests/views/test_update_recurrence_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from datetime import date

from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse

from silverstrike.models import Account, RecurringTransaction, Transaction
from silverstrike.tests import create_transaction


class UpdateRecurrenceDateTests(TestCase):
def setUp(self):
User.objects.create_superuser(username='admin', email='email@example.com', password='pass')
self.client.login(username='admin', password='pass')
self.account = Account.objects.create(name='first account')
self.foreign = Account.objects.create(
name="other account", account_type=Account.FOREIGN)
self.recurrence = RecurringTransaction.objects.create(
title='A recurrence',
amount=500,
date=date(2019, 1, 1),
src=self.account,
dst=self.foreign,
interval=RecurringTransaction.MONTHLY,
transaction_type=Transaction.WITHDRAW,
usual_month_day=1
)
self.transaction = create_transaction('title', self.account, self.foreign, 500,
Transaction.WITHDRAW, date(2019, 1, 1))
self.transaction.recurrence = self.recurrence
self.transaction.save()

def test_no_update_when_transactions_are_in_past(self):
self.recurrence.date = date(2019, 2, 1)
response = self.client.post(reverse('update_current_recurrences'))
self.assertRedirects(response, reverse('recurrences'))
self.recurrence.refresh_from_db()
self.assertEqual(self.recurrence.date, date(2019, 2, 1))

def test_recurrence_gets_updated(self):
response = self.client.post(reverse('update_current_recurrences'))
self.assertRedirects(response, reverse('recurrences'))
self.recurrence.refresh_from_db()
self.assertEqual(self.recurrence.date, date(2019, 2, 1))

def test_recurrence_with_no_transactions(self):
self.transaction.delete()
response = self.client.post(reverse('update_current_recurrences'))
self.assertRedirects(response, reverse('recurrences'))
self.recurrence.refresh_from_db()
self.assertEqual(self.recurrence.date, date(2019, 1, 1))
2 changes: 2 additions & 0 deletions silverstrike/views/recurrences.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def post(self, request, *args, **kwargs):
for r in RecurringTransaction.objects.due_in_month():
old = r.date
t = r.recurrences.first()
if not t:
continue
while t.date >= r.date:
r.date = r.update_date()
if old != r.date:
Expand Down

0 comments on commit 9d1dd96

Please sign in to comment.