Skip to content

Commit

Permalink
Fix pydecimal distribution when called with a range across 0
Browse files Browse the repository at this point in the history
Co-authored-by: Flavio Curella <89607+fcurella@users.noreply.github.com>
  • Loading branch information
AlexLitvino and fcurella authored Sep 19, 2024
1 parent 4240ef8 commit e96222c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions faker/providers/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def pydecimal(
left_number = str(self._random_int_of_length(left_digits))
else:
if min_value is not None:
left_number = str(self.random_int(int(max(max_value or 0, 0)), int(abs(min_value))))
left_number = str(self.random_int(int(abs(min(max_value or 0, 0))), int(abs(min_value))))
else:
min_left_digits = math.ceil(math.log10(abs(min(max_value or 1, 1))))
if left_digits is None:
Expand All @@ -345,7 +345,7 @@ def pydecimal(
if right_digits is None:
right_digits = self.random_int(0, max_random_digits)

right_number = "".join([str(self.random_digit()) for i in range(0, right_digits)])
right_number = "".join([str(self.random_digit()) for _ in range(0, right_digits)])

result = Decimal(f"{sign}{left_number}.{right_number}")

Expand Down
24 changes: 24 additions & 0 deletions tests/providers/test_python.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import Counter

Check failure on line 1 in tests/providers/test_python.py

View workflow job for this annotation

GitHub Actions / isort

Imports are incorrectly sorted and/or formatted.
import decimal
import sys
import unittest
Expand Down Expand Up @@ -506,6 +507,29 @@ def test_min_value_10_pow_1000_return_greater_number(self):
result = self.fake.pydecimal(min_value=10**1000)
self.assertGreater(result, 10**1000)

def test_min_value_and_max_value_have_different_signs_return_evenly_distributed_values(self):
result = []
boundary_value = 10
for _ in range(1000):
result.append(self.fake.pydecimal(min_value=-boundary_value, max_value=boundary_value, right_digits=0))
self.assertEqual(len(Counter(result)), 2 * boundary_value + 1)

def test_min_value_and_max_value_negative_return_evenly_distributed_values(self):
result = []
min_value = -60
max_value = -50
for _ in range(1000):
result.append(self.fake.pydecimal(min_value=min_value, max_value=max_value, right_digits=0))
self.assertGreater(len(Counter(result)), max_value-min_value)

def test_min_value_and_max_value_positive_return_evenly_distributed_values(self):
result = []
min_value = 50
max_value = 60
for _ in range(1000):
result.append(self.fake.pydecimal(min_value=min_value, max_value=max_value, right_digits=0))
self.assertGreater(len(Counter(result)), max_value-min_value)

def test_min_value_float_returns_correct_digit_number(self):
Faker.seed("6")
result = self.fake.pydecimal(left_digits=1, right_digits=1, min_value=0.2, max_value=0.3)
Expand Down

0 comments on commit e96222c

Please sign in to comment.