-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add named_model
function and raise warning for factories using generic models
#167
Conversation
It will be helpful when using SubFactory and RelatedFactory for generic types like dict, str, etc.
|
||
def test_generic_model_with_custom_name_no_warning(testdir): | ||
testdir.makepyfile( | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIP: let's indent whole string content or just this line to make code more readable
Co-authored-by: Łukasz Skarżyński <[email protected]>
with warnings.catch_warnings(): | ||
warnings.simplefilter("error") | ||
assert get_model_name(ModelFactory) == "foo" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIP: to make this test more explicit and obvious we can check emitted warnings in an assert statement explicitly:
with pytest.warns(None) as recorded_warnings:
assert get_model_name(ModelFactory) == "foo"
assert not recorded_warnings
We can also pass the exact warning class (UserWarning
in our case) instead of None
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the way that pytest suggests: https://docs.pytest.org/en/stable/how-to/capture-warnings.html#:~:text=To%20ensure%20that%20no%20warnings%20are%20emitted%2C%20use%3A
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice, somehow I missed it in their docs, thanks!
Implementing the idea previously described in #163 (comment).
We add a helper function,
named_model
, that makes sure the user uses a model with a specific name, rather than a generic dict, list, etc.This way we avoid problems that arises when SubFactory and RelatedFactory are used with factories for generic models. See the tests for examples.