示例#1
0
 def test_encode_hex(self):
     assert Hashids().encode_hex(
         '507f1f77bcf86cd799439011') == 'y42LW46J9luq3Xq9XMly'
     assert len(
         Hashids(min_length=1000).encode_hex(
             '507f1f77bcf86cd799439011')) >= 1000
     assert Hashids().encode_hex(
         'f000000000000000000000000000000000000000000000000000000000000000000000000000000000000f') == \
            'WxMLpERDrmh25Lp4L3xEfM6WovWYO3IjkRMKR2ogCMVzn4zQlqt1WK8jKq7OsEpy2qyw1Vi2p'
示例#2
0
def test_encode(salt, alphabet, min_length, numbers):
    alphabet = str(alphabet)
    salt = str(salt)

    hashids = Hashids(salt=salt, alphabet=alphabet, min_length=min_length)
    hashids_cffi = HashidsCFFI(salt=salt, alphabet=alphabet, min_length=min_length)

    hashids_cffi_encoded_hash = hashids_cffi.encode(*numbers)
    assert hashids.encode(*numbers) == hashids_cffi_encoded_hash

    assert len(hashids_cffi_encoded_hash) >= min_length
示例#3
0
def test_decode(salt, alphabet, min_length, hashid):
    alphabet = str(alphabet)
    salt = str(salt)
    hashid = str(hashid)

    hashids = Hashids(salt=salt, alphabet=alphabet, min_length=min_length)
    hashids_cffi = HashidsCFFI(salt=salt, alphabet=alphabet, min_length=min_length)

    hashid_decoded_numbers = hashids.decode(hashid)
    # TODO: Add overflow checks the to C library
    # These cases causes integer overflow
    assume(all((number <= MAX_ULONGLONG for number in hashid_decoded_numbers)) or (hashid_decoded_numbers == ()))

    assert hashid_decoded_numbers == hashids_cffi.decode(hashid)
示例#4
0
 def test_decode_hex(self):
     hex_str = '507f1f77bcf86cd799439011'
     assert Hashids().decode_hex('y42LW46J9luq3Xq9XMly') == hex_str
     h = Hashids(min_length=1000)
     assert h.decode_hex(h.encode_hex(hex_str)) == hex_str
     assert Hashids().decode_hex('WxMLpERDrmh25Lp4L3xEfM6WovWYO3IjkRMKR2ogCMVzn4zQlqt1WK8jKq7OsEpy2qyw1Vi2p') == \
            'f000000000000000000000000000000000000000000000000000000000000000000000000000000000000f'
示例#5
0
 def test_salt(self, numbers, hashid):
     h = Hashids(salt='Arbitrary string')
     assert h.encode(*numbers) == hashid
示例#6
0
 def test_default_salt(self):
     assert Hashids().decode('o2fXhV') == (1, 2, 3)
示例#7
0
 def test_multiple_numbers(self, numbers, hashid):
     h = Hashids()
     assert h.encode(*numbers) == hashid
示例#8
0
 def test_empty_call(self):
     assert Hashids().encode() == ''
示例#9
0
 def bench():
     h = HashidsCFFI(min_length=min_length)
     h.decode(hashid)
示例#10
0
 def test_all_parameters(self, numbers, hashid):
     h = Hashids('arbitrary salt', 16, 'abcdefghijklmnopqrstuvwxyz')
     assert h.encode(*numbers) == hashid
示例#11
0
 def test_negative_call(self):
     assert Hashids().encode(1, -2, 3) == ''
示例#12
0
 def test_min_length(self, numbers, hashid):
     h = Hashids(min_length=25)
     assert h.decode(hashid) == numbers
示例#13
0
 def test_invalid_hash(self):
     assert Hashids(alphabet='abcdefghijklmnop').decode('qrstuvwxyz') == ()
示例#14
0
 def test_default_salt(self):
     assert Hashids().encode(1, 2, 3) == 'o2fXhV'
示例#15
0
 def test_salt(self, numbers, hashid):
     h = Hashids(salt='Arbitrary string')
     assert h.decode(hashid) == numbers
示例#16
0
 def test_multiple_numbers(self, numbers, hashid):
     h = Hashids()
     assert h.decode(hashid) == numbers
示例#17
0
 def test_single_number(self, numbers, hashid):
     h = Hashids()
     assert h.decode(hashid) == numbers
示例#18
0
 def test_empty_call(self):
     assert Hashids().decode('') == ()
示例#19
0
 def test_alphabet(self, numbers, hashid):
     h = Hashids(
         alphabet=
         '!"#%&\',-/0123456789:;<=>ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz~'
     )
     assert h.encode(*numbers) == hashid
示例#20
0
 def test_alphabet_without_standard_separators(self, numbers, hashid):
     h = Hashids(
         alphabet='abdegjklmnopqrvwxyzABDEGJKLMNOPQRVWXYZ1234567890')
     assert h.decode(hashid) == numbers
示例#21
0
 def test_min_length(self, numbers, hashid):
     h = Hashids(min_length=25)
     assert h.encode(*numbers) == hashid
示例#22
0
 def test_only_one_valid(self):
     h = Hashids(min_length=6)
     assert h.decode(h.encode(1)[:-1] + '0') == ()
示例#23
0
 def test_alphabet_with_two_standard_separators(self, numbers, hashid):
     h = Hashids(
         alphabet='abdegjklmnopqrvwxyzABDEGJKLMNOPQRVWXYZ1234567890uC')
     assert h.encode(*numbers) == hashid
示例#24
0
 def test_empty_string(self):
     assert Hashids().decode('') == ()
示例#25
0
 def test_float_call(self):
     assert Hashids().encode(1, 2.5, 3) == ''
示例#26
0
 def test_single_number(self, number, hashid):
     h = Hashids()
     assert h.encode(number) == hashid
示例#27
0
 def test_illegal_hex(self):
     assert Hashids().encode_hex('') == ''
     assert Hashids().encode_hex('1234SGT8') == ''
示例#28
0
 def test_illegal_decode_hex(self):
     assert Hashids().decode_hex('') == ''
     assert Hashids().decode_hex(
         'WxMLpERDrmh25Lp4L3xEfM6WovWYO3IjkRMKR2ogCMVlqt1WK8jKq7OsEp1Vi2p'
     ) == ''
示例#29
0
 def bench():
     h = HashidsCFFI(min_length=min_length)
     h.encode(1)
示例#30
0
 def test_non_string(self):
     assert Hashids().decode(object()) == ()