|
1 | | -import datetime |
| 1 | +import os |
2 | 2 | import re |
3 | 3 | import uuid |
| 4 | +from datetime import timedelta |
4 | 5 | from unittest.mock import patch |
5 | 6 |
|
| 7 | +import pytest |
6 | 8 | from django.contrib.auth.models import User |
7 | 9 | from django.core.files.uploadedfile import SimpleUploadedFile |
8 | 10 | from django.test import override_settings |
9 | 11 | from django.urls import reverse |
| 12 | +from django.utils import timezone |
10 | 13 |
|
11 | 14 | import api.models as models |
12 | 15 | from api.factories.event import ( |
|
24 | 27 | from per.factories import OpsLearningFactory |
25 | 28 |
|
26 | 29 |
|
| 30 | +@pytest.mark.skipif( |
| 31 | + os.getenv("DJANGO_DEBUG", "false").lower() == "true", |
| 32 | + reason="Works only on Staging and Production where Power BI is configured", |
| 33 | +) |
27 | 34 | class AuthPowerBITest(APITestCase): |
28 | 35 | def setUp(self): |
29 | 36 | self.url = reverse("auth_power_bi") |
@@ -875,81 +882,80 @@ class AppealTest(APITestCase): |
875 | 882 | fixtures = ["DisasterTypes"] |
876 | 883 |
|
877 | 884 | def test_appeal_key_figure(self): |
878 | | - creation_time = datetime.datetime(2023, 1, 5, 17, 4, 42, tzinfo=datetime.timezone.utc) |
879 | | - view_time = datetime.datetime(2024, 6, 1, 12, 0, 0, tzinfo=datetime.timezone.utc) |
880 | | - |
881 | | - with patch("django.utils.timezone.now") as mock_now: |
882 | | - mock_now.return_value = creation_time |
883 | | - region1 = models.Region.objects.create(name=1) |
884 | | - region2 = models.Region.objects.create(name=2) |
885 | | - country1 = models.Country.objects.create(name="Nepal", iso3="NPL", region=region1) |
886 | | - country2 = models.Country.objects.create(name="India", iso3="IND", region=region2) |
887 | | - dtype1 = models.DisasterType.objects.get(pk=1) |
888 | | - dtype2 = models.DisasterType.objects.get(pk=2) |
889 | | - event1 = EventFactory.create( |
890 | | - name="test1", |
891 | | - dtype=dtype1, |
892 | | - ) |
893 | | - event2 = EventFactory.create(name="test0", dtype=dtype1, num_affected=10000, countries=[country1]) |
894 | | - event3 = EventFactory.create(name="test2", dtype=dtype2, num_affected=99999, countries=[country2]) |
895 | | - AppealFactory.create( |
896 | | - event=event1, |
897 | | - dtype=dtype1, |
898 | | - num_beneficiaries=9000, |
899 | | - amount_requested=10000, |
900 | | - amount_funded=1899999, |
901 | | - code=12, |
902 | | - start_date="2024-1-1", |
903 | | - end_date="2024-1-1", |
904 | | - atype=AppealType.APPEAL, |
905 | | - country=country1, |
906 | | - ) |
907 | | - AppealFactory.create( |
908 | | - event=event2, |
909 | | - dtype=dtype2, |
910 | | - num_beneficiaries=90023, |
911 | | - amount_requested=100440, |
912 | | - amount_funded=12299999, |
913 | | - code=123, |
914 | | - start_date="2024-2-2", |
915 | | - end_date="2024-2-2", |
916 | | - atype=AppealType.DREF, |
917 | | - country=country1, |
918 | | - ) |
919 | | - AppealFactory.create( |
920 | | - event=event3, |
921 | | - dtype=dtype2, |
922 | | - num_beneficiaries=91000, |
923 | | - amount_requested=10000888, |
924 | | - amount_funded=678888, |
925 | | - code=1234, |
926 | | - start_date="2024-3-3", |
927 | | - end_date="2024-3-3", |
928 | | - atype=AppealType.APPEAL, |
929 | | - country=country1, |
930 | | - ) |
931 | | - AppealFactory.create( |
932 | | - event=event3, |
933 | | - dtype=dtype2, |
934 | | - num_beneficiaries=91000, |
935 | | - amount_requested=10000888, |
936 | | - amount_funded=678888, |
937 | | - code=12345, |
938 | | - start_date="2024-4-4", |
939 | | - end_date="2024-4-4", |
940 | | - atype=AppealType.APPEAL, |
941 | | - country=country1, |
942 | | - ) |
943 | | - |
944 | | - mock_now.return_value = view_time |
945 | | - url = f"/api/v2/country/{country1.id}/figure/" |
946 | | - self.client.force_authenticate(self.user) |
947 | | - response = self.client.get(url) |
948 | | - |
949 | | - self.assert_200(response) |
950 | | - self.assertIsNotNone(response.json()) |
951 | | - self.assertEqual(response.data["active_drefs"], 1) |
952 | | - self.assertEqual(response.data["active_appeals"], 3) |
| 885 | + # Calculate dynamic dates relative to today |
| 886 | + today = timezone.now().date() |
| 887 | + four_months_ago = today - timedelta(days=120) |
| 888 | + three_months_ago = today - timedelta(days=90) |
| 889 | + two_months_ago = today - timedelta(days=60) |
| 890 | + one_month_ago = today - timedelta(days=30) |
| 891 | + |
| 892 | + region1 = models.Region.objects.create(name=1) |
| 893 | + region2 = models.Region.objects.create(name=2) |
| 894 | + country1 = models.Country.objects.create(name="Nepal", iso3="NPL", region=region1) |
| 895 | + country2 = models.Country.objects.create(name="India", iso3="IND", region=region2) |
| 896 | + dtype1 = models.DisasterType.objects.get(pk=1) |
| 897 | + dtype2 = models.DisasterType.objects.get(pk=2) |
| 898 | + event1 = EventFactory.create( |
| 899 | + name="test1", |
| 900 | + dtype=dtype1, |
| 901 | + ) |
| 902 | + event2 = EventFactory.create(name="test0", dtype=dtype1, num_affected=10000, countries=[country1]) |
| 903 | + event3 = EventFactory.create(name="test2", dtype=dtype2, num_affected=99999, countries=[country2]) |
| 904 | + AppealFactory.create( |
| 905 | + event=event1, |
| 906 | + dtype=dtype1, |
| 907 | + num_beneficiaries=9000, |
| 908 | + amount_requested=10000, |
| 909 | + amount_funded=1899999, |
| 910 | + code=12, |
| 911 | + start_date=four_months_ago, |
| 912 | + end_date=four_months_ago, |
| 913 | + atype=AppealType.APPEAL, |
| 914 | + country=country1, |
| 915 | + ) |
| 916 | + AppealFactory.create( |
| 917 | + event=event2, |
| 918 | + dtype=dtype2, |
| 919 | + num_beneficiaries=90023, |
| 920 | + amount_requested=100440, |
| 921 | + amount_funded=12299999, |
| 922 | + code=123, |
| 923 | + start_date=three_months_ago, |
| 924 | + end_date=three_months_ago, |
| 925 | + atype=AppealType.DREF, |
| 926 | + country=country1, |
| 927 | + ) |
| 928 | + AppealFactory.create( |
| 929 | + event=event3, |
| 930 | + dtype=dtype2, |
| 931 | + num_beneficiaries=91000, |
| 932 | + amount_requested=10000888, |
| 933 | + amount_funded=678888, |
| 934 | + code=1234, |
| 935 | + start_date=two_months_ago, |
| 936 | + end_date=two_months_ago, |
| 937 | + atype=AppealType.APPEAL, |
| 938 | + country=country1, |
| 939 | + ) |
| 940 | + AppealFactory.create( |
| 941 | + event=event3, |
| 942 | + dtype=dtype2, |
| 943 | + num_beneficiaries=91000, |
| 944 | + amount_requested=10000888, |
| 945 | + amount_funded=678888, |
| 946 | + code=12345, |
| 947 | + start_date=one_month_ago, |
| 948 | + end_date=one_month_ago, |
| 949 | + atype=AppealType.APPEAL, |
| 950 | + country=country1, |
| 951 | + ) |
| 952 | + url = f"/api/v2/country/{country1.id}/figure/" |
| 953 | + self.client.force_authenticate(self.user) |
| 954 | + response = self.client.get(url) |
| 955 | + self.assert_200(response) |
| 956 | + self.assertIsNotNone(response.json()) |
| 957 | + self.assertEqual(response.data["active_drefs"], 1) |
| 958 | + self.assertEqual(response.data["active_appeals"], 3) |
953 | 959 |
|
954 | 960 |
|
955 | 961 | class RegionSnippetVisibilityTest(APITestCase): |
|
0 commit comments