dxfedit/03_Python_OpenSource_DXF/tests/test_dxf_processor.py
2025-09-09 18:42:30 +08:00

88 lines
2.9 KiB
Python

"""Tests for DXF processor module"""
import pytest
import tempfile
import os
from pathlib import Path
from unittest.mock import Mock, patch
from src.cad_automation.dxf_processor import DXFProcessor
from src.cad_automation.exceptions import DXFProcessingError, FileNotFoundError
class TestDXFProcessor:
"""Test cases for DXFProcessor class"""
def test_init_with_valid_file(self):
"""Test initialization with valid DXF file"""
# This would need a test DXF file
pass
def test_init_with_nonexistent_file(self):
"""Test initialization with nonexistent file"""
with pytest.raises(FileNotFoundError):
DXFProcessor("nonexistent.dxf")
@patch('ezdxf.readfile')
def test_load_file_success(self, mock_readfile):
"""Test successful file loading"""
mock_doc = Mock()
mock_msp = Mock()
mock_doc.modelspace.return_value = mock_msp
mock_readfile.return_value = mock_doc
with patch('pathlib.Path.exists', return_value=True):
processor = DXFProcessor("test.dxf")
processor.load_file()
assert processor.doc == mock_doc
assert processor.msp == mock_msp
mock_readfile.assert_called_once()
@patch('ezdxf.readfile')
def test_load_file_dxf_error(self, mock_readfile):
"""Test file loading with DXF error"""
mock_readfile.side_effect = Exception("DXF error")
with patch('pathlib.Path.exists', return_value=True):
processor = DXFProcessor("test.dxf")
with pytest.raises(DXFProcessingError):
processor.load_file()
def test_query_entities_without_msp(self):
"""Test querying entities without loaded modelspace"""
processor = DXFProcessor("test.dxf")
with pytest.raises(DXFProcessingError):
processor.query_entities("LINE")
def test_add_line_without_msp(self):
"""Test adding line without loaded modelspace"""
processor = DXFProcessor("test.dxf")
with pytest.raises(DXFProcessingError):
processor.add_line((0, 0), (10, 10))
def test_add_text_without_msp(self):
"""Test adding text without loaded modelspace"""
processor = DXFProcessor("test.dxf")
with pytest.raises(DXFProcessingError):
processor.add_text("test", (0, 0))
@patch('pathlib.Path.mkdir')
def test_save_file_success(self, mock_mkdir):
"""Test successful file saving"""
mock_doc = Mock()
processor = DXFProcessor("test.dxf")
processor.doc = mock_doc
with patch('builtins.open', create=True):
processor.save_file("output.dxf")
mock_doc.saveas.assert_called_once_with("output.dxf")
def test_save_file_without_doc(self):
"""Test saving file without loaded document"""
processor = DXFProcessor("test.dxf")
with pytest.raises(DXFProcessingError):
processor.save_file("output.dxf")