Skip to content

Commit 19f964c

Browse files
authored
Merge pull request #176 from smart-on-fhir/mikix/patient-prop
client: fix import in patient property code
2 parents 9967711 + c0c838c commit 19f964c

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

fhirclient/client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from .server import FHIRServer, FHIRUnauthorizedException, FHIRNotFoundException
99

10-
__version__ = '4.2.0'
10+
__version__ = '4.3.0'
1111
__author__ = 'SMART Platforms Team'
1212
__license__ = 'APACHE2'
1313
__copyright__ = "Copyright 2017 Boston Children's Hospital"
@@ -171,16 +171,16 @@ def _handle_launch_context(self, ctx):
171171
@property
172172
def patient(self):
173173
if self._patient is None and self.patient_id is not None and self.ready:
174-
import models.patient
174+
from fhirclient.models.patient import Patient
175175
try:
176176
logger.debug("SMART: Attempting to read Patient {0}".format(self.patient_id))
177-
self._patient = models.patient.Patient.read(self.patient_id, self.server)
178-
except FHIRUnauthorizedException as e:
177+
self._patient = Patient.read(self.patient_id, self.server)
178+
except FHIRUnauthorizedException:
179179
if self.reauthorize():
180180
logger.debug("SMART: Attempting to read Patient {0} after reauthorizing"
181181
.format(self.patient_id))
182-
self._patient = models.patient.Patient.read(self.patient_id, self.server)
183-
except FHIRNotFoundException as e:
182+
self._patient = Patient.read(self.patient_id, self.server)
183+
except FHIRNotFoundException:
184184
logger.warning("SMART: Patient with id {0} not found".format(self.patient_id))
185185
self.patient_id = None
186186
self.save_state()

tests/client_test.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import unittest
2+
from unittest import mock
23

34
from fhirclient.client import FHIRClient
5+
from fhirclient.server import FHIRNotFoundException, FHIRUnauthorizedException
6+
7+
# Smallest valid-but-fake client state
8+
MIN_STATE = {
9+
"server": {"base_uri": "http://example.com/fhir"},
10+
}
411

512

613
class TestClient(unittest.TestCase):
@@ -39,3 +46,54 @@ def test_load_from_state(self):
3946
client.from_state({'app_id': 'NewID', 'server': state['server']})
4047
self.assertEqual('NewID', client.app_id)
4148
self.assertEqual('LaunchToken', client.launch_token)
49+
50+
@mock.patch("fhirclient.models.patient.Patient.read")
51+
def test_patient_property_happy_path(self, mock_read):
52+
save_func = mock.MagicMock()
53+
54+
# Verify that we gracefully handle no patient_id being given
55+
client = FHIRClient(state=MIN_STATE, save_func=save_func)
56+
self.assertIsNone(client.patient)
57+
self.assertEqual(mock_read.call_count, 0)
58+
self.assertEqual(save_func.call_count, 0)
59+
60+
# Verify we expose the provided patient ID as a Patient object
61+
client = FHIRClient(state={"patient_id": "P123", **MIN_STATE}, save_func=save_func)
62+
self.assertIsNotNone(client.patient)
63+
self.assertEqual(mock_read.call_count, 1)
64+
self.assertEqual(mock_read.call_args, mock.call("P123", client.server))
65+
self.assertEqual(save_func.call_count, 1)
66+
67+
@mock.patch("fhirclient.models.patient.Patient.read")
68+
@mock.patch("fhirclient.client.FHIRClient.reauthorize")
69+
def test_patient_property_unauthorized(self, mock_reauthorize, mock_read):
70+
"""We should attempt to reauthorize and re-request the patient"""
71+
72+
client = FHIRClient(state={"patient_id": "P123", **MIN_STATE})
73+
74+
# First try with a failed re-authorize
75+
mock_read.side_effect = FHIRUnauthorizedException("response")
76+
mock_reauthorize.return_value = False
77+
self.assertIsNone(client.patient)
78+
self.assertEqual(mock_read.call_count, 1)
79+
self.assertEqual(mock_reauthorize.call_count, 1)
80+
81+
# Then with a successful re-authorize
82+
mock_read.reset_mock()
83+
mock_read.side_effect = [FHIRUnauthorizedException("response"), mock.MagicMock()]
84+
mock_reauthorize.reset_mock()
85+
mock_reauthorize.return_value = True
86+
self.assertIsNotNone(client.patient)
87+
self.assertEqual(mock_read.call_count, 2)
88+
self.assertEqual(mock_reauthorize.call_count, 1)
89+
90+
@mock.patch("fhirclient.models.patient.Patient.read")
91+
def test_patient_property_not_found(self, mock_read):
92+
"""We should attempt to reauthorize and re-request the patient"""
93+
mock_read.side_effect = FHIRNotFoundException("response")
94+
95+
client = FHIRClient(state={"patient_id": "P123", **MIN_STATE})
96+
self.assertEqual(client.patient_id, "P123") # sanity check before we start
97+
98+
self.assertIsNone(client.patient)
99+
self.assertIsNone(client.patient_id) # we clear out the patient id

0 commit comments

Comments
 (0)