summaryrefslogtreecommitdiff
path: root/factory/fuzzy.py
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2013-10-04 10:52:26 -0700
committerRaphaƫl Barrois <raphael.barrois@polytechnique.org>2013-10-29 00:19:46 +0100
commit990bfaf44ce39aaa01a2107aadc1933947bcf550 (patch)
tree65ed6af3a74f781ab02b088752dccf313514fae7 /factory/fuzzy.py
parent6834fd72331f186a1f1fe793e193b3cfba36d5b7 (diff)
downloadfactory-boy-990bfaf44ce39aaa01a2107aadc1933947bcf550.tar
factory-boy-990bfaf44ce39aaa01a2107aadc1933947bcf550.tar.gz
Added FuzzyText attribute.
Useful for unique model attributes where the specific value can be fuzzy.
Diffstat (limited to 'factory/fuzzy.py')
-rw-r--r--factory/fuzzy.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/factory/fuzzy.py b/factory/fuzzy.py
index d3b130b..f3e6a31 100644
--- a/factory/fuzzy.py
+++ b/factory/fuzzy.py
@@ -23,8 +23,11 @@
"""Additional declarations for "fuzzy" attribute definitions."""
+from __future__ import unicode_literals
+
import random
+import string
import datetime
from . import compat
@@ -60,6 +63,36 @@ class FuzzyAttribute(BaseFuzzyAttribute):
return self.fuzzer()
+class FuzzyText(BaseFuzzyAttribute):
+ """Random string with a given prefix.
+
+ Generates a random string of the given length from chosen chars.
+ If a prefix or a suffix are supplied, they will be prepended / appended
+ to the generated string.
+
+ Args:
+ prefix (text): An optional prefix to prepend to the random string
+ length (int): the length of the random part
+ suffix (text): An optional suffix to append to the random string
+ chars (str list): the chars to choose from
+
+ Useful for generating unique attributes where the exact value is
+ not important.
+ """
+
+ def __init__(self, prefix='', length=12, suffix='',
+ chars=string.ascii_letters, **kwargs):
+ super(FuzzyText, self).__init__(**kwargs)
+ self.prefix = prefix
+ self.suffix = suffix
+ self.length = length
+ self.chars = tuple(chars) # Unroll iterators
+
+ def fuzz(self):
+ chars = [random.choice(self.chars) for _i in range(self.length)]
+ return self.prefix + ''.join(chars) + self.suffix
+
+
class FuzzyChoice(BaseFuzzyAttribute):
"""Handles fuzzy choice of an attribute."""