clean test model page

This commit is contained in:
Laurent Van Wambeke 2023-05-12 13:26:31 +02:00
parent fd0f168ef8
commit 710f8a14c7
2 changed files with 109 additions and 154 deletions

View File

@ -11,6 +11,21 @@ from aircox import models
def stations(): def stations():
return baker.make(models.Station, _quantity=2) 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 @pytest.fixture
def programs(stations): def programs(stations):

View File

@ -7,250 +7,190 @@ from aircox.models import Category, Page, StaticPage, NavItem
@pytest.mark.django_db @pytest.mark.django_db
class TestCategory: class TestCategory:
def test__str__(self): def test__str__(self, category):
category = baker.make(Category) assert category.__str__() == category.title
title = category.__str__()
assert title == 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.
# 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: class TestBasePageQuerySet:
@pytest.mark.django_db @pytest.mark.django_db
def test_draft(self, staticpages): def test_draft(self, staticpages):
draft_pages = StaticPage.objects.draft() for page in StaticPage.objects.draft():
for page in draft_pages:
assert page.status == StaticPage.STATUS_DRAFT assert page.status == StaticPage.STATUS_DRAFT
@pytest.mark.django_db @pytest.mark.django_db
def test_published(self, staticpages): def test_published(self, staticpages):
published_pages = StaticPage.objects.published() for page in StaticPage.objects.published():
for page in published_pages:
assert page.status == StaticPage.STATUS_PUBLISHED assert page.status == StaticPage.STATUS_PUBLISHED
@pytest.mark.django_db @pytest.mark.django_db
def test_trash(self, staticpages): def test_trash(self, staticpages):
trashed_pages = StaticPage.objects.trash() for page in StaticPage.objects.trash():
for page in trashed_pages:
assert page.status == StaticPage.STATUS_TRASH assert page.status == StaticPage.STATUS_TRASH
@pytest.mark.django_db @pytest.mark.django_db
def test_parent_return_childs_from_parent_object(self, staticpages): def test_parent_return_childs_from_parent_object(self, staticpages):
parent = baker.make(StaticPage) staticpages[0].parent = staticpages[1]
childs = baker.make(StaticPage, _quantity=3, parent=parent) for child in StaticPage.objects.parent(staticpages[1]):
nochild = baker.make(StaticPage, parent=None) assert child.parent == staticpages[1]
parent_childs = StaticPage.objects.parent(parent) assert child != staticpages[1]
for child in childs:
assert child in parent_childs
assert nochild not in parent_childs
@pytest.mark.django_db @pytest.mark.django_db
def test_parent_return_childs_from_parent_id(self, staticpages): def test_parent_return_childs_from_parent_id(self, staticpages):
parent = baker.make(StaticPage) staticpages[0].parent = staticpages[1]
childs = baker.make(StaticPage, _quantity=3, parent=parent) for child in StaticPage.objects.parent(staticpages[1].id):
nochild = baker.make(StaticPage, parent=None) assert child.parent == staticpages[1]
parent_childs = StaticPage.objects.parent(parent.id) assert child != staticpages[1]
for child in childs:
assert child in parent_childs
assert nochild not in parent_childs
@pytest.mark.django_db @pytest.mark.django_db
def test_search_with_searchcontent(self, staticpages): def test_search_with_searchcontent(self, staticpages):
page1 = baker.make(StaticPage, title="test") staticpages[0].title, staticpages[1].content = "test", "test"
page2 = baker.make(StaticPage, content="test") staticpages[0].save(), staticpages[1].save()
q = "test" assert StaticPage.objects.search(q="test").count() == 2
results = StaticPage.objects.search(q=q)
assert page1 in results
assert page2 in results
@pytest.mark.django_db @pytest.mark.django_db
def test_search_without_searchcontent(self, staticpages): def test_search_without_searchcontent(self, staticpages):
page1 = baker.make(StaticPage, title="test") staticpages[0].title, staticpages[1].content = "test", "test"
page2 = baker.make(StaticPage, content="test") staticpages[0].save(), staticpages[1].save()
q = "test" assert StaticPage.objects.search(q="test", search_content=False).count() == 1
results = StaticPage.objects.search(q=q, search_content=False) assert staticpages[0] in StaticPage.objects.search(q="test", 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. # 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: class TestBasePage:
@pytest.mark.django_db @pytest.mark.django_db
def test__str__(self, staticpages): def test__str__(self, staticpages):
page = baker.make(StaticPage, title="Test") assert staticpages[0].title == staticpages[0].__str__()
assert page.__str__() == "Test"
@pytest.mark.django_db @pytest.mark.django_db
def test_save_generate_unique_slug(self, staticpages): def test_save_generate_unique_slug(self, staticpages):
page = baker.make(StaticPage, title="Title with spaces", slug=None) staticpages[0].title, staticpages[0].slug = "Title with spaces", None
page.save() staticpages[0].save()
assert staticpages[0].slug == "title-with-spaces"
assert page.slug == "title-with-spaces"
@pytest.mark.django_db @pytest.mark.django_db
def test_save_generate_slug_that_already_exist(self, staticpages): def test_save_generate_slug_that_already_exist(self, staticpages):
baker.make(StaticPage, slug="title") staticpages[0].slug, staticpages[1].slug, staticpages[1].title = "title", None, "title"
page = baker.make(StaticPage, title="Title", slug=None) staticpages[0].save(), staticpages[1].save()
page.save() assert staticpages[1].slug == "title-1"
assert page.slug == "title-1"
@pytest.mark.django_db @pytest.mark.django_db
def test_save_without_cover_with_parent(self, staticpages): def test_save_without_cover_with_parent(self, staticpages):
parent = baker.make(StaticPage) staticpages[0].cover, staticpages[0].parent= None, staticpages[1]
child = baker.make(StaticPage, cover=None, parent=parent) staticpages[0].save()
child.save() assert staticpages[0].cover == staticpages[1].cover
assert child.cover == parent.cover
@pytest.mark.django_db @pytest.mark.django_db
def test_get_absolute_url_from_unpublished_page(self, staticpages): def test_get_absolute_url_from_unpublished_page(self, staticpages):
unpublished_page = baker.make( staticpages[0].status = StaticPage.STATUS_DRAFT
StaticPage, slug="page-slug", status=StaticPage.STATUS_DRAFT staticpages[0].save()
) assert staticpages[0].get_absolute_url() == "#"
assert unpublished_page.get_absolute_url() == "#"
@pytest.mark.django_db @pytest.mark.django_db
def test_get_absolute_url_from_published_page(self, staticpages): def test_get_absolute_url_from_published_page(self, staticpages):
published_page = baker.make( staticpages[0].status = StaticPage.STATUS_PUBLISHED
StaticPage, staticpages[0].save()
slug="another-page-slug", assert staticpages[0].get_absolute_url() == "/pages/" + staticpages[0].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 @pytest.mark.django_db
def test_is_draft(self, staticpages): def test_is_draft(self, staticpages):
page = baker.make(StaticPage, status=StaticPage.STATUS_DRAFT) staticpages[0].status = StaticPage.STATUS_DRAFT
staticpages[0].save()
assert page.is_draft == True assert staticpages[0].is_draft == True
@pytest.mark.django_db @pytest.mark.django_db
def test_is_published(self, staticpages): def test_is_published(self, staticpages):
page = baker.make(StaticPage, status=StaticPage.STATUS_PUBLISHED) staticpages[0].status = StaticPage.STATUS_PUBLISHED
staticpages[0].save()
assert page.is_published == True assert staticpages[0].is_published == True
@pytest.mark.django_db @pytest.mark.django_db
def test_is_draft(self, staticpages): def test_is_trash(self, staticpages):
page = baker.make(StaticPage, status=StaticPage.STATUS_TRASH) staticpages[0].status = StaticPage.STATUS_TRASH
staticpages[0].save()
assert page.is_trash == True assert staticpages[0].is_trash == True
@pytest.mark.django_db @pytest.mark.django_db
def test_display_title(self, staticpages): def test_display_title_from_parent_ifdraft(self, staticpages):
parent_page = baker.make( staticpages[0].title = "Parent page title"
StaticPage, staticpages[0].status = StaticPage.STATUS_PUBLISHED
title="Parent page title", staticpages[0].save()
status=StaticPage.STATUS_PUBLISHED, staticpages[1].title = "Child page title"
) staticpages[1].parent = staticpages[0]
child_unpublished_page = baker.make( staticpages[1].status = StaticPage.STATUS_DRAFT
StaticPage, staticpages[1].save()
title="Child page title", assert staticpages[0].display_title == "Parent page title"
parent=parent_page, assert staticpages[1].display_title == "Parent page title"
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 @pytest.mark.django_db
def test_headline(self, staticpages): def test_headline(self, staticpages):
page = baker.make( staticpages[0].content="<h2>My headline</h2><p>My content.</p>"
StaticPage, content="<h2>My headline</h2><p>My content.</p>" staticpages[1].content=""
) staticpages[0].save(), staticpages[1].save()
empty_content_page = baker.make(StaticPage, content="") assert staticpages[0].headline == "My headline\n"
assert staticpages[1].headline == ""
assert page.headline == "My headline\n" #for Page only, StaticPage don't have category argument.
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 @pytest.mark.django_db
def test_get_init_kwargs_from(self, staticpages): def test_get_init_kwargs_from(self, pages):
page = baker.make(Page) kwargs = pages[0].get_init_kwargs_from(pages[0])
kwargs = Page.get_init_kwargs_from(page) assert kwargs == {"cover": pages[0].cover, "category": pages[0].category}
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. #for Page only, StaticPage don't have category argument.
@pytest.mark.django_db @pytest.mark.django_db
def test_from_page(self, pages): def test_from_page(self, pages):
page_object = baker.make(Page) assert Page.from_page(pages[0]).cover == pages[0].cover
new_page_instance = Page.from_page(page_object) assert Page.from_page(pages[0]).category == pages[0].category
assert new_page_instance.cover == page_object.cover
assert new_page_instance.category == page_object.category
class TestPageQuerySet: class TestPageQuerySet:
@pytest.mark.django_db @pytest.mark.django_db
def test_published(self, pages): def test_published(self, staticpages):
baker.make(Page, _quantity=5) staticpages[0].status = Page.STATUS_PUBLISHED
published_pages_list = Page.objects.published() staticpages[0].save()
for page in Page.objects.published():
for page in published_pages_list:
assert page.status == Page.STATUS_PUBLISHED assert page.status == Page.STATUS_PUBLISHED
class TestPage: class TestPage:
@pytest.mark.django_db @pytest.mark.django_db
def test_save_published_page_pubdate(self, pages): def test_save_published_page_pubdate(self, pages):
page = baker.make(Page, status=Page.STATUS_PUBLISHED, pub_date=None) pages[0].status, pages[0].pub_date= Page.STATUS_PUBLISHED, None
page.save() pages[0].save()
assert pages[0].pub_date is not None
assert page.pub_date is not None
@pytest.mark.django_db @pytest.mark.django_db
def test_save_unpublished_page_pubdate(self, pages): def test_save_unpublished_page_pubdate(self, pages):
page = baker.make(Page, status=Page.STATUS_DRAFT, pub_date=None) pages[0].status, pages[0].pub_date= Page.STATUS_DRAFT, None
page.save() pages[0].save()
assert pages[0].pub_date is None
assert page.pub_date == None
@pytest.mark.django_db @pytest.mark.django_db
def test_save_child_page_category(self, pages): def test_save_child_page_category(self, pages):
parent = baker.make(Page) pages[0].parent, pages[0].category = pages[1], None
child = baker.make(Page, parent=parent, category=None) pages[0].save()
child.save() assert pages[0].category == pages[1].category
assert child.category == parent.category
class TestStaticPage: class TestStaticPage:
@pytest.mark.django_db @pytest.mark.django_db
def test_get_absolute_url(self, staticpages): def test_get_absolute_url(self, staticpages):
page = baker.make( staticpages[0].attach_to=StaticPage.ATTACH_TO_DIFFUSIONS
StaticPage, attach_to=StaticPage.ATTACH_TO_DIFFUSIONS assert staticpages[0].get_absolute_url() == "/week/"
)
assert page.get_absolute_url() == "/week/"
class TestNavItem: class TestNavItem:
@pytest.mark.django_db @pytest.mark.django_db
def test_get_url_from_selfurl(self, navitem): def test_get_url_from_selfurl(self, navitem):
assert navitem.url == navitem.get_url() assert navitem.url == navitem.get_url()
#for staticpage only.
@pytest.mark.django_db @pytest.mark.django_db
def test_get_url_from_pageurl(self, navitem): def test_get_url_from_pageurl(self, navitem, staticpages):
page = baker.make(StaticPage) navitem.url, navitem.page = None, staticpages[0]
item = baker.make(NavItem, url=None, page=page) navitem.save()
assert navitem.get_url() == staticpages[0].get_absolute_url()
assert item.get_url() == page.get_absolute_url()
@pytest.mark.django_db @pytest.mark.django_db
def test_get_url_without_selfurl_page(self, navitem): def test_get_url_without_selfurl_page(self, navitem):
item = baker.make(NavItem, url=None, page=None) navitem.url, navitem.page = None, None
assert navitem.get_url() == None
assert item.get_url() == None