Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 44 additions & 33 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# this needs to be above `src` imports
sys.path.insert(0, str(CWD.parent))

from tagstudio.core.constants import THUMB_CACHE_NAME, TS_FOLDER_NAME
from tagstudio.core.library.alchemy.library import Library
from tagstudio.core.library.alchemy.models import Entry, Tag
from tagstudio.qt.ts_qt import QtDriver
Expand Down Expand Up @@ -50,18 +51,30 @@ def file_mediatypes_library():
return lib


@pytest.fixture(scope="session")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the difference between the default scope vs the session scope here in practice, as they both seem to generate different temp directories when this fixture is called.

The only tangible effect I can observe is having (scope="session") causes the tests in test_refresh_new_files() to fail if using my suggested removal of the strange pre-existing "hasattr" code...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this my understanding of it is that when you use the scope="session" fixture it runs the code inside it once and then any further calls to it return the same temporary directory path so it can be reused across multiple tests and then when the session is complete it runs cleanup and deletes the directory which is why the yield is used instead of return because return causes the program to end before TempDir does it's cleanup leaving the files in the temp directory.

Which leads to the other comment, Yeah, I did notice you had more uses of TemporaryDirectory in other test but I wasn't sure if there was a reason for it maybe needing fresh directories for different tests. I only built it this way because it seemed test_file_path_options:test_title_update() depended on the path being exactly the same as used with conftest:library() which is probably why you had it the way you did.

I hope any of this made sense cause I'm only like 80% following my own words here myself

def library_dir():
"""Creates a shared library path for tests, that cleans up after the session."""
with TemporaryDirectory() as tmp_dir_name:
library_path = Path(tmp_dir_name)

thumbs_path = library_path / TS_FOLDER_NAME / THUMB_CACHE_NAME
thumbs_path.mkdir(parents=True, exist_ok=True)

yield library_path


@pytest.fixture
def library(request):
def library(request, library_dir: Path):
# when no param is passed, use the default
library_path = "/dev/null/"
library_path = library_dir
if hasattr(request, "param"):
if isinstance(request.param, TemporaryDirectory):
library_path = request.param.name
library_path = Path(request.param.name)
else:
library_path = request.param
library_path = Path(request.param)

lib = Library()
status = lib.open_library(Path(library_path), ":memory:")
status = lib.open_library(library_path, ":memory:")
assert status.success

tag = Tag(
Expand Down Expand Up @@ -130,34 +143,32 @@ def entry_full(library: Library):


@pytest.fixture
def qt_driver(qtbot, library):
with TemporaryDirectory() as tmp_dir:

class Args:
settings_file = Path(tmp_dir) / "settings.toml"
cache_file = Path(tmp_dir) / "tagstudio.ini"
open = Path(tmp_dir)
ci = True

with patch("tagstudio.qt.ts_qt.Consumer"), patch("tagstudio.qt.ts_qt.CustomRunnable"):
driver = QtDriver(Args())

driver.app = Mock()
driver.main_window = Mock()
driver.preview_panel = Mock()
driver.flow_container = Mock()
driver.item_thumbs = []
driver.autofill_action = Mock()

driver.copy_buffer = {"fields": [], "tags": []}
driver.copy_fields_action = Mock()
driver.paste_fields_action = Mock()

driver.lib = library
# TODO - downsize this method and use it
# driver.start()
driver.frame_content = list(library.get_entries())
yield driver
def qt_driver(qtbot, library, library_dir: Path):
class Args:
settings_file = library_dir / "settings.toml"
cache_file = library_dir / "tagstudio.ini"
open = library_dir
ci = True

with patch("tagstudio.qt.ts_qt.Consumer"), patch("tagstudio.qt.ts_qt.CustomRunnable"):
driver = QtDriver(Args())

driver.app = Mock()
driver.main_window = Mock()
driver.preview_panel = Mock()
driver.flow_container = Mock()
driver.item_thumbs = []
driver.autofill_action = Mock()

driver.copy_buffer = {"fields": [], "tags": []}
driver.copy_fields_action = Mock()
driver.paste_fields_action = Mock()

driver.lib = library
# TODO - downsize this method and use it
# driver.start()
driver.frame_content = list(library.get_entries())
yield driver


@pytest.fixture
Expand Down
12 changes: 7 additions & 5 deletions tests/qt/test_file_path_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ def test_file_path_display(
),
],
)
def test_title_update(qt_driver: QtDriver, filepath_option: ShowFilepathOption, expected_title):
def test_title_update(
qt_driver: QtDriver, filepath_option: ShowFilepathOption, expected_title, library_dir: Path
):
base_title = qt_driver.base_title
test_path = Path("/dev/null")

open_status = LibraryStatus(
success=True,
library_path=test_path,
library_path=library_dir,
message="",
msg_description="",
)
Expand All @@ -133,7 +135,7 @@ def test_title_update(qt_driver: QtDriver, filepath_option: ShowFilepathOption,
qt_driver.folders_to_tags_action = QAction(menu_bar)

# Trigger the update
qt_driver.init_library(test_path, open_status)
qt_driver.init_library(library_dir, open_status)

# Assert the title is updated correctly
qt_driver.main_window.setWindowTitle.assert_called_with(expected_title(test_path, base_title))
qt_driver.main_window.setWindowTitle.assert_called_with(expected_title(library_dir, base_title))
42 changes: 20 additions & 22 deletions tests/qt/test_global_settings.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
from pathlib import Path
from tempfile import TemporaryDirectory

from tagstudio.core.global_settings import GlobalSettings, Theme


def test_read_settings():
with TemporaryDirectory() as tmp_dir:
settings_path = Path(tmp_dir) / "settings.toml"
with open(settings_path, "a") as settings_file:
settings_file.write("""
language = "de"
open_last_loaded_on_startup = true
autoplay = true
show_filenames_in_grid = true
page_size = 1337
show_filepath = 0
dark_mode = 2
""")
def test_read_settings(library_dir: Path):
settings_path = library_dir / "settings.toml"
with open(settings_path, "a") as settings_file:
settings_file.write("""
language = "de"
open_last_loaded_on_startup = true
autoplay = true
show_filenames_in_grid = true
page_size = 1337
show_filepath = 0
dark_mode = 2
""")

settings = GlobalSettings.read_settings(settings_path)
assert settings.language == "de"
assert settings.open_last_loaded_on_startup
assert settings.autoplay
assert settings.show_filenames_in_grid
assert settings.page_size == 1337
assert settings.show_filepath == 0
assert settings.theme == Theme.SYSTEM
settings = GlobalSettings.read_settings(settings_path)
assert settings.language == "de"
assert settings.open_last_loaded_on_startup
assert settings.autoplay
assert settings.show_filenames_in_grid
assert settings.page_size == 1337
assert settings.show_filepath == 0
assert settings.theme == Theme.SYSTEM
29 changes: 12 additions & 17 deletions tests/test_driver.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from os import makedirs
from pathlib import Path
from tempfile import TemporaryDirectory

from PySide6.QtCore import QSettings

from tagstudio.core.constants import TS_FOLDER_NAME
from tagstudio.core.driver import DriverMixin
from tagstudio.core.enums import SettingItems
from tagstudio.core.global_settings import GlobalSettings
Expand Down Expand Up @@ -52,22 +49,20 @@ def test_evaluate_path_last_lib_not_exists():
assert result == LibraryStatus(success=True, library_path=None, message=None)


def test_evaluate_path_last_lib_present():
def test_evaluate_path_last_lib_present(library_dir: Path):
# Given
with TemporaryDirectory() as tmpdir:
cache_file = tmpdir + "/test_settings.ini"
cache = QSettings(cache_file, QSettings.Format.IniFormat)
cache.setValue(SettingItems.LAST_LIBRARY, tmpdir)
cache.sync()
cache_file = library_dir / "test_settings.ini"
cache = QSettings(str(cache_file), QSettings.Format.IniFormat)
cache.setValue(SettingItems.LAST_LIBRARY, library_dir)
cache.sync()

settings = GlobalSettings()
settings.open_last_loaded_on_startup = True
settings = GlobalSettings()
settings.open_last_loaded_on_startup = True

makedirs(Path(tmpdir) / TS_FOLDER_NAME)
driver = TestDriver(settings, cache)
driver = TestDriver(settings, cache)

# When
result = driver.evaluate_path(None)
# When
result = driver.evaluate_path(None)

# Then
assert result == LibraryStatus(success=True, library_path=Path(tmpdir))
# Then
assert result == LibraryStatus(success=True, library_path=library_dir)