diff --git a/aircox/tests/conftest.py b/aircox/tests/conftest.py index bb93cca..d1a2be6 100644 --- a/aircox/tests/conftest.py +++ b/aircox/tests/conftest.py @@ -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): diff --git a/aircox/tests/models/test_page.py b/aircox/tests/models/test_page.py index d304d95..2466e5f 100644 --- a/aircox/tests/models/test_page.py +++ b/aircox/tests/models/test_page.py @@ -7,250 +7,190 @@ 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__() + def test__str__(self, category): + assert category.__str__() == category.title - 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. +# 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): - draft_pages = StaticPage.objects.draft() - - for page in draft_pages: + for page in StaticPage.objects.draft(): 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: + for page in StaticPage.objects.published(): 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: + 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): - 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 + 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): - 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 + 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): - 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 + 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): - 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 + 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 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: @pytest.mark.django_db def test__str__(self, staticpages): - page = baker.make(StaticPage, title="Test") - - assert page.__str__() == "Test" + assert staticpages[0].title == staticpages[0].__str__() @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" + 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): - baker.make(StaticPage, slug="title") - page = baker.make(StaticPage, title="Title", slug=None) - page.save() - - assert page.slug == "title-1" + 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): - parent = baker.make(StaticPage) - child = baker.make(StaticPage, cover=None, parent=parent) - child.save() - - assert child.cover == parent.cover + 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): - unpublished_page = baker.make( - StaticPage, slug="page-slug", status=StaticPage.STATUS_DRAFT - ) - - assert unpublished_page.get_absolute_url() == "#" + 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): - published_page = baker.make( - StaticPage, - slug="another-page-slug", - status=StaticPage.STATUS_PUBLISHED, - ) + staticpages[0].status = StaticPage.STATUS_PUBLISHED + staticpages[0].save() + assert staticpages[0].get_absolute_url() == "/pages/" + staticpages[0].slug + "/" - 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 + 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): - page = baker.make(StaticPage, status=StaticPage.STATUS_PUBLISHED) - - assert page.is_published == True + staticpages[0].status = StaticPage.STATUS_PUBLISHED + staticpages[0].save() + assert staticpages[0].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 + 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(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" + 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): - page = baker.make( - StaticPage, content="

My headline

My content.

" - ) - empty_content_page = baker.make(StaticPage, content="") + staticpages[0].content="

My headline

My content.

" + staticpages[1].content="" + staticpages[0].save(), staticpages[1].save() + assert staticpages[0].headline == "My headline\n" + assert staticpages[1].headline == "" - 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. + #for Page only, StaticPage don't have category argument. @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} + 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} - #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 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 + 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, pages): - baker.make(Page, _quantity=5) - published_pages_list = Page.objects.published() - - for page in published_pages_list: + 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): - page = baker.make(Page, status=Page.STATUS_PUBLISHED, pub_date=None) - page.save() - - assert page.pub_date is not None + 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): - page = baker.make(Page, status=Page.STATUS_DRAFT, pub_date=None) - page.save() - - assert page.pub_date == None + 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): - parent = baker.make(Page) - child = baker.make(Page, parent=parent, category=None) - child.save() - - assert child.category == parent.category + 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): - page = baker.make( - StaticPage, attach_to=StaticPage.ATTACH_TO_DIFFUSIONS - ) - - assert page.get_absolute_url() == "/week/" - + 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): - page = baker.make(StaticPage) - item = baker.make(NavItem, url=None, page=page) - - assert item.get_url() == page.get_absolute_url() + 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): - item = baker.make(NavItem, url=None, page=None) - - assert item.get_url() == None + navitem.url, navitem.page = None, None + assert navitem.get_url() == None