def test_getrandstr(self, seed):
"""getrandstr()"""
from passlib.utils import getrandstr
wrapper = partial(getrandstr, self.getRandom(seed=seed))
# count 0
self.assertEqual(wrapper('abc',0), '')
# count <0
self.assertRaises(ValueError, wrapper, 'abc', -1)
# letters 0
self.assertRaises(ValueError, wrapper, '', 0)
# letters 1
self.assertEqual(wrapper('a', 5), 'aaaaa')
# NOTE: the following parts are non-deterministic,
# with a small chance of failure (outside chance it may pick
# a string w/o one char, even more remote chance of picking
# same string). to combat this, we run it against multiple
# fixed seeds (using run_with_fixed_seeds decorator),
# and hope that they're sufficient to test the range of behavior.
# letters
x = wrapper(u('abc'), 32)
y = wrapper(u('abc'), 32)
self.assertIsInstance(x, unicode)
self.assertNotEqual(x,y)
self.assertEqual(sorted(set(x)), [u('a'),u('b'),u('c')])
# bytes
x = wrapper(b'abc', 32)
y = wrapper(b'abc', 32)
self.assertIsInstance(x, bytes)
self.assertNotEqual(x,y)
# NOTE: decoding this due to py3 bytes
self.assertEqual(sorted(set(x.decode("ascii"))), [u('a'),u('b'),u('c')])
评论列表
文章目录