You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import unittest
from dmk import get_video_ids, get_danmaku, filter_danmakus
from generate import generate_wordcloud, get_random_color, pink_color_func, orange_color_func
import requests
from unittest.mock import patch, Mock
import os
from imageio import imread
from PIL import Image
class TestDanmaku(unittest.TestCase):
@patch('requests.Session.get') # 使用 mock 来模拟外部 API 调用
def test_get_danmaku_valid(self, mock_get):
# 模拟返回正确的 cid 数据
mock_cid_response = Mock()
mock_cid_response.json.return_value = {
'data': [{'cid': '123456', 'part': '测试视频'}]
}
# 模拟返回正确的 danmaku 数据
mock_danmaku_response = Mock()
mock_danmaku_response.content = '<i><d p="0,1,25,16777215,0,0,0,0">弹幕1</d></i>'.encode('utf-8')
mock_danmaku_response.encoding = 'utf-8'
# 当请求 bvid 返回 mock_cid_responsedanmaku 请求返回 mock_danmaku_response
mock_get.side_effect = [mock_cid_response, mock_danmaku_response]
# 调用被测函数
danmakus = get_danmaku("valid_bvid")
# 断言返回了正确的弹幕
self.assertEqual(danmakus, ["弹幕1"])
@patch('requests.Session.get')
def test_get_danmaku_invalid(self, mock_get):
# 模拟返回无效的 cid 数据
mock_cid_response = Mock()
mock_cid_response.json.return_value = {'data': []}
mock_get.return_value = mock_cid_response
# 调用被测函数
with self.assertRaises(IndexError): # 如果没有 cid应该抛出 IndexError
get_danmaku("invalid_bvid")
@patch('requests.Session.get')
def test_get_danmaku_no_response(self, mock_get):
# 模拟请求失败时的处理
mock_get.side_effect = requests.exceptions.RequestException
# 断言在请求失败时返回空列表
with self.assertRaises(requests.exceptions.RequestException):
get_danmaku("any_bvid")
class TestGetVideoIDs(unittest.TestCase):
@patch('dmk.fetch_page') # mock fetch_page 函数
def test_get_video_ids_valid(self, mock_fetch_page):
# 模拟 fetch_page 函数返回的视频ID
mock_fetch_page.return_value = ['BV1ab1c1d', 'BV2cd2e2f']
# 调用 get_video_ids期望返回正确的视频ID
video_ids = get_video_ids("测试关键词", 60)
# 断言结果
self.assertIn('BV1ab1c1d', video_ids)
self.assertIn('BV2cd2e2f', video_ids)
self.assertEqual(len(video_ids), 4)
# 检查文件是否正确写入
self.assertTrue(os.path.exists('videos_ids/测试关键词.txt'))
with open('videos_ids/测试关键词.txt', 'r') as file:
content = file.read()
self.assertIn('BV1ab1c1d', content)
self.assertIn('BV2cd2e2f', content)
@patch('dmk.fetch_page')
def test_get_video_ids_empty(self, mock_fetch_page):
# 模拟 fetch_page 函数返回空列表
mock_fetch_page.return_value = []
# 调用 get_video_ids期望返回空列表
video_ids = get_video_ids("空关键词", 30)
# 断言返回的列表为空
self.assertEqual(video_ids, [])
# 检查文件是否正确写入
self.assertTrue(os.path.exists('videos_ids/空关键词.txt'))
with open('videos_ids/空关键词.txt', 'r') as file:
content = file.read()
self.assertEqual(content.strip(), '')
@patch('dmk.fetch_page')
def test_get_video_ids_max_results(self, mock_fetch_page):
# 模拟 fetch_page 函数返回多个视频ID
mock_fetch_page.return_value = ['BV1ab1c1d', 'BV2cd2e2f']
# 调用 get_video_ids期望返回指定数量的视频ID
video_ids = get_video_ids("测试关键词", 10)
# 断言返回的视频ID数量正确
self.assertEqual(len(video_ids), 0)
class TestGenerateWordCloud(unittest.TestCase):
def setUp(self):
self.text = "这是一个测试文本,用于生成词云。"
self.font_path = "C:\\Windows\\Fonts\\msyh.ttc" # 请替换为实际的字体路径
self.width = 800
self.height = 600
self.styles = ['basic', 'jiyi', '537', 'family', 'other']
def test_generate_wordcloud(self):
for style in self.styles:
with self.subTest(style=style):
try:
generate_wordcloud(self.text, self.font_path, self.width, self.height, style)
except Exception as e:
self.fail(f"generate_wordcloud 函数在样式 '{style}' 下引发异常: {e}")
if __name__ == '__main__':
unittest.main()