Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IA-3711: add methods to test mixins #1822

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions iaso/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest import mock

from rest_framework.test import APITestCase as BaseAPITestCase, APIClient
from django.contrib.auth.models import AnonymousUser

from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
Expand Down Expand Up @@ -107,6 +108,36 @@ def reload_urls(urlconfs: list) -> None:
importlib.reload(importlib.import_module(urlconf))
clear_url_caches()

@staticmethod
def create_base_users(account, permissions):
# anonymous user and user without needed permissions
anon = AnonymousUser()
user_no_perms = IasoTestCaseMixin.create_user_with_profile(
username="user_no_perm", account=account, permissions=[]
)

user = IasoTestCaseMixin.create_user_with_profile(username="user", account=account, permissions=permissions)
return [user, anon, user_no_perms]

@staticmethod
def create_account_datasource_version_project(source_name, account_name, project_name):
"""Create a project and all related data: account, data source, source version"""
data_source = m.DataSource.objects.create(name=source_name)
source_version = m.SourceVersion.objects.create(data_source=data_source, number=1)
account = m.Account.objects.create(name=account_name, default_version=source_version)
project = m.Project.objects.create(name=project_name, app_id=f"{project_name}.app", account=account)
data_source.projects.set([project])

return [account, data_source, source_version, project]

@staticmethod
def create_org_unit_type(name, projects, category=None):
type_category = category if category else name
org_unit_type = m.OrgUnitType.objects.create(name=name, category=type_category)
org_unit_type.projects.set(projects)
org_unit_type.save()
return org_unit_type


class TestCase(BaseTestCase, IasoTestCaseMixin):
pass
Expand Down
63 changes: 63 additions & 0 deletions plugins/polio/tests/api/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import datetime
from iaso import models as m
from plugins.polio import models as pm


class PolioTestCaseMixin:
@staticmethod
def create_campaign(
obr_name,
account,
source_version,
country_ou_type,
district_ou_type,
country_name="Groland",
district_name="Groville",
):
country = m.OrgUnit.objects.create(
org_unit_type=country_ou_type,
version=source_version,
name=country_name,
validation_status=m.OrgUnit.VALIDATION_VALID,
source_ref="PvtAI4RUMkr",
)
district = m.OrgUnit.objects.create(
org_unit_type=district_ou_type,
version=source_version,
name=district_name,
validation_status=m.OrgUnit.VALIDATION_VALID,
source_ref="PvtAI4RUMkr",
)
campaign = pm.Campaign.objects.create(
obr_name=obr_name,
country=country,
account=account,
vacine=pm.VACCINES[0][0],
separate_scopes_per_round=False,
)
scope_group = m.Group.objects.create(name="campaign_scope", source_version=source_version)
scope_group.org_units.set([district]) # FIXME: we should actually have children org units
scope = pm.CampaignScope.objects.create(campaign=campaign, vaccine=pm.VACCINES[0][0], group=scope_group)

round_1 = pm.Round.objects.create(
campaign=campaign,
started_at=datetime.datetime(2021, 1, 1),
ended_at=datetime.datetime(2021, 1, 10),
number=1,
)

round_2 = pm.Round.objects.create(
campaign=campaign,
started_at=datetime.datetime(2021, 2, 1),
ended_at=datetime.datetime(2021, 2, 10),
number=2,
)

round_3 = pm.Round.objects.create(
campaign=campaign,
started_at=datetime.datetime(2021, 3, 1),
ended_at=datetime.datetime(2021, 3, 10),
number=3,
)

return [campaign, round_1, round_2, round_3, country, district]
Loading