Made the secret_id func more reusable. Added a test that is none.

This commit is contained in:
Martin Mahner 2014-01-07 12:57:06 +01:00
parent c687ab125a
commit ebdd36cfeb
2 changed files with 15 additions and 2 deletions

View file

@ -15,8 +15,8 @@ L = getattr(settings, 'DPASTE_SLUG_LENGTH', 4)
T = getattr(settings, 'DPASTE_SLUG_CHOICES', T = getattr(settings, 'DPASTE_SLUG_CHOICES',
'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ1234567890') 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ1234567890')
def generate_secret_id(length=L): def generate_secret_id(length=L, alphabet=T):
return ''.join([R.choice(T) for i in range(L)]) return ''.join([R.choice(alphabet) for i in range(length)])
class Snippet(models.Model): class Snippet(models.Model):
secret_id = models.CharField(_(u'Secret ID'), max_length=255, blank=True) secret_id = models.CharField(_(u'Secret ID'), max_length=255, blank=True)

View file

@ -316,3 +316,16 @@ class SnippetTestCase(TestCase):
from dpaste.highlight import pygmentize from dpaste.highlight import pygmentize
pygmentize('code', lexer_name='python') pygmentize('code', lexer_name='python')
pygmentize('code', lexer_name='doesnotexist') pygmentize('code', lexer_name='doesnotexist')
# This is actually a bad test. It is possible to have duplicates
# because even if its random, it can generate two random, equal strings.
#
# def test_random_slug_generation(self):
# """
# Generate 1000 random slugs, make sure we have no duplicates.
# """
# from dpaste.models import generate_secret_id
# result_list = []
# for i in range(0, 1000):
# result_list.append(generate_secret_id())
# self.assertEqual(len(set(result_list)), 1000)