Created 'test_dashboard.py' and fixed bug in 'test_models.py'
This commit is contained in:
parent
67a7d27e21
commit
e531dec8fe
94
expenses_manager/expenses/tests/test_dashboard.py
Normal file
94
expenses_manager/expenses/tests/test_dashboard.py
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
import pytest
|
||||||
|
from datetime import date
|
||||||
|
from decimal import Decimal
|
||||||
|
from django.urls import reverse
|
||||||
|
from expenses.models import Expense, Category
|
||||||
|
|
||||||
|
pytestmark = pytest.mark.django_db
|
||||||
|
|
||||||
|
def test_dashboard_requires_login(client):
|
||||||
|
url = reverse('dashboard')
|
||||||
|
response = client.get(url)
|
||||||
|
assert response.status_code == 302
|
||||||
|
|
||||||
|
def test_dashboard_logged_user_can_access(client, django_user_model):
|
||||||
|
user = django_user_model.objects.create_user(
|
||||||
|
username='test',
|
||||||
|
password='1234'
|
||||||
|
)
|
||||||
|
client.login(username='test', password='1234')
|
||||||
|
|
||||||
|
url = reverse('dashboard')
|
||||||
|
response = client.get(url)
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
def test_dashboard_groups_by_category(client, django_user_model):
|
||||||
|
user = django_user_model.objects.create_user(
|
||||||
|
username='test',
|
||||||
|
password='1234'
|
||||||
|
)
|
||||||
|
client.login(username='test', password='1234')
|
||||||
|
|
||||||
|
food = Category.objects.create(name='Food', owner=user)
|
||||||
|
rent = Category.objects.create(name='Rent', owner=user)
|
||||||
|
|
||||||
|
Expense.objects.create(
|
||||||
|
owner=user,
|
||||||
|
category=food,
|
||||||
|
amount=Decimal('10'),
|
||||||
|
date=date(2024, 1, 1),
|
||||||
|
)
|
||||||
|
Expense.objects.create(
|
||||||
|
owner=user,
|
||||||
|
category=food,
|
||||||
|
amount=Decimal('5'),
|
||||||
|
date=date(2024, 1, 2),
|
||||||
|
)
|
||||||
|
Expense.objects.create(
|
||||||
|
owner=user,
|
||||||
|
category=rent,
|
||||||
|
amount=Decimal('20'),
|
||||||
|
date=date(2024, 1, 3),
|
||||||
|
)
|
||||||
|
|
||||||
|
response = client.get(reverse('dashboard'))
|
||||||
|
|
||||||
|
data = list(response.context['by_category'])
|
||||||
|
|
||||||
|
assert {'category__name': 'Food', 'total': Decimal('15')} in data
|
||||||
|
assert {'category__name': 'Rent', 'total': Decimal('20')} in data
|
||||||
|
|
||||||
|
def test_dashboard_filters_by_year(client, django_user_model):
|
||||||
|
user = django_user_model.objects.create_user(
|
||||||
|
username='test',
|
||||||
|
password='1234'
|
||||||
|
)
|
||||||
|
client.login(username='test', password='1234')
|
||||||
|
|
||||||
|
cat = Category.objects.create(name='General', owner=user)
|
||||||
|
|
||||||
|
Expense.objects.create(
|
||||||
|
owner=user,
|
||||||
|
category=cat,
|
||||||
|
amount=10,
|
||||||
|
date=date(2023, 5, 1),
|
||||||
|
)
|
||||||
|
|
||||||
|
Expense.objects.create(
|
||||||
|
owner=user,
|
||||||
|
category=cat,
|
||||||
|
amount=20,
|
||||||
|
date=date(2024, 5, 1),
|
||||||
|
)
|
||||||
|
|
||||||
|
response = client.get(
|
||||||
|
reverse('dashboard'),
|
||||||
|
{'year': 2024},
|
||||||
|
)
|
||||||
|
|
||||||
|
by_month = list(response.context['by_month'])
|
||||||
|
|
||||||
|
totals = [row['total'] for row in by_month]
|
||||||
|
assert Decimal('20') in totals
|
||||||
|
assert Decimal('10') not in totals
|
||||||
@ -9,7 +9,7 @@ def test_create_basic_expense():
|
|||||||
password="password",
|
password="password",
|
||||||
)
|
)
|
||||||
|
|
||||||
category = Category.objects.create(name="Comida")
|
category = Category.objects.create(name="Comida", owner=user)
|
||||||
account = Account.objects.create(name="Cuenta principal", owner=user)
|
account = Account.objects.create(name="Cuenta principal", owner=user)
|
||||||
|
|
||||||
expense = Expense.objects.create(
|
expense = Expense.objects.create(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user