| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- """
- Tests for FileSelector UI component.
- """
- import pytest
- # Skip tests if PyQt6 is not installed
- pytest.importorskip("PyQt6")
- from pathlib import Path
- from datetime import datetime
- from PyQt6.QtWidgets import QApplication
- from PyQt6.QtCore import Qt, QFileSystemWatcher
- from PyQt6.QtTest import QTest
- from src.ui.file_selector import FileSelector
- @pytest.fixture
- def app(qtbot):
- """Create QApplication fixture."""
- test_app = QApplication.instance()
- if test_app is None:
- test_app = QApplication([])
- yield test_app
- @pytest.fixture
- def file_selector(app, qtbot, tmp_path):
- """Create FileSelector fixture with temp directory."""
- selector = FileSelector()
- qtbot.addWidget(selector)
- yield selector, tmp_path
- selector.close()
- class TestFileSelector:
- """Test FileSelector functionality."""
- def test_initialization(self, file_selector):
- """Test file selector initializes correctly."""
- selector, _ = file_selector
- assert selector.current_path is None
- assert not selector.is_valid
- assert selector._path_display.placeholderText() == selector.DEFAULT_PLACEHOLDER
- def test_no_file_selected_initially(self, file_selector):
- """Test no file is selected initially."""
- selector, _ = file_selector
- assert selector.current_path is None
- assert not selector.is_valid
- assert selector._name_value.text() == "-"
- assert selector._size_value.text() == "-"
- assert selector._lines_value.text() == "-"
- def test_set_valid_file(self, file_selector):
- """Test setting a valid file."""
- selector, tmp_path = file_selector
- # Create a test file
- test_file = tmp_path / "test.txt"
- test_file.write_text("Line 1\nLine 2\nLine 3\n", encoding='utf-8')
- # Set the file
- selector.set_file(test_file)
- assert selector.current_path == test_file
- assert selector.is_valid
- assert selector._name_value.text() == "test.txt"
- def test_file_info_display(self, file_selector):
- """Test file information is displayed correctly."""
- selector, tmp_path = file_selector
- # Create a test file with known content
- content = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n"
- test_file = tmp_path / "test.txt"
- test_file.write_text(content, encoding='utf-8')
- selector.set_file(test_file)
- # Check name
- assert selector._name_value.text() == "test.txt"
- # Check size (should be non-zero)
- size_text = selector._size_value.text()
- assert "B" in size_text
- # Check line count
- lines_text = selector._lines_value.text()
- assert "5" in lines_text or "5," in lines_text # 5 lines
- def test_clear_selection(self, file_selector):
- """Test clearing file selection."""
- selector, tmp_path = file_selector
- # Create and set a file
- test_file = tmp_path / "test.txt"
- test_file.write_text("Content\n", encoding='utf-8')
- selector.set_file(test_file)
- assert selector.is_valid
- # Clear the selection
- selector.clear()
- assert selector.current_path is None
- assert not selector.is_valid
- assert selector._name_value.text() == "-"
- def test_file_selected_signal(self, file_selector, qtbot):
- """Test file_selected signal is emitted."""
- selector, tmp_path = file_selector
- signal_received = []
- def on_file_selected(path):
- signal_received.append(path)
- selector.file_selected.connect(on_file_selected)
- # Create and set a file
- test_file = tmp_path / "test.txt"
- test_file.write_text("Content\n", encoding='utf-8')
- selector.set_file(test_file)
- assert len(signal_received) == 1
- assert signal_received[0] == test_file
- def test_selection_cleared_signal(self, file_selector):
- """Test selection_cleared signal is emitted."""
- selector, tmp_path = file_selector
- signal_received = []
- def on_cleared():
- signal_received.append(True)
- selector.selection_cleared.connect(on_cleared)
- # Create and set a file
- test_file = tmp_path / "test.txt"
- test_file.write_text("Content\n", encoding='utf-8')
- selector.set_file(test_file)
- # Clear the selection
- selector.clear()
- assert len(signal_received) == 1
- def test_size_formatting(self):
- """Test file size formatting."""
- selector = FileSelector()
- # Test different size units
- assert "B" in selector._format_size(512)
- assert "KB" in selector._format_size(2048)
- assert "MB" in selector._format_size(1024 * 1024 * 5)
- assert "GB" in selector._format_size(1024 * 1024 * 1024 * 2)
- def test_line_count_property(self, file_selector):
- """Test line_count property."""
- selector, tmp_path = file_selector
- # No file selected
- assert selector.line_count is None
- # Create a file with 10 lines
- test_file = tmp_path / "test.txt"
- test_file.write_text("\n".join([f"Line {i}" for i in range(10)]), encoding='utf-8')
- selector.set_file(test_file)
- assert selector.line_count == 10
- def test_non_existent_file(self, file_selector):
- """Test handling of non-existent file."""
- selector, tmp_path = file_selector
- # Set a non-existent path
- non_existent = tmp_path / "does_not_exist.txt"
- selector.set_file(non_existent)
- # Should handle gracefully
- assert selector.current_path == non_existent
- assert not selector.is_valid
- def test_empty_file(self, file_selector):
- """Test handling of empty file."""
- selector, tmp_path = file_selector
- # Create an empty file
- test_file = tmp_path / "empty.txt"
- test_file.write_text("", encoding='utf-8')
- selector.set_file(test_file)
- assert selector.is_valid
- assert selector.line_count == 0
- def test_chinese_text_file(self, file_selector):
- """Test handling of Chinese text file."""
- selector, tmp_path = file_selector
- # Create a file with Chinese text
- test_file = tmp_path / "chinese.txt"
- test_file.write_text("第一章:开始\n第二章:发展\n第三章:结束\n", encoding='utf-8')
- selector.set_file(test_file)
- assert selector.is_valid
- assert selector.line_count == 3
- class TestFileSelectorUI:
- """Test FileSelector UI elements."""
- def test_browse_button_exists(self, file_selector):
- """Test browse button exists."""
- selector, _ = file_selector
- assert selector._browse_btn is not None
- assert selector._browse_btn.text() == "Browse..."
- def test_clear_button_initially_disabled(self, file_selector):
- """Test clear button is initially disabled."""
- selector, _ = file_selector
- assert not selector._clear_btn.isEnabled()
- def test_clear_button_enabled_with_file(self, file_selector):
- """Test clear button is enabled when file is selected."""
- selector, tmp_path = file_selector
- test_file = tmp_path / "test.txt"
- test_file.write_text("Content\n", encoding='utf-8')
- selector.set_file(test_file)
- assert selector._clear_btn.isEnabled()
- def test_groups_exist(self, file_selector):
- """Test UI groups are created."""
- selector, _ = file_selector
- # Check for groups (they should exist)
- assert selector.layout() is not None
- assert selector.layout().count() > 0
|