Skip to content

Commit

Permalink
WIP overlap tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Mar 26, 2024
1 parent b2de290 commit 161b1b4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
9 changes: 8 additions & 1 deletion consent_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class Meta:


class SubjectConsentV2(SubjectConsent):

on_site = CurrentSiteByCdefManager()
objects = ConsentObjectsByCdefManager()

Expand All @@ -103,6 +102,14 @@ class Meta:
proxy = True


class SubjectConsentV4(SubjectConsent):
on_site = CurrentSiteByCdefManager()
objects = ConsentObjectsByCdefManager()

class Meta:
proxy = True


class SubjectConsentUpdateToV3(SubjectConsent):
class Meta:
proxy = True
Expand Down
3 changes: 3 additions & 0 deletions edc_consent/site_consents.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def register(
self.registry.update({cdef.name: cdef})
self.loaded = True

def unregister(self, cdef: ConsentDefinition) -> None:
self.registry.pop(cdef.name, None)

def get_registry_display(self):
cdefs = sorted(list(self.registry.values()), key=lambda x: x.version)
return "', '".join([cdef.display_name for cdef in cdefs])
Expand Down
10 changes: 5 additions & 5 deletions edc_consent/tests/tests/test_consent_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ def setUp(self):
self.study_close_datetime = ResearchProtocolConfig().study_close_datetime

self.consent_v1 = self.consent_factory(
proxy_model="consent_app.subjectconsentv1",
start=self.study_open_datetime,
end=self.study_open_datetime + timedelta(days=50),
model="consent_app.subjectconsentv1",
version="1.0",
)

self.consent_v2 = self.consent_factory(
proxy_model="consent_app.subjectconsentv2",
start=self.study_open_datetime + timedelta(days=51),
end=self.study_open_datetime + timedelta(days=100),
model="consent_app.subjectconsentv2",
version="2.0",
)
self.consent_v3 = self.consent_factory(
proxy_model="consent_app.subjectconsentv3",
start=self.study_open_datetime + timedelta(days=101),
end=self.study_open_datetime + timedelta(days=150),
version="3.0",
model="consent_app.subjectconsentv3",
updates=self.consent_v2,
)
site_consents.register(self.consent_v1)
Expand All @@ -92,8 +92,8 @@ def consent_factory(**kwargs):
age_max=kwargs.get("age_max", 64),
age_is_adult=kwargs.get("age_is_adult", 18),
)
model = kwargs.get("model", "consent_app.subjectconsentv1")
consent_definition = ConsentDefinition(model, **options)
proxy_model = kwargs.get("proxy_model", "consent_app.subjectconsentv1")
consent_definition = ConsentDefinition(proxy_model, **options)
return consent_definition

def cleaned_data(self, **kwargs):
Expand Down
46 changes: 24 additions & 22 deletions edc_consent/tests/tests/test_consent_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ def test_saving_with_date_past_any_consent_period_without_consent_raises(self):
self.fail("NotConsentedError unexpectedly raised")
traveller.stop()

@tag("1")
@tag("2")
def test_save_crf_with_consent_end_shortened_to_before_existing_subject_visit_raises(
self,
):
Expand Down Expand Up @@ -576,14 +576,13 @@ def test_save_crf_with_consent_end_shortened_to_before_existing_subject_visit_ra
schedule_name=schedule.name,
)
subject_visit_1.save()
subject_visit_2 = SubjectVisit.objects.create(
report_datetime=get_utcnow() + relativedelta(days=20),
subject_identifier=subject_identifier,
visit_schedule_name=visit_schedule.name,
schedule_name=schedule.name,
)
subject_visit_2.save()
traveller.stop()
# subject_visit_2 = SubjectVisit.objects.create(
# report_datetime=get_utcnow() + relativedelta(days=20),
# subject_identifier=subject_identifier,
# visit_schedule_name=visit_schedule.name,
# schedule_name=schedule.name,
# )
# subject_visit_2.save()

# cut short v3 validity period, and introduce new v4 consent definition,
updated_v3_end_datetime = datetime_within_consent_v3 + relativedelta(days=1)
Expand All @@ -594,22 +593,25 @@ def test_save_crf_with_consent_end_shortened_to_before_existing_subject_visit_ra
self.assertEqual(site_consents.registry[cdef_v3.name].updated_by, "4.0")
self.assertEqual(site_consents.registry[cdef_v3.name].updated_by, cdef_v3.updated_by)

consent_factory(
model="consent_app.subjectconsentv3",
consent_v4 = consent_factory(
proxy_model="consent_app.subjectconsentv4",
start=cdef_v3.end + relativedelta(days=1),
end=self.study_open_datetime + timedelta(days=150),
version="4.0",
updates=(self.consent_v3, "consent_app.subjectconsentupdatev3"),
)
datetime_within_consent_v4 = cdef_v3.end + relativedelta(days=20)
cdef_v4 = site_consents.get_consent_definition(
report_datetime=datetime_within_consent_v4
updates=self.consent_v3,
)

site_consents.unregister(self.consent_v3)
site_consents.register(self.consent_v3, updated_by=consent_v4)
site_consents.register(consent_v4)

traveller.stop()
traveller = time_machine.travel(cdef_v3.end + relativedelta(days=20))
traveller.start()
cdef_v4 = site_consents.get_consent_definition(report_datetime=get_utcnow())
self.assertEqual(cdef_v4.version, "4.0")
schedule.consent_definitions = [cdef_v1, cdef_v2, cdef_v3, cdef_v4]

traveller = time_machine.travel(datetime_within_consent_v4)
traveller.start()
# try saving CRF within already consented (v3) period
try:
crf_one = CrfOne.objects.create(
Expand All @@ -621,13 +623,13 @@ def test_save_crf_with_consent_end_shortened_to_before_existing_subject_visit_ra
except NotConsentedError:
self.fail("NotConsentedError unexpectedly raised")

# now try to save CRF at second visit (was within v3 period, now within v4)
# now try to save CRF at within v4 period
self.assertRaises(
NotConsentedError,
CrfOne.objects.create,
subject_visit=subject_visit_2,
subject_visit=subject_visit_1,
subject_identifier=subject_identifier,
report_datetime=datetime_within_consent_v4,
report_datetime=get_utcnow(),
)

# consent v4 and try again
Expand All @@ -636,7 +638,7 @@ def test_save_crf_with_consent_end_shortened_to_before_existing_subject_visit_ra
subject_identifier=subject_identifier,
identity=identity,
confirm_identity=identity,
consent_datetime=datetime_within_consent_v4,
consent_datetime=get_utcnow(),
dob=get_utcnow() - relativedelta(years=25),
)
self.assertEqual(subject_consent.consent_definition_name, cdef_v4.name)
Expand Down

0 comments on commit 161b1b4

Please sign in to comment.