summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-04-11 23:59:32 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-04-11 23:59:32 +0200
commit54971381fb31167d1f47b5e705480e044d702602 (patch)
treee5474cf88b5eca77515ee8fd72c84d87971b6494 /tests
parent8c1784e8c1eac65f66b4a1ecc4b8b0ddd5de9327 (diff)
downloadfactory-boy-54971381fb31167d1f47b5e705480e044d702602.tar
factory-boy-54971381fb31167d1f47b5e705480e044d702602.tar.gz
Add factory.fuzzy (Closes #41).
Diffstat (limited to 'tests')
-rw-r--r--tests/__init__.py1
-rw-r--r--tests/test_fuzzy.py104
2 files changed, 105 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
index 7531edd..3c620d6 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -4,5 +4,6 @@
from .test_base import *
from .test_containers import *
from .test_declarations import *
+from .test_fuzzy import *
from .test_using import *
from .test_utils import *
diff --git a/tests/test_fuzzy.py b/tests/test_fuzzy.py
new file mode 100644
index 0000000..70a2095
--- /dev/null
+++ b/tests/test_fuzzy.py
@@ -0,0 +1,104 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2010 Mark Sandstrom
+# Copyright (c) 2011-2013 Raphaël Barrois
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+
+from factory import fuzzy
+
+from .compat import mock, unittest
+
+
+class FuzzyAttributeTestCase(unittest.TestCase):
+ def test_simple_call(self):
+ d = fuzzy.FuzzyAttribute(lambda: 10)
+
+ res = d.evaluate(2, None, False)
+ self.assertEqual(10, res)
+
+ res = d.evaluate(2, None, False)
+ self.assertEqual(10, res)
+
+
+class FuzzyChoiceTestCase(unittest.TestCase):
+ def test_unbiased(self):
+ options = [1, 2, 3]
+ d = fuzzy.FuzzyChoice(options)
+ res = d.evaluate(2, None, False)
+ self.assertIn(res, options)
+
+ def test_mock(self):
+ options = [1, 2, 3]
+ fake_choice = lambda d: sum(d)
+
+ d = fuzzy.FuzzyChoice(options)
+
+ with mock.patch('random.choice', fake_choice):
+ res = d.evaluate(2, None, False)
+
+ self.assertEqual(6, res)
+
+ def test_generator(self):
+ def options():
+ for i in range(3):
+ yield i
+
+ d = fuzzy.FuzzyChoice(options())
+
+ res = d.evaluate(2, None, False)
+ self.assertIn(res, [0, 1, 2])
+
+ # And repeat
+ res = d.evaluate(2, None, False)
+ self.assertIn(res, [0, 1, 2])
+
+
+class FuzzyIntegerTestCase(unittest.TestCase):
+ def test_definition(self):
+ """Tests all ways of defining a FuzzyInteger."""
+ fuzz = fuzzy.FuzzyInteger(2, 3)
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertIn(res, [2, 3])
+
+ fuzz = fuzzy.FuzzyInteger(4)
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertIn(res, [0, 1, 2, 3, 4])
+
+ def test_biased(self):
+ fake_randint = lambda low, high: low + high
+
+ fuzz = fuzzy.FuzzyInteger(2, 8)
+
+ with mock.patch('random.randint', fake_randint):
+ res = fuzz.evaluate(2, None, False)
+
+ self.assertEqual(10, res)
+
+ def test_biased_high_only(self):
+ fake_randint = lambda low, high: low + high
+
+ fuzz = fuzzy.FuzzyInteger(8)
+
+ with mock.patch('random.randint', fake_randint):
+ res = fuzz.evaluate(2, None, False)
+
+ self.assertEqual(8, res)