273 lines
11 KiB
Python
273 lines
11 KiB
Python
"""
|
|
Tests for MD processor module
|
|
"""
|
|
|
|
import unittest
|
|
import tempfile
|
|
import os
|
|
from md_processor import MDProcessor, MDTranslationItem
|
|
|
|
|
|
class TestMDProcessor(unittest.TestCase):
|
|
"""Test cases for MDProcessor class"""
|
|
|
|
def setUp(self):
|
|
"""Set up test fixtures"""
|
|
self.processor = MDProcessor()
|
|
|
|
def test_get_md_files(self):
|
|
"""Test getting .md files from a folder"""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
# Create test files
|
|
open(os.path.join(tmpdir, "file1.md"), 'w').close()
|
|
open(os.path.join(tmpdir, "file2.md"), 'w').close()
|
|
open(os.path.join(tmpdir, "not_md.txt"), 'w').close()
|
|
os.makedirs(os.path.join(tmpdir, "subdir"))
|
|
open(os.path.join(tmpdir, "subdir", "file3.md"), 'w').close()
|
|
|
|
# Get .md files (non-recursive)
|
|
files = self.processor.get_md_files(tmpdir)
|
|
|
|
self.assertEqual(len(files), 2)
|
|
self.assertIn("file1.md", files)
|
|
self.assertIn("file2.md", files)
|
|
self.assertNotIn("not_md.txt", files)
|
|
self.assertNotIn("file3.md", files) # In subdir, not included
|
|
|
|
def test_get_md_files_nonexistent_folder(self):
|
|
"""Test getting .md files from a non-existent folder"""
|
|
files = self.processor.get_md_files("/nonexistent/path")
|
|
self.assertEqual(files, [])
|
|
|
|
def test_load_md_file(self):
|
|
"""Test loading markdown file content"""
|
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f:
|
|
f.write("# Test Content\n\nThis is a test.")
|
|
temp_path = f.name
|
|
|
|
try:
|
|
content = self.processor.load_md_file(temp_path)
|
|
self.assertEqual(content, "# Test Content\n\nThis is a test.")
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
def test_load_md_file_nonexistent(self):
|
|
"""Test loading a non-existent markdown file"""
|
|
content = self.processor.load_md_file("/nonexistent/file.md")
|
|
self.assertEqual(content, "")
|
|
|
|
def test_save_md_file(self):
|
|
"""Test saving markdown file"""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
file_path = os.path.join(tmpdir, "test.md")
|
|
content = "# Test Content\n\nThis is a test."
|
|
|
|
self.processor.save_md_file(content, file_path)
|
|
|
|
# Verify file was created
|
|
self.assertTrue(os.path.exists(file_path))
|
|
|
|
# Verify content
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
saved_content = f.read()
|
|
self.assertEqual(saved_content, content)
|
|
|
|
def test_save_md_file_creates_directories(self):
|
|
"""Test that save_md_file creates parent directories"""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
nested_path = os.path.join(tmpdir, "subdir1", "subdir2", "test.md")
|
|
content = "# Nested Content"
|
|
|
|
self.processor.save_md_file(content, nested_path)
|
|
|
|
# Verify file was created in nested directory
|
|
self.assertTrue(os.path.exists(nested_path))
|
|
|
|
def test_extract_content(self):
|
|
"""Test extracting content from all .md files"""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
# Create test files
|
|
with open(os.path.join(tmpdir, "file1.md"), 'w') as f:
|
|
f.write("# File 1")
|
|
with open(os.path.join(tmpdir, "file2.md"), 'w') as f:
|
|
f.write("# File 2")
|
|
|
|
items = self.processor.extract_content(tmpdir)
|
|
|
|
self.assertEqual(len(items), 2)
|
|
self.assertIn("file1.md", items)
|
|
self.assertIn("file2.md", items)
|
|
self.assertEqual(items["file1.md"].content, "# File 1")
|
|
self.assertEqual(items["file2.md"].content, "# File 2")
|
|
self.assertEqual(items["file1.md"].filename, "file1.md")
|
|
|
|
def test_extract_content_empty_folder(self):
|
|
"""Test extracting content from empty folder"""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
items = self.processor.extract_content(tmpdir)
|
|
self.assertEqual(items, {})
|
|
|
|
def test_check_asset_counts_valid(self):
|
|
"""Test asset count check when all folders have same count"""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
# Create base folder with files
|
|
base_folder = os.path.join(tmpdir, "base")
|
|
os.makedirs(base_folder)
|
|
open(os.path.join(base_folder, "file1.md"), 'w').close()
|
|
open(os.path.join(base_folder, "file2.md"), 'w').close()
|
|
|
|
# Create target folders with same files
|
|
target1 = os.path.join(tmpdir, "target1")
|
|
os.makedirs(target1)
|
|
open(os.path.join(target1, "file1.md"), 'w').close()
|
|
open(os.path.join(target1, "file2.md"), 'w').close()
|
|
|
|
target2 = os.path.join(tmpdir, "target2")
|
|
os.makedirs(target2)
|
|
open(os.path.join(target2, "file1.md"), 'w').close()
|
|
open(os.path.join(target2, "file2.md"), 'w').close()
|
|
|
|
is_valid, errors = self.processor.check_asset_counts(base_folder, [target1, target2])
|
|
|
|
self.assertTrue(is_valid)
|
|
self.assertEqual(errors, [])
|
|
|
|
def test_check_asset_counts_missing_files(self):
|
|
"""Test asset count check when target is missing files"""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
# Create base folder with files
|
|
base_folder = os.path.join(tmpdir, "base")
|
|
os.makedirs(base_folder)
|
|
open(os.path.join(base_folder, "file1.md"), 'w').close()
|
|
open(os.path.join(base_folder, "file2.md"), 'w').close()
|
|
|
|
# Create target folder with missing file
|
|
target1 = os.path.join(tmpdir, "target1")
|
|
os.makedirs(target1)
|
|
open(os.path.join(target1, "file1.md"), 'w').close()
|
|
# file2.md is missing
|
|
|
|
is_valid, errors = self.processor.check_asset_counts(base_folder, [target1])
|
|
|
|
self.assertFalse(is_valid)
|
|
self.assertEqual(len(errors), 1)
|
|
self.assertIn("file2.md", errors[0])
|
|
|
|
def test_check_asset_counts_extra_files(self):
|
|
"""Test asset count check when target has extra files"""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
# Create base folder with files
|
|
base_folder = os.path.join(tmpdir, "base")
|
|
os.makedirs(base_folder)
|
|
open(os.path.join(base_folder, "file1.md"), 'w').close()
|
|
|
|
# Create target folder with extra file
|
|
target1 = os.path.join(tmpdir, "target1")
|
|
os.makedirs(target1)
|
|
open(os.path.join(target1, "file1.md"), 'w').close()
|
|
open(os.path.join(target1, "extra.md"), 'w').close()
|
|
|
|
is_valid, errors = self.processor.check_asset_counts(base_folder, [target1])
|
|
|
|
self.assertFalse(is_valid)
|
|
self.assertEqual(len(errors), 1)
|
|
self.assertIn("extra.md", errors[0])
|
|
|
|
def test_check_asset_counts_empty_base(self):
|
|
"""Test asset count check with empty base folder"""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
base_folder = os.path.join(tmpdir, "base")
|
|
os.makedirs(base_folder)
|
|
|
|
target1 = os.path.join(tmpdir, "target1")
|
|
os.makedirs(target1)
|
|
|
|
is_valid, errors = self.processor.check_asset_counts(base_folder, [target1])
|
|
|
|
self.assertFalse(is_valid)
|
|
self.assertEqual(len(errors), 1)
|
|
self.assertIn("no .md files", errors[0])
|
|
|
|
def test_find_missing_files(self):
|
|
"""Test finding missing files in target folder"""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
# Create base folder with files
|
|
base_folder = os.path.join(tmpdir, "base")
|
|
os.makedirs(base_folder)
|
|
open(os.path.join(base_folder, "file1.md"), 'w').close()
|
|
open(os.path.join(base_folder, "file2.md"), 'w').close()
|
|
open(os.path.join(base_folder, "file3.md"), 'w').close()
|
|
|
|
# Create target folder with some files
|
|
target_folder = os.path.join(tmpdir, "target")
|
|
os.makedirs(target_folder)
|
|
open(os.path.join(target_folder, "file1.md"), 'w').close()
|
|
# file2.md and file3.md are missing
|
|
|
|
missing = self.processor.find_missing_files(base_folder, target_folder)
|
|
|
|
self.assertEqual(len(missing), 2)
|
|
self.assertIn("file2.md", missing)
|
|
self.assertIn("file3.md", missing)
|
|
self.assertNotIn("file1.md", missing)
|
|
|
|
def test_find_missing_files_all_present(self):
|
|
"""Test finding missing files when all are present"""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
base_folder = os.path.join(tmpdir, "base")
|
|
os.makedirs(base_folder)
|
|
open(os.path.join(base_folder, "file1.md"), 'w').close()
|
|
|
|
target_folder = os.path.join(tmpdir, "target")
|
|
os.makedirs(target_folder)
|
|
open(os.path.join(target_folder, "file1.md"), 'w').close()
|
|
|
|
missing = self.processor.find_missing_files(base_folder, target_folder)
|
|
|
|
self.assertEqual(missing, [])
|
|
|
|
def test_custom_extension(self):
|
|
"""Test MDProcessor with custom file extension"""
|
|
processor = MDProcessor(file_extension=".txt")
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
open(os.path.join(tmpdir, "file1.txt"), 'w').close()
|
|
open(os.path.join(tmpdir, "file2.md"), 'w').close()
|
|
|
|
files = processor.get_md_files(tmpdir)
|
|
|
|
self.assertEqual(len(files), 1)
|
|
self.assertIn("file1.txt", files)
|
|
self.assertNotIn("file2.md", files)
|
|
|
|
|
|
class TestMDTranslationItem(unittest.TestCase):
|
|
"""Test cases for MDTranslationItem dataclass"""
|
|
|
|
def test_create_item(self):
|
|
"""Test creating MDTranslationItem"""
|
|
item = MDTranslationItem(
|
|
filename="test.md",
|
|
content="# Test",
|
|
relative_path="subdir"
|
|
)
|
|
|
|
self.assertEqual(item.filename, "test.md")
|
|
self.assertEqual(item.content, "# Test")
|
|
self.assertEqual(item.relative_path, "subdir")
|
|
|
|
def test_create_item_defaults(self):
|
|
"""Test creating MDTranslationItem with defaults"""
|
|
item = MDTranslationItem(
|
|
filename="test.md",
|
|
content="# Test"
|
|
)
|
|
|
|
self.assertEqual(item.filename, "test.md")
|
|
self.assertEqual(item.content, "# Test")
|
|
self.assertEqual(item.relative_path, "")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|