Skip to content

Commit 978ae55

Browse files
authored
fix hypothesis decimal strategy to fully filter out values that are not compatible with DYNAMODB_CONTEXT (aws#25)
1 parent 50a49d4 commit 978ae55

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

test/functional/hypothesis_strategies.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@
5151
)
5252

5353

54+
def _valid_ddb_number(value):
55+
try:
56+
DYNAMODB_CONTEXT.create_decimal(float(value))
57+
except Exception:
58+
return False
59+
else:
60+
return True
61+
62+
5463
ddb_string = text(
5564
min_size=1,
5665
max_size=MAX_ITEM_BYTES
@@ -60,17 +69,17 @@
6069

6170
def _ddb_fraction_to_decimal(val):
6271
"""Hypothesis does not support providing a custom Context, so working around that."""
63-
return DYNAMODB_CONTEXT.create_decimal(Decimal(val.numerator) / Decimal(val.denominator))
72+
return Decimal(val.numerator) / Decimal(val.denominator)
6473

6574

6675
_ddb_positive_numbers = fractions(
6776
min_value=POSITIVE_NUMBER_RANGE.min,
6877
max_value=POSITIVE_NUMBER_RANGE.max
69-
).map(_ddb_fraction_to_decimal)
78+
).map(_ddb_fraction_to_decimal).filter(_valid_ddb_number)
7079
_ddb_negative_numbers = fractions(
7180
min_value=NEGATIVE_NUMBER_RANGE.min,
7281
max_value=NEGATIVE_NUMBER_RANGE.max
73-
).map(_ddb_fraction_to_decimal)
82+
).map(_ddb_fraction_to_decimal).filter(_valid_ddb_number)
7483

7584
ddb_number = _ddb_negative_numbers | just(Decimal('0')) | _ddb_positive_numbers
7685
ddb_number_set = sets(ddb_number, min_size=1)

0 commit comments

Comments
 (0)