def send_message(server, port): client = MLLPClient(server, port) f = open('test.dat', 'r') ff = f.read() messages = hl7.parse_batch(ff) for message in messages: client.send_message(str(message))
class MLLPClientTest(unittest.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(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('\x0bfoobar\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): try: with MLLPClient('localhost', 6666): raise Exception() self.fail() except: # expected pass # socket.close should be called via the with statement self.client.socket.close.assert_called_once_with()
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(u"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\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()