summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-10-29 01:07:43 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-10-29 01:07:43 +0100
commit5c10062ce0ffb83010aba800f25f2df3f6540211 (patch)
tree3ea77a4a85f33044705c002ce5fc8a469fd09385
parentcea04334adfa5ac1458465dcf7f76aa8ee2ed425 (diff)
downloadfactory-boy-5c10062ce0ffb83010aba800f25f2df3f6540211.tar
factory-boy-5c10062ce0ffb83010aba800f25f2df3f6540211.tar.gz
Fix FuzzyDecimal on Python2.6.
-rw-r--r--factory/compat.py10
-rw-r--r--factory/fuzzy.py2
-rw-r--r--tests/test_fuzzy.py15
3 files changed, 20 insertions, 7 deletions
diff --git a/factory/compat.py b/factory/compat.py
index f77458f..1e0b2d2 100644
--- a/factory/compat.py
+++ b/factory/compat.py
@@ -24,6 +24,7 @@
"""Compatibility tools"""
import datetime
+import decimal
import sys
PY2 = (sys.version_info[0] == 2)
@@ -40,6 +41,15 @@ else: # pragma: no cover
from io import BytesIO
+
+if sys.version_info[:1] == (2, 6): # pragma: no cover
+ def float_to_decimal(fl):
+ return decimal.Decimal(str(fl))
+else: # pragma: no cover
+ def float_to_decimal(fl):
+ return decimal.Decimal(fl)
+
+
try: # pragma: no cover
# Python >= 3.2
UTC = datetime.timezone.utc
diff --git a/factory/fuzzy.py b/factory/fuzzy.py
index 6c7a866..34949c5 100644
--- a/factory/fuzzy.py
+++ b/factory/fuzzy.py
@@ -136,7 +136,7 @@ class FuzzyDecimal(BaseFuzzyAttribute):
super(FuzzyDecimal, self).__init__(**kwargs)
def fuzz(self):
- base = decimal.Decimal(random.uniform(self.low, self.high))
+ base = compat.float_to_decimal(random.uniform(self.low, self.high))
return base.quantize(decimal.Decimal(10) ** -self.precision)
diff --git a/tests/test_fuzzy.py b/tests/test_fuzzy.py
index 050f8a1..d6f33bb 100644
--- a/tests/test_fuzzy.py
+++ b/tests/test_fuzzy.py
@@ -115,17 +115,20 @@ 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.Decimal(2.0) <= res <= decimal.Decimal(3.0), 'value is not between 2.0 and 3.0. It is %d' % res)
+ self.assertTrue(decimal.Decimal('2.0') <= res <= decimal.Decimal('3.0'),
+ "value %d is not between 2.0 and 3.0" % res)
fuzz = fuzzy.FuzzyDecimal(4.0)
for _i in range(20):
res = fuzz.evaluate(2, None, False)
- self.assertTrue(decimal.Decimal(0.0) <= res <= decimal.Decimal(4.0), 'value is not between 0.0 and 4.0. It is %d' % res)
+ self.assertTrue(decimal.Decimal('0.0') <= res <= decimal.Decimal('4.0'),
+ "value %d is not between 0.0 and 4.0" % res)
fuzz = fuzzy.FuzzyDecimal(1.0, 4.0, precision=5)
for _i in range(20):
res = fuzz.evaluate(2, None, False)
- self.assertTrue(decimal.Decimal(0.54) <= res <= decimal.Decimal(4.0), 'value is not between 0.54 and 4.0. It is %d' % res)
+ self.assertTrue(decimal.Decimal('0.54') <= res <= decimal.Decimal('4.0'),
+ "value %d is not between 0.54 and 4.0" % res)
self.assertTrue(res.as_tuple().exponent, -5)
def test_biased(self):
@@ -136,7 +139,7 @@ class FuzzyDecimalTestCase(unittest.TestCase):
with mock.patch('random.uniform', fake_uniform):
res = fuzz.evaluate(2, None, False)
- self.assertEqual(decimal.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 +149,7 @@ class FuzzyDecimalTestCase(unittest.TestCase):
with mock.patch('random.uniform', fake_uniform):
res = fuzz.evaluate(2, None, False)
- self.assertEqual(decimal.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 +159,7 @@ class FuzzyDecimalTestCase(unittest.TestCase):
with mock.patch('random.uniform', fake_uniform):
res = fuzz.evaluate(2, None, False)
- self.assertEqual(decimal.Decimal(8.001).quantize(decimal.Decimal(10) ** -3), res)
+ self.assertEqual(decimal.Decimal('8.001').quantize(decimal.Decimal(10) ** -3), res)
class FuzzyDateTestCase(unittest.TestCase):