From c5db6daa837b747a4c5c93d37a01f3ef4b5db68e Mon Sep 17 00:00:00 2001 From: lauvwbk Date: Mon, 8 May 2023 15:19:31 +0200 Subject: [PATCH 1/7] pytest station model --- aircox/tests/models/test_station.py | 211 ++++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 aircox/tests/models/test_station.py diff --git a/aircox/tests/models/test_station.py b/aircox/tests/models/test_station.py new file mode 100644 index 0000000..70534c9 --- /dev/null +++ b/aircox/tests/models/test_station.py @@ -0,0 +1,211 @@ +import pytest + +from model_bakery import baker + +from aircox.models import Station, Port + +from aircox.tests.conftest import stations + +from aircox.conf import settings + +#(j'ai utilisé Baker directement ici à plusieurs reprises car la fixture 'stations' du fichier conftest génère des champs aléatoires avec lesquelles j'avais du mal à réaliser les tests.) + + +@pytest.mark.django_db +class TestStationQuerySet: + + # test the default method: the method consists in recovering a single default station in different cases. The definition of the station by this method is a univocal choice (not possible to have several stations by default in the db). + # Each case assumes that several stations are already in the database according to different parameters. + def test_default_case1(self): + """ case 1 : + - no default station in db + - no station defined in method """ + + #fake db + baker.make(Station, _quantity=3, default=False) + + #method test + default_station = Station.objects.default() + + # check: we have recovered one single default station as the method indicates. + assert default_station is not None + assert default_station == Station.objects.first() + + def test_default_case2(self): + """ case 2 : + - one default station in db + - no station defined in method """ + + # fake db + baker.make(Station, _quantity=2, default=False) + default_station = baker.make(Station, default=True) + + # method test + specific_station = Station.objects.default() + + # check: default station is the one defined in the fake db + assert specific_station == default_station + + def test_default_case3(self): + """ case 3 : + - one default station in db + - one station define in method """ + + # fake db + baker.make(Station, _quantity=2, default=False) + default_station = baker.make(Station, default=True) + + # method test + specific_station = Station.objects.default(station=default_station.pk) + + # check: default station is the one defined in the method + assert specific_station == default_station + + + # test the active method: return all the active station's instance + def test_active(self): + baker.make(Station, _quantity=2, active=False) + active_station = baker.make(Station, active=True) + + # test + filtered_active_station = Station.objects.active() + + # check: active station is returned. + assert active_station in filtered_active_station + + + + +@pytest.mark.django_db +class TestStation: + + # test the streams method: create an array based on a text by separating each element by the break line character(\n). + def test_stream_field_filled(self): + #case 1 : text data with break lines is entered in the audio_streams field + streams_urls = "http://radiocampus.be/stream1.mp3\nhttp://radiocampus.be/stream2.mp3" + station = baker.make(Station, audio_streams=streams_urls) + assert station.streams == ["http://radiocampus.be/stream1.mp3", "http://radiocampus.be/stream2.mp3"] + + def test_stream_field_empty(self): + #case 2 : no data in the audio_streams field + station = baker.make(Station, audio_streams=None) + assert station.streams == [] + + # test the __str__ method = retrieve 'name' field of the station. + def test__str__(self, stations): + for station in stations: + assert station.name == station.__str__() + + # test save method : save the station data in a folder. + def test_save_without_path(self): + #case 1 : not directory path defined. + station = baker.make(Station, path=None, slug="a-slug-with-dash") + station.save() + assert station.path == settings.CONTROLLERS_WORKING_DIR + '\\a_slug_with_dash' + + def test_save_default_station_while_anotherone_is_default(self): + #case 2 : save a new station while another one is already set default. + station1 = baker.make(Station, default=True) + station2 = baker.make(Station, default=False) + + # need to switch station2 default value to true before to try save method. + station2.default = True + station2.save() + + # need to refresh fake db from baker in order to make the test. + station1.refresh_from_db() + station2.refresh_from_db() + + # check if the station we choose to be default is the only one + assert Station.objects.filter(default=True).count() == 1 + assert station1.default is False + assert station2.default is True + +@pytest.mark.django_db +class TestPortQuerySet: + # test active methode : retrieve active ports. + # issues with the preset incompatibility between port and direction. I need to control which type and direction for each fake model instance in order to run pytest without failing randomly. + def test_active(self): + port1 = baker.make(Port, active=True, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT) + port2 = baker.make(Port, active=False, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT) + + active_ports = Port.objects.active() + assert port1 in active_ports + assert port2 not in active_ports + + inactive_ports = Port.objects.active(value=False) + assert port1 not in inactive_ports + assert port2 in inactive_ports + + # test output method : retrieve output ports. + def test_output(self): + port1 = baker.make(Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT) + port2 = baker.make(Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT) + + output_ports = Port.objects.output() + assert port1 in output_ports + assert port2 not in output_ports + + # test input method : retrieve input ports. + def test_input(self): + port1 = baker.make(Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT) + port2 = baker.make(Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT) + + input_ports = Port.objects.input() + assert port1 not in input_ports + assert port2 in input_ports + +@pytest.mark.django_db +class TestPort: + # test __str__ method = return the "type", "direction" and "port id" data of a given "Port" model in one sentence. + def test__str__(self): + port = baker.make(Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT) + assert port.__str__() == 'output: icecast #1' + + # test is_valid_type method = return a Boolean value according to the compatibility criteria between the type and the port of a given port's instance. Compatibilyt criteria are defined in the method. + def test_is_valid_type_and_type_is_input(self): + # case 1 : input direction. return 'true' if the type is compatible + porthttp = baker.make(Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT) + porthttps = baker.make(Port, type=Port.TYPE_HTTPS, direction=Port.DIRECTION_INPUT) + + assert porthttp.is_valid_type() == True + assert porthttps.is_valid_type() == True + + + def test_is_valid_type_and_type_is_not_input(self): + # case 2 : is not input direction return 'true' if the type is compatible + porticecast = baker.make(Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT) + portfile = baker.make(Port, type=Port.TYPE_FILE, direction=Port.DIRECTION_OUTPUT) + + assert porticecast.is_valid_type() == True + assert portfile.is_valid_type() == True + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- 2.30.2 From 9be08a40471750a4484718224e51e32b0dab591f Mon Sep 17 00:00:00 2001 From: lauvwbk Date: Mon, 8 May 2023 15:40:11 +0200 Subject: [PATCH 2/7] pytest model station --- aircox/tests/models/test_station.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aircox/tests/models/test_station.py b/aircox/tests/models/test_station.py index 70534c9..952df10 100644 --- a/aircox/tests/models/test_station.py +++ b/aircox/tests/models/test_station.py @@ -70,7 +70,7 @@ class TestStationQuerySet: # test filtered_active_station = Station.objects.active() - # check: active station is returned. + # check: active station is returned assert active_station in filtered_active_station -- 2.30.2 From 9a624994e92e3cf3e7e0f506ca9df26c59f36178 Mon Sep 17 00:00:00 2001 From: lauvwbk Date: Mon, 8 May 2023 18:01:29 +0200 Subject: [PATCH 3/7] #99 pytest page model --- aircox/tests/models/test_page.py | 134 +++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 aircox/tests/models/test_page.py diff --git a/aircox/tests/models/test_page.py b/aircox/tests/models/test_page.py new file mode 100644 index 0000000..bff1f5d --- /dev/null +++ b/aircox/tests/models/test_page.py @@ -0,0 +1,134 @@ +import pytest + +from model_bakery import baker + +from aircox.models import Page + + + +@pytest.mark.django_db +class TestCategory: + + def test__str__(self): + page = baker.make(Page) + title = page.__str__() + assert title == page.title + +class TestBasePageQuerySet(): + + @pytest.mark.django_db + def test_draft(self): + baker.make(Page, _quantity=5) + draft_pages = Page.objects.draft() + for page in draft_pages: + assert page.status == Page.STATUS_DRAFT + + @pytest.mark.django_db + def test_published(self): + baker.make(Page, _quantity=5) + published_pages = Page.objects.published() + for page in published_pages: + assert page.status == Page.STATUS_PUBLISHED + + @pytest.mark.django_db + def test_trash(self): + baker.make(Page, _quantity=5) + trashed_pages = Page.objects.published() + for page in trashed_pages: + assert page.status == Page.STATUS_TRASH + + @pytest.mark.django_db + def test_parent_core(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) + child1 = baker.make(Page, parent=parent) + child2 = baker.make(Page, parent=parent) + child3 = baker.make(Page, parent=parent) + nochild = baker.make(Page, parent=None) + + assert parent.child_set.count() == 3 + assert child1 in parent.child_set.all() + 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 + def test_parent_with_parent_object(self): + #retrieve child pages having this parent 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) + + assert child1 in childs + assert child2 in childs + assert child3 in childs + assert nochild not in 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 + def test_search_with_searchcontent(self): + baker.make(Page, _quantity=4) + page1 = baker.make(Page, title='test') + page2 = baker.make(Page, content='test') + q = 'test' + + results = Page.objects.search(q=q) + + assert page1 in results + assert page2 in results + + @pytest.mark.django_db + def test_search_without_searchcontent(self): + baker.make(Page, _quantity=4) + page1 = baker.make(Page, title='test') + page2 = baker.make(Page, content='test') + q = 'test' + + results = Page.objects.search(q=q, search_content=False) + + assert page1 in results + assert page2 not in results + +#class TestBasePage: + # @pytest.mark.django_db + # def test__str__(self): + + #en cours par laurent TODO + + + + + + + + + + + + + + + + + + + -- 2.30.2 From ced76e8d130eb811fd9d58b42e8d935ca8d25157 Mon Sep 17 00:00:00 2001 From: lauvwbk Date: Tue, 9 May 2023 13:04:28 +0200 Subject: [PATCH 4/7] pytest model page --- aircox/models/page.py | 5 +- aircox/tests/models/test_page.py | 232 ++++++++++++++++++++++++------- 2 files changed, 187 insertions(+), 50 deletions(-) diff --git a/aircox/models/page.py b/aircox/models/page.py index c159ebe..a19f1c7 100644 --- a/aircox/models/page.py +++ b/aircox/models/page.py @@ -150,11 +150,12 @@ class BasePage(models.Model): def is_trash(self): 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 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): diff --git a/aircox/tests/models/test_page.py b/aircox/tests/models/test_page.py index bff1f5d..8c7b30c 100644 --- a/aircox/tests/models/test_page.py +++ b/aircox/tests/models/test_page.py @@ -2,87 +2,75 @@ import pytest from model_bakery import baker -from aircox.models import Page - +from aircox.models import Category, PageQuerySet, Page, StaticPage, Comment, NavItem @pytest.mark.django_db class TestCategory: def test__str__(self): - page = baker.make(Page) - title = page.__str__() - assert title == page.title + # return category title field. + category = baker.make(Category) + 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(): @pytest.mark.django_db def test_draft(self): + # return an array of all draft pages baker.make(Page, _quantity=5) draft_pages = Page.objects.draft() for page in draft_pages: assert page.status == Page.STATUS_DRAFT @pytest.mark.django_db - def test_published(self): + def test_published(self): + # retrun an array of all published pages baker.make(Page, _quantity=5) published_pages = Page.objects.published() for page in published_pages: - assert page.status == Page.STATUS_PUBLISHED + assert page.status == Page.STATUS_PUBLISHED @pytest.mark.django_db def test_trash(self): + # return an array of all trash pages baker.make(Page, _quantity=5) - trashed_pages = Page.objects.published() + trashed_pages = Page.objects.trash() for page in trashed_pages: assert page.status == Page.STATUS_TRASH + @pytest.mark.django_db - def test_parent_core(self): - # by defining parent in page make the page become childpage. Both childs and parents are link without any specific method. + def test_parent_with_page(self): 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) - assert parent.child_set.count() == 3 - assert child1 in parent.child_set.all() - assert child2 in parent.child_set.all() - assert child3 in parent.child_set.all() - assert nochild not in parent.child_set.all() + ''' retrieve all child page from same parent instance ''' + parent_childs = Page.objects.parent(parent) - @pytest.mark.django_db - def test_parent_with_parent_object(self): - #retrieve child pages having this parent 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) + assert child1 in parent_childs + assert child2 in parent_childs + assert child3 in parent_childs + assert nochild not in parent_childs - 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 child2 in childs - assert child3 in childs - assert nochild not in 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 + assert child1 in parentid_childs + assert child2 in parentid_childs + assert child3 in parentid_childs + assert nochild not in parentid_childs @pytest.mark.django_db def test_search_with_searchcontent(self): @@ -93,6 +81,7 @@ class TestBasePageQuerySet(): 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 page2 in results @@ -105,14 +94,161 @@ class TestBasePageQuerySet(): 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 page2 not in results -#class TestBasePage: - # @pytest.mark.django_db - # def test__str__(self): +class TestBasePage: + + @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='

My headline

My content.

') + 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 -- 2.30.2 From cd6bb608d40b2ce964cb5a0c189e8aad1b6b917c Mon Sep 17 00:00:00 2001 From: lauvwbk Date: Wed, 10 May 2023 10:12:25 +0200 Subject: [PATCH 5/7] #101 test model page --- aircox/models/page.py | 12 +- aircox/tests/conftest.py | 15 ++ aircox/tests/models/test_page.py | 336 +++++++++++++++---------------- 3 files changed, 184 insertions(+), 179 deletions(-) diff --git a/aircox/models/page.py b/aircox/models/page.py index a19f1c7..3eaabd9 100644 --- a/aircox/models/page.py +++ b/aircox/models/page.py @@ -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 @@ -150,7 +155,6 @@ class BasePage(models.Model): def is_trash(self): 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 def display_title(self): if self.is_published: diff --git a/aircox/tests/conftest.py b/aircox/tests/conftest.py index bb93cca..e6d2708 100644 --- a/aircox/tests/conftest.py +++ b/aircox/tests/conftest.py @@ -12,6 +12,21 @@ def stations(): return baker.make(models.Station, _quantity=2) +@pytest.fixture +def staticpages(): + return baker.make(models.StaticPage, _quantity=3) + + +@pytest.fixture +def pages(): + return baker.make(models.Page, _quantity=3) + + +@pytest.fixture +def navitem(): + return baker.make(models.NavItem) + + @pytest.fixture def programs(stations): items = list( diff --git a/aircox/tests/models/test_page.py b/aircox/tests/models/test_page.py index 8c7b30c..d304d95 100644 --- a/aircox/tests/models/test_page.py +++ b/aircox/tests/models/test_page.py @@ -2,269 +2,255 @@ import pytest from model_bakery import baker -from aircox.models import Category, PageQuerySet, Page, StaticPage, Comment, NavItem +from aircox.models import Category, Page, StaticPage, NavItem @pytest.mark.django_db class TestCategory: - def test__str__(self): - # return category title field. category = baker.make(Category) 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(): +# 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): - # return an array of all draft pages - baker.make(Page, _quantity=5) - draft_pages = Page.objects.draft() + def test_draft(self, staticpages): + draft_pages = StaticPage.objects.draft() + for page in draft_pages: - assert page.status == Page.STATUS_DRAFT + assert page.status == StaticPage.STATUS_DRAFT @pytest.mark.django_db - def test_published(self): - # retrun an array of all published pages - baker.make(Page, _quantity=5) - published_pages = Page.objects.published() + def test_published(self, staticpages): + published_pages = StaticPage.objects.published() + for page in published_pages: - assert page.status == Page.STATUS_PUBLISHED + assert page.status == StaticPage.STATUS_PUBLISHED @pytest.mark.django_db - def test_trash(self): - # return an array of all trash pages - baker.make(Page, _quantity=5) - trashed_pages = Page.objects.trash() + def test_trash(self, staticpages): + trashed_pages = StaticPage.objects.trash() + for page in trashed_pages: - assert page.status == Page.STATUS_TRASH - + assert page.status == StaticPage.STATUS_TRASH @pytest.mark.django_db - def test_parent_with_page(self): - 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) + 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) - ''' retrieve all child page from same parent instance ''' - parent_childs = Page.objects.parent(parent) - - assert child1 in parent_childs - assert child2 in parent_childs - assert child3 in parent_childs + for child in childs: + assert child in parent_childs assert nochild not in parent_childs - ''' retrieve all child page from same parent id''' - parentid_childs = Page.objects.parent(parent.id) + @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) - assert child1 in parentid_childs - assert child2 in parentid_childs - assert child3 in parentid_childs - assert nochild not in parentid_childs + 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): - baker.make(Page, _quantity=4) - page1 = baker.make(Page, title='test') - page2 = baker.make(Page, content='test') - q = 'test' + 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) - 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 page2 in results @pytest.mark.django_db - def test_search_without_searchcontent(self): - baker.make(Page, _quantity=4) - page1 = baker.make(Page, title='test') - page2 = baker.make(Page, content='test') - q = 'test' + 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) - 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 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__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) + 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' + + 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) + 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' + + 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) + 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(self): + def test_get_absolute_url_from_unpublished_page(self, staticpages): + unpublished_page = baker.make( + StaticPage, slug="page-slug", status=StaticPage.STATUS_DRAFT + ) - unpublished_page = baker.make(Page, slug='page-slug', status=Page.STATUS_DRAFT) - assert unpublished_page.get_absolute_url() == '#' + 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) + 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 - - #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) + def test_is_published(self, staticpages): + page = baker.make(StaticPage, status=StaticPage.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) + def test_is_draft(self, staticpages): + page = baker.make(StaticPage, status=StaticPage.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='

My headline

My content.

') - empty_content_page = baker.make(Page, content='') + 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 page.headline == 'My headline\n' + 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="

My headline

My content.

" + ) + empty_content_page = baker.make(StaticPage, content="") + + assert page.headline == "My headline\n" assert empty_content_page.headline == "" - #classmethod : is called on the class itself. Exemple below : Page.classmethod(arg) + + #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): + 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} - - #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'}") + #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): + def test_from_page(self, pages): 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(): + +class TestPageQuerySet: @pytest.mark.django_db - def test_published(self): + 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(): -## TODO en cours par laurent - - - - +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 -- 2.30.2 From 0f5128ca7e28895bb21e83bcbc10788f9c1bbcb8 Mon Sep 17 00:00:00 2001 From: lauvwbk Date: Wed, 10 May 2023 12:33:51 +0200 Subject: [PATCH 6/7] #101 test model page --- aircox/tests/conftest.py | 15 -- aircox/tests/models/test_station.py | 211 ---------------------------- 2 files changed, 226 deletions(-) delete mode 100644 aircox/tests/models/test_station.py diff --git a/aircox/tests/conftest.py b/aircox/tests/conftest.py index e6d2708..bb93cca 100644 --- a/aircox/tests/conftest.py +++ b/aircox/tests/conftest.py @@ -12,21 +12,6 @@ def stations(): return baker.make(models.Station, _quantity=2) -@pytest.fixture -def staticpages(): - return baker.make(models.StaticPage, _quantity=3) - - -@pytest.fixture -def pages(): - return baker.make(models.Page, _quantity=3) - - -@pytest.fixture -def navitem(): - return baker.make(models.NavItem) - - @pytest.fixture def programs(stations): items = list( diff --git a/aircox/tests/models/test_station.py b/aircox/tests/models/test_station.py deleted file mode 100644 index 952df10..0000000 --- a/aircox/tests/models/test_station.py +++ /dev/null @@ -1,211 +0,0 @@ -import pytest - -from model_bakery import baker - -from aircox.models import Station, Port - -from aircox.tests.conftest import stations - -from aircox.conf import settings - -#(j'ai utilisé Baker directement ici à plusieurs reprises car la fixture 'stations' du fichier conftest génère des champs aléatoires avec lesquelles j'avais du mal à réaliser les tests.) - - -@pytest.mark.django_db -class TestStationQuerySet: - - # test the default method: the method consists in recovering a single default station in different cases. The definition of the station by this method is a univocal choice (not possible to have several stations by default in the db). - # Each case assumes that several stations are already in the database according to different parameters. - def test_default_case1(self): - """ case 1 : - - no default station in db - - no station defined in method """ - - #fake db - baker.make(Station, _quantity=3, default=False) - - #method test - default_station = Station.objects.default() - - # check: we have recovered one single default station as the method indicates. - assert default_station is not None - assert default_station == Station.objects.first() - - def test_default_case2(self): - """ case 2 : - - one default station in db - - no station defined in method """ - - # fake db - baker.make(Station, _quantity=2, default=False) - default_station = baker.make(Station, default=True) - - # method test - specific_station = Station.objects.default() - - # check: default station is the one defined in the fake db - assert specific_station == default_station - - def test_default_case3(self): - """ case 3 : - - one default station in db - - one station define in method """ - - # fake db - baker.make(Station, _quantity=2, default=False) - default_station = baker.make(Station, default=True) - - # method test - specific_station = Station.objects.default(station=default_station.pk) - - # check: default station is the one defined in the method - assert specific_station == default_station - - - # test the active method: return all the active station's instance - def test_active(self): - baker.make(Station, _quantity=2, active=False) - active_station = baker.make(Station, active=True) - - # test - filtered_active_station = Station.objects.active() - - # check: active station is returned - assert active_station in filtered_active_station - - - - -@pytest.mark.django_db -class TestStation: - - # test the streams method: create an array based on a text by separating each element by the break line character(\n). - def test_stream_field_filled(self): - #case 1 : text data with break lines is entered in the audio_streams field - streams_urls = "http://radiocampus.be/stream1.mp3\nhttp://radiocampus.be/stream2.mp3" - station = baker.make(Station, audio_streams=streams_urls) - assert station.streams == ["http://radiocampus.be/stream1.mp3", "http://radiocampus.be/stream2.mp3"] - - def test_stream_field_empty(self): - #case 2 : no data in the audio_streams field - station = baker.make(Station, audio_streams=None) - assert station.streams == [] - - # test the __str__ method = retrieve 'name' field of the station. - def test__str__(self, stations): - for station in stations: - assert station.name == station.__str__() - - # test save method : save the station data in a folder. - def test_save_without_path(self): - #case 1 : not directory path defined. - station = baker.make(Station, path=None, slug="a-slug-with-dash") - station.save() - assert station.path == settings.CONTROLLERS_WORKING_DIR + '\\a_slug_with_dash' - - def test_save_default_station_while_anotherone_is_default(self): - #case 2 : save a new station while another one is already set default. - station1 = baker.make(Station, default=True) - station2 = baker.make(Station, default=False) - - # need to switch station2 default value to true before to try save method. - station2.default = True - station2.save() - - # need to refresh fake db from baker in order to make the test. - station1.refresh_from_db() - station2.refresh_from_db() - - # check if the station we choose to be default is the only one - assert Station.objects.filter(default=True).count() == 1 - assert station1.default is False - assert station2.default is True - -@pytest.mark.django_db -class TestPortQuerySet: - # test active methode : retrieve active ports. - # issues with the preset incompatibility between port and direction. I need to control which type and direction for each fake model instance in order to run pytest without failing randomly. - def test_active(self): - port1 = baker.make(Port, active=True, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT) - port2 = baker.make(Port, active=False, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT) - - active_ports = Port.objects.active() - assert port1 in active_ports - assert port2 not in active_ports - - inactive_ports = Port.objects.active(value=False) - assert port1 not in inactive_ports - assert port2 in inactive_ports - - # test output method : retrieve output ports. - def test_output(self): - port1 = baker.make(Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT) - port2 = baker.make(Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT) - - output_ports = Port.objects.output() - assert port1 in output_ports - assert port2 not in output_ports - - # test input method : retrieve input ports. - def test_input(self): - port1 = baker.make(Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT) - port2 = baker.make(Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT) - - input_ports = Port.objects.input() - assert port1 not in input_ports - assert port2 in input_ports - -@pytest.mark.django_db -class TestPort: - # test __str__ method = return the "type", "direction" and "port id" data of a given "Port" model in one sentence. - def test__str__(self): - port = baker.make(Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT) - assert port.__str__() == 'output: icecast #1' - - # test is_valid_type method = return a Boolean value according to the compatibility criteria between the type and the port of a given port's instance. Compatibilyt criteria are defined in the method. - def test_is_valid_type_and_type_is_input(self): - # case 1 : input direction. return 'true' if the type is compatible - porthttp = baker.make(Port, type=Port.TYPE_HTTP, direction=Port.DIRECTION_INPUT) - porthttps = baker.make(Port, type=Port.TYPE_HTTPS, direction=Port.DIRECTION_INPUT) - - assert porthttp.is_valid_type() == True - assert porthttps.is_valid_type() == True - - - def test_is_valid_type_and_type_is_not_input(self): - # case 2 : is not input direction return 'true' if the type is compatible - porticecast = baker.make(Port, type=Port.TYPE_ICECAST, direction=Port.DIRECTION_OUTPUT) - portfile = baker.make(Port, type=Port.TYPE_FILE, direction=Port.DIRECTION_OUTPUT) - - assert porticecast.is_valid_type() == True - assert portfile.is_valid_type() == True - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- 2.30.2 From 710f8a14c724050d3c83c7078cc24263bb0ceccd Mon Sep 17 00:00:00 2001 From: lauvwbk Date: Fri, 12 May 2023 13:26:31 +0200 Subject: [PATCH 7/7] clean test model page --- aircox/tests/conftest.py | 15 ++ aircox/tests/models/test_page.py | 248 ++++++++++++------------------- 2 files changed, 109 insertions(+), 154 deletions(-) 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 -- 2.30.2