pytest model page

This commit is contained in:
Laurent Van Wambeke 2023-05-09 13:04:28 +02:00
parent 9a624994e9
commit ced76e8d13
2 changed files with 187 additions and 50 deletions

View File

@ -150,11 +150,12 @@ class BasePage(models.Model):
def is_trash(self): def is_trash(self):
return self.status == self.STATUS_TRASH return self.status == self.STATUS_TRASH
# for the following property, as we call the properties is_published and display_title as property, we need to call them as property (without parenthesis) and not as method (with parenthesis)
@property @property
def display_title(self): def display_title(self):
if self.is_published(): if self.is_published:
return self.title return self.title
return self.parent.display_title() return self.parent.display_title
@cached_property @cached_property
def headline(self): def headline(self):

View File

@ -2,87 +2,75 @@ import pytest
from model_bakery import baker from model_bakery import baker
from aircox.models import Page from aircox.models import Category, PageQuerySet, Page, StaticPage, Comment, NavItem
@pytest.mark.django_db @pytest.mark.django_db
class TestCategory: class TestCategory:
def test__str__(self): def test__str__(self):
page = baker.make(Page) # return category title field.
title = page.__str__() category = baker.make(Category)
assert title == page.title title = category.__str__()
assert title == category.title
# poupée russe du modèle page.
# BasePage > Page
# BasePage > StaticPage
# Cannot create BasePage fake instance in fake db
# ? Do I need to create fake page and staticpage instances in order to test each method from BasePage ?
class TestBasePageQuerySet(): class TestBasePageQuerySet():
@pytest.mark.django_db @pytest.mark.django_db
def test_draft(self): def test_draft(self):
# return an array of all draft pages
baker.make(Page, _quantity=5) baker.make(Page, _quantity=5)
draft_pages = Page.objects.draft() draft_pages = Page.objects.draft()
for page in draft_pages: for page in draft_pages:
assert page.status == Page.STATUS_DRAFT assert page.status == Page.STATUS_DRAFT
@pytest.mark.django_db @pytest.mark.django_db
def test_published(self): def test_published(self):
# retrun an array of all published pages
baker.make(Page, _quantity=5) baker.make(Page, _quantity=5)
published_pages = Page.objects.published() published_pages = Page.objects.published()
for page in published_pages: for page in published_pages:
assert page.status == Page.STATUS_PUBLISHED assert page.status == Page.STATUS_PUBLISHED
@pytest.mark.django_db @pytest.mark.django_db
def test_trash(self): def test_trash(self):
# return an array of all trash pages
baker.make(Page, _quantity=5) baker.make(Page, _quantity=5)
trashed_pages = Page.objects.published() trashed_pages = Page.objects.trash()
for page in trashed_pages: for page in trashed_pages:
assert page.status == Page.STATUS_TRASH assert page.status == Page.STATUS_TRASH
@pytest.mark.django_db @pytest.mark.django_db
def test_parent_core(self): def test_parent_with_page(self):
# by defining parent in page make the page become childpage. Both childs and parents are link without any specific method.
parent = baker.make(Page) parent = baker.make(Page)
child1 = baker.make(Page, parent=parent) child1 = baker.make(Page, parent=parent)
child2 = baker.make(Page, parent=parent) child2 = baker.make(Page, parent=parent)
child3 = baker.make(Page, parent=parent) child3 = baker.make(Page, parent=parent)
nochild = baker.make(Page, parent=None) nochild = baker.make(Page, parent=None)
assert parent.child_set.count() == 3 ''' retrieve all child page from same parent instance '''
assert child1 in parent.child_set.all() parent_childs = Page.objects.parent(parent)
assert child2 in parent.child_set.all()
assert child3 in parent.child_set.all()
assert nochild not in parent.child_set.all()
@pytest.mark.django_db assert child1 in parent_childs
def test_parent_with_parent_object(self): assert child2 in parent_childs
#retrieve child pages having this parent as parameter of the method assert child3 in parent_childs
parent = baker.make(Page) assert nochild not in parent_childs
child1 = baker.make(Page, parent=parent)
child2 = baker.make(Page, parent=parent)
child3 = baker.make(Page, parent=parent)
nochild = baker.make(Page, parent=None)
childs = Page.objects.parent(parent) ''' retrieve all child page from same parent id'''
parentid_childs = Page.objects.parent(parent.id)
assert child1 in childs assert child1 in parentid_childs
assert child2 in childs assert child2 in parentid_childs
assert child3 in childs assert child3 in parentid_childs
assert nochild not in childs assert nochild not in parentid_childs
@pytest.mark.django_db
def test_parent_with_id(self):
#retrive child pages having this parent id as parameter of the method
parent = baker.make(Page)
child1 = baker.make(Page, parent=parent)
child2 = baker.make(Page, parent=parent)
child3 = baker.make(Page, parent=parent)
nochild = baker.make(Page, parent=None)
childs = Page.objects.parent(parent.id)
assert child1 in childs
assert child2 in childs
assert child3 in childs
assert nochild not in childs
@pytest.mark.django_db @pytest.mark.django_db
def test_search_with_searchcontent(self): def test_search_with_searchcontent(self):
@ -93,6 +81,7 @@ class TestBasePageQuerySet():
results = Page.objects.search(q=q) results = Page.objects.search(q=q)
# check : title and content data was use to determine if pages are in result from search 'test'
assert page1 in results assert page1 in results
assert page2 in results assert page2 in results
@ -105,14 +94,161 @@ class TestBasePageQuerySet():
results = Page.objects.search(q=q, search_content=False) results = Page.objects.search(q=q, search_content=False)
# check : as we search not in content, only the page with the data 'test' in its title should be return in the results
assert page1 in results assert page1 in results
assert page2 not in results assert page2 not in results
#class TestBasePage: class TestBasePage:
# @pytest.mark.django_db
# def test__str__(self): @pytest.mark.django_db
def test__str__(self):
# return page title
page = baker.make(Page, title='Test')
assert page.__str__() == 'Test'
@pytest.mark.django_db
def test_save_case1(self):
''' save case 1:
- no slug
- the slug generate with .save() is unique in db'''
page = baker.make(Page, title='Title with spaces', slug=None)
page.save()
assert page.slug == 'title-with-spaces'
@pytest.mark.django_db
def test_save_case2(self):
''' save case 2:
- no slug
- the slug generate with .save() is not unique in db'''
baker.make(Page, slug='title')
page = baker.make(Page, title='Title', slug=None)
page.save()
assert page.slug == 'title-1'
@pytest.mark.django_db
def test_save_case3(self):
''' save case 3:
- save a page without cover but with a parent with a cover
'''
parent = baker.make(Page)
child = baker.make(Page, cover=None, parent=parent)
child.save()
assert child.cover == parent.cover
@pytest.mark.django_db
def test_get_absolute_url(self):
unpublished_page = baker.make(Page, slug='page-slug', status=Page.STATUS_DRAFT)
assert unpublished_page.get_absolute_url() == '#'
''' !!! only work with staticpage instance, not page instance. error when using page : TestBasePage::test_get_absolute_url - django.urls.exceptions.NoReverseMatch: Reverse for 'None' not found. 'None' is not a valid view function or pattern name'''
published_page = baker.make(StaticPage, slug='another-page-slug', status=Page.STATUS_PUBLISHED)
assert published_page.get_absolute_url() == '/pages/another-page-slug/'
# as the following are property from page.py, we test them without the parenthesis at the end. For example: page.is_draft and not page.is_draft()
#property
@pytest.mark.django_db
def test_is_draft(self):
#return true if the page/staticapge is draft.
page = baker.make(Page, status=Page.STATUS_DRAFT)
assert page.is_draft == True
#property
@pytest.mark.django_db
def test_is_published(self):
#return true if the page/staticapge is published.
page = baker.make(Page, status=Page.STATUS_PUBLISHED)
assert page.is_published == True
#property
@pytest.mark.django_db
def test_is_draft(self):
#return true if the page/staticapge is published.
page = baker.make(Page, status=Page.STATUS_TRASH)
assert page.is_trash == True
''' !! in display_title property from page.py, we need to remove the parenthesis of .is_published() and display_title(), because they are property and not proper method, in order to work. otherwhise we get an error : "TypeError: 'bool' object is not callable" '''
#property
@pytest.mark.django_db
def test_display_title(self):
parent_page = baker.make(Page, title='Parent page title', status=Page.STATUS_PUBLISHED)
child_unpublished_page = baker.make(Page, title='Child page title', parent=parent_page, status=Page.STATUS_DRAFT)
# display title of a published page with title
assert parent_page.display_title == 'Parent page title'
# display title of the parent page if child is draft
assert child_unpublished_page.display_title == 'Parent page title'
#cached_property
@pytest.mark.django_db
def test_headline(self):
page = baker.make(Page, content='<h2>My headline</h2><p>My content.</p>')
empty_content_page = baker.make(Page, content='')
assert page.headline == 'My headline\n'
assert empty_content_page.headline == ""
#classmethod : is called on the class itself. Exemple below : Page.classmethod(arg)
@pytest.mark.django_db
def test_get_init_kwargs_from(self):
page = baker.make(Page)
kwargs = Page.get_init_kwargs_from(page)
assert kwargs == {"cover": page.cover, "category": page.category}
#classmethod
# from_page classmethod create a new instance of the class (page or staticpage) from a page object. It's a fast way to create a new instance without having to manually extract all the informations from the page object in order to create the new instance.
# Only for informations: below are some theorical explanation of the tools use in the method from_page.
# - cls from the classmethod from_page from page.py refers to the class itself
# - The **something syntax in Python is used to pass a variable number of keyword arguments (from the 'something') to a function or method
# - It (**something) is a shorthand way of passing the dictionary as individual arguments without having to unpack it explicitly. (example : unpack this dictionary "something = {'key1' : 'value1', 'key2' : 'value2'}" and "**something" return this arguments "{key1='value1', key2='value2'}")
@pytest.mark.django_db
def test_from_page(self):
page_object = baker.make(Page)
new_page_instance = Page.from_page(page_object)
# assert that after creating new instance with the methodclass "from_page", the new page instance have the same arguments than the page object (we only test here with cover and category arguments)
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):
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():
## TODO en cours par laurent
#en cours par laurent TODO