Skip to content

Commit

Permalink
Add tests for playlists and flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
AymericJak committed Nov 6, 2023
1 parent c2d8686 commit 91d20d5
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 10 deletions.
109 changes: 101 additions & 8 deletions pod/playlist/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* run with 'python manage.py test pod.playlist.tests.test_views'
"""
from django.contrib.auth.models import User
from django.http import JsonResponse
from django.urls import reverse
from django.utils.translation import ugettext as _
from django.test import override_settings, TestCase
Expand Down Expand Up @@ -121,7 +122,10 @@ def test_playlist_card_counter(self) -> None:
200,
"Test if status code equal 200.",
)
self.assertTrue("data-number-playlists=5" in response.content.decode(), "Test if there's 5 playlists visible in the playlist page.")
self.assertTrue(
"data-number-playlists=5" in response.content.decode(),
"Test if there's 5 playlists visible in the playlist page.",
)
self.client.logout()
print(" ---> test_playlist_card_counter ok")

Expand Down Expand Up @@ -164,8 +168,10 @@ def test_private_filter(self) -> None:
"bi-globe-americas" in response.content.decode(),
"Test if public icon isn't visible.",
)
print(response.content.decode())
self.assertTrue("data-number-playlists=2" in response.content.decode(), "Test if there's 2 private playlists in the playlist page.")
self.assertTrue(
"data-number-playlists=2" in response.content.decode(),
"Test if there's 2 private playlists in the playlist page.",
)
self.client.logout()
print(" ---> test_private_filter ok")

Expand All @@ -190,7 +196,10 @@ def test_protected_filter(self) -> None:
"bi-globe-americas" in response.content.decode(),
"Test if public icon isn't visible.",
)
self.assertTrue("data-number-playlists=1" in response.content.decode(), "Test if there's 1 protected playlist in the playlist page.")
self.assertTrue(
"data-number-playlists=1" in response.content.decode(),
"Test if there's 1 protected playlist in the playlist page.",
)
self.client.logout()
print(" ---> test_protected_filter ok")

Expand All @@ -214,7 +223,10 @@ def test_public_filter(self) -> None:
self.assertFalse(
"bi-lock" in response.content.decode(), "Test if protected icon isn't visible"
)
self.assertTrue("data-number-playlists=1" in response.content.decode(), "Test if there's 1 playlist visible in the playlist page.")
self.assertTrue(
"data-number-playlists=1" in response.content.decode(),
"Test if there's 1 playlist visible in the playlist page.",
)
self.client.logout()
print(" ---> test_public_filter ok")

Expand All @@ -238,7 +250,10 @@ def test_allpublic_filter(self) -> None:
self.assertFalse(
"bi-lock" in response.content.decode(), "Test if protected icon isn't visible"
)
self.assertTrue("data-number-playlists=2" in response.content.decode(), "Test if there's 2 playlists visible in the playlist page.")
self.assertTrue(
"data-number-playlists=2" in response.content.decode(),
"Test if there's 2 playlists visible in the playlist page.",
)
self.client.logout()
print(" ---> test_allpublic_filter ok")

Expand All @@ -263,9 +278,13 @@ def test_promoted_filter(self) -> None:
"bi-lock" in response.content.decode(), "Test if protected icon isn't visible"
)
self.assertTrue(
"promoted-icon" in response.content.decode(), "Test if promoted icon is visible."
"promoted-icon" in response.content.decode(),
"Test if promoted icon is visible.",
)
self.assertTrue(
"data-number-playlists=1" in response.content.decode(),
"Test if there's 1 promoted playlist in the playlist page.",
)
self.assertTrue("data-number-playlists=1" in response.content.decode(), "Test if there's 1 promoted playlist in the playlist page.")
self.client.logout()
print(" ---> test_promoted_filter ok")

Expand Down Expand Up @@ -610,6 +629,12 @@ def setUp(self) -> None:
kwargs={"slug": get_favorite_playlist_for_user(self.user2).slug},
)

self.playlist_content_1 = PlaylistContent.objects.create(
playlist=self.playlist_user1,
video=self.video,
rank=1,
)

@override_settings(USE_FAVORITES=True)
def test_playlist_video_list(self):
"""Test if the favorite video list has a correct number of video in it."""
Expand Down Expand Up @@ -700,6 +725,74 @@ def test_manage_section_for_editable_playlists(self) -> None:
self.client.logout()
print(" ---> test_manage_section_for_editable_playlists ok")

@override_settings(USE_PLAYLIST=True)
def test_remove_video_in_playlist(self):
"""Test if remove a video from a playlist works."""
importlib.reload(context_processors)
self.client.force_login(self.user)
url_content = reverse(
"playlist:content", kwargs={"slug": self.playlist_user1.slug}
)
response = self.client.get(url_content)
self.assertTrue(
'data-countvideos="1"' in response.content.decode(),
"Test if there's 1 video in the playlist.",
)
url = reverse(
"playlist:remove-video",
kwargs={
"slug": self.playlist_user1.slug,
"video_slug": self.video.slug,
},
)
response = self.client.get(url, HTTP_REFERER=url_content)
self.assertEqual(response.status_code, 302)

redirected_url = response.url
response = self.client.get(redirected_url)
self.assertTrue(
'data-countvideos="0"' in response.content.decode(),
"Test if there's 0 video in the playlist.",
)
print(" ---> test_remove_video_in_playlist ok")

@override_settings(USE_PLAYLIST=True)
def test_remove_video_in_playlist_json(self):
"""Test if remove a video from a playlist works with JSON."""
importlib.reload(context_processors)
self.client.force_login(self.user)
url_content = reverse(
"playlist:content", kwargs={"slug": self.playlist_user1.slug}
)
response = self.client.get(url_content)
self.assertTrue(
'data-countvideos="1"' in response.content.decode(),
"Test if there's 1 video in the playlist.",
)

url = (
reverse(
"playlist:remove-video",
kwargs={
"slug": self.playlist_user1.slug,
"video_slug": self.video.slug,
},
)
+ "?json=1"
)

response = self.client.get(url)
self.assertEqual(response.status_code, 200)
data = JsonResponse({"state": "out-playlist"}).content.decode("utf-8")
self.assertEqual(response.content.decode("utf-8"), data)

response = self.client.get(url_content)
self.assertTrue(
'data-countvideos="0"' in response.content.decode(),
"Test if there's 0 video in the playlist.",
)
print(" ---> test_remove_video_in_playlist_json ok")


class TestStatsInfoTestCase(TestCase):
"""Statistics informations test case."""
Expand Down
2 changes: 1 addition & 1 deletion pod/playlist/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def sort_playlist_list(playlist_list: list, sort_field: str, sort_direction="")
"date_created",
"date_updated",
}:
if sort_field in {'name'}:
if sort_field in {"name"}:
sort_field = Lower(sort_field)
if not sort_direction:
sort_field = sort_field.desc()
Expand Down
2 changes: 1 addition & 1 deletion pod/video/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def sort_videos_list(videos_list, sort_field, sort_direction=""):
"viewcount",
"rank",
}:
if sort_field in {'title', 'title_fr', 'title_en'}:
if sort_field in {"title", "title_fr", "title_en"}:
sort_field = Lower(sort_field)
if not sort_direction:
sort_field = sort_field.desc()
Expand Down

0 comments on commit 91d20d5

Please sign in to comment.