buzz/tests/conftest.py
2026-06-15 08:49:02 +03:00

124 lines
3.7 KiB
Python

import glob
import multiprocessing
import os
import platform
import random
import string
# Disable the GUI startup update check during tests. The check fires an async
# QNetworkAccessManager HTTPS request which, while in flight, interferes with
# multiprocessing spawn on Windows and crashes child transcription processes.
# Tests must also never depend on network availability.
os.environ.setdefault("BUZZ_DISABLE_UPDATE_CHECK", "1")
import pytest
# Set multiprocessing to use 'spawn' instead of 'fork' on Linux
# This is required because Qt creates threads early, and forking a multi-threaded
# process can lead to deadlocks. The main application sets this in buzz/buzz.py.
if platform.system() != "Windows":
try:
multiprocessing.set_start_method("spawn", force=True)
except RuntimeError:
pass # Already set
from PyQt6.QtSql import QSqlDatabase
from _pytest.fixtures import SubRequest
from buzz.db.dao.transcription_dao import TranscriptionDAO
from buzz.db.dao.transcription_segment_dao import TranscriptionSegmentDAO
from buzz.db.db import setup_test_db
from buzz.db.service.transcription_service import TranscriptionService
from buzz.settings.settings import Settings
from buzz.settings.shortcuts import Shortcuts
from buzz.widgets.application import Application
@pytest.fixture()
def db() -> QSqlDatabase:
db = setup_test_db()
yield db
db.close()
os.remove(db.databaseName())
@pytest.fixture()
def transcription_dao(db, request: SubRequest) -> TranscriptionDAO:
dao = TranscriptionDAO(db)
if hasattr(request, "param"):
transcriptions = request.param
for transcription in transcriptions:
dao.insert(transcription)
return dao
@pytest.fixture()
def transcription_service(
transcription_dao, transcription_segment_dao
) -> TranscriptionService:
return TranscriptionService(transcription_dao, transcription_segment_dao)
@pytest.fixture()
def transcription_segment_dao(db) -> TranscriptionSegmentDAO:
return TranscriptionSegmentDAO(db)
@pytest.fixture(scope="session")
def qapp_cls():
return Application
@pytest.fixture(scope="session")
def qapp_args(request):
if not hasattr(request, "param"):
return []
return request.param
@pytest.fixture(scope="session")
def settings():
application = "".join(
random.choice(string.ascii_letters + string.digits) for _ in range(6)
)
settings = Settings(application=application)
yield settings
settings.clear()
@pytest.fixture(scope="session")
def shortcuts(settings):
return Shortcuts(settings)
@pytest.fixture(scope="session", autouse=True)
def cleanup_testdata_exports():
"""Remove transcription export files written into testdata/ during the test session.
Transcription tests (e.g. the MainWindow flow) transcribe a bundled
``testdata/*.mp3`` with no explicit output directory, so the export lands
next to the source as ``<name> (transcribed on <date>).<ext>``. Those export
files are never checked in, so we additionally sweep that pattern at setup
and teardown to clear artifacts leaked by a previous interrupted run.
"""
testdata_dir = os.path.join(os.path.dirname(__file__), "..", "testdata")
export_glob = os.path.join(testdata_dir, "* (transcribed on *)*")
def _sweep_leaked_exports():
for path in glob.glob(export_glob):
try:
os.remove(path)
except OSError:
pass
_sweep_leaked_exports()
before = set(glob.glob(os.path.join(testdata_dir, "*")))
yield
after = set(glob.glob(os.path.join(testdata_dir, "*")))
for path in after - before:
try:
os.remove(path)
except OSError:
pass
_sweep_leaked_exports()