|
| 1 | +"""Regression tests for selecting a project to merge.""" |
| 2 | + |
| 3 | +from contextlib import ExitStack |
| 4 | +from pathlib import Path |
| 5 | +import sqlite3 |
| 6 | +import tempfile |
| 7 | +from types import SimpleNamespace |
| 8 | +from unittest import TestCase |
| 9 | +from unittest.mock import MagicMock, call, patch |
| 10 | + |
| 11 | +from qualcoder.special_functions import DialogSpecialFunctions |
| 12 | + |
| 13 | + |
| 14 | +class TestMergeProjectSelection(TestCase): |
| 15 | + |
| 16 | + def setUp(self): |
| 17 | + temporary_directory = tempfile.TemporaryDirectory() |
| 18 | + self.addCleanup(temporary_directory.cleanup) |
| 19 | + self.directory = Path(temporary_directory.name) |
| 20 | + self.current_project = self.directory / "current.qda" |
| 21 | + self.current_project.mkdir() |
| 22 | + self.dialog = SimpleNamespace( |
| 23 | + app=SimpleNamespace( |
| 24 | + settings={"directory": ""}, |
| 25 | + project_path=self.current_project.as_posix(), |
| 26 | + ), |
| 27 | + merge_project_path="previous.qda", |
| 28 | + ui=SimpleNamespace(pushButton_merge=MagicMock()), |
| 29 | + ) |
| 30 | + stack = self.enterContext(ExitStack()) |
| 31 | + stack.enter_context(patch("builtins._", side_effect=lambda text: text, create=True)) |
| 32 | + self.picker = stack.enter_context(patch( |
| 33 | + "qualcoder.special_functions.QtWidgets.QFileDialog.getExistingDirectory" |
| 34 | + )) |
| 35 | + self.message = stack.enter_context(patch("qualcoder.special_functions.Message")) |
| 36 | + |
| 37 | + def create_project(self, name: str = "source.qda", about: str = "QualCoder test") -> Path: |
| 38 | + """Create a minimal project with the requested folder name and identity.""" |
| 39 | + project_directory = self.directory / name |
| 40 | + project_directory.mkdir() |
| 41 | + with sqlite3.connect(project_directory / "data.qda") as connection: |
| 42 | + connection.execute( |
| 43 | + "create table project (databaseversion text, date text, memo text, about text)" |
| 44 | + ) |
| 45 | + connection.execute("insert into project values ('v17', '', '', ?)", (about,)) |
| 46 | + connection.close() |
| 47 | + return project_directory |
| 48 | + |
| 49 | + def select(self, selected_path: str) -> None: |
| 50 | + """Simulate selecting a folder in the merge dialog.""" |
| 51 | + self.picker.return_value = selected_path |
| 52 | + DialogSpecialFunctions.select_project_folder(self.dialog) |
| 53 | + |
| 54 | + def assert_rejected(self, expected_message: str) -> None: |
| 55 | + """Check that a rejected selection cannot run a stale merge.""" |
| 56 | + self.assertEqual("", self.dialog.merge_project_path) |
| 57 | + self.dialog.ui.pushButton_merge.setEnabled.assert_called_once_with(False) |
| 58 | + self.message.assert_called_once() |
| 59 | + self.assertIn(expected_message, self.message.call_args.args[2]) |
| 60 | + |
| 61 | + def test_trailing_separator_and_uppercase_suffix_are_accepted(self): |
| 62 | + project_directory = self.create_project("source.QDA") |
| 63 | + self.select(project_directory.as_posix() + "/") |
| 64 | + self.assertEqual(project_directory.as_posix(), self.dialog.merge_project_path) |
| 65 | + self.assertEqual( |
| 66 | + [call(False), call(True)], |
| 67 | + self.dialog.ui.pushButton_merge.setEnabled.call_args_list, |
| 68 | + ) |
| 69 | + |
| 70 | + def test_cancel_clears_previous_selection_without_error(self): |
| 71 | + self.select("") |
| 72 | + self.assertEqual("", self.dialog.merge_project_path) |
| 73 | + self.dialog.ui.pushButton_merge.setEnabled.assert_called_once_with(False) |
| 74 | + self.message.assert_not_called() |
| 75 | + |
| 76 | + def test_wrong_suffix_is_rejected(self): |
| 77 | + self.select(self.directory.as_posix()) |
| 78 | + self.assert_rejected("name must end with .qda") |
| 79 | + |
| 80 | + def test_missing_folder_is_rejected(self): |
| 81 | + self.select((self.directory / "missing.qda").as_posix()) |
| 82 | + self.assert_rejected("could not be found or accessed") |
| 83 | + |
| 84 | + def test_same_project_with_trailing_separator_is_rejected(self): |
| 85 | + self.select(self.current_project.as_posix() + "/") |
| 86 | + self.assert_rejected("The same project") |
| 87 | + |
| 88 | + def test_same_project_with_relative_components_is_rejected(self): |
| 89 | + alias_path = self.current_project / ".." / self.current_project.name |
| 90 | + self.select(alias_path.as_posix()) |
| 91 | + self.assert_rejected("The same project") |
| 92 | + |
| 93 | + def test_missing_database_is_not_created(self): |
| 94 | + project_directory = self.directory / "empty.qda" |
| 95 | + project_directory.mkdir() |
| 96 | + self.select(project_directory.as_posix()) |
| 97 | + self.assert_rejected("No data.qda database found") |
| 98 | + self.assertFalse((project_directory / "data.qda").exists()) |
| 99 | + |
| 100 | + def test_corrupt_database_is_rejected_without_changes(self): |
| 101 | + project_directory = self.directory / "corrupt.qda" |
| 102 | + project_directory.mkdir() |
| 103 | + database_path = project_directory / "data.qda" |
| 104 | + database_path.write_bytes(b"not a database") |
| 105 | + self.select(project_directory.as_posix()) |
| 106 | + self.assert_rejected("database could not be opened") |
| 107 | + self.assertEqual(b"not a database", database_path.read_bytes()) |
| 108 | + |
| 109 | + def test_unrelated_database_is_rejected(self): |
| 110 | + self.select(self.create_project(about="Another application").as_posix()) |
| 111 | + self.assert_rejected("Not a QualCoder project") |
| 112 | + |
| 113 | + def test_empty_project_table_is_rejected(self): |
| 114 | + project_directory = self.create_project() |
| 115 | + with sqlite3.connect(project_directory / "data.qda") as connection: |
| 116 | + connection.execute("delete from project") |
| 117 | + connection.close() |
| 118 | + self.select(project_directory.as_posix()) |
| 119 | + self.assert_rejected("Not a QualCoder project") |
| 120 | + |
| 121 | + def test_database_access_error_is_reported(self): |
| 122 | + project_directory = self.create_project() |
| 123 | + with patch("qualcoder.special_functions.sqlite3.connect", |
| 124 | + side_effect=sqlite3.OperationalError("unable to open database file")): |
| 125 | + self.select(project_directory.as_posix()) |
| 126 | + self.assert_rejected("unable to open database file") |
| 127 | + |
| 128 | + def test_validation_opens_database_read_only_and_closes_connection(self): |
| 129 | + project_directory = self.create_project("source #1.qda") |
| 130 | + with patch("qualcoder.special_functions.sqlite3.connect") as connect: |
| 131 | + connection = connect.return_value |
| 132 | + connection.execute.return_value.fetchone.return_value = ("v17", "", "", "QualCoder") |
| 133 | + self.select(project_directory.as_posix()) |
| 134 | + connect.assert_called_once_with( |
| 135 | + (project_directory / "data.qda").resolve().as_uri() + "?mode=ro", uri=True |
| 136 | + ) |
| 137 | + connection.close.assert_called_once() |
| 138 | + self.assertEqual(project_directory.as_posix(), self.dialog.merge_project_path) |
0 commit comments