Skip to content

Commit

Permalink
BLUEBUTTON-938 dpr switch in test pr-2 (#737)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dtisza1 authored and whytheplatypus committed Jul 19, 2019
1 parent 8b5eaf0 commit ea24679
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 52 deletions.
15 changes: 8 additions & 7 deletions apps/fhir/bluebutton/admin.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from apps.fhir.bluebutton.models import Crosswalk, check_crosswalks, update_crosswalks
from apps.fhir.bluebutton.models import Crosswalk, check_crosswalks, convert_crosswalks_to_synthetic
from django.contrib import admin
from waffle import switch_is_active


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


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


class CrosswalkAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -42,9 +43,9 @@ def get_actions(self, request):
current_state["synthetic"],
current_state["real"])

actions[update.__name__] = (update,
update.__name__,
update.short_description + current_state_text)
actions[convert.__name__] = (convert,
convert.__name__,
convert.short_description + current_state_text)

return actions

Expand Down
15 changes: 9 additions & 6 deletions apps/fhir/bluebutton/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Real fhir_id Manager subclass
class RealCrosswalkManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(~Q(fhir_id__startswith='-'))
return super().get_queryset().filter(~Q(fhir_id__startswith='-') & ~Q(fhir_id=''))


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


def update_crosswalks(*args, **kwargs):
def convert_crosswalks_to_synthetic(allowed_fhir_url_hash, *args, **kwargs):
''' NOTE: This function only used for the one-time
migration for DPR switch-over
Hash for local testing
ALLOWED_FHIR_URL_HASH = "fae87b239c5e8821899b46cff4ab2be7767b3c5c009c322c08f6ce59677a653b"
allowed_fhir_url_hash = "fae87b239c5e8821899b46cff4ab2be7767b3c5c009c322c08f6ce59677a653b"
Hash for DPR
allowed_fhir_url_hash = "e40546d58a288cc6b973a62a8d1e5f1103f468f435011e28f5dc7b626de8e69e"
'''
# Hash for DPR
ALLOWED_FHIR_URL_HASH = "e40546d58a288cc6b973a62a8d1e5f1103f468f435011e28f5dc7b626de8e69e"

''' Note: The following selects crosswalks with real/positive IDs
via the RealCrosswalkManager manager. '''
crosswalks = Crosswalk.real_objects.all()

for crosswalk in crosswalks:
fhir_url = crosswalk.fhir_source.fhir_url
fhir_url_hash = hashlib.sha256(str(fhir_url).encode('utf-8')).hexdigest()
if fhir_url_hash == ALLOWED_FHIR_URL_HASH:
if fhir_url_hash == allowed_fhir_url_hash:
crosswalk.fhir_id = "-" + crosswalk.fhir_id
# Use the parent save() to avoid user_id_hash updating
super(Crosswalk, crosswalk).save()
Expand Down
91 changes: 91 additions & 0 deletions apps/fhir/bluebutton/tests/test_crosswalks_convert_to_synthetic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from apps.test import BaseApiTest

from ..models import Crosswalk, check_crosswalks, convert_crosswalks_to_synthetic
from ...server.models import ResourceRouter


class TestCrosswalkConvertSynthetic(BaseApiTest):
def create_test_crosswalks(self, fhir_id_type, start, end):

for user_num in range(start, end):
# Create a user
user = self._create_user(fhir_id_type + str(user_num), 'password',
first_name='John',
last_name='Last' + str(user_num),
email='johnlast' + str(user_num) + '@smith.net')

# created a default resource router
fs = ResourceRouter.objects.create(name="Main Server",
fhir_url="http://localhost:8000/fhir/",
shard_by="Patient",
server_search_expiry=1800)

if fhir_id_type == 'positive':
fhir_id = "20000000000000" + str(user_num)
elif fhir_id_type == 'negative':
fhir_id = "-20000000000000" + str(user_num)
else:
fhir_id = ""

Crosswalk.objects.create(user=user,
fhir_source=fs,
fhir_id=fhir_id)

def test_crosswalks_convert_to_synth(self):
'''
Test crosswalk conversions with 9x total crosswalks of different types.
'''
# Create 4x real/positive FHIR_ID users/crosswalks
self.create_test_crosswalks('positive', 1, 5)
# Create 3x synth/negative FHIR_ID users/crosswalks
self.create_test_crosswalks('negative', 6, 9)
# Create 2x blank FHIR_ID="" users/crosswalks
self.create_test_crosswalks('blank', 10, 12)

ret = check_crosswalks()
# Verify BEFORE SYNTH count == 3:
self.assertEqual(ret['synthetic'], 3)
# Verify BEFORE REAL count == 4:
self.assertEqual(ret['real'], 4)
# Verify BEFORE BLANK count == 2:
blank_count = Crosswalk.objects.filter(fhir_id='').count()
self.assertEqual(blank_count, 2)
# Verify BEFORE total count == 9:
total_count = Crosswalk.objects.all().count()
self.assertEqual(total_count, 9)

'''
Test that the conversion DOES NOT WORK when the allowed FHIR server url
hash does not match the target FHIR server.
'''
convert_crosswalks_to_synthetic("INVALID-ALLOWED-FHIR-URL-HASH")

ret = check_crosswalks()
# Verify no change SYNTH count == 3:
self.assertEqual(ret['synthetic'], 3)
# Verify no change REAL count == 4:
self.assertEqual(ret['real'], 4)
# Verify no change BLANK count == 2:
blank_count = Crosswalk.objects.filter(fhir_id='').count()
self.assertEqual(blank_count, 2)
# Verify no change total count == 9:
total_count = Crosswalk.objects.all().count()
self.assertEqual(total_count, 9)

'''
Test that the conversion DOES WORK when the allowed FHIR server url
hash IS VALID for fhir_url="http://localhost:8000/fhir/",
'''
convert_crosswalks_to_synthetic("79f330b587728a2a607775e713161fd3b31d306091350c957e1ef4b71231ccec")

ret = check_crosswalks()
# Verify AFTER SYNTH count == 7:
self.assertEqual(ret['synthetic'], 7)
# Verify AFTER REAL count == 0:
self.assertEqual(ret['real'], 0)
# Verify AFTER BLANK count == 2:
blank_count = Crosswalk.objects.filter(fhir_id='').count()
self.assertEqual(blank_count, 2)
# Verify AFTER total count == 9.:
total_count = Crosswalk.objects.all().count()
self.assertEqual(total_count, 9)
Loading

0 comments on commit ea24679

Please sign in to comment.