Skip to content

Commit

Permalink
add optional param skip_get_current_site, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Jan 9, 2025
1 parent 31491d0 commit 50949cf
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 34 deletions.
6 changes: 5 additions & 1 deletion edc_action_item/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def __init__(
related_action_item: Optional[ActionItemStub] = None,
using: Optional[str] = None,
readonly: Optional[bool] = None,
skip_get_current_site: bool | None = None,
) -> None:
self._action_item = action_item
self._reference_obj = reference_obj
Expand All @@ -87,6 +88,7 @@ def __init__(
self.readonly = readonly
self.subject_identifier = subject_identifier
self.using = using
self.skip_get_current_site = skip_get_current_site

if self.action_item.action_cls != self.__class__:
raise ActionError(
Expand Down Expand Up @@ -225,7 +227,9 @@ def _create_new_action_item(self, subject_identifier: str = None, **opts):
Called only after checking.
"""
valid_site_for_subject_or_raise(subject_identifier)
valid_site_for_subject_or_raise(
subject_identifier, skip_get_current_site=self.skip_get_current_site
)
try:
self._action_item = create_action_item(
self.__class__,
Expand Down
25 changes: 17 additions & 8 deletions edc_action_item/tests/test_case_mixin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

from dateutil.relativedelta import relativedelta
from django.test import TestCase
from edc_consent.consent_definition import ConsentDefinition
Expand All @@ -8,7 +10,7 @@
from edc_visit_schedule.site_visit_schedules import site_visit_schedules

from .consents import consent_v1
from .visit_schedule import visit_schedule
from .visit_schedule import get_visit_schedule


class TestCaseMixin(TestCase):
Expand All @@ -17,29 +19,36 @@ def setUpTestData(cls):
import_holidays()

@staticmethod
def enroll(subject_identifier=None, cdef: ConsentDefinition | None = None):
def enroll(
subject_identifier=None,
consent_datetime: datetime | None = None,
cdef: ConsentDefinition | None = None,
):
subject_identifier = subject_identifier or "1111111"

site_visit_schedules._registry = {}
site_visit_schedules.loaded = False
site_visit_schedules.register(visit_schedule)
site_visit_schedules.register(get_visit_schedule(cdef))

site_consents.registry = {}
site_consents.register(cdef or consent_v1)

consent_datetime = get_utcnow()
consent_datetime = consent_datetime or get_utcnow()
cdef = site_consents.get_consent_definition(report_datetime=consent_datetime)
subject_consent = cdef.model_cls.objects.create(
subject_identifier=subject_identifier,
consent_datetime=consent_datetime,
dob=get_utcnow() - relativedelta(years=25),
dob=consent_datetime - relativedelta(years=25),
)
RegisteredSubject.objects.create(
subject_identifier=subject_identifier,
consent_datetime=consent_datetime,
)
RegisteredSubject.objects.create(subject_identifier=subject_identifier)
_, schedule = site_visit_schedules.get_by_onschedule_model(
"edc_visit_schedule.onschedule"
)
schedule.put_on_schedule(
subject_consent.subject_identifier, subject_consent.consent_datetime
subject_consent.subject_identifier,
subject_consent.consent_datetime,
)
return subject_identifier

Expand Down
1 change: 1 addition & 0 deletions edc_action_item/tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"sites.E101",
"edc_navbar.E002",
"edc_navbar.E003",
"edc_sites.E001",
],
SUBJECT_VISIT_MODEL="edc_visit_tracking.subjectvisit",
SUBJECT_VISIT_MISSED_MODEL="edc_metadata.subjectvisitmissed",
Expand Down
3 changes: 1 addition & 2 deletions edc_action_item/tests/tests/test_action_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dateutil.relativedelta import relativedelta
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.deletion import ProtectedError
from django.test import TestCase, override_settings, tag
from django.test import TestCase, override_settings
from edc_consent.tests.consent_test_utils import consent_definition_factory
from edc_constants.constants import CANCELLED, CLOSED, NEW, OPEN
from edc_utils import get_utcnow
Expand Down Expand Up @@ -249,7 +249,6 @@ def test_delete3(self):

self.assertRaises(ProtectedError, action_item.delete)

@tag("1")
def test_new(self):
site_action_items.register(FormOneAction)
site_action_items.register(FormTwoAction)
Expand Down
30 changes: 28 additions & 2 deletions edc_action_item/tests/tests/test_longitudinal_actions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import time_machine
from dateutil.relativedelta import relativedelta
from django.apps import apps as django_apps
from django.test import TestCase
from django.test import TestCase, override_settings
from edc_appointment.models import Appointment
from edc_consent.consent_definition import ConsentDefinition
from edc_constants.constants import FEMALE, MALE
from edc_facility import import_holidays
from edc_metadata.tests.constants import test_datetime
from edc_visit_tracking.constants import SCHEDULED

from edc_action_item import site_action_items
Expand All @@ -12,6 +17,10 @@
from ..test_case_mixin import TestCaseMixin


@override_settings(
EDC_PROTOCOL_STUDY_OPEN_DATETIME=test_datetime - relativedelta(years=3),
EDC_PROTOCOL_STUDY_CLOSE_DATETIME=test_datetime + relativedelta(years=3),
)
class TestLongitudinal(TestCaseMixin, TestCase):
@classmethod
def setUpClass(cls):
Expand All @@ -22,17 +31,30 @@ def setUp(self):
site_action_items.registry = {}
site_action_items.register(CrfLongitudinalOneAction)
site_action_items.register(CrfLongitudinalTwoAction)
self.subject_identifier = self.enroll()
consent_v1 = ConsentDefinition(
"edc_action_item.subjectconsentv1",
version="1",
start=test_datetime,
end=test_datetime + relativedelta(years=3),
age_min=18,
age_is_adult=18,
age_max=64,
gender=[MALE, FEMALE],
)
self.subject_identifier = self.enroll(consent_datetime=test_datetime, cdef=consent_v1)

def test_(self):
appointment = Appointment.objects.get(
subject_identifier=self.subject_identifier,
visit_code="1000",
)
traveller = time_machine.travel(appointment.appt_datetime)
traveller.start()
subject_visit = django_apps.get_model(
"edc_visit_tracking.subjectvisit"
).objects.create(
appointment=appointment,
report_datetime=appointment.appt_datetime,
reason=SCHEDULED,
)
crf_one_a = CrfLongitudinalOne.objects.create(subject_visit=subject_visit)
Expand All @@ -41,6 +63,9 @@ def test_(self):
subject_identifier=self.subject_identifier,
visit_code="2000",
)
traveller.stop()
traveller = time_machine.travel(appointment.appt_datetime)
traveller.start()
subject_visit = django_apps.get_model(
"edc_visit_tracking.subjectvisit"
).objects.create(
Expand All @@ -51,3 +76,4 @@ def test_(self):
crf_one_b = CrfLongitudinalOne.objects.create(subject_visit=subject_visit)
ActionItem.objects.get(action_identifier=crf_one_b.action_identifier)
self.assertNotEqual(crf_one_a.action_identifier, crf_one_b.action_identifier)
traveller.stop()
51 changes: 47 additions & 4 deletions edc_action_item/tests/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from datetime import datetime
from zoneinfo import ZoneInfo

import time_machine
from dateutil.relativedelta import relativedelta
from django.apps import apps as django_apps
from django.test import TestCase
from django.test import TestCase, override_settings
from edc_appointment.models import Appointment
from edc_consent import site_consents
from edc_consent.consent_definition import ConsentDefinition
from edc_constants.constants import FEMALE, MALE
from edc_visit_tracking.constants import SCHEDULED

from edc_action_item.models import ActionItem
Expand All @@ -15,7 +22,13 @@
from ..models import CrfOne, CrfTwo, FormOne, FormTwo
from ..test_case_mixin import TestCaseMixin

test_datetime = datetime(2019, 6, 11, 8, 00, tzinfo=ZoneInfo("utc"))


@override_settings(
EDC_PROTOCOL_STUDY_OPEN_DATETIME=test_datetime - relativedelta(years=3),
EDC_PROTOCOL_STUDY_CLOSE_DATETIME=test_datetime + relativedelta(years=3),
)
class TestHelpers(TestCaseMixin, TestCase):
def setUp(self):
site_consents.registry = {}
Expand Down Expand Up @@ -66,19 +79,48 @@ def test_create_next_parent_reference_model_instance(self):
self.assertEqual(get_related_reference_obj(action_item), self.form_one)

def test_reference_as_crf(self):
self.enroll()
consent_v1 = ConsentDefinition(
"edc_action_item.subjectconsentv1",
version="1",
start=test_datetime,
end=test_datetime + relativedelta(years=3),
age_min=18,
age_is_adult=18,
age_max=64,
gender=[MALE, FEMALE],
)
self.enroll(consent_datetime=test_datetime, cdef=consent_v1)
appointment = Appointment.objects.all().order_by("timepoint", "visit_code_sequence")[0]
traveller = time_machine.travel(appointment.appt_datetime)
traveller.start()
subject_visit = django_apps.get_model(
"edc_visit_tracking.subjectvisit"
).objects.create(appointment=appointment, reason=SCHEDULED)
).objects.create(
appointment=appointment,
report_datetime=appointment.appt_datetime,
reason=SCHEDULED,
)
crf_one = CrfOne.objects.create(subject_visit=subject_visit)
action_item = ActionItem.objects.get(action_identifier=crf_one.action_identifier)
self.assertEqual(get_reference_obj(action_item), crf_one)
self.assertIsNone(get_parent_reference_obj(action_item))
self.assertIsNone(get_related_reference_obj(action_item))
traveller.stop()

def test_reference_as_crf_create_next_model_instance(self):
self.enroll()
consent_v1 = ConsentDefinition(
"edc_action_item.subjectconsentv1",
version="1",
start=test_datetime,
end=test_datetime + relativedelta(years=3),
age_min=18,
age_is_adult=18,
age_max=64,
gender=[MALE, FEMALE],
)
self.enroll(consent_datetime=test_datetime, cdef=consent_v1)
traveller = time_machine.travel(test_datetime)
traveller.start()
appointment = Appointment.objects.all().order_by("timepoint", "visit_code_sequence")[0]
subject_visit = django_apps.get_model(
"edc_visit_tracking.subjectvisit"
Expand All @@ -89,3 +131,4 @@ def test_reference_as_crf_create_next_model_instance(self):
self.assertEqual(get_reference_obj(action_item), crf_two)
self.assertEqual(get_parent_reference_obj(action_item), crf_one)
self.assertIsNone(get_related_reference_obj(action_item))
traveller.stop()
34 changes: 17 additions & 17 deletions edc_action_item/tests/visit_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from edc_visit_schedule.visit import Crf, CrfCollection, Visit
from edc_visit_schedule.visit_schedule import VisitSchedule

from .consents import consent_v1

crfs = CrfCollection(
Crf(show_order=1, model="edc_action_item.crflongitudinalone", required=True),
Crf(show_order=2, model="edc_action_item.crflongitudinaltwo", required=True),
Expand Down Expand Up @@ -39,21 +37,23 @@
)


schedule = Schedule(
name="schedule",
onschedule_model="edc_visit_schedule.onschedule",
offschedule_model="edc_visit_schedule.offschedule",
consent_definitions=[consent_v1],
appointment_model="edc_appointment.appointment",
)
def get_visit_schedule(cdef):
schedule = Schedule(
name="schedule",
onschedule_model="edc_visit_schedule.onschedule",
offschedule_model="edc_visit_schedule.offschedule",
consent_definitions=[cdef],
appointment_model="edc_appointment.appointment",
)

schedule.add_visit(visit0)
schedule.add_visit(visit1)
schedule.add_visit(visit0)
schedule.add_visit(visit1)

visit_schedule = VisitSchedule(
name="visit_schedule",
offstudy_model="edc_offstudy.subjectoffstudy",
death_report_model="edc_metadata.deathreport",
)
visit_schedule = VisitSchedule(
name="visit_schedule",
offstudy_model="edc_offstudy.subjectoffstudy",
death_report_model="edc_metadata.deathreport",
)

visit_schedule.add_schedule(schedule)
visit_schedule.add_schedule(schedule)
return visit_schedule

0 comments on commit 50949cf

Please sign in to comment.