HypothesisWorks / hypothesis

Hypothesis is a powerful, flexible, and easy to use library for property-based testing.
https://hypothesis.works
Other
7.52k stars 584 forks source link

hypothesis[django] provides invalid strings based on field RegexValidator patterns #3714

Closed dakotahorstman closed 1 year ago

dakotahorstman commented 1 year ago

I have the following field in one of my models:

from django.core.validators import RegexValidator
from django.db import models
from django.utils.translation import gettext_lazy as _

...
serial_no = models.CharField(_("Serial Number"), max_length=16, validators=[RegexValidator(regex="^[a-zA-Z]{3}[0-9]{8,13}$")],)

In my tests file, I have the following:

from hypothesis import given
from hypothesis.extra.django import TestCase, from_model

class TestModels(TestCase):

    @given(myobject=from_model(MyObject))
    def test_myobject(self, myobject: MyObject) -> None:
        assert MyObject.objects.filter(id=myobject.id).exists()
        assert myobject.serial_no.isalnum()

assert result.serial_no.isalnum() is falling with the following error from pytest:

    @given(myobject=from_model(MyObject))
    def test_myobject(self, myobject: MyObject) -> None:
        assert MyObject.objects.filter(id=myobject.id).exists()
>       assert myobject.serial_no.isalnum()
E       AssertionError: assert False
E        +  where False = <built-in method isalnum of str object at 0x7f82ae657d70>()
E        +    where <built-in method isalnum of str object at 0x7f82ae657d70> = 'AAA00000000\n'.isalnum
E        +      where 'AAA00000000\n' = <MyObject: MyObject object (45)>.serial_no
E       Falsifying example: test_myobject(
E           self=<thesauce.models.tests.test_models.TestModels testMethod=test_myobject>,
E           myobject=<MyObject: MyObject object (45)>,
E       )

Am I missing something with how my tests are being ran or configured? Perhaps my regex is wrong and I'm just not seeing where?

dakotahorstman commented 1 year ago

Figured out why. Needed \A and \Z instead of ^ and $, respectively.