Coverage for cc_modules/tests/cc_formatter_tests.py: 45%
20 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-30 13:48 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-30 13:48 +0000
1"""
2camcops_server/cc_modules/tests/cc_formatter_tests.py
4===============================================================================
6 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
9 This file is part of CamCOPS.
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.
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.
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/>.
24===============================================================================
26"""
28from unittest import TestCase
30from camcops_server.cc_modules.cc_formatter import SafeFormatter
33class SafeFormatterTests(TestCase):
34 def setUp(self) -> None:
35 super().setUp()
37 self.formatter = SafeFormatter(["forename", "surname", "email"])
39 def test_formats_with_allowed_keys(self) -> None:
40 output = self.formatter.format(
41 "{forename} {surname} <{email}>",
42 forename="Erin",
43 surname="Byrne",
44 email="erin.byrne@example.com",
45 )
46 self.assertEqual(output, "Erin Byrne <erin.byrne@example.com>")
48 def test_format_raises_with_disallowed_keys(self) -> None:
49 with self.assertRaises(KeyError):
50 self.formatter.format("{email.__class__}")
52 def test_returns_valid_parameters_string(self) -> None:
53 self.assertEqual(
54 self.formatter.get_valid_parameters_string(),
55 "{forename}, {surname}, {email}",
56 )
58 def test_validate_raises_key_error_for_unknown_key(self) -> None:
59 with self.assertRaises(KeyError):
60 self.formatter.validate("{phone}")
62 def test_validate_raises_value_error_for_mismatched_brackets(self) -> None:
63 with self.assertRaises(ValueError):
64 self.formatter.validate("{forename")