Coverage for cc_modules/tests/cc_hl7_tests.py: 37%

27 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-08 23:14 +0000

1#!/usr/bin/env python 

2 

3""" 

4camcops_server/cc_modules/tests/cc_hl7_tests.py 

5 

6=============================================================================== 

7 

8 Copyright (C) 2012, University of Cambridge, Department of Psychiatry. 

9 Created by Rudolf Cardinal (rnc1001@cam.ac.uk). 

10 

11 This file is part of CamCOPS. 

12 

13 CamCOPS is free software: you can redistribute it and/or modify 

14 it under the terms of the GNU General Public License as published by 

15 the Free Software Foundation, either version 3 of the License, or 

16 (at your option) any later version. 

17 

18 CamCOPS is distributed in the hope that it will be useful, 

19 but WITHOUT ANY WARRANTY; without even the implied warranty of 

20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

21 GNU General Public License for more details. 

22 

23 You should have received a copy of the GNU General Public License 

24 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>. 

25 

26=============================================================================== 

27""" 

28 

29 

30import hl7 

31from pendulum import Date, DateTime as Pendulum 

32 

33from camcops_server.cc_modules.cc_constants import FileType 

34from camcops_server.cc_modules.cc_hl7 import ( 

35 escape_hl7_text, 

36 get_mod11_checkdigit, 

37 make_msh_segment, 

38 make_obr_segment, 

39 make_obx_segment, 

40 make_pid_segment, 

41) 

42from camcops_server.cc_modules.cc_simpleobjects import ( 

43 HL7PatientIdentifier, 

44 TaskExportOptions, 

45) 

46from camcops_server.cc_modules.cc_unittest import DemoDatabaseTestCase 

47from camcops_server.tasks.phq9 import Phq9 

48 

49 

50# ============================================================================= 

51# Unit tests 

52# ============================================================================= 

53 

54 

55class HL7CoreTests(DemoDatabaseTestCase): 

56 """ 

57 Unit tests. 

58 """ 

59 

60 def test_hl7core_func(self) -> None: 

61 self.announce("test_hl7core_func") 

62 

63 pitlist = [ 

64 HL7PatientIdentifier( 

65 pid="1", id_type="TT", assigning_authority="AA" 

66 ) 

67 ] 

68 # noinspection PyTypeChecker 

69 dob = Date.today() # type: Date 

70 now = Pendulum.now() 

71 task = self.dbsession.query(Phq9).first() 

72 assert task, "Missing Phq9 in demo database!" 

73 

74 self.assertIsInstance(get_mod11_checkdigit("12345"), str) 

75 self.assertIsInstance(get_mod11_checkdigit("badnumber"), str) 

76 self.assertIsInstance(get_mod11_checkdigit("None"), str) 

77 self.assertIsInstance(make_msh_segment(now, "control_id"), hl7.Segment) 

78 self.assertIsInstance( 

79 make_pid_segment( 

80 forename="fname", 

81 surname="sname", 

82 dob=dob, 

83 sex="M", 

84 address="Somewhere", 

85 patient_id_list=pitlist, 

86 ), 

87 hl7.Segment, 

88 ) 

89 self.assertIsInstance(make_obr_segment(task), hl7.Segment) 

90 for task_format in (FileType.PDF, FileType.HTML, FileType.XML): 

91 for comments in (True, False): 

92 export_options = TaskExportOptions( 

93 xml_include_comments=comments, 

94 xml_with_header_comments=comments, 

95 ) 

96 self.assertIsInstance( 

97 make_obx_segment( 

98 req=self.req, 

99 task=task, 

100 task_format=task_format, 

101 observation_identifier="obs_id", 

102 observation_datetime=now, 

103 responsible_observer="responsible_observer", 

104 export_options=export_options, 

105 ), 

106 hl7.Segment, 

107 ) 

108 self.assertIsInstance(escape_hl7_text("blahblah"), str)