Hide keyboard shortcuts

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

2camcops_server/cc_modules/tests/cc_proquint_tests.py 

3 

4=============================================================================== 

5 

6 Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com). 

7 

8 This file is part of CamCOPS. 

9 

10 CamCOPS is free software: you can redistribute it and/or modify 

11 it under the terms of the GNU General Public License as published by 

12 the Free Software Foundation, either version 3 of the License, or 

13 (at your option) any later version. 

14 

15 CamCOPS is distributed in the hope that it will be useful, 

16 but WITHOUT ANY WARRANTY; without even the implied warranty of 

17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

18 GNU General Public License for more details. 

19 

20 You should have received a copy of the GNU General Public License 

21 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>. 

22 

23=============================================================================== 

24 

25""" 

26 

27import random 

28import uuid 

29from unittest import TestCase 

30 

31from camcops_server.cc_modules.cc_proquint import ( 

32 int_from_proquint, 

33 InvalidProquintException, 

34 proquint_from_int, 

35 proquint_from_uuid, 

36 uuid_from_proquint, 

37) 

38 

39 

40# ============================================================================= 

41# Unit tests 

42# ============================================================================= 

43 

44class ProquintTest(TestCase): 

45 def test_int_encoded_as_proquint(self) -> None: 

46 self.assertEqual(proquint_from_int(0x493b05ee, 32), "hohur-bilov-j") 

47 

48 def test_uuid_encoded_as_proquint(self) -> None: 

49 self.assertEqual( 

50 proquint_from_uuid( 

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

52 ), 

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

54 ) 

55 

56 def test_proquint_decoded_as_int(self) -> None: 

57 self.assertEqual(int_from_proquint("hohur-bilov-j"), 0x493b05ee) 

58 

59 def test_proquint_decoded_as_uuid(self) -> None: 

60 self.assertEqual( 

61 uuid_from_proquint( 

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

63 ), 

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

65 ) 

66 

67 def test_ints_converted_to_proquints_and_back(self) -> None: 

68 for bits in [16, 32, 48, 64, 80, 96, 128, 256]: 

69 for i in range(1000): 

70 random_int = random.getrandbits(bits) 

71 

72 encoded = proquint_from_int(random_int, bits) 

73 

74 num_expected_words = bits // 16 

75 num_expected_dashes = num_expected_words 

76 check_character_length = 1 

77 expected_proquint_length = (5 * num_expected_words 

78 + num_expected_dashes 

79 + check_character_length) 

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

81 

82 decoded = int_from_proquint(encoded) 

83 

84 self.assertEqual( 

85 decoded, 

86 random_int, 

87 msg=(f"Conversion failed for {random_int}, " 

88 f"encoded={encoded}, decoded={decoded} ") 

89 ) 

90 

91 def test_raises_when_bits_not_multiple_of_16(self) -> None: 

92 with self.assertRaises(ValueError) as cm: 

93 proquint_from_int(0, 5) 

94 

95 self.assertEqual(str(cm.exception), 

96 "size_in_bits (5) must be a multiple of 16") 

97 

98 def test_raises_when_proquint_has_invalid_chars(self) -> None: 

99 with self.assertRaises(InvalidProquintException) as cm: 

100 int_from_proquint("lusab-rrrrr-s") 

101 

102 self.assertEqual( 

103 str(cm.exception), 

104 "'lusab-rrrrr-s' contains invalid or transposed characters" 

105 ) 

106 

107 def test_raises_when_proquint_has_chars_in_wrong_order(self) -> None: 

108 with self.assertRaises(InvalidProquintException) as cm: 

109 int_from_proquint("lusab-abadu-b") 

110 

111 self.assertEqual( 

112 str(cm.exception), 

113 "'lusab-abadu-b' contains invalid or transposed characters" 

114 ) 

115 

116 def test_raises_when_check_character_doesnt_match(self) -> None: 

117 with self.assertRaises(InvalidProquintException) as cm: 

118 int_from_proquint("hohur-dilov-j") 

119 

120 self.assertEqual( 

121 str(cm.exception), 

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

123 )