Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_assign_value_traversal(self):
m1 = Message('OML_O33')
m2 = Message('OML_O33')
m1.oml_o33_specimen.value = self.oml_o33_specimen
m2.oml_o33_specimen = self.oml_o33_specimen
self.assertEqual(m1.to_er7(), m2.to_er7())
m1 = Message('RSP_K21', reference=self.rsp_k21_mp)
m2 = Message('RSP_K21', reference=self.rsp_k21_mp)
m1.rsp_k21_query_response.value = self.rsp_k21_query_response
m2.rsp_k21_query_response = self.rsp_k21_query_response
self.assertEqual(m1.to_er7(), m2.to_er7())
self.assertEqual(m1.to_er7(), m2.to_er7())
def test_assign_wrong_segment_to_known_position(self):
a = Message('OML_O33', validation_level=VALIDATION_LEVEL.STRICT)
b = Message('OML_O33')
with self.assertRaises(ChildNotValid):
a.msh = Segment('SPM')
with self.assertRaises(ChildNotValid):
a.pid = 'EVN||20080115153000||||20080114003000'
with self.assertRaises(InvalidName):
a.zin = 'PAP||abc||'
with self.assertRaises(InvalidName):
a.msh = 'PAP||abc||'
with self.assertRaises(ChildNotValid):
b.msh = Segment('SPM')
with self.assertRaises(ChildNotValid):
b.pid = 'EVN||20080115153000||||20080114003000'
with self.assertRaises(InvalidName):
b.zin = 'PAP||abc||'
def test_recursive_traversal(self):
obr = Segment('OBR')
obr.obr_26 = 'xxx&yyy^zzz^www'
by_name = obr.parent_result.parent_observation_identifier
by_position = obr.obr_26.obr_26_1
self.assertEqual(by_name[0], by_position[0])
m = Message('RSP_K21', reference=self.rsp_k21_mp)
qpd = m.add_segment('qpd')
qpd.qpd_8 = '^^^IHEFACILITY&1.3.6.1.4.1.21367.3000.1.6&ISO'
by_name = qpd.what_domains_returned.assigning_authority
by_position = qpd.qpd_8.qpd_8_4
self.assertEqual(by_name[0], by_position[0])
def test_assign_value(self):
msg = _get_test_msg()
a = Message('OML_O33', validation_level=VALIDATION_LEVEL.TOLERANT)
parsed_a = parse_message(msg, validation_level=VALIDATION_LEVEL.TOLERANT)
a.value = msg
self.assertEqual(a.to_er7(), parsed_a.to_er7())
b = Message('OML_O33', validation_level=VALIDATION_LEVEL.STRICT)
b.value = msg
parsed_b = parse_message(msg, validation_level=VALIDATION_LEVEL.STRICT)
self.assertEqual(b.to_er7(), parsed_b.to_er7())
self.assertEqual(list(b.children.indexes.keys()), list(parsed_b.children.indexes.keys()))
c = Message('ADT_A01', validation_level=VALIDATION_LEVEL.TOLERANT)
with self.assertRaises(OperationNotAllowed):
c.value = msg
msg = msg.replace('^', 'x')
with self.assertRaises(OperationNotAllowed):
a.value = msg
c = Message('OML_O33', version='2.6')
with self.assertRaises(OperationNotAllowed):
c.value = msg
msg = _get_rsp_k21_mp_msg()
a = Message('RSP_K21', validation_level=VALIDATION_LEVEL.TOLERANT, reference=self.rsp_k21_mp)
parsed_a = parse_message(msg, message_profile=self.rsp_k21_mp,
validation_level=VALIDATION_LEVEL.TOLERANT)
a.value = msg
def test_traversal_by_name(self):
obr = Segment('OBR')
obr_26 = obr.parent_result
self.assertTrue(obr_26.is_named('OBR_26'))
self.assertTrue(obr_26.is_named('PARENT_RESULT'))
m = Message('RSP_K21', reference=self.rsp_k21_mp)
msa = m.msa
msa_1 = msa.acknowledgment_code
self.assertTrue(msa_1.is_named('MSA_1'))
self.assertTrue(msa_1.is_named('ACKNOWLEDGMENT_CODE'))
def _create_test_message(self, msh_values):
"""
Create a test message - RSP_K11 - with only the msh segment.
The msh is filled with the sent in input
"""
msg = Message('RSP_K11')
msg.msh = self._get_msh(msh_values)
return msg
def test_access_to_unknown_field(self):
s1 = Segment('MSH', validation_level=VALIDATION_LEVEL.STRICT)
s2 = Segment('PID')
with self.assertRaises(ChildNotFound):
s1.msh_100
with self.assertRaises(ChildNotFound):
s2.pid_100
m1 = Message('RSP_K21', validation_level=VALIDATION_LEVEL.STRICT, reference=self.rsp_k21_mp)
m2 = Message('RSP_K21', validation_level=VALIDATION_LEVEL.TOLERANT, reference=self.rsp_k21_mp)
s1 = m1.add_segment('QPD')
s2 = m2.add_segment('QPD')
with self.assertRaises(ChildNotFound):
s1.qpd_100
with self.assertRaises(ChildNotFound):
s2.qpd_100
def test_create_z_message(self):
Message('ZDT_ZDT')
Message('ZA1_ZB2')
Message('za1_zb2')
self.assertRaises(InvalidName, Message, 'za1azb2')
self.assertRaises(InvalidName, Message, 'z##_azb2')
self.assertRaises(InvalidName, Message, 'zab_zaba')
self.assertRaises(InvalidName, Message, 'zaba_zab')
self.assertRaises(InvalidName, Message, 'OML_ZAB')
self.assertRaises(InvalidName, Message, 'zab_oml')
Message('ZDT_ZDT', validation_level=VALIDATION_LEVEL.STRICT)
Message('ZA1_ZB2', validation_level=VALIDATION_LEVEL.STRICT)
Message('za1_zb2', validation_level=VALIDATION_LEVEL.STRICT)
self.assertRaises(InvalidName, Message, 'za1azb2', validation_level=VALIDATION_LEVEL.STRICT)
self.assertRaises(InvalidName, Message, 'z##_azb2', validation_level=VALIDATION_LEVEL.STRICT)
self.assertRaises(InvalidName, Message, 'zab_zaba', validation_level=VALIDATION_LEVEL.STRICT)
self.assertRaises(InvalidName, Message, 'zaba_zab', validation_level=VALIDATION_LEVEL.STRICT)
self.assertRaises(InvalidName, Message, 'OML_ZAB', validation_level=VALIDATION_LEVEL.STRICT)
def reply(self):
if isinstance(self.exc, UnsupportedMessageType):
err_code, err_msg = 101, 'Unsupported message'
elif isinstance(self.exc, InvalidHL7Message):
err_code, err_msg = 102, 'Incoming message is not an HL7 valid message'
else:
err_code, err_msg = 100, 'Unknown error occurred'
parsed_message = parse_message(self.incoming_message)
m = Message("ACK")
m.MSH.MSH_9 = "ACK^ACK"
m.MSA.MSA_1 = "AR"
m.MSA.MSA_2 = parsed_message.MSH.MSH_10
m.ERR.ERR_1 = "%s" % err_code
m.ERR.ERR_2 = "%s" % err_msg
return m.to_mllp()
def nak(message):
"""
Build a NAK response for the incoming message
:param message: incoming message
:return: a NAK message
"""
response = Message("ACK")
response.MSH.MSH_9 = "ACK"
response.MSA.MSA_1 = "AE"
response.MSA.MSA_2 = message.MSH.MSH_10
response.MSA.MSA_3 = "Message type not supported"
return response