Skip to content

Commit

Permalink
Validate scale is not too big
Browse files Browse the repository at this point in the history
  • Loading branch information
faph committed Nov 6, 2023
1 parent b314a2d commit 1057659
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
9 changes: 7 additions & 2 deletions src/py_avro_schema/_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,13 @@ def _size(cls, py_type: Type) -> Tuple[int, int]:

@staticmethod
def _validate_size_tuple(tuple_: Tuple) -> bool:
"""Checks whether a given tuple is a tuple of 2 integers (precision, scale)"""
return len(tuple_) == 2 and all(isinstance(item, int) for item in tuple_)
"""
Checks whether a given tuple is a tuple of (precision, scale)
Must be integers.
Scale must not be greater than precision
"""
return len(tuple_) == 2 and all(isinstance(item, int) for item in tuple_) and tuple_[0] >= tuple_[1]

def data(self, names: NamesType) -> JSONObj:
"""Return the schema data"""
Expand Down
32 changes: 10 additions & 22 deletions tests/test_logicals.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,40 +112,28 @@ def test_annotated_decimal_neg_scale():
assert_schema(py_type, expected)


def test_annotated_decimal_bad_no_tuple():
def test_annotated_decimal_scale_too_big():
py_type = Annotated[decimal.Decimal, (5, 6)]
with pytest.raises(pas.TypeNotSupportedError):
assert_schema(py_type, {})


def test_annotated_decimal_no_tuple():
py_type = Annotated[decimal.Decimal, ...]
expected = {
"type": "bytes",
"logicalType": "decimal",
"precision": 5,
"scale": 2,
}
with pytest.raises(pas.TypeNotSupportedError):
assert_schema(py_type, expected)
assert_schema(py_type, {})


def test_annotated_decimal_tuple_wrong_length():
py_type = Annotated[decimal.Decimal, (3, 2, 1)]
expected = {
"type": "bytes",
"logicalType": "decimal",
"precision": 5,
"scale": 2,
}
with pytest.raises(pas.TypeNotSupportedError):
assert_schema(py_type, expected)
assert_schema(py_type, {})


def test_annotated_decimal_tuple_wrong_type():
py_type = Annotated[decimal.Decimal, ("a", 1)]
expected = {
"type": "bytes",
"logicalType": "decimal",
"precision": 5,
"scale": 2,
}
with pytest.raises(pas.TypeNotSupportedError):
assert_schema(py_type, expected)
assert_schema(py_type, {})


def test_multiple_decimals():
Expand Down

0 comments on commit 1057659

Please sign in to comment.