diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..adfb4ad --- /dev/null +++ b/test/README.md @@ -0,0 +1,39 @@ +# MoneyPrinterTurbo Test Directory + +This directory contains unit tests for the **MoneyPrinterTurbo** project. + +## Directory Structure + +- `services/`: Tests for components in the `app/services` directory + - `test_video.py`: Tests for the video service + - `test_task.py`: Tests for the task service + +## Running Tests + +You can run the tests using Python’s built-in `unittest` framework: + +```bash +# Run all tests +python -m unittest discover -s test + +# Run a specific test file +python -m unittest test/services/test_video.py + +# Run a specific test class +python -m unittest test.services.test_video.TestVideoService + +# Run a specific test method +python -m unittest test.services.test_video.TestVideoService.test_preprocess_video +```` + +## Adding New Tests + +To add tests for other components, follow these guidelines: + +1. Create test files prefixed with `test_` in the appropriate subdirectory +2. Use `unittest.TestCase` as the base class for your test classes +3. Name test methods with the `test_` prefix + +## Test Resources + +Place any resource files required for testing in the `test/resources` directory. \ No newline at end of file diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..584e3c9 --- /dev/null +++ b/test/__init__.py @@ -0,0 +1 @@ +# Unit test package for test diff --git a/test/resources/1.png b/test/resources/1.png new file mode 100644 index 0000000..8c62fb6 Binary files /dev/null and b/test/resources/1.png differ diff --git a/test/resources/1.png.mp4 b/test/resources/1.png.mp4 new file mode 100644 index 0000000..05e351b Binary files /dev/null and b/test/resources/1.png.mp4 differ diff --git a/test/resources/2.png b/test/resources/2.png new file mode 100644 index 0000000..2d11adc Binary files /dev/null and b/test/resources/2.png differ diff --git a/test/resources/2.png.mp4 b/test/resources/2.png.mp4 new file mode 100644 index 0000000..6e622c7 Binary files /dev/null and b/test/resources/2.png.mp4 differ diff --git a/test/resources/3.png b/test/resources/3.png new file mode 100644 index 0000000..924dcee Binary files /dev/null and b/test/resources/3.png differ diff --git a/test/resources/3.png.mp4 b/test/resources/3.png.mp4 new file mode 100644 index 0000000..9f06ed3 Binary files /dev/null and b/test/resources/3.png.mp4 differ diff --git a/test/resources/4.png b/test/resources/4.png new file mode 100644 index 0000000..9d6516d Binary files /dev/null and b/test/resources/4.png differ diff --git a/test/resources/5.png b/test/resources/5.png new file mode 100644 index 0000000..fdd5925 Binary files /dev/null and b/test/resources/5.png differ diff --git a/test/resources/6.png b/test/resources/6.png new file mode 100644 index 0000000..94c8987 Binary files /dev/null and b/test/resources/6.png differ diff --git a/test/resources/7.png b/test/resources/7.png new file mode 100644 index 0000000..54bd1ce Binary files /dev/null and b/test/resources/7.png differ diff --git a/test/resources/8.png b/test/resources/8.png new file mode 100644 index 0000000..a9cf7ed Binary files /dev/null and b/test/resources/8.png differ diff --git a/test/resources/9.png b/test/resources/9.png new file mode 100644 index 0000000..c39d181 Binary files /dev/null and b/test/resources/9.png differ diff --git a/test/services/__init__.py b/test/services/__init__.py new file mode 100644 index 0000000..a07aa62 --- /dev/null +++ b/test/services/__init__.py @@ -0,0 +1 @@ +# Unit test package for services \ No newline at end of file diff --git a/test/services/test_task.py b/test/services/test_task.py new file mode 100644 index 0000000..8b241a9 --- /dev/null +++ b/test/services/test_task.py @@ -0,0 +1,66 @@ +import unittest +import os +import sys +from pathlib import Path + +# add project root to python path +sys.path.insert(0, str(Path(__file__).parent.parent.parent)) + +from app.services import task as tm +from app.models.schema import MaterialInfo, VideoParams + +resources_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "resources") + +class TestTaskService(unittest.TestCase): + def setUp(self): + pass + + def tearDown(self): + pass + + def test_task_local_materials(self): + task_id = "00000000-0000-0000-0000-000000000000" + video_materials=[] + for i in range(1, 4): + video_materials.append(MaterialInfo( + provider="local", + url=os.path.join(resources_dir, f"{i}.png"), + duration=0 + )) + + params = VideoParams( + video_subject="金钱的作用", + video_script="金钱不仅是交换媒介,更是社会资源的分配工具。它能满足基本生存需求,如食物和住房,也能提供教育、医疗等提升生活品质的机会。拥有足够的金钱意味着更多选择权,比如职业自由或创业可能。但金钱的作用也有边界,它无法直接购买幸福、健康或真诚的人际关系。过度追逐财富可能导致价值观扭曲,忽视精神层面的需求。理想的状态是理性看待金钱,将其作为实现目标的工具而非终极目的。", + video_terms="money importance, wealth and society, financial freedom, money and happiness, role of money", + video_aspect="9:16", + video_concat_mode="random", + video_transition_mode="None", + video_clip_duration=3, + video_count=1, + video_source="local", + video_materials=video_materials, + video_language="", + voice_name="zh-CN-XiaoxiaoNeural-Female", + voice_volume=1.0, + voice_rate=1.0, + bgm_type="random", + bgm_file="", + bgm_volume=0.2, + subtitle_enabled=True, + subtitle_position="bottom", + custom_position=70.0, + font_name="MicrosoftYaHeiBold.ttc", + text_fore_color="#FFFFFF", + text_background_color=True, + font_size=60, + stroke_color="#000000", + stroke_width=1.5, + n_threads=2, + paragraph_number=1 + ) + result = tm.start(task_id=task_id, params=params) + print(result) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/test/services/test_video.py b/test/services/test_video.py new file mode 100644 index 0000000..d204acc --- /dev/null +++ b/test/services/test_video.py @@ -0,0 +1,85 @@ + +import unittest +import os +import sys +from pathlib import Path +from moviepy import ( + VideoFileClip, +) +# add project root to python path +sys.path.insert(0, str(Path(__file__).parent.parent.parent)) +from app.models.schema import MaterialInfo +from app.services import video as vd +from app.utils import utils + +resources_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "resources") + +class TestVideoService(unittest.TestCase): + def setUp(self): + self.test_img_path = os.path.join(resources_dir, "1.png") + + def tearDown(self): + pass + + def test_preprocess_video(self): + if not os.path.exists(self.test_img_path): + self.fail(f"test image not found: {self.test_img_path}") + + # test preprocess_video function + m = MaterialInfo() + m.url = self.test_img_path + m.provider = "local" + print(m) + + materials = vd.preprocess_video([m], clip_duration=4) + print(materials) + + # verify result + self.assertIsNotNone(materials) + self.assertEqual(len(materials), 1) + self.assertTrue(materials[0].url.endswith(".mp4")) + + # moviepy get video info + clip = VideoFileClip(materials[0].url) + print(clip) + + # clean generated test video file + if os.path.exists(materials[0].url): + os.remove(materials[0].url) + + def test_wrap_text(self): + """test text wrapping function""" + try: + font_path = os.path.join(utils.font_dir(), "STHeitiMedium.ttc") + if not os.path.exists(font_path): + self.fail(f"font file not found: {font_path}") + + # test english text wrapping + test_text_en = "This is a test text for wrapping long sentences in english language" + + wrapped_text_en, text_height_en = vd.wrap_text( + text=test_text_en, + max_width=300, + font=font_path, + fontsize=30 + ) + print(wrapped_text_en, text_height_en) + # verify text is wrapped + self.assertIn("\n", wrapped_text_en) + + # test chinese text wrapping + test_text_zh = "这是一段用来测试中文长句换行的文本内容,应该会根据宽度限制进行换行处理" + wrapped_text_zh, text_height_zh = vd.wrap_text( + text=test_text_zh, + max_width=300, + font=font_path, + fontsize=30 + ) + print(wrapped_text_zh, text_height_zh) + # verify chinese text is wrapped + self.assertIn("\n", wrapped_text_zh) + except Exception as e: + self.fail(f"test wrap_text failed: {str(e)}") + +if __name__ == "__main__": + unittest.main() \ No newline at end of file