|
1 | | -#!/usr/bin/env python3 |
2 | | -""" |
3 | | -Integration tests for export functionality. |
| 1 | +"""Integration tests for SessionExporter (PySide6, small fixtures).""" |
4 | 2 |
|
5 | | -Tests the complete export workflow from UI to file generation. |
6 | | -""" |
| 3 | +from __future__ import annotations |
7 | 4 |
|
8 | | -import sys |
9 | | -import tempfile |
10 | | -import unittest |
11 | | -from pathlib import Path |
12 | 5 | from unittest.mock import patch |
13 | 6 |
|
14 | 7 | import pandas as pd |
| 8 | +import pytest |
15 | 9 |
|
16 | | -# Add src directory to path |
17 | | -sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src")) |
18 | | - |
19 | | -from PyQt5.QtWidgets import QApplication |
20 | | - |
21 | | -from dbs_annotator.controllers.wizard_controller import WizardController |
22 | 10 | from dbs_annotator.models.session_data import SessionData |
23 | 11 | from dbs_annotator.utils.session_exporter import SessionExporter |
24 | | -from dbs_annotator.views.step3_view import Step3View |
25 | | - |
26 | | - |
27 | | -class TestExportIntegration(unittest.TestCase): |
28 | | - """Test export functionality integration.""" |
29 | | - |
30 | | - def setUp(self): |
31 | | - """Set up test environment.""" |
32 | | - self.app = QApplication(sys.argv) |
33 | | - |
34 | | - # Create sample data |
35 | | - self.sample_data = [ |
36 | | - { |
37 | | - "date": "2024-01-15", |
38 | | - "time": "09:30:00", |
39 | | - "block_id": "1", |
40 | | - "scale_name": "YBOCS", |
41 | | - "scale_value": "20", |
42 | | - "stim_freq": "130", |
43 | | - "left_contact": "e1-e3", |
44 | | - "left_amplitude": "3.5", |
45 | | - "left_pulse_width": "60", |
46 | | - "right_contact": "e2-e4", |
47 | | - "right_amplitude": "4.0", |
48 | | - "right_pulse_width": "60", |
49 | | - "notes": "Baseline measurement", |
50 | | - }, |
51 | | - { |
52 | | - "date": "2024-01-15", |
53 | | - "time": "10:30:00", |
54 | | - "block_id": "2", |
55 | | - "scale_name": "YBOCS", |
56 | | - "scale_value": "18", |
57 | | - "stim_freq": "130", |
58 | | - "left_contact": "e1-e3", |
59 | | - "left_amplitude": "4.0", |
60 | | - "left_pulse_width": "60", |
61 | | - "right_contact": "e2-e4", |
62 | | - "right_amplitude": "4.5", |
63 | | - "right_pulse_width": "60", |
64 | | - "notes": "After stimulation", |
65 | | - }, |
66 | | - ] |
67 | | - |
68 | | - # Create temporary TSV file |
69 | | - self.temp_file = tempfile.NamedTemporaryFile( |
70 | | - mode="w", suffix=".tsv", delete=False |
71 | | - ) |
72 | | - df = pd.DataFrame(self.sample_data) |
73 | | - df.to_csv(self.temp_file.name, sep="\t", index=False) |
74 | | - self.temp_file.close() |
75 | | - |
76 | | - # Create session data with temporary file |
77 | | - self.session_data = SessionData() |
78 | | - self.session_data.file_path = self.temp_file.name |
79 | | - |
80 | | - # Create exporter |
81 | | - self.exporter = SessionExporter(self.session_data) |
82 | | - |
83 | | - def tearDown(self): |
84 | | - """Clean up test environment.""" |
85 | | - Path(self.temp_file.name).unlink(missing_ok=True) |
86 | | - |
87 | | - @patch("PyQt5.QtWidgets.QFileDialog.getSaveFileName") |
88 | | - @patch("PyQt5.QtWidgets.QMessageBox.information") |
89 | | - def test_excel_export_integration(self, mock_msgbox, mock_file_dialog): |
90 | | - """Test Excel export integration.""" |
91 | | - with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as excel_file: |
92 | | - excel_path = excel_file.name |
93 | | - |
94 | | - mock_file_dialog.return_value = (excel_path, "Excel Files (*.xlsx)") |
95 | | - |
96 | | - result = self.exporter.export_to_excel() |
97 | | - |
98 | | - self.assertTrue(result) |
99 | | - self.assertTrue(Path(excel_path).exists()) |
100 | | - mock_msgbox.assert_called_once() |
101 | | - |
102 | | - @patch("PyQt5.QtWidgets.QFileDialog.getSaveFileName") |
103 | | - @patch("PyQt5.QtWidgets.QMessageBox.information") |
104 | | - def test_word_export_integration(self, mock_msgbox, mock_file_dialog): |
105 | | - """Test Word export integration.""" |
106 | | - with tempfile.NamedTemporaryFile(suffix=".docx", delete=False) as word_file: |
107 | | - word_path = word_file.name |
108 | | - |
109 | | - mock_file_dialog.return_value = (word_path, "Word Files (*.docx)") |
110 | | - |
111 | | - result = self.exporter.export_to_word() |
112 | | - |
113 | | - self.assertTrue(result) |
114 | | - self.assertTrue(Path(word_path).exists()) |
115 | | - mock_msgbox.assert_called_once() |
116 | | - |
117 | | - @patch("PyQt5.QtWidgets.QFileDialog.getSaveFileName") |
118 | | - @patch("PyQt5.QtWidgets.QMessageBox.information") |
119 | | - def test_pdf_export_integration(self, mock_msgbox, mock_file_dialog): |
120 | | - """Test PDF export integration.""" |
121 | | - with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as pdf_file: |
122 | | - pdf_path = pdf_file.name |
123 | | - |
124 | | - mock_file_dialog.return_value = (pdf_path, "PDF Files (*.pdf)") |
125 | | - |
126 | | - result = self.exporter.export_to_pdf() |
127 | | - |
128 | | - self.assertTrue(result) |
129 | | - self.assertTrue(Path(pdf_path).exists()) |
130 | | - mock_msgbox.assert_called_once() |
131 | | - |
132 | | - def test_export_with_no_data(self): |
133 | | - """Test export behavior with no data.""" |
134 | | - # Create empty session data |
135 | | - empty_session = SessionData() |
136 | | - empty_exporter = SessionExporter(empty_session) |
137 | | - |
138 | | - with patch("PyQt5.QtWidgets.QMessageBox.warning") as mock_warning: |
139 | | - result = empty_exporter.export_to_excel() |
140 | | - self.assertFalse(result) |
141 | | - mock_warning.assert_called_once() |
142 | | - |
143 | | - def test_export_with_no_file_open(self): |
144 | | - """Test export behavior with no file open.""" |
145 | | - # Create session data without file |
146 | | - no_file_session = SessionData() |
147 | | - no_file_session.file_path = None |
148 | | - no_file_exporter = SessionExporter(no_file_session) |
149 | | - |
150 | | - with patch("PyQt5.QtWidgets.QMessageBox.warning") as mock_warning: |
151 | | - result = no_file_exporter.export_to_excel() |
152 | | - self.assertFalse(result) |
153 | | - mock_warning.assert_called_once() |
154 | | - |
155 | | - |
156 | | -class TestExportUIIntegration(unittest.TestCase): |
157 | | - """Test export UI integration.""" |
158 | | - |
159 | | - def setUp(self): |
160 | | - """Set up UI test environment.""" |
161 | | - self.app = QApplication(sys.argv) |
162 | | - self.controller = WizardController() |
163 | | - self.step3_view = Step3View() |
164 | | - |
165 | | - def test_export_menu_creation(self): |
166 | | - """Test export menu is created correctly.""" |
167 | | - self.assertIsNotNone(self.step3_view.export_button) |
168 | | - self.assertIsNotNone(self.step3_view.export_menu) |
169 | | - self.assertEqual(len(self.step3_view.export_menu.actions()), 3) |
170 | | - |
171 | | - def test_export_actions_exist(self): |
172 | | - """Test all export actions exist.""" |
173 | | - self.assertIsNotNone(self.step3_view.export_excel_action) |
174 | | - self.assertIsNotNone(self.step3_view.export_word_action) |
175 | | - self.assertIsNotNone(self.step3_view.export_pdf_action) |
176 | | - |
177 | 12 |
|
178 | | -if __name__ == "__main__": |
179 | | - unittest.main() |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def sample_tsv(tmp_path): |
| 16 | + rows = [ |
| 17 | + { |
| 18 | + "date": "2024-01-15", |
| 19 | + "time": "09:30:00", |
| 20 | + "block_id": "1", |
| 21 | + "is_initial": "1", |
| 22 | + "scale_name": "YBOCS", |
| 23 | + "scale_value": "20", |
| 24 | + "notes": "n", |
| 25 | + }, |
| 26 | + { |
| 27 | + "date": "2024-01-15", |
| 28 | + "time": "10:30:00", |
| 29 | + "block_id": "2", |
| 30 | + "is_initial": "0", |
| 31 | + "scale_name": "YBOCS", |
| 32 | + "scale_value": "18", |
| 33 | + "notes": "n2", |
| 34 | + }, |
| 35 | + ] |
| 36 | + p = tmp_path / "sub-01_task-prog_run-01_events.tsv" |
| 37 | + pd.DataFrame(rows).to_csv(p, sep="\t", index=False) |
| 38 | + return p |
| 39 | + |
| 40 | + |
| 41 | +def test_export_to_word_integration(monkeypatch, tmp_path, sample_tsv): |
| 42 | + sd = SessionData() |
| 43 | + sd.open_file_append(str(sample_tsv)) |
| 44 | + try: |
| 45 | + exporter = SessionExporter(sd) |
| 46 | + out = tmp_path / "report.docx" |
| 47 | + monkeypatch.setattr(exporter, "_export_to_word_path", lambda *a, **k: True) |
| 48 | + with ( |
| 49 | + patch( |
| 50 | + "PySide6.QtWidgets.QFileDialog.getSaveFileName", |
| 51 | + return_value=(str(out), "Word"), |
| 52 | + ), |
| 53 | + patch( |
| 54 | + "dbs_annotator.utils.session_exporter.SessionExporter._show_transient_message", |
| 55 | + ), |
| 56 | + ): |
| 57 | + assert exporter.export_to_word() is True |
| 58 | + finally: |
| 59 | + sd.close_file() |
| 60 | + |
| 61 | + |
| 62 | +def test_export_to_pdf_integration(monkeypatch, tmp_path, sample_tsv): |
| 63 | + sd = SessionData() |
| 64 | + sd.open_file_append(str(sample_tsv)) |
| 65 | + try: |
| 66 | + exporter = SessionExporter(sd) |
| 67 | + pdf = tmp_path / "report.pdf" |
| 68 | + monkeypatch.setattr(exporter, "_export_to_word_path", lambda *a, **k: True) |
| 69 | + monkeypatch.setattr(exporter, "_convert_docx_to_pdf", lambda *a, **k: None) |
| 70 | + monkeypatch.setattr(exporter, "_open_file", lambda *a, **k: None) |
| 71 | + with ( |
| 72 | + patch( |
| 73 | + "PySide6.QtWidgets.QFileDialog.getSaveFileName", |
| 74 | + return_value=(str(pdf), "PDF"), |
| 75 | + ), |
| 76 | + patch( |
| 77 | + "dbs_annotator.utils.session_exporter.SessionExporter._show_transient_message", |
| 78 | + ), |
| 79 | + ): |
| 80 | + assert exporter.export_to_pdf() is True |
| 81 | + finally: |
| 82 | + sd.close_file() |
| 83 | + |
| 84 | + |
| 85 | +def test_export_word_no_data_warns(): |
| 86 | + empty = SessionData() |
| 87 | + ex = SessionExporter(empty) |
| 88 | + with patch("dbs_annotator.utils.session_exporter.QMessageBox.warning") as w: |
| 89 | + assert ex.export_to_word() is False |
| 90 | + w.assert_called() |
| 91 | + |
| 92 | + |
| 93 | +def test_export_word_no_file_warns(): |
| 94 | + sd = SessionData() |
| 95 | + sd.file_path = None |
| 96 | + ex = SessionExporter(sd) |
| 97 | + with patch("dbs_annotator.utils.session_exporter.QMessageBox.warning") as w: |
| 98 | + assert ex.export_to_word() is False |
| 99 | + w.assert_called() |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.gui |
| 103 | +def test_step3_export_actions_exist(qtbot, qapp): |
| 104 | + from dbs_annotator.views.step3_view import Step3View |
| 105 | + |
| 106 | + v = Step3View() |
| 107 | + qtbot.addWidget(v) |
| 108 | + assert v.export_word_action is not None |
| 109 | + assert v.export_pdf_action is not None |
| 110 | + assert len(v.export_menu.actions()) == 2 |
0 commit comments