add tests for urlparser

This commit is contained in:
Simon 2024-05-21 13:05:40 +02:00
parent ae89f47072
commit 3e3e3ae78e
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
3 changed files with 128 additions and 42 deletions

View File

@ -0,0 +1,128 @@
"""tests for url parser"""
import pytest
from home.src.ta.urlparser import Parser
# video id parsing
VIDEO_URL_IN = [
"7DKv5H5Frt0",
"https://www.youtube.com/watch?v=7DKv5H5Frt0",
"https://www.youtube.com/watch?v=7DKv5H5Frt0&t=113&feature=shared",
"https://www.youtube.com/watch?v=7DKv5H5Frt0&list=PL96C35uN7xGJu6skU4TBYrIWxggkZBrF5&index=1&pp=iAQB" # noqa: E501
"https://youtu.be/7DKv5H5Frt0",
]
VIDEO_OUT = [{"type": "video", "url": "7DKv5H5Frt0", "vid_type": "unknown"}]
VIDEO_TEST_CASES = [(i, VIDEO_OUT) for i in VIDEO_URL_IN]
# shorts id parsing
SHORTS_URL_IN = [
"https://www.youtube.com/shorts/YG3-Pw3rixU",
"https://youtube.com/shorts/YG3-Pw3rixU?feature=shared",
]
SHORTS_OUT = [{"type": "video", "url": "YG3-Pw3rixU", "vid_type": "shorts"}]
SHORTS_TEST_CASES = [(i, SHORTS_OUT) for i in SHORTS_URL_IN]
# channel id parsing
CHANNEL_URL_IN = [
"UCBa659QWEk1AI4Tg--mrJ2A",
"@TomScottGo",
"https://www.youtube.com/channel/UCBa659QWEk1AI4Tg--mrJ2A",
"https://www.youtube.com/@TomScottGo",
]
CHANNEL_OUT = [
{
"type": "channel",
"url": "UCBa659QWEk1AI4Tg--mrJ2A",
"vid_type": "unknown",
}
]
CHANNEL_TEST_CASES = [(i, CHANNEL_OUT) for i in CHANNEL_URL_IN]
# channel vid type parsing
CHANNEL_VID_TYPES = [
(
"https://www.youtube.com/@IBRACORP/videos",
[
{
"type": "channel",
"url": "UC7aW7chIafJG6ECYAd3N5uQ",
"vid_type": "videos",
}
],
),
(
"https://www.youtube.com/@IBRACORP/shorts",
[
{
"type": "channel",
"url": "UC7aW7chIafJG6ECYAd3N5uQ",
"vid_type": "shorts",
}
],
),
(
"https://www.youtube.com/@IBRACORP/streams",
[
{
"type": "channel",
"url": "UC7aW7chIafJG6ECYAd3N5uQ",
"vid_type": "streams",
}
],
),
]
# playlist id parsing
PLAYLIST_URL_IN = [
"PL96C35uN7xGJu6skU4TBYrIWxggkZBrF5",
"https://www.youtube.com/playlist?list=PL96C35uN7xGJu6skU4TBYrIWxggkZBrF5",
]
PLAYLIST_OUT = [
{
"type": "playlist",
"url": "PL96C35uN7xGJu6skU4TBYrIWxggkZBrF5",
"vid_type": "unknown",
}
]
PLAYLIST_TEST_CASES = [(i, PLAYLIST_OUT) for i in PLAYLIST_URL_IN]
# personal playlists
EXPECTED_WL = [{"type": "playlist", "url": "WL", "vid_type": "unknown"}]
EXPECTED_LL = [{"type": "playlist", "url": "LL", "vid_type": "unknown"}]
PERSONAL_PLAYLISTS_TEST_CASES = [
("WL", EXPECTED_WL),
("https://www.youtube.com/playlist?list=WL", EXPECTED_WL),
("LL", EXPECTED_LL),
("https://www.youtube.com/playlist?list=LL", EXPECTED_LL),
]
# collect tests expected to pass
PASSTING_TESTS = []
PASSTING_TESTS.extend(VIDEO_TEST_CASES)
PASSTING_TESTS.extend(SHORTS_TEST_CASES)
PASSTING_TESTS.extend(CHANNEL_TEST_CASES)
PASSTING_TESTS.extend(CHANNEL_VID_TYPES)
PASSTING_TESTS.extend(PLAYLIST_TEST_CASES)
PASSTING_TESTS.extend(PERSONAL_PLAYLISTS_TEST_CASES)
@pytest.mark.parametrize("url_str, expected_result", PASSTING_TESTS)
def test_passing_parse(url_str, expected_result):
"""test parser"""
parser = Parser(url_str)
parsed = parser.parse()
assert parsed == expected_result
INVALID_IDS_ERRORS = [
"aaaaa",
"https://www.youtube.com/playlist?list=AAAA",
]
@pytest.mark.parametrize("invalid_value", INVALID_IDS_ERRORS)
def test_utility_class_init_raises_value_error(invalid_value):
"""test for invalid IDs"""
with pytest.raises(ValueError, match="not a valid id_str"):
parser = Parser(invalid_value)
parser.parse()

View File

@ -1,42 +0,0 @@
"""All test classes"""
from django.test import TestCase
class URLTests(TestCase):
"""test if all expected URL are there"""
def test_home_view(self):
"""check homepage"""
response = self.client.get("/")
self.assertEqual(response.status_code, 200)
def test_about_view(self):
"""check about page"""
response = self.client.get("/about/")
self.assertEqual(response.status_code, 200)
def test_downloads_view(self):
"""check downloads page"""
response = self.client.get("/downloads/")
self.assertEqual(response.status_code, 200)
def test_channel_view(self):
"""check channel page"""
response = self.client.get("/channel/")
self.assertEqual(response.status_code, 200)
def test_settings_view(self):
"""check settings page"""
response = self.client.get("/settings/")
self.assertEqual(response.status_code, 200)
def test_progress_view(self):
"""check ajax progress endpoint"""
response = self.client.get("/downloads/progress/")
self.assertEqual(response.status_code, 200)
def test_process_view(self):
"""check process ajax endpoint"""
response = self.client.get("/process/")
self.assertEqual(response.status_code, 200)