#101 : Test model page #105
|
@ -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):
|
||||
|
|
|
@ -11,6 +11,21 @@ from aircox import models
|
|||
def stations():
|
||||
return baker.make(models.Station, _quantity=2)
|
||||
|
||||
@pytest.fixture
|
||||
def category():
|
||||
return baker.make(models.Category)
|
||||
|
||||
@pytest.fixture
|
||||
def staticpages():
|
||||
return baker.make(models.StaticPage, _quantity=2)
|
||||
|
||||
@pytest.fixture
|
||||
def pages():
|
||||
return baker.make(models.Page, _quantity=2)
|
||||
|
||||
@pytest.fixture
|
||||
def navitem():
|
||||
return baker.make(models.NavItem)
|
||||
|
||||
@pytest.fixture
|
||||
def programs(stations):
|
||||
|
|
196
aircox/tests/models/test_page.py
Normal file
196
aircox/tests/models/test_page.py
Normal file
|
@ -0,0 +1,196 @@
|
|||
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):
|
||||
assert category.__str__() == category.title
|
||||
|
||||
# BasePAgeQuerySet is queryset, we use StaticPage in the test.
|
||||
# We do not use instances of the Page Model because it's use as abstract.
|
||||
class TestBasePageQuerySet:
|
||||
@pytest.mark.django_db
|
||||
def test_draft(self, staticpages):
|
||||
for page in StaticPage.objects.draft():
|
||||
assert page.status == StaticPage.STATUS_DRAFT
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_published(self, staticpages):
|
||||
for page in StaticPage.objects.published():
|
||||
assert page.status == StaticPage.STATUS_PUBLISHED
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_trash(self, staticpages):
|
||||
for page in StaticPage.objects.trash():
|
||||
assert page.status == StaticPage.STATUS_TRASH
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_parent_return_childs_from_parent_object(self, staticpages):
|
||||
staticpages[0].parent = staticpages[1]
|
||||
for child in StaticPage.objects.parent(staticpages[1]):
|
||||
assert child.parent == staticpages[1]
|
||||
assert child != staticpages[1]
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_parent_return_childs_from_parent_id(self, staticpages):
|
||||
staticpages[0].parent = staticpages[1]
|
||||
for child in StaticPage.objects.parent(staticpages[1].id):
|
||||
assert child.parent == staticpages[1]
|
||||
assert child != staticpages[1]
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_search_with_searchcontent(self, staticpages):
|
||||
staticpages[0].title, staticpages[1].content = "test", "test"
|
||||
staticpages[0].save(), staticpages[1].save()
|
||||
assert StaticPage.objects.search(q="test").count() == 2
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_search_without_searchcontent(self, staticpages):
|
||||
staticpages[0].title, staticpages[1].content = "test", "test"
|
||||
staticpages[0].save(), staticpages[1].save()
|
||||
assert StaticPage.objects.search(q="test", search_content=False).count() == 1
|
||||
assert staticpages[0] in StaticPage.objects.search(q="test", search_content=False)
|
||||
|
||||
|
||||
# BasePage is abstract, we use StaticPage in the test.
|
||||
# We do not use instances of the Page Model because it's use as abstract.
|
||||
class TestBasePage:
|
||||
@pytest.mark.django_db
|
||||
def test__str__(self, staticpages):
|
||||
assert staticpages[0].title == staticpages[0].__str__()
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_save_generate_unique_slug(self, staticpages):
|
||||
staticpages[0].title, staticpages[0].slug = "Title with spaces", None
|
||||
staticpages[0].save()
|
||||
assert staticpages[0].slug == "title-with-spaces"
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_save_generate_slug_that_already_exist(self, staticpages):
|
||||
staticpages[0].slug, staticpages[1].slug, staticpages[1].title = "title", None, "title"
|
||||
staticpages[0].save(), staticpages[1].save()
|
||||
assert staticpages[1].slug == "title-1"
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_save_without_cover_with_parent(self, staticpages):
|
||||
staticpages[0].cover, staticpages[0].parent= None, staticpages[1]
|
||||
staticpages[0].save()
|
||||
assert staticpages[0].cover == staticpages[1].cover
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_get_absolute_url_from_unpublished_page(self, staticpages):
|
||||
staticpages[0].status = StaticPage.STATUS_DRAFT
|
||||
staticpages[0].save()
|
||||
assert staticpages[0].get_absolute_url() == "#"
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_get_absolute_url_from_published_page(self, staticpages):
|
||||
staticpages[0].status = StaticPage.STATUS_PUBLISHED
|
||||
staticpages[0].save()
|
||||
assert staticpages[0].get_absolute_url() == "/pages/" + staticpages[0].slug + "/"
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_is_draft(self, staticpages):
|
||||
staticpages[0].status = StaticPage.STATUS_DRAFT
|
||||
staticpages[0].save()
|
||||
assert staticpages[0].is_draft == True
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_is_published(self, staticpages):
|
||||
staticpages[0].status = StaticPage.STATUS_PUBLISHED
|
||||
staticpages[0].save()
|
||||
assert staticpages[0].is_published == True
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_is_trash(self, staticpages):
|
||||
staticpages[0].status = StaticPage.STATUS_TRASH
|
||||
staticpages[0].save()
|
||||
assert staticpages[0].is_trash == True
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_display_title_from_parent_ifdraft(self, staticpages):
|
||||
staticpages[0].title = "Parent page title"
|
||||
staticpages[0].status = StaticPage.STATUS_PUBLISHED
|
||||
staticpages[0].save()
|
||||
staticpages[1].title = "Child page title"
|
||||
staticpages[1].parent = staticpages[0]
|
||||
staticpages[1].status = StaticPage.STATUS_DRAFT
|
||||
staticpages[1].save()
|
||||
assert staticpages[0].display_title == "Parent page title"
|
||||
assert staticpages[1].display_title == "Parent page title"
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_headline(self, staticpages):
|
||||
staticpages[0].content="<h2>My headline</h2><p>My content.</p>"
|
||||
staticpages[1].content=""
|
||||
staticpages[0].save(), staticpages[1].save()
|
||||
assert staticpages[0].headline == "My headline\n"
|
||||
assert staticpages[1].headline == ""
|
||||
|
||||
#for Page only, StaticPage don't have category argument.
|
||||
@pytest.mark.django_db
|
||||
def test_get_init_kwargs_from(self, pages):
|
||||
kwargs = pages[0].get_init_kwargs_from(pages[0])
|
||||
assert kwargs == {"cover": pages[0].cover, "category": pages[0].category}
|
||||
|
||||
#for Page only, StaticPage don't have category argument.
|
||||
@pytest.mark.django_db
|
||||
def test_from_page(self, pages):
|
||||
assert Page.from_page(pages[0]).cover == pages[0].cover
|
||||
assert Page.from_page(pages[0]).category == pages[0].category
|
||||
|
||||
|
||||
class TestPageQuerySet:
|
||||
@pytest.mark.django_db
|
||||
def test_published(self, staticpages):
|
||||
staticpages[0].status = Page.STATUS_PUBLISHED
|
||||
staticpages[0].save()
|
||||
for page in Page.objects.published():
|
||||
assert page.status == Page.STATUS_PUBLISHED
|
||||
|
||||
class TestPage:
|
||||
@pytest.mark.django_db
|
||||
def test_save_published_page_pubdate(self, pages):
|
||||
pages[0].status, pages[0].pub_date= Page.STATUS_PUBLISHED, None
|
||||
pages[0].save()
|
||||
assert pages[0].pub_date is not None
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_save_unpublished_page_pubdate(self, pages):
|
||||
pages[0].status, pages[0].pub_date= Page.STATUS_DRAFT, None
|
||||
pages[0].save()
|
||||
assert pages[0].pub_date is None
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_save_child_page_category(self, pages):
|
||||
pages[0].parent, pages[0].category = pages[1], None
|
||||
pages[0].save()
|
||||
assert pages[0].category == pages[1].category
|
||||
|
||||
|
||||
class TestStaticPage:
|
||||
@pytest.mark.django_db
|
||||
def test_get_absolute_url(self, staticpages):
|
||||
staticpages[0].attach_to=StaticPage.ATTACH_TO_DIFFUSIONS
|
||||
assert staticpages[0].get_absolute_url() == "/week/"
|
||||
|
||||
class TestNavItem:
|
||||
@pytest.mark.django_db
|
||||
def test_get_url_from_selfurl(self, navitem):
|
||||
assert navitem.url == navitem.get_url()
|
||||
|
||||
#for staticpage only.
|
||||
@pytest.mark.django_db
|
||||
def test_get_url_from_pageurl(self, navitem, staticpages):
|
||||
navitem.url, navitem.page = None, staticpages[0]
|
||||
navitem.save()
|
||||
assert navitem.get_url() == staticpages[0].get_absolute_url()
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_get_url_without_selfurl_page(self, navitem):
|
||||
navitem.url, navitem.page = None, None
|
||||
assert navitem.get_url() == None
|
Loading…
Reference in New Issue
Block a user