Skip to content

Commit

Permalink
add tests for new balance api
Browse files Browse the repository at this point in the history
  • Loading branch information
qedi-r authored and simhnna committed Oct 31, 2022
1 parent ac98147 commit fe56ce6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
29 changes: 25 additions & 4 deletions silverstrike/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import json
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, AccountType
from silverstrike.models import Account, AccountType, Transaction
from silverstrike.tests import create_transaction


class ApiTests(TestCase):
def setUp(self):
User.objects.create_superuser(username='admin', email='email@example.com', password='pass')
self.client.login(username='admin', password='pass')
Account.objects.bulk_create(
[Account(name=t[1], account_type=t[0],
show_on_dashboard=True) for t in AccountType.choices])
self.personal = Account.objects.create(name="Personal", account_type=AccountType.PERSONAL,
show_on_dashboard=True)
self.foreign = Account.objects.create(name="Foreign", account_type=AccountType.FOREIGN,
show_on_dashboard=True)
self.system = Account.objects.create(name="System", account_type=AccountType.SYSTEM,
show_on_dashboard=True)
self.cash = Account.objects.create(name="Cash", account_type=AccountType.PERSONAL,
show_on_dashboard=False)
create_transaction('meh', self.foreign, self.personal, 1000,
Transaction.DEPOSIT, date(2022, 1, 2))
create_transaction('meh', self.foreign, self.cash, 1000,
Transaction.DEPOSIT, date(2022, 1, 3))

def test_get_accounts_return_value(self):
for t in AccountType.choices:
Expand All @@ -23,6 +34,16 @@ def test_get_accounts_return_value(self):
queryset = queryset.exclude(account_type=AccountType.SYSTEM)
self.assertEqual(data, list(queryset.values_list('name', flat=True)))

def test_get_balance_data_excludes_non_dashboard_accounts(self):
response = self.client.get(reverse('api_balance', args=['2022-01-02', '2022-01-02']))
data = json.loads(response.content.decode('utf-8'))
self.assertEqual(['1000.00'], data.get('data'))

def test_get_non_dashboard_balance_return_value(self):
response = self.client.get(reverse('api_non_dashboard_balance', args=['2022-01-03', '2022-01-03']))
data = json.loads(response.content.decode('utf-8'))
self.assertEqual(['2000.00'], data.get('data'))

def test_get_account_balance_invalid_date(self):
response = self.client.get(reverse('api_account_balance', args=['1', '2019-01-01', '20']))
self.assertEqual(response.status_code, 400)
Expand Down
11 changes: 10 additions & 1 deletion silverstrike/tests/views/test_IndexView.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ 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', show_on_dashboard=True)
self.personal = Account.objects.create(name='personal')
self.personal = Account.objects.create(name='personal', show_on_dashboard=True)
self.cash = Account.objects.create(name='cash', show_on_dashboard=False)
self.foreign = Account.objects.create(
name="foreign account", account_type=AccountType.FOREIGN)

Expand Down Expand Up @@ -44,6 +45,14 @@ def test_balance_does_not_count_future_transactions(self):
context = self.client.get(reverse('index')).context
self.assertEqual(context['balance'], 0)

def test_balance_does_not_count_non_dashboard_accounts(self):
create_transaction('meh', self.foreign, self.account, 1000,
Transaction.DEPOSIT, date(2015, 1, 1))
create_transaction('meh', self.cash, self.foreign, 500,
Transaction.DEPOSIT, date(2015, 1, 1))
context = self.client.get(reverse('index')).context
self.assertEqual(context['balance'], 1000)

def test_income(self):
pass

Expand Down

0 comments on commit fe56ce6

Please sign in to comment.