aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2015-03-26 22:22:03 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2015-03-26 22:22:03 +0100
commit69befae5fde1897cf68c4d44a146db5ba642c814 (patch)
tree88d696ef1784cd73552a5ecd13bc3f4af5d2ac2a /tests
parent40d4a4b13d4ca959879d1798f24d510fd7abf4dc (diff)
downloadfactory-boy-69befae5fde1897cf68c4d44a146db5ba642c814.tar
factory-boy-69befae5fde1897cf68c4d44a146db5ba642c814.tar.gz
Allow lazy evaluation of FuzzyChoice's iterators (Closes #184).
This allows the following idiom: ``user = factory.fuzzy.FuzzyChoice(User.objects.all())`` Previously, the ``User.objects.all()`` queryset would have been evaluated *at import time*; it is now evaluated with the first use of the ``FuzzyChoice``.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_fuzzy.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/tests/test_fuzzy.py b/tests/test_fuzzy.py
index fd32705..c7e1106 100644
--- a/tests/test_fuzzy.py
+++ b/tests/test_fuzzy.py
@@ -74,6 +74,24 @@ class FuzzyChoiceTestCase(unittest.TestCase):
res = d.evaluate(2, None, False)
self.assertIn(res, [0, 1, 2])
+ def test_lazy_generator(self):
+ class Gen(object):
+ def __init__(self, options):
+ self.options = options
+ self.unrolled = False
+
+ def __iter__(self):
+ self.unrolled = True
+ return iter(self.options)
+
+ opts = Gen([1, 2, 3])
+ d = fuzzy.FuzzyChoice(opts)
+ self.assertFalse(opts.unrolled)
+
+ res = d.evaluate(2, None, False)
+ self.assertIn(res, [1, 2, 3])
+ self.assertTrue(opts.unrolled)
+
class FuzzyIntegerTestCase(unittest.TestCase):
def test_definition(self):