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

1"""

2 camcops_server/cc_modules/tests/cc_formatter_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 unittest import TestCase

29

30 from camcops_server.cc_modules.cc_formatter import SafeFormatter

31

32

33 class SafeFormatterTests(TestCase):

34 def setUp(self) -> None:

35 super().setUp()

36

37 self.formatter = SafeFormatter(["forename", "surname", "email"])

38

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>")

47

48 def test_format_raises_with_disallowed_keys(self) -> None:

49 with self.assertRaises(KeyError):

50 self.formatter.format("{email.__class__}")

51

52 def test_returns_valid_parameters_string(self) -> None:

53 self.assertEqual(

54 self.formatter.get_valid_parameters_string(),

55 "{forename}, {surname}, {email}",

56 )

57

58 def test_validate_raises_key_error_for_unknown_key(self) -> None:

59 with self.assertRaises(KeyError):

60 self.formatter.validate("{phone}")

61

62 def test_validate_raises_value_error_for_mismatched_brackets(self) -> None:

63 with self.assertRaises(ValueError):

64 self.formatter.validate("{forename")