19-test_hashids.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. from hashids import Hashids
  2. import pytest
  3. class TestConstructor(object):
  4. def test_small_alphabet_with_no_repeating_characters(self):
  5. pytest.raises(ValueError, Hashids, alphabet='abcdefghijklmno')
  6. def test_small_alphabet_with_repeating_characters(self):
  7. pytest.raises(ValueError, Hashids, alphabet='abcdecfghijklbmnoa')
  8. class TestEncoding(object):
  9. def test_empty_call(self):
  10. assert Hashids().encode() == ''
  11. def test_default_salt(self):
  12. assert Hashids().encode(1, 2, 3) == 'o2fXhV'
  13. def test_single_number(self):
  14. h = Hashids()
  15. assert h.encode(12345) == 'j0gW'
  16. assert h.encode(1) == 'jR'
  17. assert h.encode(22) == 'Lw'
  18. assert h.encode(333) == 'Z0E'
  19. assert h.encode(9999) == 'w0rR'
  20. def test_multiple_numbers(self):
  21. h = Hashids()
  22. assert h.encode(683, 94108, 123, 5) == 'vJvi7On9cXGtD'
  23. assert h.encode(1, 2, 3) == 'o2fXhV'
  24. assert h.encode(2, 4, 6) == 'xGhmsW'
  25. assert h.encode(99, 25) == '3lKfD'
  26. def test_salt(self):
  27. h = Hashids(salt='Arbitrary string')
  28. assert h.encode(683, 94108, 123, 5) == 'QWyf8yboH7KT2'
  29. assert h.encode(1, 2, 3) == 'neHrCa'
  30. assert h.encode(2, 4, 6) == 'LRCgf2'
  31. assert h.encode(99, 25) == 'JOMh1'
  32. def test_alphabet(self):
  33. h = Hashids(alphabet='!"#%&\',-/0123456789:;<=>ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz~')
  34. assert h.encode(2839, 12, 32, 5) == '_nJUNTVU3'
  35. assert h.encode(1, 2, 3) == '7xfYh2'
  36. assert h.encode(23832) == 'Z6R>'
  37. assert h.encode(99, 25) == 'AYyIB'
  38. def test_short_alphabet(self):
  39. h = Hashids(alphabet='ABcfhistuCFHISTU')
  40. assert h.encode(2839, 12, 32, 5) == 'AABAABBBABAAAuBBAAUABBBBBCBAB'
  41. assert h.encode(1, 2, 3) == 'AAhBAiAA'
  42. assert h.encode(23832) == 'AABAAABABBBAABBB'
  43. assert h.encode(99, 25) == 'AAABBBAAHBBAAB'
  44. def test_min_length(self):
  45. h = Hashids(min_length=25)
  46. assert h.encode(7452, 2967, 21401) == 'pO3K69b86jzc6krI416enr2B5'
  47. assert h.encode(1, 2, 3) == 'gyOwl4B97bo2fXhVaDR0Znjrq'
  48. assert h.encode(6097) == 'Nz7x3VXyMYerRmWeOBQn6LlRG'
  49. assert h.encode(99, 25) == 'k91nqP3RBe3lKfDaLJrvy8XjV'
  50. def test_all_parameters(self):
  51. h = Hashids('arbitrary salt', 16, 'abcdefghijklmnopqrstuvwxyz')
  52. assert h.encode(7452, 2967, 21401) == 'wygqxeunkatjgkrw'
  53. assert h.encode(1, 2, 3) == 'pnovxlaxuriowydb'
  54. assert h.encode(60125) == 'jkbgxljrjxmlaonp'
  55. assert h.encode(99, 25) == 'erdjpwrgouoxlvbx'
  56. def test_alphabet_without_standard_separators(self):
  57. h = Hashids(alphabet='abdegjklmnopqrvwxyzABDEGJKLMNOPQRVWXYZ1234567890')
  58. assert h.encode(7452, 2967, 21401) == 'X50Yg6VPoAO4'
  59. assert h.encode(1, 2, 3) == 'GAbDdR'
  60. assert h.encode(60125) == '5NMPD'
  61. assert h.encode(99, 25) == 'yGya5'
  62. def test_alphabet_with_two_standard_separators(self):
  63. h = Hashids(alphabet='abdegjklmnopqrvwxyzABDEGJKLMNOPQRVWXYZ1234567890uC')
  64. assert h.encode(7452, 2967, 21401) == 'GJNNmKYzbPBw'
  65. assert h.encode(1, 2, 3) == 'DQCXa4'
  66. assert h.encode(60125) == '38V1D'
  67. assert h.encode(99, 25) == '373az'
  68. def test_negative_call(self):
  69. assert Hashids().encode(1, -2, 3) == ''
  70. def test_float_call(self):
  71. assert Hashids().encode(1, 2.5, 3) == ''
  72. def test_encode_hex(self):
  73. assert Hashids().encode_hex('507f1f77bcf86cd799439011') == 'y42LW46J9luq3Xq9XMly'
  74. assert len(Hashids(min_length=1000).encode_hex('507f1f77bcf86cd799439011')) >= 1000
  75. assert Hashids().encode_hex('f000000000000000000000000000000000000000000000000000000000000000000000000000000000000f') == \
  76. 'WxMLpERDrmh25Lp4L3xEfM6WovWYO3IjkRMKR2ogCMVzn4zQlqt1WK8jKq7OsEpy2qyw1Vi2p'
  77. def test_illegal_hex(self):
  78. assert Hashids().encode_hex('') == ''
  79. assert Hashids().encode_hex('1234SGT8') == ''
  80. class TestDecoding(object):
  81. def test_empty_string(self):
  82. assert Hashids().decode('') == ()
  83. def test_non_string(self):
  84. assert Hashids().decode(object()) == ()
  85. def test_default_salt(self):
  86. assert Hashids().decode('o2fXhV') == (1, 2, 3)
  87. def test_empty_call(self):
  88. assert Hashids().decode('') == ()
  89. def test_single_number(self):
  90. h = Hashids()
  91. assert h.decode('j0gW') == (12345,)
  92. assert h.decode('jR') == (1,)
  93. assert h.decode('Lw') == (22,)
  94. assert h.decode('Z0E') == (333,)
  95. assert h.decode('w0rR') == (9999,)
  96. def test_multiple_numbers(self):
  97. h = Hashids()
  98. assert h.decode('vJvi7On9cXGtD') == (683, 94108, 123, 5)
  99. assert h.decode('o2fXhV') == (1, 2, 3)
  100. assert h.decode('xGhmsW') == (2, 4, 6)
  101. assert h.decode('3lKfD') == (99, 25)
  102. def test_salt(self):
  103. h = Hashids(salt='Arbitrary string')
  104. assert h.decode('QWyf8yboH7KT2') == (683, 94108, 123, 5)
  105. assert h.decode('neHrCa') == (1, 2, 3)
  106. assert h.decode('LRCgf2') == (2, 4, 6)
  107. assert h.decode('JOMh1') == (99, 25)
  108. def test_alphabet(self):
  109. h = Hashids(alphabet='!"#%&\',-/0123456789:;<=>ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz~')
  110. assert h.decode('_nJUNTVU3') == (2839, 12, 32, 5)
  111. assert h.decode('7xfYh2') == (1, 2, 3)
  112. assert h.decode('Z6R>') == (23832,)
  113. assert h.decode('AYyIB') == (99, 25)
  114. def test_min_length(self):
  115. h = Hashids(min_length=25)
  116. assert h.decode('pO3K69b86jzc6krI416enr2B5') == (7452, 2967, 21401)
  117. assert h.decode('gyOwl4B97bo2fXhVaDR0Znjrq') == (1, 2, 3)
  118. assert h.decode('Nz7x3VXyMYerRmWeOBQn6LlRG') == (6097,)
  119. assert h.decode('k91nqP3RBe3lKfDaLJrvy8XjV') == (99, 25)
  120. def test_all_parameters(self):
  121. h = Hashids('arbitrary salt', 16, 'abcdefghijklmnopqrstuvwxyz')
  122. assert h.decode('wygqxeunkatjgkrw') == (7452, 2967, 21401)
  123. assert h.decode('pnovxlaxuriowydb') == (1, 2, 3)
  124. assert h.decode('jkbgxljrjxmlaonp') == (60125,)
  125. assert h.decode('erdjpwrgouoxlvbx') == (99, 25)
  126. def test_invalid_hash(self):
  127. assert Hashids(alphabet='abcdefghijklmnop').decode('qrstuvwxyz') == ()
  128. def test_alphabet_without_standard_separators(self):
  129. h = Hashids(alphabet='abdegjklmnopqrvwxyzABDEGJKLMNOPQRVWXYZ1234567890')
  130. assert h.decode('X50Yg6VPoAO4') == (7452, 2967, 21401)
  131. assert h.decode('GAbDdR') == (1, 2, 3)
  132. assert h.decode('5NMPD') == (60125,)
  133. assert h.decode('yGya5') == (99, 25)
  134. def test_alphabet_with_two_standard_separators(self):
  135. h = Hashids(alphabet='abdegjklmnopqrvwxyzABDEGJKLMNOPQRVWXYZ1234567890uC')
  136. assert h.decode('GJNNmKYzbPBw') == (7452, 2967, 21401)
  137. assert h.decode('DQCXa4') == (1, 2, 3)
  138. assert h.decode('38V1D') == (60125,)
  139. assert h.decode('373az') == (99, 25)
  140. def test_only_one_valid(self):
  141. h = Hashids(min_length=6)
  142. assert h.decode(h.encode(1)[:-1] + '0') == ()
  143. def test_decode_hex(self):
  144. hex_str = '507f1f77bcf86cd799439011'
  145. assert Hashids().decode_hex('y42LW46J9luq3Xq9XMly') == hex_str
  146. h = Hashids(min_length=1000)
  147. assert h.decode_hex(h.encode_hex(hex_str)) == hex_str
  148. assert Hashids().decode_hex('WxMLpERDrmh25Lp4L3xEfM6WovWYO3IjkRMKR2ogCMVzn4zQlqt1WK8jKq7OsEpy2qyw1Vi2p') == \
  149. 'f000000000000000000000000000000000000000000000000000000000000000000000000000000000000f'
  150. def test_illegal_decode_hex(self):
  151. assert Hashids().decode_hex('') == ''
  152. assert Hashids().decode_hex('WxMLpERDrmh25Lp4L3xEfM6WovWYO3IjkRMKR2ogCMVlqt1WK8jKq7OsEp1Vi2p') == ''