-
Notifications
You must be signed in to change notification settings - Fork 586
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
FailedHealthCheck with hypothesis.extra.django for django.contrib.auth.models.User model #1112
Comments
|
I would guess you have some complex validation on one or more fields, and the default strategy is giving you data of the right type (eg You can override the strategy for each field of a model, with the example of customer age here - but I'll admit the docs don't make it obvious! (We'll therefore keep this issue open to improve them.) For example: Does that help? |
|
Yeah I suspected that at first too, because we have a lot of ForeignKeys to User. But even in a fresh project the same happens. from django.contrib.auth.models import User
from django.test import TestCase
from hypothesis import given
from hypothesis.extra.django import models as model_st
USER_STRATEGY = model_st.models(User)
class TestUser(TestCase):
@given(USER_STRATEGY)
def test_user(self, user):
assert user.usernameMy django version is Django 2.0.2 with Hypothesis 3.45.0 |
|
You're using We should probably be better about signalling this problem. |
|
I'm going to close this as it's not actually a bug in Hypothesis, but I've open #1113 for communicating this problem more clearly. |
|
That doesn't change it for me. # Create your tests here.
import hypothesis.extra.django
from django.contrib.auth.models import User
from hypothesis import given
from hypothesis.extra.django import models as model_st
USER_STRATEGY = model_st.models(User)
class TestUser(hypothesis.extra.django.TestCase):
@given(USER_STRATEGY)
def test_user(self, user):
assert user.usernamestill gives me
|
|
Interesting. You're absolutely right. I could have sworn we had tests that this worked with the standard auth User, but apparently we don't! Sorry for jumping to conclusions. |
|
Yeah, regressions happen. No worries ;) |
|
Based on some investigation, I'm pretty sure the problem is that the user model has a very restrictive idea of what characters a username can contain, and we're trying to generate arbitrary unicode text for it. We should figure out a proper fix, but the following will let you work around it until we do: import hypothesis.strategies as st
import string
USER_STRATEGY = model_st.models(
User,
username=st.text(
alphabet=string.ascii_letters + string.digits + '@/./+/-/_')) |
|
nitpick: @DRMacIver, I don't think you meant to include the slashes in the character list there 😉 |
I thought this might be new in Django 2.0, but no - supported since 1.10 😞
That's straight outta the Django docs, but you're right that they shouldn't be in the string literal. Instead, why not use the regex that we're failing to validate against? Which, uh, probably is the proper fix: for all string fields that use a |
Uh, yeah, I just copied those out of the validator error message without really thinking about it. |
I mean I do know that there are people using this stuff! I know there are people using Hypothesis + Django reasonably heavily, but I think the number of people using model introspection must be way lower than I thought if this is the first time we've seen this. |
|
Notes dump:
This is a substantial new feature, but it would be really really nice to be able to generate instances of the default user model (!!), and the principle unlocks a massive improvement in specificity of inference and I assume real-world usefulness. If anyone else is using it in the real world 😕 |
|
I suggest we put together a minimal fix - just improving the docs and handling RegexValidator for CharField and TextField - and merge that, leaving a new issue (linked from said docs) with more detailed notes on the larger body of work for external or funded contributions. |
👍 with the possible caveat that it would be really nice to support the default User object and I bet we'll find some other problems when we try to do that. But I agree that just supporting the minimal amount to make that work is 100% the right starting point. |
I just use create the most simple strategy like this:
USER_STRATEGY = models.models(User)Then I get a FailedHealthCheck:
hypothesis.errors.FailedHealthCheck: It looks like your strategy is filtering out a lot of data. Health check found 50 filtered examples but only 3 good ones. This will make your tests much slower ...Any idea what I can do to get User objects for my tests?
The text was updated successfully, but these errors were encountered: