Even just learning the django and testwriting, so I need a bit of confirmation. I would like to know that the following tests are "correct" spelling of it.
test_view.py
@pytest.mark.django_db
def test_create_form(client):
form_data = {'title': 'Test Title', 'tags': 'HTML', 'content': 'New Content for lambs'}
response = client.post(reverse("public:create"), form_data, follow=True)
assert b'Test Title' in response.content
@pytest.mark.django_db
def test_context_create(client):
form_data = {'title': 'Test Title', 'tags': 'HTML', 'content': 'New Content for lambs'}
response = client.post(reverse("public:create"), form_data)
assert response.context['form'].cleaned_data['title'] == 'Test Title'
@pytest.mark.django_db
def test_TITLE_is_blank(client):
form_data = {'title': '', 'tags': 'HTML', 'content': 'New Content for lambs'}
response = client.post(reverse("public:create"), form_data)
assert response.context['form'].cleaned_data['title'] == ''
@pytest.mark.django_db
def test_CONTENT_is_blank(client):
form_data = {'title': 'Test Title', 'tags': 'HTML', 'content': ''}
response = client.post(reverse("public:create"), form_data)
assert response.context['form'].cleaned_data['content'] == ''
@pytest.mark.django_db
def test_CONTENT_and_TITLE_is_blank(client):
form_data = {'title': '', 'tags': 'HTML', 'content': ''}
response = client.post(reverse("public:create"), form_data)
assert (not response.context['form'].is_valid())
forms.py
class PostForm(ModelForm):
def __init__(self, *args, **kwargs):
super(PostForm, self).__init__(*args,**kwargs)
self.fields['title'].required = False
self.fields['content'].required = False
class Meta:
model = Post
fields = ['title', 'tags', 'content']
def clean(self):
form_data = self.cleaned_data
if (form_data['title'] is not "") or (form_data['content'] is not ""):
return form_data
else:
self._errors["title"] = ["Two field is empty"]
def save(self, *args, **kwargs):
instance = super(PostForm, self).save(commit=False)
instance.slug = slugify(instance.title)
instance.save()
return instance
This is the model
class Post(models.Model):
title = models.CharField(max_length=200)
tags = models.ManyToManyField(Tags)
slug = models.SlugField(max_length=200, blank=True)
creation_date = models.DateTimeField(auto_now_add=True)
modify_date = models.DateTimeField(auto_now=True)
content = models.TextField()
Thank you for your help and advice.
Aucun commentaire:
Enregistrer un commentaire