Skip to content

Commit 570392b

Browse files
test: Add unit tests for ParseNumberFromBrackets (#3878)
* test: Add unit tests for ParseNumberFromBrackets pyiceberg/utils/parsing.py was the only module under pyiceberg/utils without a dedicated test. ParseNumberFromBrackets backs parsing of bucket[N], truncate[N] and fixed[L], so cover its match behaviour and the ValidationError raised on malformed input. * test: Assert the exact error message and drop redundant cases Verify the full ValidationError message for the invalid-input cases and parametrize them. Remove the multi-digit case (already covered) and the cases that relied on re.search tolerating text around the pattern.
1 parent 8408a5d commit 570392b

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

‎tests/utils/test_parsing.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import pytest
19+
20+
from pyiceberg.exceptions import ValidationError
21+
from pyiceberg.utils.parsing import ParseNumberFromBrackets
22+
23+
24+
def test_match_returns_the_bracketed_number() -> None:
25+
assert ParseNumberFromBrackets("fixed").match("fixed[22]") == 22
26+
assert ParseNumberFromBrackets("bucket").match("bucket[8]") == 8
27+
assert ParseNumberFromBrackets("truncate").match("truncate[16]") == 16
28+
29+
30+
@pytest.mark.parametrize(
31+
"prefix, value",
32+
[
33+
pytest.param("fixed", "decimal[8]", id="wrong-prefix"),
34+
pytest.param("fixed", "fixed", id="missing-brackets"),
35+
pytest.param("truncate", "truncate[abc]", id="non-numeric"),
36+
pytest.param("truncate", "truncate[-1]", id="negative"),
37+
],
38+
)
39+
def test_match_raises_with_expected_message(prefix: str, value: str) -> None:
40+
with pytest.raises(ValidationError) as exc_info:
41+
ParseNumberFromBrackets(prefix).match(value)
42+
assert str(exc_info.value) == f"Could not match {value}, expected format {prefix}[22]"

0 commit comments

Comments
 (0)