aboutsummaryrefslogtreecommitdiff
path: root/tests
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 /tests
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 'tests')
-rw-r--r--tests/test_fuzzy.py41
1 files changed, 40 insertions, 1 deletions
diff --git a/tests/test_fuzzy.py b/tests/test_fuzzy.py
index 97abece..a521ee2 100644
--- a/tests/test_fuzzy.py
+++ b/tests/test_fuzzy.py
@@ -63,7 +63,7 @@ class FuzzyChoiceTestCase(unittest.TestCase):
def options():
for i in range(3):
yield i
-
+
d = fuzzy.FuzzyChoice(options())
res = d.evaluate(2, None, False)
@@ -401,3 +401,42 @@ class FuzzyDateTimeTestCase(unittest.TestCase):
res = fuzz.evaluate(2, None, False)
self.assertEqual(datetime.datetime(2013, 1, 2, tzinfo=compat.UTC), res)
+
+
+class FuzzyTextTestCase(unittest.TestCase):
+
+ def test_unbiased(self):
+ chars = ['a', 'b', 'c']
+ fuzz = fuzzy.FuzzyText(prefix='pre', suffix='post', chars=chars, length=12)
+ res = fuzz.evaluate(2, None, False)
+
+ self.assertEqual('pre', res[:3])
+ self.assertEqual('post', res[-4:])
+ self.assertEqual(3 + 12 + 4, len(res))
+
+ for char in res[3:-4]:
+ self.assertIn(char, chars)
+
+ def test_mock(self):
+ fake_choice = lambda chars: chars[0]
+
+ chars = ['a', 'b', 'c']
+ fuzz = fuzzy.FuzzyText(prefix='pre', suffix='post', chars=chars, length=4)
+ with mock.patch('random.choice', fake_choice):
+ res = fuzz.evaluate(2, None, False)
+
+ self.assertEqual('preaaaapost', res)
+
+ def test_generator(self):
+ def options():
+ yield 'a'
+ yield 'b'
+ yield 'c'
+
+ fuzz = fuzzy.FuzzyText(chars=options(), length=12)
+ res = fuzz.evaluate(2, None, False)
+
+ self.assertEqual(12, len(res))
+
+ for char in res:
+ self.assertIn(char, ['a', 'b', 'c'])