How to use hl7apy - 10 common examples

To help you get started, we’ve selected a few hl7apy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github crs4 / hl7apy / tests / test_core.py View on Github external
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())
github crs4 / hl7apy / tests / test_core.py View on Github external
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||'
github crs4 / hl7apy / tests / test_core.py View on Github external
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])
github crs4 / hl7apy / tests / test_core.py View on Github external
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
github crs4 / hl7apy / tests / test_core.py View on Github external
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'))
github crs4 / hl7apy / tests / test_to_string.py View on Github external
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
github crs4 / hl7apy / tests / test_core.py View on Github external
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
github crs4 / hl7apy / tests / test_core.py View on Github external
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)
github crs4 / hl7apy / tests / test_to_string.py View on Github external
def test_to_string_msh_field_v27(self):
        for v in ('2.7', '2.8', '2.8.1', '2.8.2'):
            m = Message('OML_O33', version=v)
            msh = m.msh
            self.assertEqual(msh.msh_1.to_er7(), '|')
            self.assertEqual(msh.msh_2.to_er7(), '^~\\&#')
            msh_1 = Field('MSH_1')
            msh_2 = Field('MSH_2')
            self.assertRaises(IndexError, msh_1.to_er7)
            self.assertRaises(IndexError, msh_2.to_er7)
github crs4 / hl7apy / tests / test_core.py View on Github external
# name with base datatype
        m = Message('RSP_K21')
        m.pid.pid_39.cwe_1 = ST(cmp_str)
        self.assertEqual(m.pid.pid_39.cwe_1.to_er7(), cmp_str)

        # value with base datatype
        m = Message('RSP_K21')
        m.pid.pid_39.cwe_1.value = ST(cmp_str)
        self.assertEqual(m.pid.pid_39.cwe_1.to_er7(), cmp_str)

        # complex datatype
        complex_cmp_str = 'xxx&yyy&zzz'

        # name
        f = Field('PID_4')
        f.cx_10 = complex_cmp_str
        self.assertEqual(f.cx_10.to_er7(), complex_cmp_str)
        self.assertEqual(len(f.cx_10.children), 3)

        # value
        f = Field('PID_4')
        f.cx_10.value = complex_cmp_str
        self.assertEqual(f.cx_10.to_er7(), complex_cmp_str)
        self.assertEqual(len(f.cx_10.children), 3)