Skip to content

Commit

Permalink
add unit test and correct spelling of opal serialiser
Browse files Browse the repository at this point in the history
  • Loading branch information
fredkingham committed Mar 30, 2016
1 parent 225a4e8 commit 563450c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
3 changes: 2 additions & 1 deletion gloss/import_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def process_message(self):
admission_diagnosis=self.pv2.admission_diagnosis,
)]


class InpatientDischarge(InpatientAdmit):
message_type = u"ADT"
trigger_event = "A03"
Expand Down Expand Up @@ -241,6 +240,8 @@ def get_obx_dict(obxs, comments):
comments=comments.get(obxs.obx.set_id, None)
)



def get_comments(ntes):
set_id_to_comment = defaultdict(list)
for nte_package in ntes:
Expand Down
6 changes: 3 additions & 3 deletions gloss/serialisers/opal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from twisted.logger import Logger


class OpalJSONSerializer(json.JSONEncoder):
class OpalJSONSerialiser(json.JSONEncoder):
""" Encodes a dictionary to json in the format that OPAL likes it
"""
def default(self, o):
Expand All @@ -15,15 +15,15 @@ def default(self, o):
return datetime.datetime.combine(
o, datetime.datetime.min.time()
).strftime(settings.DATE_FORMAT)
super(OpalJSONSerializer, self).default(o)
super(OpalJSONSerialiser, self).default(o)


def send_to_opal(message_container, end_point):
""" sends a message to an opal application
"""
as_dict = message_container.to_dict()
response = requests.post(
end_point, json=json.dumps(as_dict, cls=OpalJSONSerializer)
end_point, json=json.dumps(as_dict, cls=OpalJSONSerialiser)
)

if response.status_code > 300:
Expand Down
35 changes: 35 additions & 0 deletions gloss/tests/test_opal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from gloss.tests.core import GlossTestCase
from mock import patch, MagicMock
from gloss.serialisers.opal import send_to_opal, OpalJSONSerialiser
from datetime import datetime, date
import json


class OpalTestCase(GlossTestCase):
def test_json_serialiser(self):
as_dict = {
"a": datetime(2000, 2, 1, 10, 20),
"b": date(2001, 3, 4)
}
found = json.loads(json.dumps(as_dict, cls=OpalJSONSerialiser))
self.assertEqual(found["a"], "01/02/2000 10:20:00")
self.assertEqual(found["b"], "04/03/2001")


@patch("gloss.serialisers.opal.Logger")
def test_send_to_opal(self, logger_mock):
message_container = MagicMock()
message_container.to_dict = MagicMock()
message_container.to_dict.return_value = {"a": "dict"}
response = MagicMock()
response.status_code = 400
self.mock_requests_post.return_value = response
log = MagicMock()
logger_mock.return_value = log
log.error = MagicMock()
send_to_opal(message_container, "fake")
expected_msg = "failed to send to elcid with 400"
log.error.assert_called_once_with(expected_msg)
self.mock_requests_post.assert_called_once_with(
"fake", json=json.dumps({"a": "dict"})
)
4 changes: 2 additions & 2 deletions gloss/tests/test_serialisers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
from mock import patch, MagicMock
from gloss.tests.core import GlossTestCase
from gloss.serialisers.opal import OpalJSONSerializer, send_to_opal
from gloss.serialisers.opal import OpalJSONSerialiser, send_to_opal


class JsonSerialiserTestCase(TestCase):
Expand All @@ -17,7 +17,7 @@ def test_date_serialisation(self):
now="11/03/2016 10:10:00"
)

input_json = json.dumps(input_dict, cls=OpalJSONSerializer)
input_json = json.dumps(input_dict, cls=OpalJSONSerialiser)
self.assertEqual(expected_dict, json.loads(input_json))


Expand Down

0 comments on commit 563450c

Please sign in to comment.