Coverage for tasks/tests/diagnosis_tests.py: 24%

1"""

2 camcops_server/tasks/tests/diagnosis_tests.py

3

4===============================================================================

5

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

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

8

9 This file is part of CamCOPS.

10

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

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

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

14 (at your option) any later version.

15

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

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

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

19 GNU General Public License for more details.

20

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

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

23

24===============================================================================

25

26"""

27

28 from camcops_server.cc_modules.cc_testfactories import (

29 NHSIdNumDefinitionFactory,

30 NHSPatientIdNumFactory,

31 PatientFactory,

32 UserFactory,

33)

34 from camcops_server.cc_modules.cc_unittest import DemoRequestTestCase

35 from camcops_server.cc_modules.cc_pyramid import ViewParam

36 from camcops_server.tasks.diagnosis import DiagnosisICD10FinderReport

37 from camcops_server.tasks.tests.factories import (

38 DiagnosisIcd10Factory,

39 DiagnosisIcd10ItemFactory,

40)

41

42

43 class DiagnosisICD10FinderReportTests(DemoRequestTestCase):

44 def setUp(self) -> None:

45 super().setUp()

46

47 self.report = DiagnosisICD10FinderReport()

48 self.req._debugging_user = UserFactory(superuser=True)

49

50 def test_no_records_creates_empty_report(self) -> None:

51 pages = self.report.get_spreadsheet_pages(self.req)

52

53 self.assertEqual(len(pages), 1)

54 self.assertEqual(pages[0].headings, [])

55 self.assertEqual(pages[0].rows, [])

56

57 def test_creates_report_from_one_record(self) -> None:

58 patient = PatientFactory()

59 idnum = NHSPatientIdNumFactory(patient=patient)

60 diagnosis = DiagnosisIcd10Factory(patient=patient)

61 item = DiagnosisIcd10ItemFactory(

62 diagnosis_icd10=diagnosis,

63 code="code",

64 description="description",

65 )

66

67 params = {

68 ViewParam.WHICH_IDNUM: idnum.which_idnum,

69 }

70

71 self.req.set_get_params(params)

72 pages = self.report.get_spreadsheet_pages(self.req)

73

74 self.assertEqual(len(pages), 1)

75 self.assertEqual(

76 pages[0].rows[0],

77 {

78 "surname": patient.surname,

79 "forename": patient.forename,

80 "dob": patient.dob,

81 "sex": patient.sex,

82 "NHS number": idnum.idnum_value,

83 "when_created": diagnosis.when_created,

84 "system": "ICD-10",

85 "code": item.code,

86 "description": item.description,

87 },

88 )

89

90 def test_code_excluded(self) -> None:

91 patient1 = PatientFactory()

92 nhs_iddef = NHSIdNumDefinitionFactory()

93 idnum1 = NHSPatientIdNumFactory(iddef=nhs_iddef, patient=patient1)

94 diagnosis1 = DiagnosisIcd10Factory(patient=patient1)

95 item1 = DiagnosisIcd10ItemFactory(

96 diagnosis_icd10=diagnosis1,

97 code="code1",

98 description="description1",

99 )

100

101 patient2 = PatientFactory()

102 NHSPatientIdNumFactory(patient=patient2, iddef=nhs_iddef)

103 diagnosis2 = DiagnosisIcd10Factory(patient=patient2)

104

105 DiagnosisIcd10ItemFactory(

106 diagnosis_icd10=diagnosis2,

107 code="code2",

108 description="description2",

109 )

110

111 params = {

112 ViewParam.WHICH_IDNUM: nhs_iddef.which_idnum,

113 ViewParam.DIAGNOSES_INCLUSION: "code1",

114 ViewParam.DIAGNOSES_EXCLUSION: "code2",

115 }

116

117 self.req.set_get_params(params)

118 pages = self.report.get_spreadsheet_pages(self.req)

119

120 self.assertEqual(len(pages), 1)

121 page = pages[0]

122 self.assertEqual(len(page.rows), 1)

123 self.assertEqual(

124 page.rows[0],

125 {

126 "surname": patient1.surname,

127 "forename": patient1.forename,

128 "dob": patient1.dob,

129 "sex": patient1.sex,

130 "NHS number": idnum1.idnum_value,

131 "when_created": diagnosis1.when_created,

132 "system": "ICD-10",

133 "code": item1.code,

134 "description": item1.description,

135 },

136 )