Add some models tests

This commit is contained in:
Gabriel Augendre 2020-12-03 22:04:45 +01:00
parent e237f5799b
commit 5384af6846
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
2 changed files with 43 additions and 0 deletions

View file

@ -110,11 +110,13 @@ class Article(AdminUrlMixin, models.Model):
self.published_at = timezone.now()
self.status = self.PUBLISHED
self.save()
return self
def unpublish(self):
self.published_at = None
self.status = self.DRAFT
self.save()
return self
def save(self, *args, **kwargs):
if not self.slug:

View file

@ -0,0 +1,41 @@
import pytest
from articles.models import Article, User
@pytest.mark.django_db
def test_publish_article(unpublished_article: Article):
assert unpublished_article.status == Article.DRAFT
assert unpublished_article.published_at is None
published_article = unpublished_article.publish()
assert published_article.status == Article.PUBLISHED
assert published_article.published_at is not None
@pytest.mark.django_db
def test_unpublish_article(published_article: Article):
assert published_article.status == Article.PUBLISHED
assert published_article.published_at is not None
unpublished_article = published_article.unpublish()
assert unpublished_article.status == Article.DRAFT
assert unpublished_article.published_at is None
@pytest.mark.django_db
def test_save_article_adds_missing_slug(author: User):
# Explicitly calling bulk_create with one article because it doesn't call save().
articles = Article.objects.bulk_create(
[Article(author=author, title="noice title", slug="", status=Article.DRAFT)]
)
article = articles[0]
assert article.slug == ""
article.save()
assert article.slug != ""
@pytest.mark.django_db
def test_save_article_doesnt_change_existing_slug(published_article: Article):
original_slug = published_article.slug
published_article.title = "This is a brand new title"
published_article.save()
assert published_article.slug == original_slug