6
6
from django .contrib .sites .models import Site
7
7
from django .test import TestCase , override_settings , tag
8
8
from edc_protocol .research_protocol_config import ResearchProtocolConfig
9
- from edc_sites .site import sites as site_sites
10
9
from edc_utils import get_utcnow
11
10
from edc_visit_schedule .site_visit_schedules import site_visit_schedules
12
11
from faker import Faker
13
12
from model_bakery import baker
14
13
15
- from consent_app .models import CrfOne , SubjectVisit
14
+ from consent_app .models import CrfOne , SubjectConsent , SubjectVisit
16
15
from consent_app .visit_schedules import get_visit_schedule
17
16
from edc_consent .field_mixins import IdentityFieldsMixinError
18
17
from edc_consent .site_consents import site_consents
@@ -51,6 +50,8 @@ def setUp(self):
51
50
end = self .study_open_datetime + timedelta (days = 100 ),
52
51
version = "2.0" ,
53
52
)
53
+
54
+ self .consent_v3_start_date = self .study_open_datetime + timedelta (days = 101 )
54
55
self .consent_v3 = consent_factory (
55
56
proxy_model = "consent_app.subjectconsentv3" ,
56
57
start = self .study_open_datetime + timedelta (days = 101 ),
@@ -63,6 +64,7 @@ def setUp(self):
63
64
site_consents .register (self .consent_v3 )
64
65
self .dob = self .study_open_datetime - relativedelta (years = 25 )
65
66
67
+ @tag ("1" )
66
68
def test_encryption (self ):
67
69
subject_consent = baker .make_recipe (
68
70
"consent_app.subjectconsentv1" ,
@@ -72,6 +74,7 @@ def test_encryption(self):
72
74
)
73
75
self .assertEqual (subject_consent .first_name , "ERIK" )
74
76
77
+ @tag ("1" )
75
78
def test_gets_subject_identifier (self ):
76
79
"""Asserts a blank subject identifier is set to the
77
80
subject_identifier_as_pk.
@@ -89,6 +92,7 @@ def test_gets_subject_identifier(self):
89
92
self .assertIsNotNone (consent .subject_identifier )
90
93
self .assertNotEqual (consent .subject_identifier , consent .subject_identifier_as_pk )
91
94
95
+ @tag ("1" )
92
96
def test_subject_has_current_consent (self ):
93
97
subject_identifier = "123456789"
94
98
identity = "987654321"
@@ -125,7 +129,12 @@ def test_subject_has_current_consent(self):
125
129
)
126
130
self .assertEqual (subject_consent .version , "2.0" )
127
131
128
- def test_model_updates (self ):
132
+ @tag ("1" )
133
+ def test_model_updates_version_according_to_cdef_used (self ):
134
+ """Asserts the consent model finds the cdef and updates
135
+ column `version` using to the version number on the
136
+ cdef.
137
+ """
129
138
subject_identifier = "123456789"
130
139
identity = "987654321"
131
140
consent = baker .make_recipe (
@@ -156,20 +165,29 @@ def test_model_updates(self):
156
165
)
157
166
self .assertEqual (consent .version , "3.0" )
158
167
159
- def test_model_updates2 (self ):
168
+ @tag ("1" )
169
+ def test_model_updates_version_according_to_cdef_used2 (self ):
170
+ """Asserts the consent model finds the `cdef` and updates
171
+ column `version` using to the version number on the
172
+ `cdef`.
173
+
174
+ Note: we get the `model_cls` by looking up the `cdef` first.
175
+ """
160
176
subject_identifier = "123456789"
161
177
identity = "987654321"
178
+ cdef = site_consents .get_consent_definition (report_datetime = self .study_open_datetime )
162
179
consent = baker .make_recipe (
163
- "consent_app.subjectconsentv1" ,
180
+ cdef . model ,
164
181
subject_identifier = subject_identifier ,
165
182
identity = identity ,
166
183
confirm_identity = identity ,
167
184
consent_datetime = self .study_open_datetime ,
168
185
dob = get_utcnow () - relativedelta (years = 25 ),
169
186
)
170
187
self .assertEqual (consent .version , "1.0" )
188
+ cdef = site_consents .get_consent_definition (report_datetime = self .study_open_datetime )
171
189
consent = baker .make_recipe (
172
- "consent_app.subjectconsentv3" ,
190
+ cdef . model ,
173
191
subject_identifier = subject_identifier ,
174
192
identity = identity ,
175
193
confirm_identity = identity ,
@@ -178,7 +196,11 @@ def test_model_updates2(self):
178
196
)
179
197
self .assertEqual (consent .version , "3.0" )
180
198
181
- def test_model_updates_or_first_based_on_date (self ):
199
+ def test_model_correctly_gets_v3_by_date (self ):
200
+ """Asserts that a consent model instance created when the
201
+ current date is within the V3 validity period correctly
202
+ has `instance.version == 3.0`.
203
+ """
182
204
traveller = time_machine .travel (self .study_open_datetime + timedelta (days = 110 ))
183
205
traveller .start ()
184
206
subject_identifier = "123456789"
@@ -194,11 +216,19 @@ def test_model_updates_or_first_based_on_date(self):
194
216
self .assertEqual (consent .version , "3.0" )
195
217
196
218
def test_model_updates_from_v1_to_v2 (self ):
197
- traveller = time_machine .travel (self .study_open_datetime )
198
- traveller .start ()
219
+ """Assert, for a single participant, a second consent model
220
+ instance submitted within the v2 validity period has
221
+ version == 2.0.
222
+
223
+ Also note that there are now 2 instances of the consent
224
+ model for this participant.
225
+ """
199
226
subject_identifier = "123456789"
200
227
identity = "987654321"
201
228
229
+ # travel to V1 validity period
230
+ traveller = time_machine .travel (self .study_open_datetime )
231
+ traveller .start ()
202
232
cdef = site_consents .get_consent_definition (report_datetime = get_utcnow ())
203
233
subject_consent = baker .make_recipe (
204
234
cdef .model ,
@@ -208,15 +238,18 @@ def test_model_updates_from_v1_to_v2(self):
208
238
consent_datetime = get_utcnow (),
209
239
dob = get_utcnow () - relativedelta (years = 25 ),
210
240
)
241
+ self .assertEqual (subject_consent .version , "2.0" )
211
242
self .assertEqual (subject_consent .subject_identifier , subject_identifier )
212
243
self .assertEqual (subject_consent .identity , identity )
213
244
self .assertEqual (subject_consent .confirm_identity , identity )
214
245
self .assertEqual (subject_consent .version , cdef .version )
215
246
self .assertEqual (subject_consent .consent_definition_name , cdef .name )
216
247
traveller .stop ()
248
+
249
+ # travel to V2 validity period
250
+ # create second consent for the same individual
217
251
traveller = time_machine .travel (self .study_open_datetime + timedelta (days = 51 ))
218
252
traveller .start ()
219
-
220
253
cdef = site_consents .get_consent_definition (report_datetime = get_utcnow ())
221
254
subject_consent = cdef .model_cls (
222
255
subject_identifier = subject_identifier ,
@@ -227,80 +260,13 @@ def test_model_updates_from_v1_to_v2(self):
227
260
)
228
261
subject_consent .save ()
229
262
subject_consent .refresh_from_db ()
263
+ self .assertEqual (subject_consent .version , "2.0" )
230
264
self .assertEqual (subject_consent .subject_identifier , subject_identifier )
231
265
self .assertEqual (subject_consent .identity , identity )
232
266
self .assertEqual (subject_consent .confirm_identity , identity )
233
267
self .assertEqual (subject_consent .consent_definition_name , cdef .name )
234
268
235
- def test_v3_extends_v2_end_date_up_to_v3_consent_datetime (self ):
236
- # TODO: is this a valid test? How does it fill in data from
237
- # the previous consent?
238
- traveller = time_machine .travel (self .study_open_datetime )
239
- traveller .start ()
240
- subject_identifier = "123456789"
241
- identity = "987654321"
242
-
243
- # consent version 1
244
- cdef = site_consents .get_consent_definition (report_datetime = get_utcnow ())
245
- subject_consent = baker .make_recipe (
246
- cdef .model ,
247
- subject_identifier = subject_identifier ,
248
- identity = identity ,
249
- confirm_identity = identity ,
250
- consent_datetime = get_utcnow (),
251
- dob = get_utcnow () - relativedelta (years = 25 ),
252
- )
253
- self .assertEqual (subject_consent .consent_definition_name , cdef .name )
254
- self .assertEqual (subject_consent .version , "1.0" )
255
- traveller .stop ()
256
-
257
- # consent version 2
258
- traveller = time_machine .travel (self .study_open_datetime + timedelta (days = 51 ))
259
- traveller .start ()
260
- cdef = site_consents .get_consent_definition (report_datetime = get_utcnow ())
261
- subject_consent = baker .make_recipe (
262
- cdef .model ,
263
- subject_identifier = subject_identifier ,
264
- consent_datetime = get_utcnow (),
265
- dob = get_utcnow () - relativedelta (years = 25 ),
266
- )
267
- self .assertEqual (subject_consent .consent_definition_name , cdef .name )
268
- self .assertEqual (subject_consent .version , "2.0" )
269
- traveller .stop ()
270
-
271
- # consent version 3.0
272
- traveller = time_machine .travel (cdef .end + relativedelta (days = 5 ))
273
- traveller .start ()
274
- cdef = site_consents .get_consent_definition (report_datetime = get_utcnow ())
275
- subject_consent = baker .make_recipe (
276
- cdef .model ,
277
- subject_identifier = subject_identifier ,
278
- consent_datetime = get_utcnow (),
279
- dob = get_utcnow () - relativedelta (years = 25 ),
280
- )
281
- self .assertEqual (subject_consent .consent_definition_name , cdef .name )
282
- self .assertEqual (subject_consent .version , "3.0" )
283
- self .assertEqual (cdef .version , "3.0" )
284
-
285
- # get cdef for 3.0
286
- cdef = site_consents .get_consent_definition (
287
- report_datetime = get_utcnow (), site = site_sites .get (subject_consent .site .id )
288
- )
289
- self .assertEqual (cdef .version , "3.0" )
290
-
291
- # use cdef-3.0 to get subject_consent 3.0
292
- subject_consent = cdef .get_consent_for (
293
- subject_identifier = subject_identifier , report_datetime = get_utcnow ()
294
- )
295
- self .assertEqual (subject_consent .version , "3.0" )
296
-
297
- # use cdef-3.0 to get subject_consent 2.0 showing that the lower bound
298
- # of a cdef that updates is extended to return a 2.0 consent
299
- subject_consent = cdef .get_consent_for (
300
- subject_identifier = subject_identifier ,
301
- report_datetime = cdef .start - relativedelta (days = 1 ),
302
- )
303
- self .assertEqual (subject_consent .version , "2.0" )
269
+ self .assertEqual (SubjectConsent .objects .filter (identity , identity ).count (), 2 )
304
270
305
271
def test_first_consent_is_v2 (self ):
306
272
traveller = time_machine .travel (self .study_open_datetime + timedelta (days = 51 ))
@@ -619,10 +585,16 @@ def test_save_crf_with_consent_end_shortened_to_before_existing_subject_visit_ra
619
585
subject_identifier = subject_identifier ,
620
586
report_datetime = datetime_within_consent_v3 ,
621
587
)
588
+ except NotConsentedError :
589
+ self .fail ("NotConsentedError unexpectedly raised" )
590
+ try :
622
591
crf_one .save ()
623
592
except NotConsentedError :
624
593
self .fail ("NotConsentedError unexpectedly raised" )
625
594
595
+ crf_one .report_datetime = get_utcnow ()
596
+ crf_one .save ()
597
+
626
598
# now try to save CRF at within v4 period
627
599
self .assertRaises (
628
600
NotConsentedError ,
0 commit comments