musicmuster/tests/test_file_importer.py

83 lines
2.2 KiB
Python

# Standard library imports
import unittest
from unittest.mock import patch
# PyQt imports
# Third party imports
import pytest
from pytestqt.plugin import QtBot # type: ignore
# App imports
from config import Config
from app.models import (
db,
Playlists,
)
from app import musicmuster
# Custom fixture to adapt qtbot for use with unittest.TestCase
@pytest.fixture(scope="class")
def qtbot_adapter(qapp, request):
"""Adapt qtbot fixture for usefixtures and unittest.TestCase"""
request.cls.qtbot = QtBot(request)
# Wrapper to handle setup/teardown operations
def with_updown(function):
def test_wrapper(self, *args, **kwargs):
if callable(getattr(self, "up", None)):
self.up()
try:
function(self, *args, **kwargs)
finally:
if callable(getattr(self, "down", None)):
self.down()
test_wrapper.__doc__ = function.__doc__
return test_wrapper
# Apply the custom fixture to the test class
@pytest.mark.usefixtures("qtbot_adapter")
class MyTestCase(unittest.TestCase):
def up(self):
db.create_all()
self.widget = musicmuster.Window()
playlist_name = "file importer playlist"
with db.Session() as session:
playlist = Playlists(session, playlist_name)
self.widget.create_playlist_tab(playlist)
with self.qtbot.waitExposed(self.widget):
self.widget.show()
def down(self):
db.drop_all()
@with_updown
@patch("file_importer.show_OK")
def test_import_no_files(self, mock_show_ok):
"""Try importing with no files to import"""
self.widget.import_files_wrapper()
mock_show_ok.assert_called_once_with(
"File import",
f"No files in {Config.REPLACE_FILES_DEFAULT_SOURCE} to import",
None,
)
# @with_updown
# def test_import_no_files(self):
# """Try importing with no files to import"""
# with patch("file_importer.show_OK") as mock_show_ok:
# self.widget.import_files_wrapper()
# mock_show_ok.assert_called_once_with(
# "File import",
# f"No files in {Config.REPLACE_FILES_DEFAULT_SOURCE} to import",
# None,
# )