Skip to content

Commit

Permalink
Fix issue where Faker does not properly convert min/max value to deci…
Browse files Browse the repository at this point in the history
…mal (#2101)
  • Loading branch information
bdjellabaldebaran authored Sep 4, 2024
1 parent 3a75ddb commit cd6e847
Show file tree
Hide file tree
Showing 2 changed files with 12 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 @@ -352,9 +352,9 @@ def pydecimal(
# Because the random result might have the same number of decimals as max_value the random number
# might be above max_value or below min_value
if max_value is not None and result > max_value:
result = Decimal(max_value)
result = Decimal(str(max_value))
if min_value is not None and result < min_value:
result = Decimal(min_value)
result = Decimal(str(min_value))

return result

Expand Down
10 changes: 10 additions & 0 deletions tests/providers/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,16 @@ 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_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)
self.assertEqual(decimal.Decimal("0.2"), result)

def test_max_value_float_returns_correct_digit_number(self):
Faker.seed("3")
result = self.fake.pydecimal(left_digits=1, right_digits=1, min_value=0.2, max_value=0.3)
self.assertEqual(decimal.Decimal("0.3"), result)


class TestPystr(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit cd6e847

Please sign in to comment.