Coverage for cc_modules/tests/cc_formatter_tests.py : 48%

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/cc_modules/tests/cc_formatter_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 unittest import TestCase
31from camcops_server.cc_modules.cc_formatter import SafeFormatter
34class SafeFormatterTests(TestCase):
35 def setUp(self) -> None:
36 super().setUp()
38 self.formatter = SafeFormatter(["forename", "surname", "email"])
40 def test_formats_with_allowed_keys(self) -> None:
41 output = self.formatter.format("{forename} {surname} <{email}>",
42 forename="Erin", surname="Byrne",
43 email="erin.byrne@example.com")
44 self.assertEqual(output, "Erin Byrne <erin.byrne@example.com>")
46 def test_format_raises_with_disallowed_keys(self) -> None:
47 with self.assertRaises(KeyError):
48 self.formatter.format("{email.__class__}")
50 def test_returns_valid_parameters_string(self) -> None:
51 self.assertEqual(self.formatter.get_valid_parameters_string(),
52 "{forename}, {surname}, {email}")
54 def test_validate_raises_key_error_for_unknown_key(self) -> None:
55 with self.assertRaises(KeyError):
56 self.formatter.validate("{phone}")
58 def test_validate_raises_value_error_for_mismatched_brackets(self) -> None:
59 with self.assertRaises(ValueError):
60 self.formatter.validate("{forename")