diff options
| author | A Farzat <a@farzat.xyz> | 2025-10-08 12:48:20 +0300 |
|---|---|---|
| committer | A Farzat <a@farzat.xyz> | 2025-10-08 12:48:20 +0300 |
| commit | 6d02de42105e1fa390984c665fd73b8e6f6116f5 (patch) | |
| tree | e1041072b2e622e1121ce3b72bc73bbe23eb58b9 | |
| parent | 4372982119140dba5b132e09200b94c1b422a1dd (diff) | |
| download | csca5028-6d02de42105e1fa390984c665fd73b8e6f6116f5.tar.gz csca5028-6d02de42105e1fa390984c665fd73b8e6f6116f5.zip | |
Modify analyser tests to reflect the use of extractor
| -rw-r--r-- | tests/analyser.py | 76 | ||||
| -rw-r--r-- | tests/utils/get_random_vid_info.py | 5 | ||||
| -rw-r--r-- | tests/utils/vid_url_to_html.py | 4 |
3 files changed, 29 insertions, 56 deletions
diff --git a/tests/analyser.py b/tests/analyser.py index e91e72f..c698942 100644 --- a/tests/analyser.py +++ b/tests/analyser.py @@ -1,4 +1,3 @@ -from hashlib import md5 from typing import Any, Dict from unittest import TestCase from unittest.mock import MagicMock, patch @@ -9,27 +8,9 @@ from pymongo.collection import Collection from components.subscriptions.main import Subscription from components.subscriptions.typing import SubsDict from components.videos import VideoTuple -from components.ytdlp import obtain_vid_info from data_analyser.utils import analyse_video, analyse_subscription, analyse_collection - -def url_based_extract_info(url: str, **_: Any) -> Dict[str, Any]: - hash = md5(url.encode()).hexdigest() - hash_int = int(hash[:4], 16) - seconds = hash_int % 60 - minutes = hash_int // 60 - hours = minutes // 60 - minutes = minutes % 60 - if hours: - duration_string = "%d:%02d:%02d" % (hours, minutes, seconds) - elif minutes: - duration_string = "%d:%02d" % (minutes, seconds) - else: - duration_string = "%d" % (seconds, ) - return { - "duration_string": duration_string, - "other_key": "other_value", - "other key": "other value", - } +from .utils.vid_url_to_html import obtain_vid_duration +from .utils.get_random_vid_info import get_random_vid_duration class TestAnalyser(TestCase): def setUp(self) -> None: @@ -45,69 +26,52 @@ class TestAnalyser(TestCase): self.sub1.insert() self.sub1.fetch() - self.mock_ydl_class = patch('components.ytdlp.YoutubeDL').start() - self.mock_ydl_instance = MagicMock() - self.mock_ydl_class.return_value.__enter__.return_value = self.mock_ydl_instance - self.mock_ydl_instance.extract_info.side_effect = url_based_extract_info + self.mock_vid_duration = patch('data_analyser.utils.obtain_vid_duration').start() + self.mock_vid_duration.side_effect = obtain_vid_duration self.addCleanup(patch.stopall) - def test_obtain_vid_info(self) -> None: - result = obtain_vid_info(self.sub1.videos[0].link) - expected_duration = url_based_extract_info(self.sub1.videos[0].link)["duration_string"] - self.assertEqual(result, {"duration_string": expected_duration}) - - def test_obtain_vid_info_with_exception(self) -> None: - self.mock_ydl_instance.extract_info.side_effect = Exception("Network error") - - result = obtain_vid_info(self.sub1.videos[0].link) - expected_keys = {"duration_string"} - actual_keys = set(result.keys()) - self.assertEqual(expected_keys, actual_keys, - "Expected keys %s, got %s." % (expected_keys, actual_keys)) - self.assertRegex(result["duration_string"], "^-1:[0-5][0-9]$") - def test_analyse_video(self) -> None: modified_vid = analyse_video(self.sub1.videos[1]) - expected_duration = url_based_extract_info(self.sub1.videos[1].link)["duration_string"] + expected_duration = get_random_vid_duration(self.sub1.videos[1].link) self.assertIsInstance(modified_vid, VideoTuple) - self.assertEqual(modified_vid.duration_string, expected_duration) + self.assertEqual(modified_vid.duration, expected_duration) self.assertTrue(modified_vid.analysed) def test_analyse_video_with_exception(self) -> None: - self.mock_ydl_instance.extract_info.side_effect = Exception("Network error") + self.mock_vid_duration.side_effect = Exception("Network error") modified_vid = analyse_video(self.sub1.videos[0]) self.assertIsInstance(modified_vid, VideoTuple) - self.assertRegex(modified_vid.duration_string, "^-1:[0-5][0-9]$") + self.assertLess(modified_vid.duration, 0) self.assertTrue(modified_vid.analysed) def test_analyse_subscription(self) -> None: self.assertTrue(analyse_subscription(self.sub1)) for vid in self.sub1.videos: - expected_duration = url_based_extract_info(vid.link)["duration_string"] - self.assertEqual(vid.duration_string, expected_duration) + expected_duration = get_random_vid_duration(vid.link) + self.assertEqual(vid.duration, expected_duration) self.assertTrue(vid.analysed) def test_analyse_subscription_with_further_fetch(self) -> None: self.assertTrue(analyse_subscription(self.sub1)) for vid in self.sub1.videos: - expected_duration = url_based_extract_info(vid.link)["duration_string"] - self.assertEqual(vid.duration_string, expected_duration) + expected_duration = get_random_vid_duration(vid.link) + self.assertEqual(vid.duration, expected_duration) self.assertTrue(vid.analysed) self.sub1.link = r"tests/data/feed@ytnnews24@002.xml" self.sub1.fetch() self.assertTrue(analyse_subscription(self.sub1)) for vid in self.sub1.videos: - expected_duration = url_based_extract_info(vid.link)["duration_string"] - self.assertEqual(vid.duration_string, expected_duration) + expected_duration = get_random_vid_duration(vid.link) + self.assertEqual(vid.duration, expected_duration) self.assertTrue(vid.analysed) def test_analyse_subscription_without_further_fetch(self) -> None: self.assertTrue(analyse_subscription(self.sub1)) for vid in self.sub1.videos: - expected_duration = url_based_extract_info(vid.link)["duration_string"] - self.assertEqual(vid.duration_string, expected_duration) + expected_duration = get_random_vid_duration(vid.link) + self.assertEqual(vid.duration, expected_duration) self.assertTrue(vid.analysed) self.assertFalse(analyse_subscription(self.sub1)) @@ -115,8 +79,8 @@ class TestAnalyser(TestCase): self.assertEqual(analyse_collection(self.collection), 1) for sub_dict in self.collection.find(): for vid in map(VideoTuple._make, sub_dict["videos"]): - expected_duration = url_based_extract_info(vid.link)["duration_string"] - self.assertEqual(vid.duration_string, expected_duration) + expected_duration = get_random_vid_duration(vid.link) + self.assertEqual(vid.duration, expected_duration) self.assertTrue(vid.analysed) self.sub2 = Subscription( _id="yt:channel:7YOGHUfC1Tb6E4pudI9STA", @@ -130,8 +94,8 @@ class TestAnalyser(TestCase): for sub_dict in self.collection.find(): sub = Subscription(**sub_dict) for vid in map(VideoTuple._make, sub_dict["videos"]): - expected_duration = url_based_extract_info(vid.link)["duration_string"] - self.assertEqual(vid.duration_string, expected_duration) + expected_duration = get_random_vid_duration(vid.link) + self.assertEqual(vid.duration, expected_duration) self.assertTrue(vid.analysed) def tearDown(self) -> None: diff --git a/tests/utils/get_random_vid_info.py b/tests/utils/get_random_vid_info.py new file mode 100644 index 0000000..f85a226 --- /dev/null +++ b/tests/utils/get_random_vid_info.py @@ -0,0 +1,5 @@ +from hashlib import md5 + +def get_random_vid_duration(url: str) -> int: + hash = md5(url.encode()).hexdigest() + return int(hash[:4], 16) diff --git a/tests/utils/vid_url_to_html.py b/tests/utils/vid_url_to_html.py index 355e830..60f6ccc 100644 --- a/tests/utils/vid_url_to_html.py +++ b/tests/utils/vid_url_to_html.py @@ -1,6 +1,7 @@ from urllib.parse import urlparse, parse_qs from components.extractor.check_url import is_video +from .get_random_vid_info import get_random_vid_duration def extract_vid_id(url: str) -> str: if not is_video(url): @@ -16,3 +17,6 @@ def extract_vid_id(url: str) -> str: def get_vid_html_from_url(url: str) -> str: with open(f'tests/data/video@{extract_vid_id(url)}.html', 'r') as file: return file.read() + +def obtain_vid_duration(url:str, html: str = '') -> int: + return get_random_vid_duration(url) |
