summaryrefslogtreecommitdiff
path: root/tests/test_fuzzy.py
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-04-21 19:35:43 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-04-21 19:38:50 +0200
commita48f6482f21994a44da14dafdeff8e7e1237cec9 (patch)
treedaa9e1dc5ffdeeaf069cb2c759ce31bc7d4b4c0e /tests/test_fuzzy.py
parentb6ecfdf7b8f0b05b3e78fe3aafaf26d9c00c3259 (diff)
downloadfactory-boy-a48f6482f21994a44da14dafdeff8e7e1237cec9.tar
factory-boy-a48f6482f21994a44da14dafdeff8e7e1237cec9.tar.gz
Add FuzzyDateTime/FuzzyNaiveDateTime.
Diffstat (limited to 'tests/test_fuzzy.py')
-rw-r--r--tests/test_fuzzy.py238
1 files changed, 238 insertions, 0 deletions
diff --git a/tests/test_fuzzy.py b/tests/test_fuzzy.py
index e3b5772..97abece 100644
--- a/tests/test_fuzzy.py
+++ b/tests/test_fuzzy.py
@@ -23,9 +23,11 @@
import datetime
+from factory import compat
from factory import fuzzy
from .compat import mock, unittest
+from . import utils
class FuzzyAttributeTestCase(unittest.TestCase):
@@ -163,3 +165,239 @@ class FuzzyDateTestCase(unittest.TestCase):
res = fuzz.evaluate(2, None, False)
self.assertEqual(datetime.date(2013, 1, 2), res)
+
+
+class FuzzyNaiveDateTimeTestCase(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ # Setup useful constants
+ cls.jan1 = datetime.datetime(2013, 1, 1)
+ cls.jan3 = datetime.datetime(2013, 1, 3)
+ cls.jan31 = datetime.datetime(2013, 1, 31)
+
+ def test_accurate_definition(self):
+ """Tests explicit definition of a FuzzyNaiveDateTime."""
+ fuzz = fuzzy.FuzzyNaiveDateTime(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 FuzzyNaiveDateTime without passing an end date."""
+ with utils.mocked_datetime_now(self.jan3, fuzzy):
+ fuzz = fuzzy.FuzzyNaiveDateTime(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_aware_start(self):
+ """Tests that a timezone-aware start datetime is rejected."""
+ self.assertRaises(ValueError, fuzzy.FuzzyNaiveDateTime,
+ self.jan1.replace(tzinfo=compat.UTC), self.jan31)
+
+ def test_aware_end(self):
+ """Tests that a timezone-aware end datetime is rejected."""
+ self.assertRaises(ValueError, fuzzy.FuzzyNaiveDateTime,
+ self.jan1, self.jan31.replace(tzinfo=compat.UTC))
+
+ def test_force_year(self):
+ fuzz = fuzzy.FuzzyNaiveDateTime(self.jan1, self.jan31, force_year=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.year)
+
+ def test_force_month(self):
+ fuzz = fuzzy.FuzzyNaiveDateTime(self.jan1, self.jan31, force_month=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.month)
+
+ def test_force_day(self):
+ fuzz = fuzzy.FuzzyNaiveDateTime(self.jan1, self.jan31, force_day=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.day)
+
+ def test_force_hour(self):
+ fuzz = fuzzy.FuzzyNaiveDateTime(self.jan1, self.jan31, force_hour=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.hour)
+
+ def test_force_minute(self):
+ fuzz = fuzzy.FuzzyNaiveDateTime(self.jan1, self.jan31, force_minute=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.minute)
+
+ def test_force_second(self):
+ fuzz = fuzzy.FuzzyNaiveDateTime(self.jan1, self.jan31, force_second=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.second)
+
+ def test_force_microsecond(self):
+ fuzz = fuzzy.FuzzyNaiveDateTime(self.jan1, self.jan31, force_microsecond=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.microsecond)
+
+ def test_invalid_definition(self):
+ self.assertRaises(ValueError, fuzzy.FuzzyNaiveDateTime,
+ self.jan31, self.jan1)
+
+ def test_invalid_partial_definition(self):
+ with utils.mocked_datetime_now(self.jan1, fuzzy):
+ self.assertRaises(ValueError, fuzzy.FuzzyNaiveDateTime,
+ self.jan31)
+
+ def test_biased(self):
+ """Tests a FuzzyDate with a biased random.randint."""
+
+ fake_randint = lambda low, high: (low + high) // 2
+ fuzz = fuzzy.FuzzyNaiveDateTime(self.jan1, self.jan31)
+
+ with mock.patch('random.randint', fake_randint):
+ res = fuzz.evaluate(2, None, False)
+
+ self.assertEqual(datetime.datetime(2013, 1, 16), res)
+
+ def test_biased_partial(self):
+ """Tests a FuzzyDate with a biased random and implicit upper bound."""
+ with utils.mocked_datetime_now(self.jan3, fuzzy):
+ fuzz = fuzzy.FuzzyNaiveDateTime(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.datetime(2013, 1, 2), res)
+
+
+class FuzzyDateTimeTestCase(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ # Setup useful constants
+ cls.jan1 = datetime.datetime(2013, 1, 1, tzinfo=compat.UTC)
+ cls.jan3 = datetime.datetime(2013, 1, 3, tzinfo=compat.UTC)
+ cls.jan31 = datetime.datetime(2013, 1, 31, tzinfo=compat.UTC)
+
+ def test_accurate_definition(self):
+ """Tests explicit definition of a FuzzyDateTime."""
+ fuzz = fuzzy.FuzzyDateTime(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 FuzzyDateTime without passing an end date."""
+ with utils.mocked_datetime_now(self.jan3, fuzzy):
+ fuzz = fuzzy.FuzzyDateTime(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.FuzzyDateTime,
+ self.jan31, self.jan1)
+
+ def test_invalid_partial_definition(self):
+ with utils.mocked_datetime_now(self.jan1, fuzzy):
+ self.assertRaises(ValueError, fuzzy.FuzzyDateTime,
+ self.jan31)
+
+ def test_naive_start(self):
+ """Tests that a timezone-naive start datetime is rejected."""
+ self.assertRaises(ValueError, fuzzy.FuzzyDateTime,
+ self.jan1.replace(tzinfo=None), self.jan31)
+
+ def test_naive_end(self):
+ """Tests that a timezone-naive end datetime is rejected."""
+ self.assertRaises(ValueError, fuzzy.FuzzyDateTime,
+ self.jan1, self.jan31.replace(tzinfo=None))
+
+ def test_force_year(self):
+ fuzz = fuzzy.FuzzyDateTime(self.jan1, self.jan31, force_year=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.year)
+
+ def test_force_month(self):
+ fuzz = fuzzy.FuzzyDateTime(self.jan1, self.jan31, force_month=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.month)
+
+ def test_force_day(self):
+ fuzz = fuzzy.FuzzyDateTime(self.jan1, self.jan31, force_day=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.day)
+
+ def test_force_hour(self):
+ fuzz = fuzzy.FuzzyDateTime(self.jan1, self.jan31, force_hour=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.hour)
+
+ def test_force_minute(self):
+ fuzz = fuzzy.FuzzyDateTime(self.jan1, self.jan31, force_minute=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.minute)
+
+ def test_force_second(self):
+ fuzz = fuzzy.FuzzyDateTime(self.jan1, self.jan31, force_second=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.second)
+
+ def test_force_microsecond(self):
+ fuzz = fuzzy.FuzzyDateTime(self.jan1, self.jan31, force_microsecond=4)
+
+ for _i in range(20):
+ res = fuzz.evaluate(2, None, False)
+ self.assertEqual(4, res.microsecond)
+
+ def test_biased(self):
+ """Tests a FuzzyDate with a biased random.randint."""
+
+ fake_randint = lambda low, high: (low + high) // 2
+ fuzz = fuzzy.FuzzyDateTime(self.jan1, self.jan31)
+
+ with mock.patch('random.randint', fake_randint):
+ res = fuzz.evaluate(2, None, False)
+
+ self.assertEqual(datetime.datetime(2013, 1, 16, tzinfo=compat.UTC), res)
+
+ def test_biased_partial(self):
+ """Tests a FuzzyDate with a biased random and implicit upper bound."""
+ with utils.mocked_datetime_now(self.jan3, fuzzy):
+ fuzz = fuzzy.FuzzyDateTime(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.datetime(2013, 1, 2, tzinfo=compat.UTC), res)