|
| 1 | +import os |
| 2 | +import pathlib |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +from PySide6.QtGui import ( |
| 7 | + QAction, |
| 8 | +) |
| 9 | +from PySide6.QtWidgets import QMenu, QMenuBar |
| 10 | + |
| 11 | +from tagstudio.core.enums import SettingItems, ShowFilepathOption |
| 12 | +from tagstudio.core.library.alchemy.library import LibraryStatus |
| 13 | +from tagstudio.qt.modals.settings_panel import SettingsPanel |
| 14 | +from tagstudio.qt.widgets.preview_panel import PreviewPanel |
| 15 | + |
| 16 | + |
| 17 | +# Tests to see if the file path setting is applied correctly |
| 18 | +@pytest.mark.parametrize( |
| 19 | + "filepath_option", |
| 20 | + [ |
| 21 | + ShowFilepathOption.SHOW_FULL_PATHS.value, |
| 22 | + ShowFilepathOption.SHOW_RELATIVE_PATHS.value, |
| 23 | + ShowFilepathOption.SHOW_FILENAMES_ONLY.value, |
| 24 | + ], |
| 25 | +) |
| 26 | +def test_filepath_setting(qtbot, qt_driver, filepath_option): |
| 27 | + settings_panel = SettingsPanel(qt_driver) |
| 28 | + qtbot.addWidget(settings_panel) |
| 29 | + |
| 30 | + # Mock the update_recent_lib_menu method |
| 31 | + with patch.object(qt_driver, "update_recent_lib_menu", return_value=None): |
| 32 | + # Set the file path option |
| 33 | + settings_panel.filepath_combobox.setCurrentIndex(filepath_option) |
| 34 | + settings_panel.apply_filepath_setting() |
| 35 | + |
| 36 | + # Assert the setting is applied |
| 37 | + assert qt_driver.settings.value(SettingItems.SHOW_FILEPATH) == filepath_option |
| 38 | + |
| 39 | + |
| 40 | +# Tests to see if the file paths are being displayed correctly |
| 41 | +@pytest.mark.parametrize( |
| 42 | + "filepath_option, expected_path", |
| 43 | + [ |
| 44 | + ( |
| 45 | + ShowFilepathOption.SHOW_FULL_PATHS, |
| 46 | + lambda library: pathlib.Path(library.library_dir / "one/two/bar.md"), |
| 47 | + ), |
| 48 | + (ShowFilepathOption.SHOW_RELATIVE_PATHS, lambda library: pathlib.Path("one/two/bar.md")), |
| 49 | + (ShowFilepathOption.SHOW_FILENAMES_ONLY, lambda library: pathlib.Path("bar.md")), |
| 50 | + ], |
| 51 | +) |
| 52 | +def test_file_path_display(qt_driver, library, filepath_option, expected_path): |
| 53 | + panel = PreviewPanel(library, qt_driver) |
| 54 | + |
| 55 | + # Select 2 |
| 56 | + qt_driver.toggle_item_selection(2, append=False, bridge=False) |
| 57 | + panel.update_widgets() |
| 58 | + |
| 59 | + with patch.object(qt_driver.settings, "value", return_value=filepath_option): |
| 60 | + # Apply the mock value |
| 61 | + filename = library.get_entry(2).path |
| 62 | + panel.file_attrs.update_stats(filepath=pathlib.Path(library.library_dir / filename)) |
| 63 | + |
| 64 | + # Generate the expected file string. |
| 65 | + # This is copied directly from the file_attributes.py file |
| 66 | + # can be imported as a function in the future |
| 67 | + display_path = expected_path(library) |
| 68 | + file_str: str = "" |
| 69 | + separator: str = f"<a style='color: #777777'><b>{os.path.sep}</a>" # Gray |
| 70 | + for i, part in enumerate(display_path.parts): |
| 71 | + part_ = part.strip(os.path.sep) |
| 72 | + if i != len(display_path.parts) - 1: |
| 73 | + file_str += f"{"\u200b".join(part_)}{separator}</b>" |
| 74 | + else: |
| 75 | + if file_str != "": |
| 76 | + file_str += "<br>" |
| 77 | + file_str += f"<b>{"\u200b".join(part_)}</b>" |
| 78 | + |
| 79 | + # Assert the file path is displayed correctly |
| 80 | + assert panel.file_attrs.file_label.text() == file_str |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize( |
| 84 | + "filepath_option, expected_title", |
| 85 | + [ |
| 86 | + ( |
| 87 | + ShowFilepathOption.SHOW_FULL_PATHS.value, |
| 88 | + lambda path, base_title: f"{base_title} - Library '{path}'", |
| 89 | + ), |
| 90 | + ( |
| 91 | + ShowFilepathOption.SHOW_RELATIVE_PATHS.value, |
| 92 | + lambda path, base_title: f"{base_title} - Library '{path.name}'", |
| 93 | + ), |
| 94 | + ( |
| 95 | + ShowFilepathOption.SHOW_FILENAMES_ONLY.value, |
| 96 | + lambda path, base_title: f"{base_title} - Library '{path.name}'", |
| 97 | + ), |
| 98 | + ], |
| 99 | +) |
| 100 | +def test_title_update(qtbot, qt_driver, filepath_option, expected_title): |
| 101 | + base_title = qt_driver.base_title |
| 102 | + test_path = pathlib.Path("/dev/null") |
| 103 | + open_status = LibraryStatus( |
| 104 | + success=True, |
| 105 | + library_path=test_path, |
| 106 | + message="", |
| 107 | + msg_description="", |
| 108 | + ) |
| 109 | + # Set the file path option |
| 110 | + qt_driver.settings.setValue(SettingItems.SHOW_FILEPATH, filepath_option) |
| 111 | + menu_bar = QMenuBar() |
| 112 | + |
| 113 | + qt_driver.open_recent_library_menu = QMenu(menu_bar) |
| 114 | + qt_driver.manage_file_ext_action = QAction(menu_bar) |
| 115 | + qt_driver.save_library_backup_action = QAction(menu_bar) |
| 116 | + qt_driver.close_library_action = QAction(menu_bar) |
| 117 | + qt_driver.refresh_dir_action = QAction(menu_bar) |
| 118 | + qt_driver.tag_manager_action = QAction(menu_bar) |
| 119 | + qt_driver.color_manager_action = QAction(menu_bar) |
| 120 | + qt_driver.new_tag_action = QAction(menu_bar) |
| 121 | + qt_driver.fix_dupe_files_action = QAction(menu_bar) |
| 122 | + qt_driver.fix_unlinked_entries_action = QAction(menu_bar) |
| 123 | + qt_driver.clear_thumb_cache_action = QAction(menu_bar) |
| 124 | + qt_driver.folders_to_tags_action = QAction(menu_bar) |
| 125 | + |
| 126 | + # Trigger the update |
| 127 | + qt_driver.init_library(pathlib.Path(test_path), open_status) |
| 128 | + |
| 129 | + # Assert the title is updated correctly |
| 130 | + qt_driver.main_window.setWindowTitle.assert_called_with(expected_title(test_path, base_title)) |
0 commit comments