-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from akvo/feature/70-build-statistics-page
Feature/70 build statistics page
- Loading branch information
Showing
42 changed files
with
832 additions
and
67 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
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
80 changes: 80 additions & 0 deletions
80
backend/api/v1/v1_sessions/tests/tests_session_statistics_endpoint.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,80 @@ | ||
from datetime import timedelta | ||
from django.test import TestCase | ||
from django.core.management import call_command | ||
from django.db.models import Count, DateField | ||
from django.db.models.functions import TruncMonth | ||
from django.test.utils import override_settings | ||
from django.utils import timezone | ||
from api.v1.v1_sessions.models import PATSession | ||
from api.v1.v1_users.models import SystemUser | ||
from api.v1.v1_users.tests.mixins import ProfileTestHelperMixin | ||
|
||
|
||
@override_settings(USE_TZ=False) | ||
class SessionStatisticsEndpointTestCase(TestCase, ProfileTestHelperMixin): | ||
def setUp(self): | ||
call_command("fake_users_seeder", "--test", True, "--repeat", 15) | ||
self.admin = SystemUser.objects.create_superuser( | ||
full_name="Super Admin", | ||
email="admin@akvo.org", | ||
gender=1, | ||
account_purpose=2, | ||
country="EN", | ||
password="Secret123!", | ||
) | ||
self.token = self.get_auth_token( | ||
email="admin@akvo.org", | ||
password="Secret123!", | ||
) | ||
call_command("fake_sessions_seeder", "--test", True, "--repeat", 15) | ||
|
||
def test_get_total_session_completed(self): | ||
req = self.client.get( | ||
"/api/v1/admin/sessions/completed", | ||
content_type="application/json", | ||
HTTP_AUTHORIZATION=f"Bearer {self.token}" | ||
) | ||
self.assertEqual(req.status_code, 200) | ||
res = req.json() | ||
total_completed = PATSession.objects.filter( | ||
closed_at__isnull=False | ||
).count() | ||
self.assertEqual(res["total_completed"], total_completed) | ||
total_completed_last_30_days = PATSession.objects.filter( | ||
closed_at__isnull=False, | ||
closed_at__gte=timezone.now() - timedelta(days=30) | ||
).count() | ||
self.assertEqual( | ||
res["total_completed_last_30_days"], | ||
total_completed_last_30_days | ||
) | ||
|
||
def test_get_total_session_per_month(self): | ||
req = self.client.get( | ||
"/api/v1/admin/sessions/per-month", | ||
content_type="application/json", | ||
HTTP_AUTHORIZATION=f"Bearer {self.token}" | ||
) | ||
self.assertEqual(req.status_code, 200) | ||
res = req.json() | ||
total_sessions_per_month = PATSession.objects.annotate( | ||
month=TruncMonth("created_at", output_field=DateField()) | ||
).values("month").annotate(total_sessions=Count("id")) | ||
sessions_per_month = [] | ||
for session in total_sessions_per_month: | ||
sessions_per_month.append({ | ||
"month": session["month"].strftime("%Y-%m-%d"), | ||
"total_sessions": session["total_sessions"] | ||
}) | ||
self.assertEqual(len(res), len(sessions_per_month)) | ||
|
||
def test_get_total_session_per_last_3_years(self): | ||
req = self.client.get( | ||
"/api/v1/admin/sessions/per-last-3-years", | ||
content_type="application/json", | ||
HTTP_AUTHORIZATION=f"Bearer {self.token}" | ||
) | ||
self.assertEqual(req.status_code, 200) | ||
res = req.json() | ||
self.assertEqual(len(res), 3) | ||
self.assertEqual(len(res[0]["total_sessions"]), 12) |
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
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
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
62 changes: 62 additions & 0 deletions
62
backend/api/v1/v1_users/tests/tests_users_statistics_endpoint.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,62 @@ | ||
from django.test import TestCase | ||
from django.core.management import call_command | ||
from django.test.utils import override_settings | ||
from django.utils import timezone | ||
from django.db.models import Count | ||
from datetime import timedelta | ||
from api.v1.v1_users.models import SystemUser | ||
from api.v1.v1_users.constants import AccountPurpose | ||
from api.v1.v1_users.tests.mixins import ProfileTestHelperMixin | ||
|
||
|
||
@override_settings(USE_TZ=False) | ||
class UsersStatisticsTestCase(TestCase, ProfileTestHelperMixin): | ||
def setUp(self): | ||
call_command("fake_users_seeder", "--test", True, "--repeat", 15) | ||
self.admin = SystemUser.objects.create_superuser( | ||
full_name="Super Admin", | ||
email="admin@akvo.org", | ||
gender=1, | ||
account_purpose=2, | ||
country="EN", | ||
password="Secret123!", | ||
) | ||
self.token = self.get_auth_token( | ||
email="admin@akvo.org", | ||
password="Secret123!", | ||
) | ||
|
||
def test_get_users_statistics(self): | ||
req = self.client.get( | ||
"/api/v1/admin/statistics/users", | ||
content_type="application/json", | ||
HTTP_AUTHORIZATION=f"Bearer {self.token}", | ||
) | ||
self.assertEqual(req.status_code, 200) | ||
res = req.json() | ||
self.assertEqual(res["total_users"], 16) | ||
total_users_last_30_days = SystemUser.objects.filter( | ||
date_joined__gte=timezone.now() - timedelta(days=30) | ||
).count() | ||
self.assertEqual( | ||
res["total_users_last_30_days"], | ||
total_users_last_30_days | ||
) | ||
|
||
total_users_per_account_purpose = SystemUser.objects.values( | ||
"account_purpose" | ||
).annotate( | ||
total=Count("account_purpose") | ||
) | ||
account_purpose_total = [] | ||
for total in total_users_per_account_purpose: | ||
category_name = AccountPurpose.FieldStr[total["account_purpose"]] | ||
account_purpose_total.append({ | ||
"account_purpose": category_name, | ||
"total": total["total"] | ||
}) | ||
|
||
self.assertEqual( | ||
res["total_users_per_account_purpose"], | ||
account_purpose_total | ||
) |
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
Oops, something went wrong.