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
|
import os
import socket
from optparse import Values
from shutil import rmtree
from tempfile import mkdtemp
from unittest import TestCase
from unittest.mock import Mock, patch
import hl7
from hl7 import __version__ as hl7_version
from hl7.client import CR, EB, SB, MLLPClient, MLLPException, mllp_send
class MLLPClientTest(TestCase):
def setUp(self):
# use a mock version of socket
self.socket_patch = patch("hl7.client.socket.socket")
self.mock_socket = self.socket_patch.start()
self.client = MLLPClient("localhost", 6666)
def tearDown(self):
# unpatch socket
self.socket_patch.stop()
def test_connect(self):
self.mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM)
self.client.socket.connect.assert_called_once_with(("localhost", 6666))
def test_close(self):
self.client.close()
self.client.socket.close.assert_called_once_with()
def test_send(self):
self.client.socket.recv.return_value = "thanks"
result = self.client.send("foobar\n")
self.assertEqual(result, "thanks")
self.client.socket.send.assert_called_once_with("foobar\n")
self.client.socket.recv.assert_called_once_with(4096)
def test_send_message_unicode(self):
self.client.socket.recv.return_value = "thanks"
result = self.client.send_message("foobar")
self.assertEqual(result, "thanks")
self.client.socket.send.assert_called_once_with(b"\x0bfoobar\x1c\x0d")
def test_send_message_bytestring(self):
self.client.socket.recv.return_value = "thanks"
result = self.client.send_message(b"foobar")
self.assertEqual(result, "thanks")
self.client.socket.send.assert_called_once_with(b"\x0bfoobar\x1c\x0d")
def test_send_message_hl7_message(self):
self.client.socket.recv.return_value = "thanks"
message = hl7.parse(r"MSH|^~\&|GHH LAB|ELAB")
result = self.client.send_message(message)
self.assertEqual(result, "thanks")
self.client.socket.send.assert_called_once_with(
b"\x0bMSH|^~\\&|GHH LAB|ELAB\r\x1c\x0d"
)
def test_context_manager(self):
with MLLPClient("localhost", 6666) as client:
client.send("hello world")
self.client.socket.send.assert_called_once_with("hello world")
self.client.socket.close.assert_called_once_with()
def test_context_manager_exception(self):
with self.assertRaises(Exception):
with MLLPClient("localhost", 6666):
raise Exception()
# socket.close should be called via the with statement
self.client.socket.close.assert_called_once_with()
class MLLPSendTest(TestCase):
def setUp(self):
# patch to avoid touching sys and socket
self.socket_patch = patch("hl7.client.socket.socket")
self.mock_socket = self.socket_patch.start()
self.mock_socket().recv.return_value = "thanks"
self.stdout_patch = patch("hl7.client.stdout")
self.mock_stdout = self.stdout_patch.start()
self.stdin_patch = patch("hl7.client.stdin")
self.mock_stdin = self.stdin_patch.start()
self.stderr_patch = patch("hl7.client.stderr")
self.mock_stderr = self.stderr_patch.start()
self.exit_patch = patch("hl7.client.sys.exit")
self.mock_exit = self.exit_patch.start()
# we need a temporary directory
self.dir = mkdtemp()
self.write(SB + b"foobar" + EB + CR)
self.option_values = Values(
{
"port": 6661,
"filename": os.path.join(self.dir, "test.hl7"),
"verbose": True,
"loose": False,
"version": False,
}
)
self.options_patch = patch("hl7.client.OptionParser")
option_parser = self.options_patch.start()
self.mock_options = Mock()
option_parser.return_value = self.mock_options
self.mock_options.parse_args.return_value = (self.option_values, ["localhost"])
def tearDown(self):
# unpatch
self.socket_patch.stop()
self.options_patch.stop()
self.stdout_patch.stop()
self.stdin_patch.stop()
self.stderr_patch.stop()
self.exit_patch.stop()
# clean up the temp directory
rmtree(self.dir)
def write(self, content, path="test.hl7"):
with open(os.path.join(self.dir, path), "wb") as f:
f.write(content)
def test_send(self):
mllp_send()
self.mock_socket().connect.assert_called_once_with(("localhost", 6661))
self.mock_socket().send.assert_called_once_with(SB + b"foobar" + EB + CR)
self.mock_stdout.assert_called_once_with("thanks")
self.assertFalse(self.mock_exit.called)
def test_send_multiple(self):
self.mock_socket().recv.return_value = "thanks"
self.write(SB + b"foobar" + EB + CR + SB + b"hello" + EB + CR)
mllp_send()
self.assertEqual(
self.mock_socket().send.call_args_list[0][0][0], SB + b"foobar" + EB + CR
)
self.assertEqual(
self.mock_socket().send.call_args_list[1][0][0], SB + b"hello" + EB + CR
)
def test_leftover_buffer(self):
self.write(SB + b"foobar" + EB + CR + SB + b"stuff")
self.assertRaises(MLLPException, mllp_send)
self.mock_socket().send.assert_called_once_with(SB + b"foobar" + EB + CR)
def test_quiet(self):
self.option_values.verbose = False
mllp_send()
self.mock_socket().send.assert_called_once_with(SB + b"foobar" + EB + CR)
self.assertFalse(self.mock_stdout.called)
def test_port(self):
self.option_values.port = 7890
mllp_send()
self.mock_socket().connect.assert_called_once_with(("localhost", 7890))
def test_stdin(self):
self.option_values.filename = None
self.mock_stdin.return_value = FakeStream()
mllp_send()
self.mock_socket().send.assert_called_once_with(SB + b"hello" + EB + CR)
def test_loose_no_stdin(self):
self.option_values.loose = True
self.option_values.filename = None
self.mock_stdin.return_value = FakeStream()
mllp_send()
self.assertFalse(self.mock_socket().send.called)
self.mock_stderr().write.assert_called_with("--loose requires --file\n")
self.mock_exit.assert_called_with(1)
def test_loose_windows_newline(self):
self.option_values.loose = True
self.write(SB + b"MSH|^~\\&|foo\r\nbar\r\n" + EB + CR)
mllp_send()
self.mock_socket().send.assert_called_once_with(
SB + b"MSH|^~\\&|foo\rbar" + EB + CR
)
def test_loose_unix_newline(self):
self.option_values.loose = True
self.write(SB + b"MSH|^~\\&|foo\nbar\n" + EB + CR)
mllp_send()
self.mock_socket().send.assert_called_once_with(
SB + b"MSH|^~\\&|foo\rbar" + EB + CR
)
def test_loose_no_mllp_characters(self):
self.option_values.loose = True
self.write(b"MSH|^~\\&|foo\r\nbar\r\n")
mllp_send()
self.mock_socket().send.assert_called_once_with(
SB + b"MSH|^~\\&|foo\rbar" + EB + CR
)
def test_loose_send_mutliple(self):
self.option_values.loose = True
self.mock_socket().recv.return_value = "thanks"
self.write(b"MSH|^~\\&|1\r\nOBX|1\r\nMSH|^~\\&|2\r\nOBX|2\r\n")
mllp_send()
self.assertEqual(
self.mock_socket().send.call_args_list[0][0][0],
SB + b"MSH|^~\\&|1\rOBX|1" + EB + CR,
)
self.assertEqual(
self.mock_socket().send.call_args_list[1][0][0],
SB + b"MSH|^~\\&|2\rOBX|2" + EB + CR,
)
def test_version(self):
self.option_values.version = True
mllp_send()
self.assertFalse(self.mock_socket().connect.called)
self.mock_stdout.assert_called_once_with(str(hl7_version))
class FakeStream(object):
count = 0
def read(self, buf):
self.count += 1
if self.count == 1:
return SB + b"hello" + EB + CR
else:
return b""
|