Coverage for cc_modules/tests/cc_request_tests.py: 43%

1"""

2 camcops_server/cc_modules/tests/cc_request_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 mock

29

30 from camcops_server.cc_modules.cc_unittest import DemoRequestTestCase

31

32

33 class RequestTests(DemoRequestTestCase):

34 DEFAULT_LANGUAGE = "da_DK"

35 DEFAULT_LANGUAGE_ISO_639_1 = "da"

36

37 def setUp(self) -> None:

38 super().setUp()

39

40 self.req.config.language = self.DEFAULT_LANGUAGE

41

42 def test_gettext_danish(self) -> None:

43 self.req._debugging_user = mock.Mock(language="da_DK")

44

45 # Something unlikely to change

46 self.assertEqual(self.req.gettext("Cancel"), "Annuller")

47

48 def test_language_returns_default_if_no_user(self) -> None:

49 self.req._debugging_user = None

50

51 self.assertEqual(self.req.language, self.DEFAULT_LANGUAGE)

52

53 def test_language_returns_users_if_set(self) -> None:

54 self.req._debugging_user = mock.Mock(language="en_GB")

55

56 self.assertEqual(self.req.language, "en_GB")

57

58 def test_language_returns_default_if_users_not_set(self) -> None:

59 self.req._debugging_user = mock.Mock(language=None)

60

61 self.assertEqual(self.req.language, self.DEFAULT_LANGUAGE)

62

63 def test_language_returns_default_if_users_not_valid(self) -> None:

64 self.req._debugging_user = mock.Mock(language="es_ES")

65

66 self.assertEqual(self.req.language, self.DEFAULT_LANGUAGE)

67

68 def test_language_iso_639_1_returns_default_if_no_user(self) -> None:

69 self.req._debugging_user = None

70

71 self.assertEqual(

72 self.req.language_iso_639_1, self.DEFAULT_LANGUAGE_ISO_639_1

73 )

74

75 def test_language_iso_639_1_returns_users_if_set(self) -> None:

76 self.req._debugging_user = mock.Mock(language="en_GB")

77

78 self.assertEqual(self.req.language_iso_639_1, "en")

79

80 def test_language_iso_639_1_returns_default_if_users_not_set(self) -> None:

81 self.req._debugging_user = mock.Mock(language=None)

82

83 self.assertEqual(

84 self.req.language_iso_639_1, self.DEFAULT_LANGUAGE_ISO_639_1

85 )

86

87 def test_language_iso_639_1_returns_default_if_users_not_valid(

88 self,

89 ) -> None:

90 self.req._debugging_user = mock.Mock(language="d")

91

92 self.assertEqual(

93 self.req.language_iso_639_1, self.DEFAULT_LANGUAGE_ISO_639_1

94 )