Skip to content

Commit

Permalink
Merge pull request #789 from openhealthcare/lab-test-bug-fix
Browse files Browse the repository at this point in the history
Lab test bug fix
  • Loading branch information
fredkingham committed Feb 2, 2020
2 parents 21113a1 + 7d7b3d4 commit ea8e6c7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
21 changes: 7 additions & 14 deletions intrahospital_api/apis/prod_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
@hospital_number ORDER BY last_updated DESC;"

ALL_DATA_QUERY_FOR_HOSPITAL_NUMBER = "SELECT * FROM {view} WHERE Patient_Number = \
@hospital_number AND date_inserted > @since ORDER BY date_inserted DESC;"
@hospital_number ORDER BY date_inserted DESC;"

ALL_DATA_QUERY_WITH_LAB_NUMBER = "SELECT * FROM {view} WHERE Patient_Number = \
@hospital_number AND date_inserted > @since and Result_ID = @lab_number ORDER BY date_inserted DESC;"
@hospital_number and Result_ID = @lab_number ORDER BY date_inserted DESC;"

ALL_DATA_QUERY_WITH_LAB_TEST_TYPE = "SELECT * FROM {view} WHERE Patient_Number = \
@hospital_number AND date_inserted > @since and OBR_exam_code_Text = @test_type ORDER BY date_inserted DESC;"
@hospital_number AND OBR_exam_code_Text = @test_type ORDER BY date_inserted DESC;"

ALL_DATA_SINCE = "SELECT * FROM {view} WHERE date_inserted > @since ORDER BY Patient_Number, date_inserted DESC;"

Expand Down Expand Up @@ -348,7 +348,6 @@ def __init__(self):
)

def execute_hospital_query(self, query, params=None):
result = []
with pytds.connect(
self.hospital_settings["ip_address"],
self.hospital_settings["database"],
Expand All @@ -361,12 +360,11 @@ def execute_hospital_query(self, query, params=None):
"Running upstream query {} {}".format(query, params)
)
cur.execute(query, params)
result.extend(cur.fetchmany(1000))
result = cur.fetchall()
logger.debug(result)
return result

def execute_trust_query(self, query, params=None):
result = []
with pytds.connect(
self.trust_settings["ip_address"],
self.trust_settings["database"],
Expand All @@ -379,10 +377,11 @@ def execute_trust_query(self, query, params=None):
"Running upstream query {} {}".format(query, params)
)
cur.execute(query, params)
result.extend(cur.fetchmany(1000))
result = cur.fetchall()
logger.debug(result)
return result


@property
def pathology_demographics_query(self):
return PATHOLOGY_DEMOGRAPHICS_QUERY.format(
Expand Down Expand Up @@ -454,16 +453,11 @@ def pathology_demographics(self, hospital_number):
return PathologyRow(rows[0]).get_demographics_dict()

def raw_data(self, hospital_number, lab_number=None, test_type=None):
""" not all data, I lied. Only the last year's
"""
db_date = datetime.date.today() - datetime.timedelta(365)

if lab_number:
return self.execute_trust_query(
self.all_data_query_for_lab_number,
params=dict(
hospital_number=hospital_number,
since=db_date,
lab_number=lab_number
)
)
Expand All @@ -472,14 +466,13 @@ def raw_data(self, hospital_number, lab_number=None, test_type=None):
self.all_data_query_for_lab_test_type,
params=dict(
hospital_number=hospital_number,
since=db_date,
test_type=test_type
)
)
else:
return self.execute_trust_query(
self.all_data_for_hospital_number_query,
params=dict(hospital_number=hospital_number, since=db_date)
params=dict(hospital_number=hospital_number)
)

@timing
Expand Down
11 changes: 5 additions & 6 deletions intrahospital_api/test/test_prod_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def test_execute_hospital_query_with_params(self, pytds):
api = self.get_api()
conn = pytds.connect().__enter__()
cursor = conn.cursor().__enter__()
cursor.fetchmany.return_value = ["some_results"]
cursor.fetchall.return_value = ["some_results"]
result = api.execute_hospital_query(
"some query", dict(hospital_number="1231222222")
)
Expand All @@ -449,20 +449,20 @@ def test_execute_hospital_query_with_params(self, pytds):
"some query",
dict(hospital_number="1231222222")
)
self.assertTrue(cursor.fetchmany.called)
self.assertTrue(cursor.fetchall.called)

@mock.patch('intrahospital_api.apis.prod_api.pytds')
def test_execute_hospital_query_without_params(self, pytds):
api = self.get_api()
conn = pytds.connect().__enter__()
cursor = conn.cursor().__enter__()
cursor.fetchmany.return_value = ["some_results"]
cursor.fetchall.return_value = ["some_results"]
result = api.execute_hospital_query("some query")
self.assertEqual(
result, ["some_results"]
)
cursor.execute.assert_called_once_with("some query", None)
self.assertTrue(cursor.fetchmany.called)
self.assertTrue(cursor.fetchall.called)

@mock.patch("intrahospital_api.apis.prod_api.datetime.date")
def test_raw_data(self, dt):
Expand All @@ -476,14 +476,13 @@ def test_raw_data(self, dt):

# make sure we query by the correct db date
expected_query = "SELECT * FROM some_view WHERE Patient_Number = \
@hospital_number AND date_inserted > @since ORDER BY date_inserted DESC;"
@hospital_number ORDER BY date_inserted DESC;"
self.assertEqual(
execute_query.call_args[0][0], expected_query
)
self.assertEqual(
execute_query.call_args[1]["params"], dict(
hospital_number="12312222",
since=date(2016, 10, 1)
)
)

Expand Down

0 comments on commit ea8e6c7

Please sign in to comment.