-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for JSON with basket evolution (#451)
Co-authored-by: tvedeane <marekjeszka@fastmail.com>
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
tapir/statistics/tests/test_main_statistics_purchases_evolution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import datetime | ||
|
||
from django.urls import reverse | ||
|
||
from tapir.coop.tests.factories import PurchaseBasketFactory | ||
from tapir.statistics.models import ProcessedPurchaseFiles | ||
from tapir.utils.tests_utils import TapirFactoryTestBase, mock_timezone_now | ||
|
||
|
||
class TestBasketSumEvolutionData(TapirFactoryTestBase): | ||
NOW = datetime.datetime(year=2023, month=4, day=1, hour=12) | ||
|
||
def test_get_basket_sums_per_month(self): | ||
user = self.login_as_normal_user() | ||
mock_timezone_now(self, self.NOW) | ||
|
||
self.create_payment( | ||
date=datetime.date(year=2023, month=1, day=1), | ||
amount=10.00, | ||
source_file=self.getSourceFile(), | ||
user=user, | ||
) | ||
self.create_payment( | ||
date=datetime.date(year=2023, month=4, day=1), | ||
amount=20.00, | ||
source_file=self.getSourceFile(), | ||
user=user, | ||
) | ||
self.create_payment( | ||
date=datetime.date(year=2023, month=4, day=1), | ||
amount=10.50, | ||
source_file=self.getSourceFile(), | ||
user=user, | ||
) | ||
|
||
response = self.client.get( | ||
reverse("statistics:basket_sum_evolution_json", kwargs={"pk": user.pk}), | ||
) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual( # assert months | ||
response.json()["data"]["labels"], | ||
["2023-01", "2023-02", "2023-03", "2023-04"], | ||
) | ||
self.assertEqual( # assert values | ||
response.json()["data"]["datasets"][0]["data"], [10.0, 0, 0, 30.5] | ||
) | ||
|
||
def test_requires_permissions(self): | ||
user = self.login_as_normal_user() | ||
|
||
another_user_pk = user.pk + 1 | ||
response = self.client.get( | ||
reverse( | ||
"statistics:basket_sum_evolution_json", kwargs={"pk": another_user_pk} | ||
), | ||
) | ||
self.assertEqual(response.status_code, 403) | ||
|
||
@staticmethod | ||
def create_payment(date, amount, source_file, user): | ||
PurchaseBasketFactory.create( | ||
purchase_date=date, | ||
gross_amount=amount, | ||
source_file=source_file, | ||
tapir_user=user, | ||
) | ||
|
||
@staticmethod | ||
def getSourceFile(): | ||
return ProcessedPurchaseFiles.objects.create( | ||
file_name=f"test_basket_file", | ||
processed_on=datetime.datetime.now(), | ||
) |