-
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
Postgres Django backend doesn't allow the null character in non-null strings #1045
Comments
|
I think a possible solution would be to either a) further restrict the set of allowed characters based upon the db backend for the model as this isn't something we can change or b) make it possible to configure this via settings |
|
Would it be enough to name that compound strategy and export it for use in your tests instead? They're first class values, so that'd look something like this: # helper.py
postgres_text = text(alphabet=characters(blacklisted_categories=('Cc', 'Cs')))
# test.py
from helper import postgres_text
@given(postgres_text, ...)
def test_something(pg_txt, ...):
... |
|
Unfortunately not, 99% of the code I'm implementing testing on, operate on
the Django model instances directly. This is part of a put tests in first
and then refactor away the cruft exercise so the refactor to make this
possible is defeating the objective.
I'm currently using a variant of this where I pass a named instance like
this into every text field, but with thousands and thousands of cases where
this will be needed it seems like boilerplate that should be taken care of
by the Django module directly as passing a null string to a model in
postgres is always an error.
…On Fri., 22 Dec. 2017, 21:50 Jack Firth, ***@***.***> wrote:
Would it be enough to name that compound strategy and export it for use in
your tests instead? They're first class values, so that'd look something
like this:
# helper.py
postgres_text = text(alphabet=characters(blacklisted_categories=('Cc', 'Cs')))
# test.py
from helper import postgres_text
@given(postgres_text, ...)def test_something(pg_txt, ...):
...
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1045 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AARy8tXiQPvgyFqnyiENdpf8Qe9QYZh6ks5tDCQ0gaJpZM4RLE_P>
.
|
|
Ah, we're only a few months late to the party! This is a validation error in Django 2, so I'll blacklist null characters in our default TextField / CharField strategies. |
|
Updated to Hypothesis 3.44.13 and tried this out, I'm still getting |
|
That's certainly not good - can you post a reproducing example? Minimal examples are ideal but anything that would let me reproduce this would be great! |
|
@Zac-HD I have the same problem trying to test a django detail view. So I tried to make a minimal sample and I decided to make a model test. I tried to reduce it to the minimal expresion: @given(st.text(min_size=4), st.text(min_size=4))
@pytest.mark.django_db
def test_book_detail_ok(client, first_name, last_name):
author = Author.objects.create(first_name=first_name, last_name=last_name)
assert authorThe error is with this example: If this is a bug I would like to contribute in a solution 😄 |
|
@eduzen This is a separate issue. The problem you're seeing there is that due to limitations in how pytest fixtures work, Hypothesis is not compatible with pytest style tests for Django. You need to use the test case subclasseds. |
|
@DRMacIver thank you. Sorry for the noise. |
|
No worries! It's a very non-obvious error message (for reasons that are mostly out of our control 😢) so confusion is understandable. |
|
Hi @DRMacIver, I did what you said about the test case subclasseds: class Author(models.Model):
"""Model representing an author."""
first_name = models.CharField(max_length=100, blank=True, null=True)
last_name = models.CharField(max_length=100)
date_of_birth = models.DateField(null=True, blank=True)
date_of_death = models.DateField(null=True, blank=True)
slug = models.SlugField(max_length=50, blank=True, null=True)
class TestCompany(TestCase):
@given(st.text(), st.text())
def test_can_find_unique_name(self, first_name, last_name):
assert Author.objects.create(first_name=first_name, last_name=last_name)And I get: E ValueError: A string literal cannot contain NUL (0x00) characters.
/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py:85: ValueError
-------------------------------------------------------------------- Hypothesis ---------------------------------------------------------------------
Falsifying example: test_can_find_unique_name(self=<books.tests.models.test_author.TestCompany testMethod=test_can_find_unique_name>, first_name='',last_name='\x00')is it ok? or is the same thing reported? I also tried with |
|
I think you might need to check the Hypothesis for Django docs - I'd write it as follows, but I'm not entirely sure what you're testing here. class TestCompany(hypothesis.extra.django.TestCase):
@given(hypothesis.extra.django.models.models(Author))
def test_can_find_unique_name(self, author):
assert authorAlternatively, you can specify the |
Generating Model instances with
@givenusing a postgres backend db for testing gives me the following error.It's making it really hard to test my stuff with Hypothesis because I'll have to manually specify all text fields to use something like
text(alphabet=characters(blacklisted_categories=('Cc', 'Cs')))The text was updated successfully, but these errors were encountered: