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