Skip to content

Add more unit tests, use DataclassValidatorFieldException#5

Merged
binaryDiv merged 4 commits into
mainfrom
more-unit-tests
Apr 8, 2026
Merged

Add more unit tests, use DataclassValidatorFieldException#5
binaryDiv merged 4 commits into
mainfrom
more-unit-tests

Conversation

@binaryDiv
Copy link
Copy Markdown
Contributor

This PR adds some more unit tests to the project. I figured it makes sense to have some more unit tests before updating to validataclass 0.12 and changing several things related to that.

I also noticed an inconsistency between the @search_query_dataclass and @validataclass decorators, where the former didn't use the DataclassValidatorFieldException class for errors that are similar or the same as those in the @validataclass decorator, so I fixed that. I don't consider this a breaking change, because it's just a different exception class for errors that effectively mean your code isn't valid.

@binaryDiv binaryDiv self-assigned this Mar 26, 2026
@binaryDiv binaryDiv added the testing Related to testing (e.g. unit tests) label Mar 26, 2026
Comment on lines +102 to +109
assert type(fields['regular_required'].metadata.get('validator')) is StringValidator
assert type(fields['regular_optional'].metadata.get('validator')) is StringValidator
assert type(fields['explicit_field'].metadata.get('validator')) is StringValidator
assert type(fields['filter1'].metadata.get('validator')) is IntegerValidator
assert type(fields['filter2'].metadata.get('validator')) is IntegerValidator
assert type(fields['filter3'].metadata.get('validator')) is IntegerValidator
assert type(fields['filter4'].metadata.get('validator')) is MultiSelectIntegerValidator
assert type(fields['filter5'].metadata.get('validator')) is BooleanValidator
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I wonder if isinstance is more correct here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In general it's better to use isinstance() because it works with subtypes as well, yes. It's less strict.

But here in the unit tests, I don't really see a reason to use a less strict test. The validators should always have the exact types that we used in the dataclass definition. Otherwise something is happening that somehow modifies the validators, which would be unexpected behavior.

@binaryDiv binaryDiv merged commit 21d4709 into main Apr 8, 2026
7 checks passed
@binaryDiv binaryDiv deleted the more-unit-tests branch April 8, 2026 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Related to testing (e.g. unit tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants