Skip to content

Commit

Permalink
DRY tests, increase coverage. By @bigtedde (#1769)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigtedde authored Aug 17, 2023
1 parent fb42614 commit ee71bca
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 122 deletions.
15 changes: 14 additions & 1 deletion tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
WifiSettingModel,
)
from vorta.utils import borg_compat
from vorta.views.main_window import MainWindow
from vorta.views.main_window import ArchiveTab, MainWindow

models = [
RepoModel,
Expand Down Expand Up @@ -206,6 +206,19 @@ def rootdir():
return os.path.dirname(os.path.abspath(__file__))


@pytest.fixture()
def archive_env(qapp, qtbot):
"""
Common setup for integration tests involving the archive tab.
"""
main: MainWindow = qapp.main_window
tab: ArchiveTab = main.archiveTab
main.tabWidget.setCurrentIndex(3)
tab.refresh_archive_list()
qtbot.waitUntil(lambda: tab.archiveTable.rowCount() > 0, **pytest._wait_defaults)
return main, tab


@pytest.fixture(autouse=True)
def min_borg_version(borg_version, request):
if request.node.get_closest_marker('min_borg_version'):
Expand Down
67 changes: 14 additions & 53 deletions tests/integration/test_archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ def test_repo_list(qapp, qtbot):
"""Test that the archives are created and repo list is populated correctly"""
main = qapp.main_window
tab = main.archiveTab

main.tabWidget.setCurrentIndex(3)
tab.refresh_archive_list()
qtbot.waitUntil(lambda: not tab.bCheck.isEnabled(), **pytest._wait_defaults)

assert not tab.bCheck.isEnabled()

qtbot.waitUntil(lambda: 'Refreshing archives done.' in main.progressText.text(), **pytest._wait_defaults)
Expand All @@ -31,45 +29,28 @@ def test_repo_list(qapp, qtbot):
assert tab.bCheck.isEnabled()


def test_repo_prune(qapp, qtbot):
def test_repo_prune(qapp, qtbot, archive_env):
"""Test for archive pruning"""
main = qapp.main_window
tab = main.archiveTab

main.tabWidget.setCurrentIndex(3)
tab.refresh_archive_list()
qtbot.waitUntil(lambda: tab.archiveTable.rowCount() > 0, **pytest._wait_defaults)

main, tab = archive_env
qtbot.mouseClick(tab.bPrune, QtCore.Qt.MouseButton.LeftButton)
qtbot.waitUntil(lambda: 'Pruning old archives' in main.progressText.text(), **pytest._wait_defaults)
qtbot.waitUntil(lambda: 'Refreshing archives done.' in main.progressText.text(), **pytest._wait_defaults)


@pytest.mark.min_borg_version('1.2.0a1')
def test_repo_compact(qapp, qtbot):
def test_repo_compact(qapp, qtbot, archive_env):
"""Test for archive compaction"""
main = qapp.main_window
tab = main.archiveTab

main.tabWidget.setCurrentIndex(3)
tab.refresh_archive_list()
qtbot.waitUntil(lambda: tab.archiveTable.rowCount() > 0, **pytest._wait_defaults)

main, tab = archive_env
qtbot.waitUntil(lambda: tab.compactButton.isEnabled(), **pytest._wait_defaults)
assert tab.compactButton.isEnabled()

qtbot.mouseClick(tab.compactButton, QtCore.Qt.MouseButton.LeftButton)
qtbot.waitUntil(lambda: 'compaction freed about' in main.logText.text().lower(), **pytest._wait_defaults)


def test_check(qapp, qtbot):
def test_check(qapp, qtbot, archive_env):
"""Test for archive consistency check"""
main = qapp.main_window
tab = main.archiveTab

main.tabWidget.setCurrentIndex(3)
tab.refresh_archive_list()
qtbot.waitUntil(lambda: tab.archiveTable.rowCount() > 0, **pytest._wait_defaults)
main, tab = archive_env

qapp.check_failed_event.disconnect()

Expand All @@ -81,7 +62,7 @@ def test_check(qapp, qtbot):


@pytest.mark.skipif(sys.platform == 'darwin', reason="Macos fuse support is uncertain")
def test_mount(qapp, qtbot, monkeypatch, choose_file_dialog, tmpdir):
def test_mount(qapp, qtbot, monkeypatch, choose_file_dialog, tmpdir, archive_env):
"""Test for archive mounting and unmounting"""

def psutil_disk_partitions(**kwargs):
Expand All @@ -91,12 +72,7 @@ def psutil_disk_partitions(**kwargs):
monkeypatch.setattr(psutil, "disk_partitions", psutil_disk_partitions)
monkeypatch.setattr(vorta.views.archive_tab, "choose_file_dialog", choose_file_dialog)

main = qapp.main_window
tab = main.archiveTab

main.tabWidget.setCurrentIndex(3)
tab.refresh_archive_list()
qtbot.waitUntil(lambda: tab.archiveTable.rowCount() > 0, **pytest._wait_defaults)
main, tab = archive_env
tab.archiveTable.selectRow(0)

qtbot.waitUntil(lambda: tab.bMountRepo.isEnabled(), **pytest._wait_defaults)
Expand All @@ -114,14 +90,9 @@ def psutil_disk_partitions(**kwargs):
qtbot.waitUntil(lambda: tab.mountErrors.text().startswith('Un-mounted successfully.'), **pytest._wait_defaults)


def test_archive_extract(qapp, qtbot, monkeypatch, choose_file_dialog, tmpdir):
def test_archive_extract(qapp, qtbot, monkeypatch, choose_file_dialog, tmpdir, archive_env):
"""Test for archive extraction"""
main = qapp.main_window
tab = main.archiveTab

main.tabWidget.setCurrentIndex(3)
tab.refresh_archive_list()
qtbot.waitUntil(lambda: tab.archiveTable.rowCount() > 0, **pytest._wait_defaults)
main, tab = archive_env

tab.archiveTable.selectRow(2)
tab.extract_action()
Expand All @@ -139,14 +110,9 @@ def test_archive_extract(qapp, qtbot, monkeypatch, choose_file_dialog, tmpdir):
assert [item.basename for item in tmpdir.listdir()] == ['private' if sys.platform == 'darwin' else 'tmp']


def test_archive_delete(qapp, qtbot, mocker):
def test_archive_delete(qapp, qtbot, mocker, archive_env):
"""Test for archive deletion"""
main = qapp.main_window
tab = main.archiveTab

main.tabWidget.setCurrentIndex(3)
tab.refresh_archive_list()
qtbot.waitUntil(lambda: tab.archiveTable.rowCount() > 0, **pytest._wait_defaults)
main, tab = archive_env

archivesCount = tab.archiveTable.rowCount()

Expand All @@ -160,14 +126,9 @@ def test_archive_delete(qapp, qtbot, mocker):
assert tab.archiveTable.rowCount() == archivesCount - 1


def test_archive_rename(qapp, qtbot, mocker):
def test_archive_rename(qapp, qtbot, mocker, archive_env):
"""Test for archive renaming"""
main = qapp.main_window
tab = main.archiveTab

main.tabWidget.setCurrentIndex(3)
tab.refresh_archive_list()
qtbot.waitUntil(lambda: tab.archiveTable.rowCount() > 0, **pytest._wait_defaults)
main, tab = archive_env

tab.archiveTable.selectRow(0)
new_archive_name = 'idf89d8f9d8fd98'
Expand Down
9 changes: 1 addition & 8 deletions tests/integration/test_borg.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def test_borg_prune(qapp, qtbot):
assert blocker.args[0]['returncode'] == 0


# test borg info
def test_borg_repo_info(qapp, qtbot, tmpdir):
"""This test runs borg info on a test repo directly without UI"""
repo_info = {
Expand All @@ -45,14 +44,8 @@ def test_borg_repo_info(qapp, qtbot, tmpdir):
assert blocker.args[0]['returncode'] == 0


def test_borg_archive_info(qapp, qtbot, tmpdir):
def test_borg_archive_info(qapp, qtbot, archive_env):
"""Check that archive info command works"""
main = qapp.main_window
tab = main.archiveTab
main.tabWidget.setCurrentIndex(3)
tab.refresh_archive_list()
qtbot.waitUntil(lambda: tab.archiveTable.rowCount() > 0, **pytest._wait_defaults)

params = BorgInfoArchiveJob.prepare(vorta.store.models.BackupProfileModel.select().first(), "test-archive1")
thread = BorgInfoArchiveJob(params['cmd'], params, qapp)

Expand Down
6 changes: 2 additions & 4 deletions tests/integration/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
from vorta.store.models import ArchiveModel, EventLogModel


def test_create(qapp, qtbot):
def test_create(qapp, qtbot, archive_env):
"""Test for manual archive creation"""
main = qapp.main_window
main.archiveTab.refresh_archive_list()
qtbot.waitUntil(lambda: main.archiveTab.archiveTable.rowCount() > 0, **pytest._wait_defaults)
main, tab = archive_env

qtbot.mouseClick(main.createStartBtn, QtCore.Qt.MouseButton.LeftButton)
qtbot.waitUntil(lambda: 'Backup finished.' in main.progressText.text(), **pytest._wait_defaults)
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
SourceFileModel,
WifiSettingModel,
)
from vorta.views.main_window import MainWindow
from vorta.views.main_window import ArchiveTab, MainWindow

models = [
RepoModel,
Expand Down Expand Up @@ -105,3 +105,16 @@ def _read_json(subcommand):
@pytest.fixture
def rootdir():
return os.path.dirname(os.path.abspath(__file__))


@pytest.fixture()
def archive_env(qapp, qtbot):
"""
Common setup for unit tests involving the archive tab.
"""
main: MainWindow = qapp.main_window
tab: ArchiveTab = main.archiveTab
main.tabWidget.setCurrentIndex(3)
tab.populate_from_profile()
qtbot.waitUntil(lambda: tab.archiveTable.rowCount() == 2, **pytest._wait_defaults)
return main, tab
Loading

0 comments on commit ee71bca

Please sign in to comment.