summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSaul Shanabrook <s.shanabrook@gmail.com>2013-04-16 10:49:31 -0300
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-04-21 19:38:29 +0200
commitb6ecfdf7b8f0b05b3e78fe3aafaf26d9c00c3259 (patch)
treee2b212ba77490b55fe6dc04ebc3f171b4d7ecc45 /tests
parent876845102c4a217496d0f6435bfe1e3726d31fe4 (diff)
downloadfactory-boy-b6ecfdf7b8f0b05b3e78fe3aafaf26d9c00c3259.tar
factory-boy-b6ecfdf7b8f0b05b3e78fe3aafaf26d9c00c3259.tar.gz
Add FuzzyDate
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/test_fuzzy.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/test_fuzzy.py b/tests/test_fuzzy.py
index 70a2095..e3b5772 100644
--- a/tests/test_fuzzy.py
+++ b/tests/test_fuzzy.py
@@ -21,6 +21,8 @@
# THE SOFTWARE.
+import datetime
+
from factory import fuzzy
from .compat import mock, unittest
@@ -102,3 +104,62 @@ class FuzzyIntegerTestCase(unittest.TestCase):
res = fuzz.evaluate(2, None, False)
self.assertEqual(8, res)
+
+
+class FuzzyDateTestCase(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ # Setup useful constants
+ cls.jan1 = datetime.date(2013, 1, 1)
+ cls.jan3 = datetime.date(2013, 1, 3)
+ cls.jan31 = datetime.date(2013, 1, 31)
+
+ def test_accurate_definition(self):
+ """Tests all ways of defining a FuzzyDate."""
+ fuzz = fuzzy.FuzzyDate(self.jan1, self.jan31)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertLessEqual(self.jan1, res)
+ self.assertLessEqual(res, self.jan31)
+
+ def test_partial_definition(self):
+ """Test defining a FuzzyDate without passing an end date."""
+ with utils.mocked_date_today(self.jan3, fuzzy):
+ fuzz = fuzzy.FuzzyDate(self.jan1)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertLessEqual(self.jan1, res)
+ self.assertLessEqual(res, self.jan3)
+
+ def test_invalid_definition(self):
+ self.assertRaises(ValueError, fuzzy.FuzzyDate,
+ self.jan31, self.jan1)
+
+ def test_invalid_partial_definition(self):
+ with utils.mocked_date_today(self.jan1, fuzzy):
+ self.assertRaises(ValueError, fuzzy.FuzzyDate,
+ self.jan31)
+
+ def test_biased(self):
+ """Tests a FuzzyDate with a biased random.randint."""
+
+ fake_randint = lambda low, high: (low + high) // 2
+ fuzz = fuzzy.FuzzyDate(self.jan1, self.jan31)
+
+ with mock.patch('random.randint', fake_randint):
+ res = fuzz.evaluate(2, None, False)
+
+ self.assertEqual(datetime.date(2013, 1, 16), res)
+
+ def test_biased_partial(self):
+ """Tests a FuzzyDate with a biased random and implicit upper bound."""
+ with utils.mocked_date_today(self.jan3, fuzzy):
+ fuzz = fuzzy.FuzzyDate(self.jan1)
+
+ fake_randint = lambda low, high: (low + high) // 2
+ with mock.patch('random.randint', fake_randint):
+ res = fuzz.evaluate(2, None, False)
+
+ self.assertEqual(datetime.date(2013, 1, 2), res)