#101 pytest model page #102

Closed
lvanwambeke wants to merge 6 commits from dev-1.0-99-pytest-page-model into develop-1.0
2 changed files with 266 additions and 5 deletions

View File

@ -123,9 +123,14 @@ class BasePage(models.Model):
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)[:100]
count = Page.objects.filter(slug__startswith=self.slug).count()
if count:
self.slug += "-" + str(count)
pagecount = Page.objects.filter(slug__startswith=self.slug).count()
staticpagecount = StaticPage.objects.filter(
slug__startswith=self.slug
).count()
if pagecount:
self.slug += "-" + str(pagecount)
if staticpagecount:
self.slug += "-" + str(staticpagecount)
if self.parent and not self.cover:
self.cover = self.parent.cover
@ -152,9 +157,9 @@ class BasePage(models.Model):
@property
def display_title(self):
if self.is_published():
if self.is_published:
return self.title
return self.parent.display_title()
return self.parent.display_title
@cached_property
def headline(self):

View File

@ -0,0 +1,256 @@
import pytest
from model_bakery import baker
from aircox.models import Category, Page, StaticPage, NavItem
@pytest.mark.django_db
class TestCategory:
def test__str__(self):
category = baker.make(Category)
title = category.__str__()
assert title == category.title
# BasePAgeQuerySet is queryset, we use as fake db instances of the model StaticPage in the test. We do not use instances of the Page Model because it's use also as an abstract class.
class TestBasePageQuerySet:
@pytest.mark.django_db
def test_draft(self, staticpages):
draft_pages = StaticPage.objects.draft()
for page in draft_pages:
assert page.status == StaticPage.STATUS_DRAFT
@pytest.mark.django_db
def test_published(self, staticpages):
published_pages = StaticPage.objects.published()
for page in published_pages:
assert page.status == StaticPage.STATUS_PUBLISHED
@pytest.mark.django_db
def test_trash(self, staticpages):
trashed_pages = StaticPage.objects.trash()
for page in trashed_pages:
assert page.status == StaticPage.STATUS_TRASH
@pytest.mark.django_db
def test_parent_return_childs_from_parent_object(self, staticpages):
parent = baker.make(StaticPage)
childs = baker.make(StaticPage, _quantity=3, parent=parent)
nochild = baker.make(StaticPage, parent=None)
parent_childs = StaticPage.objects.parent(parent)
for child in childs:
assert child in parent_childs
assert nochild not in parent_childs
@pytest.mark.django_db
def test_parent_return_childs_from_parent_id(self, staticpages):
parent = baker.make(StaticPage)
childs = baker.make(StaticPage, _quantity=3, parent=parent)
nochild = baker.make(StaticPage, parent=None)
parent_childs = StaticPage.objects.parent(parent.id)
for child in childs:
assert child in parent_childs
assert nochild not in parent_childs
@pytest.mark.django_db
def test_search_with_searchcontent(self, staticpages):
page1 = baker.make(StaticPage, title="test")
page2 = baker.make(StaticPage, content="test")
q = "test"
results = StaticPage.objects.search(q=q)
assert page1 in results
assert page2 in results
@pytest.mark.django_db
def test_search_without_searchcontent(self, staticpages):
page1 = baker.make(StaticPage, title="test")
page2 = baker.make(StaticPage, content="test")
q = "test"
results = StaticPage.objects.search(q=q, search_content=False)
assert page1 in results
assert page2 not in results
# BasePage is abstract, we use as fake db instances of the model StaticPage in the test. We do not use instances of the Page Model because it's use as an abstract class also.
class TestBasePage:
@pytest.mark.django_db
def test__str__(self, staticpages):
page = baker.make(StaticPage, title="Test")
assert page.__str__() == "Test"
@pytest.mark.django_db
def test_save_generate_unique_slug(self, staticpages):
page = baker.make(StaticPage, title="Title with spaces", slug=None)
page.save()
assert page.slug == "title-with-spaces"
@pytest.mark.django_db
def test_save_generate_slug_that_already_exist(self, staticpages):
baker.make(StaticPage, slug="title")
page = baker.make(StaticPage, title="Title", slug=None)
page.save()
assert page.slug == "title-1"
@pytest.mark.django_db
def test_save_without_cover_with_parent(self, staticpages):
parent = baker.make(StaticPage)
child = baker.make(StaticPage, cover=None, parent=parent)
child.save()
assert child.cover == parent.cover
@pytest.mark.django_db
def test_get_absolute_url_from_unpublished_page(self, staticpages):
unpublished_page = baker.make(
StaticPage, slug="page-slug", status=StaticPage.STATUS_DRAFT
)
assert unpublished_page.get_absolute_url() == "#"
@pytest.mark.django_db
def test_get_absolute_url_from_published_page(self, staticpages):
published_page = baker.make(
StaticPage,
slug="another-page-slug",
status=StaticPage.STATUS_PUBLISHED,
)
assert published_page.get_absolute_url() == "/pages/another-page-slug/"
# as the following test methods are property from page.py, we call them without the parenthesis.
@pytest.mark.django_db
def test_is_draft(self, staticpages):
page = baker.make(StaticPage, status=StaticPage.STATUS_DRAFT)
assert page.is_draft == True
@pytest.mark.django_db
def test_is_published(self, staticpages):
page = baker.make(StaticPage, status=StaticPage.STATUS_PUBLISHED)
assert page.is_published == True
@pytest.mark.django_db
def test_is_draft(self, staticpages):
page = baker.make(StaticPage, status=StaticPage.STATUS_TRASH)
assert page.is_trash == True
@pytest.mark.django_db
def test_display_title(self, staticpages):
parent_page = baker.make(
StaticPage,
title="Parent page title",
status=StaticPage.STATUS_PUBLISHED,
)
child_unpublished_page = baker.make(
StaticPage,
title="Child page title",
parent=parent_page,
status=StaticPage.STATUS_DRAFT,
)
assert parent_page.display_title == "Parent page title"
assert child_unpublished_page.display_title == "Parent page title"
@pytest.mark.django_db
def test_headline(self, staticpages):
page = baker.make(
StaticPage, content="<h2>My headline</h2><p>My content.</p>"
)
empty_content_page = baker.make(StaticPage, content="")
assert page.headline == "My headline\n"
assert empty_content_page.headline == ""
#this classmethod is not suitable for StaticPage because staticpage don't have category argument. It's intend only for Page.
@pytest.mark.django_db
def test_get_init_kwargs_from(self, staticpages):
page = baker.make(Page)
kwargs = Page.get_init_kwargs_from(page)
assert kwargs == {"cover": page.cover, "category": page.category}
#this classmethod is not suitable for StaticPage because staticpage don't have category argument. It's intend only for Page.
@pytest.mark.django_db
def test_from_page(self, pages):
page_object = baker.make(Page)
new_page_instance = Page.from_page(page_object)
assert new_page_instance.cover == page_object.cover
assert new_page_instance.category == page_object.category
class TestPageQuerySet:
@pytest.mark.django_db
def test_published(self, pages):
baker.make(Page, _quantity=5)
published_pages_list = Page.objects.published()
for page in published_pages_list:
assert page.status == Page.STATUS_PUBLISHED
class TestPage:
@pytest.mark.django_db
def test_save_published_page_pubdate(self, pages):
page = baker.make(Page, status=Page.STATUS_PUBLISHED, pub_date=None)
page.save()
assert page.pub_date is not None
@pytest.mark.django_db
def test_save_unpublished_page_pubdate(self, pages):
page = baker.make(Page, status=Page.STATUS_DRAFT, pub_date=None)
page.save()
assert page.pub_date == None
@pytest.mark.django_db
def test_save_child_page_category(self, pages):
parent = baker.make(Page)
child = baker.make(Page, parent=parent, category=None)
child.save()
assert child.category == parent.category
class TestStaticPage:
@pytest.mark.django_db
def test_get_absolute_url(self, staticpages):
page = baker.make(
StaticPage, attach_to=StaticPage.ATTACH_TO_DIFFUSIONS
)
assert page.get_absolute_url() == "/week/"
class TestNavItem:
@pytest.mark.django_db
def test_get_url_from_selfurl(self, navitem):
assert navitem.url == navitem.get_url()
@pytest.mark.django_db
def test_get_url_from_pageurl(self, navitem):
page = baker.make(StaticPage)
item = baker.make(NavItem, url=None, page=page)
assert item.get_url() == page.get_absolute_url()
@pytest.mark.django_db
def test_get_url_without_selfurl_page(self, navitem):
item = baker.make(NavItem, url=None, page=None)
assert item.get_url() == None