aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-10-29 00:23:11 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-10-29 00:26:21 +0100
commit382e319a934d27e16b9d0ae8597923ab85976694 (patch)
tree550e295beb260e3ad6961d36a57f559e6135d7cc
parent132eca8fa36bc7360a3b2270da0c8833378bc718 (diff)
downloadfactory-boy-382e319a934d27e16b9d0ae8597923ab85976694.tar
factory-boy-382e319a934d27e16b9d0ae8597923ab85976694.tar.gz
Style fixes for FuzzyDecimal (Closes #94).
-rw-r--r--docs/changelog.rst1
-rw-r--r--docs/fuzzy.rst2
-rw-r--r--factory/fuzzy.py5
-rw-r--r--tests/test_fuzzy.py34
4 files changed, 22 insertions, 20 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst
index 630b45f..09de792 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -10,6 +10,7 @@ ChangeLog
*New:*
- Add :class:`~factory.fuzzy.FuzzyText`, thanks to `jdufresne <https://github.com/jdufresne>`_ (:issue:`97`)
+ - Add :class:`~factory.fuzzy.FuzzyDecimal`, thanks to `thedrow <https://github.com/thedrow>`_ (:issue:`94`)
.. _v2.2.1:
diff --git a/docs/fuzzy.rst b/docs/fuzzy.rst
index 1843920..b94dfa5 100644
--- a/docs/fuzzy.rst
+++ b/docs/fuzzy.rst
@@ -97,7 +97,7 @@ FuzzyInteger
.. attribute:: high
int, the inclusive higher bound of generated integers
-
+
FuzzyDecimal
------------
diff --git a/factory/fuzzy.py b/factory/fuzzy.py
index 7fa0908..6c7a866 100644
--- a/factory/fuzzy.py
+++ b/factory/fuzzy.py
@@ -25,7 +25,7 @@
from __future__ import unicode_literals
-from decimal import Decimal
+import decimal
import random
import string
import datetime
@@ -136,7 +136,8 @@ class FuzzyDecimal(BaseFuzzyAttribute):
super(FuzzyDecimal, self).__init__(**kwargs)
def fuzz(self):
- return Decimal(random.uniform(self.low, self.high)).quantize(Decimal(10) ** -self.precision)
+ base = decimal.Decimal(random.uniform(self.low, self.high))
+ return base.quantize(decimal.Decimal(10) ** -self.precision)
class FuzzyDate(BaseFuzzyAttribute):
diff --git a/tests/test_fuzzy.py b/tests/test_fuzzy.py
index 2202c8f..b97f4bd 100644
--- a/tests/test_fuzzy.py
+++ b/tests/test_fuzzy.py
@@ -22,7 +22,7 @@
import datetime
-from decimal import Decimal
+import decimal
from factory import compat
from factory import fuzzy
@@ -115,17 +115,17 @@ class FuzzyDecimalTestCase(unittest.TestCase):
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)
+ self.assertTrue(decimal.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)
+ self.assertTrue(decimal.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(decimal.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):
@@ -136,7 +136,7 @@ class FuzzyDecimalTestCase(unittest.TestCase):
with mock.patch('random.uniform', fake_uniform):
res = fuzz.evaluate(2, None, False)
- self.assertEqual(Decimal(10.0), res)
+ self.assertEqual(decimal.Decimal(10.0), res)
def test_biased_high_only(self):
fake_uniform = lambda low, high: low + high
@@ -146,7 +146,7 @@ class FuzzyDecimalTestCase(unittest.TestCase):
with mock.patch('random.uniform', fake_uniform):
res = fuzz.evaluate(2, None, False)
- self.assertEqual(Decimal(8.0), res)
+ self.assertEqual(decimal.Decimal(8.0), res)
def test_precision(self):
fake_uniform = lambda low, high: low + high + 0.001
@@ -156,7 +156,7 @@ class FuzzyDecimalTestCase(unittest.TestCase):
with mock.patch('random.uniform', fake_uniform):
res = fuzz.evaluate(2, None, False)
- self.assertEqual(Decimal(8.001).quantize(Decimal(10) ** -3), res)
+ self.assertEqual(decimal.Decimal(8.001).quantize(Decimal(10) ** -3), res)
class FuzzyDateTestCase(unittest.TestCase):
@@ -188,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."""
@@ -248,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)
@@ -306,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."""
@@ -365,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)