1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
# -*- coding: utf-8 -*-
from unittest import TestCase
import hl7
from .samples import rep_sample_hl7
SEP = r"|^~\&"
CR_SEP = "\r"
class ConstructionTest(TestCase):
def test_create_msg(self):
# Create a message
MSH = hl7.Segment(SEP[0], [hl7.Field(SEP[2], ["MSH"])])
MSA = hl7.Segment(SEP[0], [hl7.Field(SEP[2], ["MSA"])])
response = hl7.Message(CR_SEP, [MSH, MSA])
response["MSH.F1.R1"] = SEP[0]
response["MSH.F2.R1"] = SEP[1:]
self.assertEqual(str(response), "MSH|^~\\&|\rMSA\r")
def test_append(self):
# Append a segment to a message
MSH = hl7.Segment(SEP[0], [hl7.Field(SEP[2], ["MSH"])])
response = hl7.Message(CR_SEP, [MSH])
response["MSH.F1.R1"] = SEP[0]
response["MSH.F2.R1"] = SEP[1:]
MSA = hl7.Segment(SEP[0], [hl7.Field(SEP[2], ["MSA"])])
response.append(MSA)
self.assertEqual(str(response), "MSH|^~\\&|\rMSA\r")
def test_append_from_source(self):
# Copy a segment between messages
MSH = hl7.Segment(SEP[0], [hl7.Field(SEP[2], ["MSH"])])
MSA = hl7.Segment(SEP[0], [hl7.Field(SEP[2], ["MSA"])])
response = hl7.Message(CR_SEP, [MSH, MSA])
response["MSH.F1.R1"] = SEP[0]
response["MSH.F2.R1"] = SEP[1:]
self.assertEqual(str(response), "MSH|^~\\&|\rMSA\r")
src_msg = hl7.parse(rep_sample_hl7)
PID = src_msg["PID"][0]
self.assertEqual(
str(PID),
"PID|Field1|Component1^Component2|Component1^Sub-Component1&Sub-Component2^Component3|Repeat1~Repeat2",
)
response.append(PID)
self.assertEqual(
str(response),
"MSH|^~\\&|\rMSA\rPID|Field1|Component1^Component2|Component1^Sub-Component1&Sub-Component2^Component3|Repeat1~Repeat2\r",
)
|