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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
|
# -*- coding: utf-8 -*-
from unittest import TestCase
import hl7
from hl7 import Accessor, Component, Field, Message, ParseException, Repetition, Segment
from .samples import (
rep_sample_hl7,
sample_bad_batch,
sample_bad_batch1,
sample_bad_file,
sample_bad_file1,
sample_bad_file2,
sample_bad_file3,
sample_batch,
sample_batch1,
sample_batch2,
sample_batch3,
sample_batch4,
sample_file,
sample_file1,
sample_file2,
sample_file3,
sample_file4,
sample_file5,
sample_file6,
sample_hl7,
)
class ParseTest(TestCase):
def test_parse(self):
msg = hl7.parse(sample_hl7)
self.assertEqual(len(msg), 5)
self.assertIsInstance(msg[0][0][0], str)
self.assertEqual(msg[0][0][0], "MSH")
self.assertEqual(msg[3][0][0], "OBX")
self.assertEqual(
msg[3][3],
[[["1554-5"], ["GLUCOSE"], ["POST 12H CFST:MCNC:PT:SER/PLAS:QN"]]],
)
# Make sure MSH-1 and MSH-2 are valid
self.assertEqual(msg[0][1][0], "|")
self.assertIsInstance(msg[0][1], hl7.Field)
self.assertEqual(msg[0][2][0], r"^~\&")
self.assertIsInstance(msg[0][2], hl7.Field)
# MSH-9 is the message type
self.assertEqual(msg[0][9], [[["ORU"], ["R01"]]])
# Do it twice to make sure text coercion is idempotent
self.assertEqual(str(msg), sample_hl7)
self.assertEqual(str(msg), sample_hl7)
def test_parse_batch(self):
batch = hl7.parse_batch(sample_batch)
self.assertEqual(len(batch), 1)
self.assertIsInstance(batch[0], hl7.Message)
self.assertIsInstance(batch.header, hl7.Segment)
self.assertEqual(batch.header[0][0], "BHS")
self.assertEqual(batch.header[4][0], "ABCHS")
self.assertIsInstance(batch.trailer, hl7.Segment)
self.assertEqual(batch.trailer[0][0], "BTS")
self.assertEqual(batch.trailer[1][0], "1")
def test_parse_batch1(self):
batch = hl7.parse_batch(sample_batch1)
self.assertEqual(len(batch), 2)
self.assertIsInstance(batch[0], hl7.Message)
self.assertEqual(batch[0][0][10][0], "12334456778890")
self.assertIsInstance(batch[1], hl7.Message)
self.assertEqual(batch[1][0][10][0], "12334456778891")
self.assertIsInstance(batch.header, hl7.Segment)
self.assertEqual(batch.header[0][0], "BHS")
self.assertEqual(batch.header[4][0], "ABCHS")
self.assertIsInstance(batch.trailer, hl7.Segment)
self.assertEqual(batch.trailer[0][0], "BTS")
self.assertEqual(batch.trailer[1][0], "2")
def test_parse_batch2(self):
batch = hl7.parse_batch(sample_batch2)
self.assertEqual(len(batch), 2)
self.assertIsInstance(batch[0], hl7.Message)
self.assertEqual(batch[0][0][10][0], "12334456778890")
self.assertIsInstance(batch[1], hl7.Message)
self.assertEqual(batch[1][0][10][0], "12334456778891")
self.assertFalse(batch.header)
self.assertFalse(batch.trailer)
def test_parse_batch3(self):
batch = hl7.parse_batch(sample_batch3)
self.assertEqual(len(batch), 1)
self.assertIsInstance(batch[0], hl7.Message)
self.assertIsInstance(batch.header, hl7.Segment)
self.assertEqual(batch.header[0][0], "BHS")
self.assertEqual(batch.header[4][0], "ABCHS")
self.assertIsInstance(batch.trailer, hl7.Segment)
self.assertEqual(batch.trailer[0][0], "BTS")
def test_parse_batch4(self):
batch = hl7.parse_batch(sample_batch4)
self.assertEqual(len(batch), 1)
self.assertIsInstance(batch[0], hl7.Message)
self.assertIsNone(batch.header)
self.assertIsNone(batch.trailer)
def test_parse_bad_batch(self):
with self.assertRaises(ParseException) as cm:
hl7.parse_batch(sample_bad_batch)
self.assertIn("Segment received before message header", cm.exception.args[0])
def test_parse_bad_batch1(self):
with self.assertRaises(ParseException) as cm:
hl7.parse_batch(sample_bad_batch1)
self.assertIn(
"Batch cannot have more than one BHS segment", cm.exception.args[0]
)
def test_parse_file(self):
file = hl7.parse_file(sample_file)
self.assertEqual(len(file), 1)
self.assertIsInstance(file[0], hl7.Batch)
self.assertIsInstance(file.header, hl7.Segment)
self.assertEqual(file.header[0][0], "FHS")
self.assertEqual(file.header[4][0], "ABCHS")
self.assertIsInstance(file.trailer, hl7.Segment)
self.assertEqual(file.trailer[0][0], "FTS")
self.assertEqual(file.trailer[1][0], "1")
def test_parse_file1(self):
file = hl7.parse_file(sample_file1)
self.assertEqual(len(file), 2)
self.assertIsInstance(file[0], hl7.Batch)
self.assertEqual(file[0].trailer[1][0], "2")
self.assertIsInstance(file[1], hl7.Batch)
self.assertEqual(file[1].trailer[1][0], "1")
self.assertNotEqual(file[0], file[1])
self.assertIsInstance(file.header, hl7.Segment)
self.assertEqual(file.header[0][0], "FHS")
self.assertEqual(file.header[4][0], "ABCHS")
self.assertIsInstance(file.trailer, hl7.Segment)
self.assertEqual(file.trailer[0][0], "FTS")
self.assertEqual(file.trailer[1][0], "2")
def test_parse_file2(self):
file = hl7.parse_file(sample_file2)
self.assertEqual(len(file), 1)
self.assertIsInstance(file[0], hl7.Batch)
self.assertIsInstance(file.header, hl7.Segment)
self.assertEqual(file.header[0][0], "FHS")
self.assertEqual(file.header[4][0], "ABCHS")
self.assertIsInstance(file.trailer, hl7.Segment)
self.assertEqual(file.trailer[0][0], "FTS")
self.assertEqual(file.trailer[1][0], "1")
def test_parse_file3(self):
file = hl7.parse_file(sample_file3)
self.assertEqual(len(file), 1)
self.assertIsInstance(file[0], hl7.Batch)
self.assertIsInstance(file.header, hl7.Segment)
self.assertEqual(file.header[0][0], "FHS")
self.assertEqual(file.header[4][0], "ABCHS")
self.assertIsInstance(file.trailer, hl7.Segment)
self.assertEqual(file.trailer[0][0], "FTS")
def test_parse_file4(self):
file = hl7.parse_file(sample_file4)
self.assertEqual(len(file), 1)
self.assertIsInstance(file[0], hl7.Batch)
self.assertIsNone(file.header)
self.assertIsNone(file.trailer)
def test_parse_file5(self):
file = hl7.parse_file(sample_file5)
self.assertEqual(len(file), 1)
self.assertIsInstance(file[0], hl7.Batch)
self.assertIsInstance(file.header, hl7.Segment)
self.assertEqual(file.header[0][0], "FHS")
self.assertEqual(file.header[4][0], "ABCHS")
self.assertIsInstance(file.trailer, hl7.Segment)
self.assertEqual(file.trailer[0][0], "FTS")
self.assertEqual(file.trailer[1][0], "1")
def test_parse_file6(self):
file = hl7.parse_file(sample_file6)
self.assertEqual(len(file), 1)
self.assertIsInstance(file[0], hl7.Batch)
self.assertIsInstance(file.header, hl7.Segment)
self.assertEqual(file.header[0][0], "FHS")
self.assertEqual(file.header[4][0], "ABCHS")
self.assertIsInstance(file.trailer, hl7.Segment)
self.assertEqual(file.trailer[0][0], "FTS")
self.assertEqual(file.trailer[1][0], "1")
def test_parse_bad_file(self):
with self.assertRaises(ParseException) as cm:
hl7.parse_file(sample_bad_file)
self.assertIn("Segment received before message header", cm.exception.args[0])
def test_parse_bad_file1(self):
with self.assertRaises(ParseException) as cm:
hl7.parse_file(sample_bad_file1)
self.assertIn(
"Batch cannot have more than one BHS segment", cm.exception.args[0]
)
def test_parse_bad_file2(self):
with self.assertRaises(ParseException) as cm:
hl7.parse_file(sample_bad_file2)
self.assertIn(
"File cannot have more than one FHS segment", cm.exception.args[0]
)
def test_parse_bad_file3(self):
with self.assertRaises(ParseException) as cm:
hl7.parse_file(sample_bad_file3)
self.assertIn("Segment received before message header", cm.exception.args[0])
def test_parse_hl7(self):
obj = hl7.parse_hl7(sample_hl7)
self.assertIsInstance(obj, hl7.Message)
obj = hl7.parse_hl7(sample_batch)
self.assertIsInstance(obj, hl7.Batch)
obj = hl7.parse_hl7(sample_batch1)
self.assertIsInstance(obj, hl7.Batch)
obj = hl7.parse_hl7(sample_batch2)
self.assertIsInstance(obj, hl7.Batch)
obj = hl7.parse_hl7(sample_file)
self.assertIsInstance(obj, hl7.File)
obj = hl7.parse_hl7(sample_file1)
self.assertIsInstance(obj, hl7.File)
obj = hl7.parse_hl7(sample_file2)
self.assertIsInstance(obj, hl7.File)
def test_bytestring_converted_to_unicode(self):
msg = hl7.parse(str(sample_hl7))
self.assertEqual(len(msg), 5)
self.assertIsInstance(msg[0][0][0], str)
self.assertEqual(msg[0][0][0], "MSH")
def test_non_ascii_bytestring(self):
# \x96 - valid cp1252, not valid utf8
# it is the responsibility of the caller to convert to unicode
msg = hl7.parse(b"MSH|^~\\&|GHH LAB|ELAB\x963", encoding="cp1252")
self.assertEqual(msg[0][4][0], "ELAB\u20133")
def test_non_ascii_bytestring_no_encoding(self):
# \x96 - valid cp1252, not valid utf8
# it is the responsibility of the caller to convert to unicode
self.assertRaises(UnicodeDecodeError, hl7.parse, b"MSH|^~\\&|GHH LAB|ELAB\x963")
def test_parsing_classes(self):
msg = hl7.parse(sample_hl7)
self.assertIsInstance(msg, hl7.Message)
self.assertIsInstance(msg[3], hl7.Segment)
self.assertIsInstance(msg[3][0], hl7.Field)
self.assertIsInstance(msg[3][0][0], str)
def test_nonstandard_separators(self):
nonstd = "MSH$%~\\&$GHH LAB\rPID$$$555-44-4444$$EVERYWOMAN%EVE%E%%%L\r"
msg = hl7.parse(nonstd)
self.assertEqual(str(msg), nonstd)
self.assertEqual(len(msg), 2)
self.assertEqual(
msg[1][5], [[["EVERYWOMAN"], ["EVE"], ["E"], [""], [""], ["L"]]]
)
def test_repetition(self):
msg = hl7.parse(rep_sample_hl7)
self.assertEqual(msg[1][4], [["Repeat1"], ["Repeat2"]])
self.assertIsInstance(msg[1][4], Field)
self.assertIsInstance(msg[1][4][0], Repetition)
self.assertIsInstance(msg[1][4][1], Repetition)
self.assertEqual(str(msg[1][4][0][0]), "Repeat1")
self.assertIsInstance(msg[1][4][0][0], str)
self.assertEqual(str(msg[1][4][1][0]), "Repeat2")
self.assertIsInstance(msg[1][4][1][0], str)
def test_empty_initial_repetition(self):
# Switch to look like "|~Repeat2|
msg = hl7.parse(rep_sample_hl7.replace("Repeat1", ""))
self.assertEqual(msg[1][4], [[""], ["Repeat2"]])
def test_subcomponent(self):
msg = hl7.parse(rep_sample_hl7)
self.assertEqual(
msg[1][3],
[[["Component1"], ["Sub-Component1", "Sub-Component2"], ["Component3"]]],
)
def test_elementnumbering(self):
# Make sure that the numbering of repetitions. components and
# sub-components is indexed from 1 when invoked as callable
# (for compatibility with HL7 spec numbering)
# and not 0-based (default for Python list)
msg = hl7.parse(rep_sample_hl7)
f = msg(2)(3)(1)(2)(2)
self.assertIs(f, msg["PID.3.1.2.2"])
self.assertIs(f, msg[1][3][0][1][1])
f = msg(2)(4)(2)(1)
self.assertIs(f, msg["PID.4.2.1"])
self.assertIs(f, msg[1][4][1][0])
# Repetition level accessed in list-form doesn't make much sense...
self.assertIs(f, msg["PID.4.2"])
def test_extract(self):
msg = hl7.parse(rep_sample_hl7)
# Full correct path
self.assertEqual(msg["PID.3.1.2.2"], "Sub-Component2")
self.assertEqual(msg[Accessor("PID", 1, 3, 1, 2, 2)], "Sub-Component2")
# Shorter Paths
self.assertEqual(msg["PID.1.1"], "Field1")
self.assertEqual(msg[Accessor("PID", 1, 1, 1)], "Field1")
self.assertEqual(msg["PID.1"], "Field1")
self.assertEqual(msg["PID1.1"], "Field1")
self.assertEqual(msg["PID.3.1.2"], "Sub-Component1")
# Longer Paths
self.assertEqual(msg["PID.1.1.1.1"], "Field1")
# Incorrect path
self.assertRaisesRegex(
IndexError,
"PID.1.1.1.2",
msg.extract_field,
*Accessor.parse_key("PID.1.1.1.2")
)
# Optional field, not included in message
self.assertEqual(msg["MSH.20"], "")
# Optional sub-component, not included in message
self.assertEqual(msg["PID.3.1.2.3"], "")
self.assertEqual(msg["PID.3.1.3"], "Component3")
self.assertEqual(msg["PID.3.1.4"], "")
def test_assign(self):
msg = hl7.parse(rep_sample_hl7)
# Field
msg["MSH.20"] = "FIELD 20"
self.assertEqual(msg["MSH.20"], "FIELD 20")
# Component
msg["MSH.21.1.1"] = "COMPONENT 21.1.1"
self.assertEqual(msg["MSH.21.1.1"], "COMPONENT 21.1.1")
# Sub-Component
msg["MSH.21.1.2.4"] = "SUBCOMPONENT 21.1.2.4"
self.assertEqual(msg["MSH.21.1.2.4"], "SUBCOMPONENT 21.1.2.4")
# Verify round-tripping (i.e. that separators are correct)
msg2 = hl7.parse(str(msg))
self.assertEqual(msg2["MSH.20"], "FIELD 20")
self.assertEqual(msg2["MSH.21.1.1"], "COMPONENT 21.1.1")
self.assertEqual(msg2["MSH.21.1.2.4"], "SUBCOMPONENT 21.1.2.4")
def test_unescape(self):
msg = hl7.parse(rep_sample_hl7)
# Replace Separators
self.assertEqual(msg.unescape("\\E\\"), "\\")
self.assertEqual(msg.unescape("\\F\\"), "|")
self.assertEqual(msg.unescape("\\S\\"), "^")
self.assertEqual(msg.unescape("\\T\\"), "&")
self.assertEqual(msg.unescape("\\R\\"), "~")
# Replace Highlighting
self.assertEqual(msg.unescape("\\H\\text\\N\\"), "_text_")
# Application Overrides
self.assertEqual(msg.unescape("\\H\\text\\N\\", {"H": "*", "N": "*"}), "*text*")
# Hex Codes
self.assertEqual(msg.unescape("\\X20202020\\"), " ")
self.assertEqual(msg.unescape("\\Xe1\\\\Xe9\\\\Xed\\\\Xf3\\\\Xfa\\"), "áéíóú")
def test_escape(self):
msg = hl7.parse(rep_sample_hl7)
# Escape Separators
self.assertEqual(msg.escape("\\"), "\\E\\")
self.assertEqual(msg.escape("|"), "\\F\\")
self.assertEqual(msg.escape("^"), "\\S\\")
self.assertEqual(msg.escape("&"), "\\T\\")
self.assertEqual(msg.escape("~"), "\\R\\")
# Escape ASCII characters
self.assertEqual(msg.escape("asdf"), "asdf")
# Escape non-ASCII characters
self.assertEqual(msg.escape("áéíóú"), "\\Xe1\\\\Xe9\\\\Xed\\\\Xf3\\\\Xfa\\")
self.assertEqual(msg.escape("äsdf"), "\\Xe4\\sdf")
def test_file(self):
# Extract message from file
self.assertTrue(hl7.isfile(sample_file))
messages = hl7.split_file(sample_file)
self.assertEqual(len(messages), 1)
# message can be parsed
msg = hl7.parse(messages[0])
# message has expected content
self.assertEqual(
[s[0][0] for s in msg], ["MSH", "EVN", "PID", "PD1", "NK1", "PV1"]
)
class ParsePlanTest(TestCase):
def test_create_parse_plan(self):
plan = hl7.parser.create_parse_plan(sample_hl7)
self.assertEqual(plan.separators, "\r|~^&")
self.assertEqual(
plan.containers, [Message, Segment, Field, Repetition, Component]
)
def test_parse_plan(self):
plan = hl7.parser.create_parse_plan(sample_hl7)
self.assertEqual(plan.separator, "\r")
con = plan.container([1, 2])
self.assertIsInstance(con, Message)
self.assertEqual(con, [1, 2])
self.assertEqual(con.separator, "\r")
def test_parse_plan_next(self):
plan = hl7.parser.create_parse_plan(sample_hl7)
n1 = plan.next()
self.assertEqual(n1.separators, "\r|~^&")
self.assertEqual(n1.separator, "|")
self.assertEqual(n1.containers, [Segment, Field, Repetition, Component])
n2 = n1.next()
self.assertEqual(n2.separators, "\r|~^&")
self.assertEqual(n2.separator, "~")
self.assertEqual(n2.containers, [Field, Repetition, Component])
n3 = n2.next()
self.assertEqual(n3.separators, "\r|~^&")
self.assertEqual(n3.separator, "^")
self.assertEqual(n3.containers, [Repetition, Component])
n4 = n3.next()
self.assertEqual(n4.separators, "\r|~^&")
self.assertEqual(n4.separator, "&")
self.assertEqual(n4.containers, [Component])
n5 = n4.next()
self.assertTrue(n5 is None)
|