summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--factory/compat.py8
-rw-r--r--factory/fuzzy.py2
-rw-r--r--tests/test_fuzzy.py12
3 files changed, 13 insertions, 9 deletions
diff --git a/factory/compat.py b/factory/compat.py
index 785d174..737d91a 100644
--- a/factory/compat.py
+++ b/factory/compat.py
@@ -42,14 +42,6 @@ else: # pragma: no cover
from io import BytesIO
-if sys.version_info[:2] == (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 923d8b7..a7e834c 100644
--- a/factory/fuzzy.py
+++ b/factory/fuzzy.py
@@ -164,7 +164,7 @@ class FuzzyDecimal(BaseFuzzyAttribute):
super(FuzzyDecimal, self).__init__(**kwargs)
def fuzz(self):
- base = compat.float_to_decimal(_random.uniform(self.low, self.high))
+ base = decimal.Decimal(str(_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 3f9c434..d83f3dd 100644
--- a/tests/test_fuzzy.py
+++ b/tests/test_fuzzy.py
@@ -189,6 +189,18 @@ class FuzzyDecimalTestCase(unittest.TestCase):
self.assertEqual(decimal.Decimal('8.001').quantize(decimal.Decimal(10) ** -3), res)
+ def test_no_approximation(self):
+ """We should not go through floats in our fuzzy calls unless actually needed."""
+ fuzz = fuzzy.FuzzyDecimal(0, 10)
+
+ decimal_context = decimal.getcontext()
+ old_traps = decimal_context.traps[decimal.FloatOperation]
+ try:
+ decimal_context.traps[decimal.FloatOperation] = True
+ fuzz.evaluate(2, None, None)
+ finally:
+ decimal_context.traps[decimal.FloatOperation] = old_traps
+
class FuzzyDateTestCase(unittest.TestCase):
@classmethod