Coverage for tasks/tests/perinatalpoem_tests.py : 27%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
3"""
4camcops_server/tasks/tests/perinatalpoem_tests.py
6===============================================================================
8 Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com).
10 This file is part of CamCOPS.
12 CamCOPS is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 CamCOPS is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.
25===============================================================================
27"""
29from typing import Generator
31import pendulum
33from camcops_server.cc_modules.cc_unittest import DemoDatabaseTestCase
35from camcops_server.tasks.perinatalpoem import (
36 PerinatalPoem,
37 PerinatalPoemReport,
38)
41# =============================================================================
42# Unit tests
43# =============================================================================
45class PerinatalPoemReportTestCase(DemoDatabaseTestCase):
46 def __init__(self, *args, **kwargs) -> None:
47 super().__init__(*args, **kwargs)
48 self.id_sequence = self.get_id()
50 def setUp(self) -> None:
51 super().setUp()
53 self.report = PerinatalPoemReport()
55 # Really only needed for tests
56 self.report.start_datetime = None
57 self.report.end_datetime = None
59 @staticmethod
60 def get_id() -> Generator[int, None, None]:
61 i = 1
63 while True:
64 yield i
65 i += 1
67 def create_task(self, **kwargs) -> None:
68 task = PerinatalPoem()
69 self.apply_standard_task_fields(task)
70 task.id = next(self.id_sequence)
72 era = kwargs.pop('era', None)
73 if era is not None:
74 task.when_created = pendulum.parse(era)
76 for name, value in kwargs.items():
77 setattr(task, name, value)
79 self.dbsession.add(task)
82class PerinatalPoemReportTests(PerinatalPoemReportTestCase):
83 """
84 Most of the base class tested in APEQCPFT Perinatal so just some basic
85 sanity checking here
86 """
88 def create_tasks(self):
89 self.create_task(general_comments="comment 1")
90 self.create_task(general_comments="comment 2")
91 self.create_task(general_comments="comment 3")
93 self.dbsession.commit()
95 def test_qa_rows_counts(self) -> None:
96 tables = self.report._get_html_tables(self.req)
98 rows = tables[0].rows
100 self.assertEqual(len(rows), 1)
101 self.assertEqual(len(rows[0]), 4)
103 def test_qb_rows_counts(self) -> None:
104 tables = self.report._get_html_tables(self.req)
106 rows = tables[1].rows
108 self.assertEqual(len(rows), 1)
109 self.assertEqual(len(rows[0]), 4)
111 def test_q1_rows_counts(self) -> None:
112 tables = self.report._get_html_tables(self.req)
114 rows = tables[2].rows
116 self.assertEqual(len(rows), 2)
117 self.assertEqual(len(rows[0]), 7)
119 def test_q2_rows_counts(self) -> None:
120 tables = self.report._get_html_tables(self.req)
122 rows = tables[3].rows
124 self.assertEqual(len(rows), 12)
125 self.assertEqual(len(rows[0]), 6)
127 def test_q3_rows_counts(self) -> None:
128 tables = self.report._get_html_tables(self.req)
130 rows = tables[4].rows
132 self.assertEqual(len(rows), 6)
133 self.assertEqual(len(rows[0]), 6)
135 def test_participation_rows_counts(self) -> None:
136 tables = self.report._get_html_tables(self.req)
138 rows = tables[5].rows
140 self.assertEqual(len(rows), 1)
141 self.assertEqual(len(rows[0]), 4)
143 def test_comments(self) -> None:
144 expected_comments = [
145 "comment 1", "comment 2", "comment 3",
146 ]
147 comments = self.report._get_comments(self.req)
148 self.assertEqual(comments, expected_comments)
151class PerinatalPoemReportDateRangeTests(PerinatalPoemReportTestCase):
152 def create_tasks(self) -> None:
153 self.create_task(general_comments="comments 1",
154 era="2018-10-01T00:00:00.000000+00:00")
155 self.create_task(general_comments="comments 2",
156 era="2018-10-02T00:00:00.000000+00:00")
157 self.create_task(general_comments="comments 3",
158 era="2018-10-03T00:00:00.000000+00:00")
159 self.create_task(general_comments="comments 4",
160 era="2018-10-04T00:00:00.000000+00:00")
161 self.create_task(general_comments="comments 5",
162 era="2018-10-05T00:00:00.000000+00:00")
163 self.dbsession.commit()
165 def test_comments_filtered_by_date(self) -> None:
166 self.report.start_datetime = "2018-10-02T00:00:00.000000+00:00"
167 self.report.end_datetime = "2018-10-05T00:00:00.000000+00:00"
169 comments = self.report._get_comments(self.req)
170 self.assertEqual(len(comments), 3)
172 self.assertEqual(comments[0], "comments 2")
173 self.assertEqual(comments[1], "comments 3")
174 self.assertEqual(comments[2], "comments 4")