Skip to content

Commit 397abfb

Browse files
John OlanipekunJohn Olanipekun
authored andcommitted
adding tests for the views
1 parent d3f6fb3 commit 397abfb

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

FinanceTracker/FinTech/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def dashboard(request):
1616
for catg in my_catg:
1717
out_dict[catg.name]=0
1818

19-
expenses= Expense.objects.filter(user=request.user.id)
19+
expenses= Expense.objects.filter(user=request.user.id).order_by('id')
2020
for expense in expenses:
2121
catg=expense.category
2222
pre_amount=expense.amount
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from django.test import TestCase, Client
2+
from django.urls import reverse
3+
from django.contrib.auth.models import User
4+
from datetime import date
5+
from unittest.mock import patch, MagicMock
6+
from FinTech.models import IncomeCategory, ExpenseCategory, Income, Expense, Budget, Account
7+
import json
8+
9+
10+
class TestViews(TestCase):
11+
12+
def setUp(self):
13+
# Create a user and log them in
14+
self.user = User.objects.create_user(username='testuser', password='testpass')
15+
self.client.login(username='testuser', password='testpass')
16+
17+
# Create an account for the user
18+
self.account = Account.objects.create(user=self.user, balance=500)
19+
20+
# Create expense categories for the user
21+
self.catg_food = ExpenseCategory.objects.create(user=self.user, name='Food')
22+
self.catg_transport = ExpenseCategory.objects.create(user=self.user, name='Transport')
23+
24+
# Create expenses for the user
25+
self.expense1 = Expense.objects.create(user=self.user, amount=50, category=self.catg_food, date=date.today())
26+
self.expense2 = Expense.objects.create(user=self.user, amount=20, category=self.catg_transport, date=date.today())
27+
self.expense3 = Expense.objects.create(user=self.user, amount=30, category=self.catg_food, date=date.today())
28+
29+
def test_dashboard_view_expenses_and_balance(self):
30+
# Call the dashboard view
31+
response = self.client.get(reverse('dashboard'))
32+
33+
# Check if the response is 200 OK
34+
self.assertEqual(response.status_code, 200)
35+
36+
# Check if the account balance is passed correctly to the template
37+
self.assertContains(response, self.account.balance)
38+
39+
# Check if the expenses by category are calculated correctly
40+
context = response.context
41+
expected_expenses = {
42+
'Food': 80, # 50 + 30
43+
'Transport': 20 # 20
44+
}
45+
46+
# Verify that the output dictionary has correct totals
47+
# self.assertEqual(context['catg_list'], ['Food', 'Transport'])
48+
self.assertEqual(context['catg_total_list'], [80, 20])

0 commit comments

Comments
 (0)