mardi 30 avril 2019

How to make a test on a regex

I'm new in Django and I have just started to make tests on my web application. Firstly I'm not sure if it's necessary to run tests on regex model fields (like the one in my code), secondly, if the testing is necessary, how can I do it?

I've already tried this solution: Unit Tests pass against regex validator of models in Django but it's not working. cf field needs a 16 chars string, but my function test_cf_max_length() is returning seller object even if the cf entered is wrong(<16 chars)

models.py

class Seller(User):
    cf = models.CharField(validators=[RegexValidator(regex='^.{16}$', message='Social Security Number', code='nomatch')], max_length=16)
    iban = models.CharField(validators=[RegexValidator(regex='^.{27}$', message='IBAN', code='nomatch')], max_length=27)
    is_seller = models.BooleanField(default=False)

tests.py

def setUpTestData(cls):
    Seller.objects.create(username='clara', cf='12345690123456', iban='123456789012345678901234567')


def test_cf_max_length(self):
    seller = Seller.objects.get(id=1)
    with self.assertRaises(ValidationError):
        if seller.full_clean():
            seller.save()
    self.assertEqual(Seller.objects.filter(id=1).count(), 0)

Aucun commentaire:

Enregistrer un commentaire