I am just starting to use pytest and faker for testing
while trying to create text for a field in testing db the constraints are being ignored and i don't know how to fix it.
models.py
from django.db import models
# Create your models here.
class Note(models.Model):
body = models.TextField(null=True, blank=True, max_length=5)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.body[0:50]
factories.py
import factory
from faker import Faker
fake = Faker()
from mynotes_api.models import Note
class NoteFactory(factory.django.DjangoModelFactory):
class Meta:
model = Note
body = fake.text()
conftest.py
import pytest
from django.contrib.auth.models import User
from pytest_factoryboy import register
from tests.factories import NoteFactory
register(NoteFactory)
@pytest.fixture
def new_user1(db, note_factory):
note = note_factory.create()
return note
test_ex1.py
import pytest
def test_product(new_user1):
note = new_user1
print(note.body)
assert True
the problem as visible in output is that the length of the text generated and being stored in the testing db is more than 5.
kindly guide me in this regard.
CodePudding user response:
Iain Shelvington's advice to use a CharField is the main issue here. It will enforce the database constraint.
Django offers more types of validation that cannot be enforced by the database. These will not be checked on a model.save call:
Note that validators will not be run automatically when you save a model, but if you are using a ModelForm, it will run your validators on any fields that are included in your form.

