Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sravfeyn committed Sep 23, 2024
1 parent 5879393 commit d218ad4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion commcare_connect/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def user(db) -> User:

@pytest.fixture()
def opportunity():
factory = OpportunityFactory()
factory = OpportunityFactory(is_test=False)
OpportunityVerificationFlagsFactory(opportunity=factory)
return factory

Expand Down
9 changes: 9 additions & 0 deletions commcare_connect/opportunity/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ class Meta:
model = "opportunity.HQApiKey"


class DeliveryTypeFactory(DjangoModelFactory):
name = Faker("name")
slug = Faker("pystr")

class Meta:
model = "opportunity.DeliveryType"


class OpportunityFactory(DjangoModelFactory):
organization = SubFactory(OrganizationFactory)
name = Faker("name")
Expand All @@ -47,6 +55,7 @@ class OpportunityFactory(DjangoModelFactory):
budget_per_visit = Faker("pyint", min_value=1, max_value=10)
total_budget = Faker("pyint", min_value=1000, max_value=10000)
api_key = SubFactory(HQApiKeyFactory)
delivery_type = SubFactory(DeliveryTypeFactory)

class Meta:
model = "opportunity.Opportunity"
Expand Down
Empty file.
64 changes: 64 additions & 0 deletions commcare_connect/reports/tests/test_reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import datetime
import math

import pytest
from factory.faker import Faker

from commcare_connect.conftest import MobileUserFactory
from commcare_connect.opportunity.models import CompletedWorkStatus, Opportunity, VisitValidationStatus
from commcare_connect.opportunity.tests.factories import (
CompletedWorkFactory,
DeliverUnitFactory,
OpportunityAccessFactory,
PaymentUnitFactory,
UserVisitFactory,
)
from commcare_connect.reports.views import _get_table_data_for_quarter


@pytest.mark.django_db
def test_delivery_stats(opportunity: Opportunity):
payment_units = PaymentUnitFactory.create_batch(2, opportunity=opportunity)
mobile_users = MobileUserFactory.create_batch(5)
for payment_unit in payment_units:
DeliverUnitFactory.create_batch(2, payment_unit=payment_unit, app=opportunity.deliver_app, optional=False)
access_objects = []
for mobile_user in mobile_users:
access = OpportunityAccessFactory(user=mobile_user, opportunity=opportunity, accepted=True)
access_objects.append(access)
for payment_unit in payment_units:
completed_work = CompletedWorkFactory(
opportunity_access=access,
payment_unit=payment_unit,
status=CompletedWorkStatus.approved.value,
)
for deliver_unit in payment_unit.deliver_units.all():
UserVisitFactory(
opportunity=opportunity,
user=mobile_user,
deliver_unit=deliver_unit,
status=VisitValidationStatus.approved.value,
opportunity_access=access,
completed_work=completed_work,
visit_date=Faker("date_this_month"),
)

quarter = math.ceil(datetime.datetime.utcnow().month / 12 * 4)

# delivery_type filter not applied
all_data = _get_table_data_for_quarter((datetime.datetime.utcnow().year, quarter), None)
assert all_data["users"] == 5
assert all_data["services"] == 10
assert all_data["beneficiaries"] == 10

# test delivery_type filter
filtered_data = _get_table_data_for_quarter(
(datetime.datetime.utcnow().year, quarter), opportunity.delivery_type.slug
)
assert filtered_data == all_data

# unknown delivery-type should have no data
unknown_delivery_type_data = _get_table_data_for_quarter((datetime.datetime.utcnow().year, quarter), "unknown")
assert unknown_delivery_type_data["users"] == 0
assert unknown_delivery_type_data["services"] == 0
assert unknown_delivery_type_data["beneficiaries"] == 0

0 comments on commit d218ad4

Please sign in to comment.