Skip to content

test: Add unit tests for ParseNumberFromBrackets - #3878

Open
damansingh1313 wants to merge 2 commits into
apache:mainfrom
damansingh1313:test-parse-number-from-brackets
Open

test: Add unit tests for ParseNumberFromBrackets#3878
damansingh1313 wants to merge 2 commits into
apache:mainfrom
damansingh1313:test-parse-number-from-brackets

Conversation

@damansingh1313

Copy link
Copy Markdown

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.

Rationale for this change

Are these changes tested?

It is a unit tests which are tested in local console.

Are there any user-facing changes?

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.
@damansingh1313

Copy link
Copy Markdown
Author

Hi @kevinjqliu, @Fokko
Could you please review the PR.

Comment thread tests/utils/test_parsing.py Outdated


def test_match_reads_multi_digit_values() -> None:
assert ParseNumberFromBrackets("fixed").match("fixed[1024]") == 1024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The multi digit is already covered by test_match_returns_the_bracketed_number, right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, fixed[22] / truncate[16] already cover it. Removed the extra case, thanks.

Comment thread tests/utils/test_parsing.py Outdated
Comment on lines +34 to +41
def test_match_ignores_text_around_the_prefix() -> None:
# the implementation searches for the pattern, so surrounding text is tolerated
assert ParseNumberFromBrackets("fixed").match(" fixed[5] ") == 5
assert ParseNumberFromBrackets("bucket").match("transform=bucket[4]") == 4


def test_match_returns_the_first_occurrence() -> None:
assert ParseNumberFromBrackets("bucket").match("bucket[3] bucket[7]") == 3

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true today, but I'm curious if that should be the expected behavior. Raising an exception for such cases is standard practice for me.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, I'd lean the same way. I've dropped the tests that relied on that lenient re.search behavior so this PR doesn't pin it, tightening match to reject surrounding text seems worth a separate follow-up.

Comment thread tests/utils/test_parsing.py Outdated
Comment on lines +50 to +63
def test_match_raises_when_brackets_are_missing() -> None:
with pytest.raises(ValidationError):
ParseNumberFromBrackets("fixed").match("fixed")


def test_match_raises_for_a_non_numeric_argument() -> None:
with pytest.raises(ValidationError):
ParseNumberFromBrackets("truncate").match("truncate[abc]")


def test_match_raises_for_a_negative_number() -> None:
# the pattern only accepts digits, so a leading minus sign does not match
with pytest.raises(ValidationError):
ParseNumberFromBrackets("truncate").match("truncate[-1]")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we verify the exception message?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, the invalid-input cases are parametrized now and assert the full message (Could not match , expected format [22]).

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants