Compare commits

...

4 Commits

Author SHA1 Message Date
0f5128ca7e #101 test model page 2023-05-10 12:33:51 +02:00
cd6bb608d4 #101 test model page 2023-05-10 10:12:25 +02:00
ced76e8d13 pytest model page 2023-05-09 13:04:28 +02:00
9a624994e9 #99 pytest page model 2023-05-08 18:01:29 +02:00
3 changed files with 266 additions and 216 deletions

View File

@ -123,9 +123,14 @@ class BasePage(models.Model):
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)[:100]
count = Page.objects.filter(slug__startswith=self.slug).count()
if count:
self.slug += "-" + str(count)
pagecount = Page.objects.filter(slug__startswith=self.slug).count()
staticpagecount = StaticPage.objects.filter(
slug__startswith=self.slug
).count()
if pagecount:
self.slug += "-" + str(pagecount)
if staticpagecount:
self.slug += "-" + str(staticpagecount)
if self.parent and not self.cover:
self.cover = self.parent.cover
@ -152,9 +157,9 @@ class BasePage(models.Model):
@property
def display_title(self):
if self.is_published():
if self.is_published:
return self.title
return self.parent.display_title()
return self.parent.display_title
@cached_property
def headline(self):

View File

@ -0,0 +1,256 @@
import pytest
from model_bakery import baker
from aircox.models import Category, Page, StaticPage, NavItem
@pytest.mark.django_db
class TestCategory:
def test__str__(self):
category = baker.make(Category)
title = category.__str__()
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.
class TestBasePageQuerySet:
@pytest.mark.django_db
def test_draft(self, staticpages):
draft_pages = StaticPage.objects.draft()
for page in draft_pages:
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:
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:
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
@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
@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
@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
# 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_save_generate_unique_slug(self, staticpages):
page = baker.make(StaticPage, title="Title with spaces", slug=None)
page.save()
assert page.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"
@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
@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() == "#"
@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,
)
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
@pytest.mark.django_db
def test_is_published(self, staticpages):
page = baker.make(StaticPage, status=StaticPage.STATUS_PUBLISHED)
assert page.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
@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"
@pytest.mark.django_db
def test_headline(self, staticpages):
page = baker.make(
StaticPage, content="<h2>My headline</h2><p>My content.</p>"
)
empty_content_page = baker.make(StaticPage, content="")
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.
@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}
#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, 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
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:
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
@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

View File

@ -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