Skip to content

Commit ea24679

Browse files
dtisza1whytheplatypus
authored andcommitted
BLUEBUTTON-938 dpr switch in test pr-2 (#737)
* Handle blank fhir_id's and cleanup namings * Update DEFAULT_SAMPLE_FHIR_ID negative and tests * Squash tests and fix comments - Add tests for convert_crosswalks_to_synthetic() - Fix comments in test
1 parent 8b5eaf0 commit ea24679

File tree

7 files changed

+143
-52
lines changed

7 files changed

+143
-52
lines changed

apps/fhir/bluebutton/admin.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
from apps.fhir.bluebutton.models import Crosswalk, check_crosswalks, update_crosswalks
1+
from apps.fhir.bluebutton.models import Crosswalk, check_crosswalks, convert_crosswalks_to_synthetic
22
from django.contrib import admin
33
from waffle import switch_is_active
44

55

6-
def update(modeladmin, request, queryset):
6+
def convert(modeladmin, request, queryset):
77
''' NOTE: This function only used for the one-time
88
migration for DPR switch-over
99
'''
10-
update_crosswalks()
10+
# Note: Hash for allowed FHIR server passed in below:
11+
convert_crosswalks_to_synthetic("e40546d58a288cc6b973a62a8d1e5f1103f468f435011e28f5dc7b626de8e69e")
1112

1213

13-
update.short_description = "Update Crosswalks to negative ID values for DPR switch-over. NOTE: SPECIAL CASE USE!"
14+
convert.short_description = "Convert Crosswalks to negative ID values for DPR switch-over. NOTE: SPECIAL CASE USE!"
1415

1516

1617
class CrosswalkAdmin(admin.ModelAdmin):
@@ -42,9 +43,9 @@ def get_actions(self, request):
4243
current_state["synthetic"],
4344
current_state["real"])
4445

45-
actions[update.__name__] = (update,
46-
update.__name__,
47-
update.short_description + current_state_text)
46+
actions[convert.__name__] = (convert,
47+
convert.__name__,
48+
convert.short_description + current_state_text)
4849

4950
return actions
5051

apps/fhir/bluebutton/models.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Real fhir_id Manager subclass
1717
class RealCrosswalkManager(models.Manager):
1818
def get_queryset(self):
19-
return super().get_queryset().filter(~Q(fhir_id__startswith='-'))
19+
return super().get_queryset().filter(~Q(fhir_id__startswith='-') & ~Q(fhir_id=''))
2020

2121

2222
# Synthetic fhir_id Manager subclass
@@ -126,21 +126,24 @@ def __init__(self, req_response=Response):
126126
self.__dict__[k] = v
127127

128128

129-
def update_crosswalks(*args, **kwargs):
129+
def convert_crosswalks_to_synthetic(allowed_fhir_url_hash, *args, **kwargs):
130130
''' NOTE: This function only used for the one-time
131131
migration for DPR switch-over
132132
133133
Hash for local testing
134-
ALLOWED_FHIR_URL_HASH = "fae87b239c5e8821899b46cff4ab2be7767b3c5c009c322c08f6ce59677a653b"
134+
allowed_fhir_url_hash = "fae87b239c5e8821899b46cff4ab2be7767b3c5c009c322c08f6ce59677a653b"
135+
Hash for DPR
136+
allowed_fhir_url_hash = "e40546d58a288cc6b973a62a8d1e5f1103f468f435011e28f5dc7b626de8e69e"
135137
'''
136-
# Hash for DPR
137-
ALLOWED_FHIR_URL_HASH = "e40546d58a288cc6b973a62a8d1e5f1103f468f435011e28f5dc7b626de8e69e"
138138

139+
''' Note: The following selects crosswalks with real/positive IDs
140+
via the RealCrosswalkManager manager. '''
139141
crosswalks = Crosswalk.real_objects.all()
142+
140143
for crosswalk in crosswalks:
141144
fhir_url = crosswalk.fhir_source.fhir_url
142145
fhir_url_hash = hashlib.sha256(str(fhir_url).encode('utf-8')).hexdigest()
143-
if fhir_url_hash == ALLOWED_FHIR_URL_HASH:
146+
if fhir_url_hash == allowed_fhir_url_hash:
144147
crosswalk.fhir_id = "-" + crosswalk.fhir_id
145148
# Use the parent save() to avoid user_id_hash updating
146149
super(Crosswalk, crosswalk).save()
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from apps.test import BaseApiTest
2+
3+
from ..models import Crosswalk, check_crosswalks, convert_crosswalks_to_synthetic
4+
from ...server.models import ResourceRouter
5+
6+
7+
class TestCrosswalkConvertSynthetic(BaseApiTest):
8+
def create_test_crosswalks(self, fhir_id_type, start, end):
9+
10+
for user_num in range(start, end):
11+
# Create a user
12+
user = self._create_user(fhir_id_type + str(user_num), 'password',
13+
first_name='John',
14+
last_name='Last' + str(user_num),
15+
email='johnlast' + str(user_num) + '@smith.net')
16+
17+
# created a default resource router
18+
fs = ResourceRouter.objects.create(name="Main Server",
19+
fhir_url="http://localhost:8000/fhir/",
20+
shard_by="Patient",
21+
server_search_expiry=1800)
22+
23+
if fhir_id_type == 'positive':
24+
fhir_id = "20000000000000" + str(user_num)
25+
elif fhir_id_type == 'negative':
26+
fhir_id = "-20000000000000" + str(user_num)
27+
else:
28+
fhir_id = ""
29+
30+
Crosswalk.objects.create(user=user,
31+
fhir_source=fs,
32+
fhir_id=fhir_id)
33+
34+
def test_crosswalks_convert_to_synth(self):
35+
'''
36+
Test crosswalk conversions with 9x total crosswalks of different types.
37+
'''
38+
# Create 4x real/positive FHIR_ID users/crosswalks
39+
self.create_test_crosswalks('positive', 1, 5)
40+
# Create 3x synth/negative FHIR_ID users/crosswalks
41+
self.create_test_crosswalks('negative', 6, 9)
42+
# Create 2x blank FHIR_ID="" users/crosswalks
43+
self.create_test_crosswalks('blank', 10, 12)
44+
45+
ret = check_crosswalks()
46+
# Verify BEFORE SYNTH count == 3:
47+
self.assertEqual(ret['synthetic'], 3)
48+
# Verify BEFORE REAL count == 4:
49+
self.assertEqual(ret['real'], 4)
50+
# Verify BEFORE BLANK count == 2:
51+
blank_count = Crosswalk.objects.filter(fhir_id='').count()
52+
self.assertEqual(blank_count, 2)
53+
# Verify BEFORE total count == 9:
54+
total_count = Crosswalk.objects.all().count()
55+
self.assertEqual(total_count, 9)
56+
57+
'''
58+
Test that the conversion DOES NOT WORK when the allowed FHIR server url
59+
hash does not match the target FHIR server.
60+
'''
61+
convert_crosswalks_to_synthetic("INVALID-ALLOWED-FHIR-URL-HASH")
62+
63+
ret = check_crosswalks()
64+
# Verify no change SYNTH count == 3:
65+
self.assertEqual(ret['synthetic'], 3)
66+
# Verify no change REAL count == 4:
67+
self.assertEqual(ret['real'], 4)
68+
# Verify no change BLANK count == 2:
69+
blank_count = Crosswalk.objects.filter(fhir_id='').count()
70+
self.assertEqual(blank_count, 2)
71+
# Verify no change total count == 9:
72+
total_count = Crosswalk.objects.all().count()
73+
self.assertEqual(total_count, 9)
74+
75+
'''
76+
Test that the conversion DOES WORK when the allowed FHIR server url
77+
hash IS VALID for fhir_url="http://localhost:8000/fhir/",
78+
'''
79+
convert_crosswalks_to_synthetic("79f330b587728a2a607775e713161fd3b31d306091350c957e1ef4b71231ccec")
80+
81+
ret = check_crosswalks()
82+
# Verify AFTER SYNTH count == 7:
83+
self.assertEqual(ret['synthetic'], 7)
84+
# Verify AFTER REAL count == 0:
85+
self.assertEqual(ret['real'], 0)
86+
# Verify AFTER BLANK count == 2:
87+
blank_count = Crosswalk.objects.filter(fhir_id='').count()
88+
self.assertEqual(blank_count, 2)
89+
# Verify AFTER total count == 9.:
90+
total_count = Crosswalk.objects.all().count()
91+
self.assertEqual(total_count, 9)

0 commit comments

Comments
 (0)