parent
fef96f5093
commit
cc63c843a0
@ -0,0 +1,121 @@
|
||||
import unittest
|
||||
from unittest.mock import patch, Mock
|
||||
import requests
|
||||
import json
|
||||
from bilibili_search import search_bilibili, get_video_cid, fetch_danmu, count_and_rank_danmu,fetch_bilibili_videos
|
||||
class TestBilibiliFunctions(unittest.TestCase):
|
||||
|
||||
@patch('bilibili_search.requests.get')
|
||||
#测试 search_bilibili 函数在成功情况下的行为
|
||||
def test_search_bilibili_success(self, mock_get):
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {'code': 0, 'data': {'result': [{'bvid': 'BV1'}]}}
|
||||
mock_get.return_value = mock_response
|
||||
result = search_bilibili('test', 1)
|
||||
self.assertEqual(result, ['BV1'])
|
||||
|
||||
@patch('bilibili_search.requests.get')
|
||||
#测试 search_bilibili 函数在 JSON 解码错误情况下的行为
|
||||
def test_search_bilibili_json_error(self, mock_get):
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.side_effect = json.JSONDecodeError('error', '', 0)
|
||||
mock_get.return_value = mock_response
|
||||
result = search_bilibili('test', 1)
|
||||
self.assertEqual(result, [])
|
||||
|
||||
@patch('bilibili_search.requests.get')
|
||||
#测试 get_video_cid 函数在成功情况下的行为
|
||||
def test_get_video_cid_success(self, mock_get):
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {'data': {'cid': 123}}
|
||||
mock_get.return_value = mock_response
|
||||
result = get_video_cid('BV1')
|
||||
self.assertEqual(result, 123)
|
||||
|
||||
@patch('bilibili_search.requests.get')
|
||||
#测试 get_video_cid 函数在请求失败的情况下的行为
|
||||
def test_get_video_cid_failure(self, mock_get):
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 404
|
||||
mock_get.return_value = mock_response
|
||||
result = get_video_cid('BV1')
|
||||
self.assertIsNone(result)
|
||||
|
||||
@patch('bilibili_search.requests.get')
|
||||
#测试 fetch_danmu 函数在成功情况下的行为
|
||||
def test_fetch_danmu_success(self, mock_get):
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.content = b'<d>test danmu</d>'
|
||||
mock_get.return_value = mock_response
|
||||
result = fetch_danmu(123)
|
||||
self.assertEqual(result, ['test danmu'])
|
||||
|
||||
@patch('bilibili_search.requests.get')
|
||||
#测试 fetch_danmu 函数在成功情况下的行为
|
||||
def test_fetch_danmu_http_error(self, mock_get):
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 500
|
||||
mock_get.return_value = mock_response
|
||||
result = fetch_danmu(123)
|
||||
self.assertEqual(result, [])
|
||||
|
||||
|
||||
@patch('bilibili_search.requests.get')
|
||||
def test_search_bilibili_no_results(self, mock_get):
|
||||
# 模拟没有搜索结果的API响应
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
'code': 0,
|
||||
'data': {
|
||||
'result': []
|
||||
}
|
||||
}
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
result = search_bilibili('不存在的查询词', 5)
|
||||
self.assertEqual(result, [])
|
||||
|
||||
|
||||
@patch('bilibili_search.requests.get')
|
||||
def test_search_bilibili_page_limit(self, mock_get):
|
||||
# 模拟分页
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
'code': 0,
|
||||
'data': {
|
||||
'result': [{'bvid': f'BV1J54y1D7E{page}'} for page in range(42)]
|
||||
}
|
||||
}
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
result = search_bilibili('2024巴黎奥运会', 100)
|
||||
self.assertEqual(len(result), 100)
|
||||
|
||||
@patch('bilibili_search.requests.get')
|
||||
def test_fetch_no_videos(self):
|
||||
# # 模拟返回空的视频列表
|
||||
mock_response = {'code': 0, 'data': {'result': []}}
|
||||
with patch('bilibili_search.requests.get', return_value=Mock(status_code=200, json=lambda: mock_response)):
|
||||
video_list = fetch_bilibili_videos('2024 Paris Olympics', 1)
|
||||
self.assertEqual(video_list, [])
|
||||
|
||||
|
||||
|
||||
@patch('bilibili_search.export_to_excel')
|
||||
def test_count_and_rank_danmu(self, mock_export):
|
||||
# 测试弹幕统计和排名
|
||||
danmu_texts = ['人工智能', '机器学习', '人工智能', '人工智能']
|
||||
count_and_rank_danmu(danmu_texts)
|
||||
|
||||
# 检查是否调用了导出到Excel的函数
|
||||
mock_export.assert_called_once()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in new issue