|
|
import unittest
|
|
|
from unittest.mock import mock_open, patch
|
|
|
import os
|
|
|
import requests
|
|
|
from unittest.mock import call
|
|
|
# 假设上面的 writeintxt 函数在 writeintxt_module 中定义
|
|
|
from getdm import writeintxt,get_web_url,get_videos_url
|
|
|
|
|
|
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0"}
|
|
|
|
|
|
cookies = " buvid3=8231EFA3-C943-B46F-C28D-10CA6EB40DD180949infoc; CURRENT_FNVAL=4048; rpdid=|(JYYJ|u)luu0J'uYmkl~mY)k; buvid4=CA2A9538-3AB7-5FF0-1AD2-830935987DFB07953-024012212-75GkjfNGeaGAWMbBOvqWtQ%3D%3D; buvid_fp=f29e4e3ef42f7eb5eeb379115e870ad9; LIVE_BUVID=AUTO7217086875647310; PVID=2; b_nut=100; _uuid=7FDC1091C-5D99-24A8-4133-F89DFB510EA6531786infoc; header_theme_version=CLOSE; enable_web_push=DISABLE; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjY2NDk2NTcsImlhdCI6MTcyNjM5MDM5NywicGx0IjotMX0.3Z1vsF3Xzu2nPlPrZBCesXytvgQb_37-XNcBtrRlMzE; bili_ticket_expires=1726649597; bsource=search_bing; bmg_af_switch=1; bmg_src_def_domain=i1.hdslb.com; home_feed_column=4; SESSDATA=188e2907%2C1742055275%2C569b4%2A92CjDCOHUaxUkq_L5c6PfUab0-sYNi5nOwFET1z4H9q14EuO2HVsnxLmx1932gpvUQBgESVnFIY3lzUFJhZWxtY1N1d21POEc5bDRKRldVTlJSTDNWdG8ybkJSYmdGN3g3S2hjRk1uYzhSVEJUUkVOMThzb2Zfb0txZlUwbDNVdnZEM1h6UjJ6amhBIIEC; bili_jct=b1a79048046b927643ac32fe2ce260a0; DedeUserID=1986103749; DedeUserID__ckMd5=be6d80b5f24d01e0; browser_resolution=1220-150; sid=6jgymz8o; bp_t_offset_1986103749=977826403892330496; b_lsid=EF11D7E6_191FBE2F27F"
|
|
|
|
|
|
dic_cookies = {}
|
|
|
for i in cookies.split("; "):
|
|
|
dic_cookies[i.split("=")[0]] = i.split("=")[1]
|
|
|
|
|
|
|
|
|
class TestWriteintxt(unittest.TestCase):
|
|
|
def setUp(self):
|
|
|
# 1创建一个临时文件路径
|
|
|
self.test_file_path = "test.txt"
|
|
|
|
|
|
def tearDown(self):
|
|
|
# 2删除测试文件
|
|
|
if os.path.exists(self.test_file_path):
|
|
|
os.remove(self.test_file_path)
|
|
|
|
|
|
def test_writeintxt_empty_list(self):
|
|
|
# 3测试空列表
|
|
|
writeintxt([], self.test_file_path)
|
|
|
with open(self.test_file_path, 'r', encoding='utf-8') as file:
|
|
|
self.assertEqual(file.read(), "")
|
|
|
|
|
|
def test_writeintxt_single_item(self):
|
|
|
# 4测试单个元素列表
|
|
|
writeintxt(["hello"], self.test_file_path)
|
|
|
with open(self.test_file_path, 'r', encoding='utf-8') as file:
|
|
|
self.assertEqual(file.read(), "hello\n")
|
|
|
|
|
|
def test_writeintxt_multiple_items(self):
|
|
|
# 5测试多个元素列表
|
|
|
writeintxt(["hello", "world"], self.test_file_path)
|
|
|
with open(self.test_file_path, 'r', encoding='utf-8') as file:
|
|
|
self.assertEqual(file.read(), "hello\nworld\n")
|
|
|
|
|
|
def test_writeintxt_with_mock(self):
|
|
|
# 6使用 mock 测试文件写入
|
|
|
mocked_open = mock_open()
|
|
|
with patch('builtins.open', mocked_open):
|
|
|
writeintxt(["hello", "world"], self.test_file_path)
|
|
|
# 检查是否按照预期的顺序写入了所有元素
|
|
|
mocked_open().write.assert_has_calls([call("hello\n"), call("world\n")])
|
|
|
|
|
|
def test_writeintxt_non_string_items(self):
|
|
|
#7 测试非字符串元素
|
|
|
writeintxt([123, 456], self.test_file_path)
|
|
|
with open(self.test_file_path, 'r', encoding='utf-8') as file:
|
|
|
self.assertEqual(file.read(), "123\n456\n")
|
|
|
|
|
|
def test_writeintxt_with_special_characters(self):
|
|
|
# 8测试包含特殊字符的字符串
|
|
|
writeintxt(["hello\nworld", "test\"string"], self.test_file_path)
|
|
|
with open(self.test_file_path, 'r', encoding='utf-8') as file:
|
|
|
self.assertEqual(file.read(), "hello\nworld\ntest\"string\n")
|
|
|
|
|
|
def test_writeintxt_with_unicode(self):
|
|
|
# 9测试包含 Unicode 字符的字符串
|
|
|
writeintxt(["你好", "世界"], self.test_file_path)
|
|
|
with open(self.test_file_path, 'r', encoding='utf-8') as file:
|
|
|
self.assertEqual(file.read(), "你好\n世界\n")
|
|
|
|
|
|
def test_writeintxt_file_not_created(self):
|
|
|
# 10测试文件未创建的情况(模拟)
|
|
|
with patch('builtins.open', side_effect=FileNotFoundError):
|
|
|
with self.assertRaises(FileNotFoundError):
|
|
|
writeintxt(["hello"], "non_existent_path/test.txt")
|
|
|
|
|
|
def test_writeintxt_file_permission_error(self):
|
|
|
# 11测试文件权限错误的情况(模拟)
|
|
|
with patch('builtins.open', side_effect=PermissionError):
|
|
|
with self.assertRaises(PermissionError):
|
|
|
writeintxt(["hello"], "/non_existent_path/test.txt")
|
|
|
|
|
|
# 可以继续添加更多的测试用例来覆盖不同的场景
|
|
|
|
|
|
class TestGetWebUrl(unittest.TestCase):
|
|
|
def test_get_web_url_no_pages(self):
|
|
|
# 1测试没有页数的情况
|
|
|
keyword = "test"
|
|
|
pages = 0
|
|
|
expected_urls = [] # 调整为期望返回空列表
|
|
|
self.assertEqual(get_web_url(keyword, pages), expected_urls)
|
|
|
|
|
|
def test_get_web_url_one_page(self):
|
|
|
# 2测试只有一页的情况
|
|
|
keyword = "test"
|
|
|
pages = 1
|
|
|
expected_urls = [
|
|
|
"https://search.bilibili.com/video?keyword=test&from_source=webtop_search&spm_id_from=333.1007&search_source=5"
|
|
|
]
|
|
|
self.assertEqual(get_web_url(keyword, pages), expected_urls)
|
|
|
|
|
|
def test_get_web_url_multiple_pages(self):
|
|
|
# 3测试多页的情况
|
|
|
keyword = "test"
|
|
|
pages = 3
|
|
|
expected_urls = [
|
|
|
"https://search.bilibili.com/video?keyword=test&from_source=webtop_search&spm_id_from=333.1007&search_source=5",
|
|
|
"https://search.bilibili.com/video?keyword=test&from_source=webtop_search&spm_id_from=333.1007&search_source=5&page=2&o=30",
|
|
|
"https://search.bilibili.com/video?keyword=test&from_source=webtop_search&spm_id_from=333.1007&search_source=5&page=3&o=60"
|
|
|
]
|
|
|
self.assertEqual(get_web_url(keyword, pages), expected_urls)
|
|
|
|
|
|
def test_get_web_url_keyword_encoding(self):
|
|
|
# 4测试关键词包含特殊字符的情况
|
|
|
keyword = "test keyword"
|
|
|
pages = 1
|
|
|
expected_urls = [
|
|
|
"https://search.bilibili.com/video?keyword=test%20keyword&from_source=webtop_search&spm_id_from=333.1007&search_source=5"
|
|
|
]
|
|
|
self.assertEqual(get_web_url(keyword, pages), expected_urls)
|
|
|
|
|
|
def test_get_web_url_pages(self):
|
|
|
# 5测试关键词包含错误页数的情况
|
|
|
keyword = "abc"
|
|
|
pages = -1
|
|
|
expected_urls = []
|
|
|
self.assertEqual(get_web_url(keyword, pages), expected_urls)
|
|
|
|
|
|
class TestGetVideosUrl(unittest.TestCase):
|
|
|
@patch('requests.get')
|
|
|
def test_get_videos_url_success(self, mock_get):
|
|
|
# 准备模拟的响应
|
|
|
mock_response = requests.Response()
|
|
|
mock_response.status_code = 200
|
|
|
mock_response._content = b'<html><body><div class="video-list row"><div class=""><div><div><a href="/video1">Video 1</a></div></div></div></body></html>'
|
|
|
mock_response.encoding = 'utf-8'
|
|
|
mock_get.return_value = mock_response
|
|
|
# 调用函数
|
|
|
web_url = 'http://example.com'
|
|
|
videos_url = get_videos_url(web_url)
|
|
|
mock_get.assert_called_once_with(web_url, headers=headers, cookies=dic_cookies)
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
unittest.main() |