diff options
author | Omer <omer@stokeet.com> | 2013-09-30 12:37:15 +0300 |
---|---|---|
committer | Raphaƫl Barrois <raphael.barrois@polytechnique.org> | 2013-10-29 00:25:32 +0100 |
commit | 132eca8fa36bc7360a3b2270da0c8833378bc718 (patch) | |
tree | 366eff75d3c2d12c67d96aeb56da61d8d3bc746a /tests | |
parent | dc482f67c83be2b2607e2e9c4b259c03a59d9532 (diff) | |
download | factory-boy-132eca8fa36bc7360a3b2270da0c8833378bc718.tar factory-boy-132eca8fa36bc7360a3b2270da0c8833378bc718.tar.gz |
Added a Fuzzy Decimal attribute.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_fuzzy.py | 71 |
1 files changed, 61 insertions, 10 deletions
diff --git a/tests/test_fuzzy.py b/tests/test_fuzzy.py index a521ee2..2202c8f 100644 --- a/tests/test_fuzzy.py +++ b/tests/test_fuzzy.py @@ -22,6 +22,7 @@ import datetime +from decimal import Decimal from factory import compat from factory import fuzzy @@ -108,6 +109,56 @@ class FuzzyIntegerTestCase(unittest.TestCase): self.assertEqual(8, res) +class FuzzyDecimalTestCase(unittest.TestCase): + def test_definition(self): + """Tests all ways of defining a FuzzyDecimal.""" + fuzz = fuzzy.FuzzyDecimal(2.0, 3.0) + for _i in range(20): + res = fuzz.evaluate(2, None, False) + self.assertTrue(Decimal(2.0) <= res <= Decimal(3.0), 'value is not between 2.0 and 3.0. It is %d' % res) + + fuzz = fuzzy.FuzzyDecimal(4.0) + for _i in range(20): + res = fuzz.evaluate(2, None, False) + self.assertTrue(Decimal(0.0) <= res <= Decimal(4.0), 'value is not between 0.0 and 4.0. It is %d' % res) + + fuzz = fuzzy.FuzzyDecimal(1.0, 4.0, precision=5) + for _i in range(20): + res = fuzz.evaluate(2, None, False) + self.assertTrue(Decimal(0.54) <= res <= Decimal(4.0), 'value is not between 0.54 and 4.0. It is %d' % res) + self.assertTrue(res.as_tuple().exponent, -5) + + def test_biased(self): + fake_uniform = lambda low, high: low + high + + fuzz = fuzzy.FuzzyDecimal(2.0, 8.0) + + with mock.patch('random.uniform', fake_uniform): + res = fuzz.evaluate(2, None, False) + + self.assertEqual(Decimal(10.0), res) + + def test_biased_high_only(self): + fake_uniform = lambda low, high: low + high + + fuzz = fuzzy.FuzzyDecimal(8.0) + + with mock.patch('random.uniform', fake_uniform): + res = fuzz.evaluate(2, None, False) + + self.assertEqual(Decimal(8.0), res) + + def test_precision(self): + fake_uniform = lambda low, high: low + high + 0.001 + + fuzz = fuzzy.FuzzyDecimal(8.0, precision=3) + + with mock.patch('random.uniform', fake_uniform): + res = fuzz.evaluate(2, None, False) + + self.assertEqual(Decimal(8.001).quantize(Decimal(10) ** -3), res) + + class FuzzyDateTestCase(unittest.TestCase): @classmethod def setUpClass(cls): @@ -137,12 +188,12 @@ class FuzzyDateTestCase(unittest.TestCase): def test_invalid_definition(self): self.assertRaises(ValueError, fuzzy.FuzzyDate, - self.jan31, self.jan1) + 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) + self.jan31) def test_biased(self): """Tests a FuzzyDate with a biased random.randint.""" @@ -197,12 +248,12 @@ class FuzzyNaiveDateTimeTestCase(unittest.TestCase): 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) + 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)) + self.jan1, self.jan31.replace(tzinfo=compat.UTC)) def test_force_year(self): fuzz = fuzzy.FuzzyNaiveDateTime(self.jan1, self.jan31, force_year=4) @@ -255,12 +306,12 @@ class FuzzyNaiveDateTimeTestCase(unittest.TestCase): def test_invalid_definition(self): self.assertRaises(ValueError, fuzzy.FuzzyNaiveDateTime, - self.jan31, self.jan1) + 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) + self.jan31) def test_biased(self): """Tests a FuzzyDate with a biased random.randint.""" @@ -314,22 +365,22 @@ class FuzzyDateTimeTestCase(unittest.TestCase): def test_invalid_definition(self): self.assertRaises(ValueError, fuzzy.FuzzyDateTime, - self.jan31, self.jan1) + 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) + 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) + 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)) + self.jan1, self.jan31.replace(tzinfo=None)) def test_force_year(self): fuzz = fuzzy.FuzzyDateTime(self.jan1, self.jan31, force_year=4) |