import csv, json, sqlite3 import pytest from tests.qt_helpers import trigger_menu_action, accept_all_message_boxes # Export filters used by the app (format is chosen by this name filter, not by extension) EXPORT_FILTERS = { ".txt": "Text (*.txt)", ".json": "JSON (*.json)", ".csv": "CSV (*.csv)", ".html": "HTML (*.html)", ".sql": "SQL (*.sql)", # app writes a SQLite DB here } BACKUP_FILTER = "SQLCipher (*.db)" def _write_sample_entries(win, qtbot): win.editor.setPlainText("alpha bold") win._save_current(explicit=True) d = win.calendar.selectedDate().addDays(1) win.calendar.setSelectedDate(d) win.editor.setPlainText("beta text") win._save_current(explicit=True) @pytest.mark.parametrize( "ext,verifier", [ (".txt", lambda p: p.read_text(encoding="utf-8").strip()), (".json", lambda p: json.loads(p.read_text(encoding="utf-8"))), (".csv", lambda p: list(csv.reader(p.open("r", encoding="utf-8-sig")))), (".html", lambda p: p.read_text(encoding="utf-8")), (".sql", lambda p: p), ], ) def test_export_all_formats(open_window, qtbot, tmp_path, ext, verifier, monkeypatch): win = open_window _write_sample_entries(win, qtbot) out = tmp_path / f"export_test{ext}" # 1) Short-circuit the file dialog so it returns our path + the filter we want. from PySide6.QtWidgets import QFileDialog def fake_getSaveFileName(*args, **kwargs): return (str(out), EXPORT_FILTERS[ext]) monkeypatch.setattr( QFileDialog, "getSaveFileName", staticmethod(fake_getSaveFileName) ) # 2) Kick off the export trigger_menu_action(win, "Export") # 3) Click through the "unencrypted export" warning accept_all_message_boxes() # 4) Wait for the file to appear (export happens synchronously after the stub) qtbot.waitUntil(out.exists, timeout=5000) # 5) Dismiss the "Export complete" info box so it can't block later tests accept_all_message_boxes() # 6) Assert as before val = verifier(out) if ext == ".json": assert isinstance(val, list) and all( "date" in d and "content" in d for d in val ) elif ext == ".csv": flat = [cell for row in val for cell in row] assert any("alpha" in c for c in flat) and any("beta" in c for c in flat) elif ext == ".html": lower = val.lower() assert "