Coverage for cc_modules/tests/cc_proquint_tests.py: 34%

41 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-08 23:14 +0000

1""" 

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

28import random 

29import uuid 

30from unittest import TestCase 

31 

32from camcops_server.cc_modules.cc_proquint import ( 

33 int_from_proquint, 

34 InvalidProquintException, 

35 proquint_from_int, 

36 proquint_from_uuid, 

37 uuid_from_proquint, 

38) 

39 

40 

41# ============================================================================= 

42# Unit tests 

43# ============================================================================= 

44 

45 

46class ProquintTest(TestCase): 

47 def test_int_encoded_as_proquint(self) -> None: 

48 self.assertEqual(proquint_from_int(0x493B05EE, 32), "hohur-bilov-j") 

49 

50 def test_uuid_encoded_as_proquint(self) -> None: 

51 self.assertEqual( 

52 proquint_from_uuid( 

53 uuid.UUID("6457cb90-1ca0-47a7-9f40-767567819bee") 

54 ), 

55 "kidil-sovib-dufob-hivol-nutab-linuj-kivad-nozov-t", 

56 ) 

57 

58 def test_proquint_decoded_as_int(self) -> None: 

59 self.assertEqual(int_from_proquint("hohur-bilov-j"), 0x493B05EE) 

60 

61 def test_proquint_decoded_as_uuid(self) -> None: 

62 self.assertEqual( 

63 uuid_from_proquint( 

64 "kidil-sovib-dufob-hivol-nutab-linuj-kivad-nozov-t" 

65 ), 

66 uuid.UUID("6457cb90-1ca0-47a7-9f40-767567819bee"), 

67 ) 

68 

69 def test_ints_converted_to_proquints_and_back(self) -> None: 

70 for bits in (16, 32, 48, 64, 80, 96, 128, 256): 

71 for i in range(1000): 

72 random_int = random.getrandbits(bits) 

73 

74 encoded = proquint_from_int(random_int, bits) 

75 

76 num_expected_words = bits // 16 

77 num_expected_dashes = num_expected_words 

78 check_character_length = 1 

79 expected_proquint_length = ( 

80 5 * num_expected_words 

81 + num_expected_dashes 

82 + check_character_length 

83 ) 

84 self.assertEqual(len(encoded), expected_proquint_length) 

85 

86 decoded = int_from_proquint(encoded) 

87 

88 self.assertEqual( 

89 decoded, 

90 random_int, 

91 msg=( 

92 f"Conversion failed for {random_int}, " 

93 f"encoded={encoded}, decoded={decoded} " 

94 ), 

95 ) 

96 

97 def test_raises_when_bits_not_multiple_of_16(self) -> None: 

98 with self.assertRaises(ValueError) as cm: 

99 proquint_from_int(0, 5) 

100 

101 self.assertEqual( 

102 str(cm.exception), "size_in_bits (5) must be a multiple of 16" 

103 ) 

104 

105 def test_raises_when_proquint_has_invalid_chars(self) -> None: 

106 with self.assertRaises(InvalidProquintException) as cm: 

107 int_from_proquint("lusab-rrrrr-s") 

108 

109 self.assertEqual( 

110 str(cm.exception), 

111 "'lusab-rrrrr-s' contains invalid or transposed characters", 

112 ) 

113 

114 def test_raises_when_proquint_has_chars_in_wrong_order(self) -> None: 

115 with self.assertRaises(InvalidProquintException) as cm: 

116 int_from_proquint("lusab-abadu-b") 

117 

118 self.assertEqual( 

119 str(cm.exception), 

120 "'lusab-abadu-b' contains invalid or transposed characters", 

121 ) 

122 

123 def test_raises_when_check_character_doesnt_match(self) -> None: 

124 with self.assertRaises(InvalidProquintException) as cm: 

125 int_from_proquint("hohur-dilov-j") 

126 

127 self.assertEqual( 

128 str(cm.exception), 

129 "'hohur-dilov-j' is not valid (check character mismatch)", 

130 )