Coverage for cc_modules/tests/cc_formatter_tests.py: 48%
21 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-08 23:14 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-08 23:14 +0000
1#!/usr/bin/env python
3"""
4camcops_server/cc_modules/tests/cc_formatter_tests.py
6===============================================================================
8 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
9 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
11 This file is part of CamCOPS.
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.
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.
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/>.
26===============================================================================
28"""
30from unittest import TestCase
32from camcops_server.cc_modules.cc_formatter import SafeFormatter
35class SafeFormatterTests(TestCase):
36 def setUp(self) -> None:
37 super().setUp()
39 self.formatter = SafeFormatter(["forename", "surname", "email"])
41 def test_formats_with_allowed_keys(self) -> None:
42 output = self.formatter.format(
43 "{forename} {surname} <{email}>",
44 forename="Erin",
45 surname="Byrne",
46 email="erin.byrne@example.com",
47 )
48 self.assertEqual(output, "Erin Byrne <erin.byrne@example.com>")
50 def test_format_raises_with_disallowed_keys(self) -> None:
51 with self.assertRaises(KeyError):
52 self.formatter.format("{email.__class__}")
54 def test_returns_valid_parameters_string(self) -> None:
55 self.assertEqual(
56 self.formatter.get_valid_parameters_string(),
57 "{forename}, {surname}, {email}",
58 )
60 def test_validate_raises_key_error_for_unknown_key(self) -> None:
61 with self.assertRaises(KeyError):
62 self.formatter.validate("{phone}")
64 def test_validate_raises_value_error_for_mismatched_brackets(self) -> None:
65 with self.assertRaises(ValueError):
66 self.formatter.validate("{forename")