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)
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'
def test_salt(self, numbers, hashid): h = Hashids(salt='Arbitrary string') assert h.encode(*numbers) == hashid
def test_default_salt(self): assert Hashids().decode('o2fXhV') == (1, 2, 3)
def test_multiple_numbers(self, numbers, hashid): h = Hashids() assert h.encode(*numbers) == hashid
def test_empty_call(self): assert Hashids().encode() == ''
def bench(): h = HashidsCFFI(min_length=min_length) h.decode(hashid)
def test_all_parameters(self, numbers, hashid): h = Hashids('arbitrary salt', 16, 'abcdefghijklmnopqrstuvwxyz') assert h.encode(*numbers) == hashid
def test_negative_call(self): assert Hashids().encode(1, -2, 3) == ''
def test_min_length(self, numbers, hashid): h = Hashids(min_length=25) assert h.decode(hashid) == numbers
def test_invalid_hash(self): assert Hashids(alphabet='abcdefghijklmnop').decode('qrstuvwxyz') == ()
def test_default_salt(self): assert Hashids().encode(1, 2, 3) == 'o2fXhV'
def test_salt(self, numbers, hashid): h = Hashids(salt='Arbitrary string') assert h.decode(hashid) == numbers
def test_multiple_numbers(self, numbers, hashid): h = Hashids() assert h.decode(hashid) == numbers
def test_single_number(self, numbers, hashid): h = Hashids() assert h.decode(hashid) == numbers
def test_empty_call(self): assert Hashids().decode('') == ()
def test_alphabet(self, numbers, hashid): h = Hashids( alphabet= '!"#%&\',-/0123456789:;<=>ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz~' ) assert h.encode(*numbers) == hashid
def test_alphabet_without_standard_separators(self, numbers, hashid): h = Hashids( alphabet='abdegjklmnopqrvwxyzABDEGJKLMNOPQRVWXYZ1234567890') assert h.decode(hashid) == numbers
def test_min_length(self, numbers, hashid): h = Hashids(min_length=25) assert h.encode(*numbers) == hashid
def test_only_one_valid(self): h = Hashids(min_length=6) assert h.decode(h.encode(1)[:-1] + '0') == ()
def test_alphabet_with_two_standard_separators(self, numbers, hashid): h = Hashids( alphabet='abdegjklmnopqrvwxyzABDEGJKLMNOPQRVWXYZ1234567890uC') assert h.encode(*numbers) == hashid
def test_empty_string(self): assert Hashids().decode('') == ()
def test_float_call(self): assert Hashids().encode(1, 2.5, 3) == ''
def test_single_number(self, number, hashid): h = Hashids() assert h.encode(number) == hashid
def test_illegal_hex(self): assert Hashids().encode_hex('') == '' assert Hashids().encode_hex('1234SGT8') == ''
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)
def test_non_string(self): assert Hashids().decode(object()) == ()