2018-11-01 06:01:44 +00:00
|
|
|
import pytest
|
|
|
|
import peewee
|
2018-11-30 00:40:18 +00:00
|
|
|
import sys
|
2020-05-30 11:49:21 +00:00
|
|
|
import os
|
2018-11-30 00:40:18 +00:00
|
|
|
from datetime import datetime as dt
|
2020-03-03 05:19:36 +00:00
|
|
|
from unittest.mock import MagicMock
|
2018-11-01 06:01:44 +00:00
|
|
|
|
|
|
|
import vorta
|
2020-03-03 05:19:36 +00:00
|
|
|
from vorta.models import (RepoModel, RepoPassword, BackupProfileModel, SourceFileModel,
|
|
|
|
SettingsModel, ArchiveModel, WifiSettingModel, EventLogModel, SchemaVersion)
|
2020-10-03 06:27:31 +00:00
|
|
|
from vorta.views.main_window import MainWindow
|
2020-03-03 05:19:36 +00:00
|
|
|
|
|
|
|
models = [RepoModel, RepoPassword, BackupProfileModel, SourceFileModel,
|
|
|
|
SettingsModel, ArchiveModel, WifiSettingModel, EventLogModel, SchemaVersion]
|
2018-11-01 06:01:44 +00:00
|
|
|
|
2018-11-05 10:30:59 +00:00
|
|
|
|
2018-11-27 11:33:16 +00:00
|
|
|
def pytest_configure(config):
|
|
|
|
sys._called_from_test = True
|
2021-02-17 02:14:58 +00:00
|
|
|
pytest._wait_defaults = {'timeout': 20000}
|
|
|
|
os.environ['LANG'] = 'en' # Ensure we test an English UI
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def qapp(tmpdir_factory):
|
|
|
|
# DB is required to init QApplication. New DB used for every test.
|
|
|
|
tmp_db = tmpdir_factory.mktemp('Vorta').join('settings.sqlite')
|
|
|
|
mock_db = peewee.SqliteDatabase(str(tmp_db))
|
|
|
|
vorta.models.init_db(mock_db)
|
|
|
|
|
|
|
|
from vorta.application import VortaApp
|
|
|
|
VortaApp.set_borg_details_action = MagicMock() # Can't use pytest-mock in session scope
|
|
|
|
VortaApp.scheduler = MagicMock()
|
|
|
|
|
|
|
|
qapp = VortaApp([]) # Only init QApplication once to avoid segfaults while testing.
|
|
|
|
|
|
|
|
yield qapp
|
2021-02-22 01:45:43 +00:00
|
|
|
qapp.quit()
|
2018-11-27 11:33:16 +00:00
|
|
|
|
|
|
|
|
2020-03-03 05:19:36 +00:00
|
|
|
@pytest.fixture(scope='function', autouse=True)
|
2021-02-17 02:14:58 +00:00
|
|
|
def init_db(qapp, tmpdir_factory):
|
|
|
|
tmp_db = tmpdir_factory.mktemp('Vorta').join('settings.sqlite')
|
|
|
|
mock_db = peewee.SqliteDatabase(str(tmp_db), pragmas={'journal_mode': 'wal', })
|
|
|
|
vorta.models.init_db(mock_db)
|
2018-11-06 06:47:04 +00:00
|
|
|
|
|
|
|
new_repo = RepoModel(url='i0fi93@i593.repo.borgbase.com:repo')
|
2021-01-18 07:52:07 +00:00
|
|
|
new_repo.encryption = 'none'
|
2018-11-06 06:47:04 +00:00
|
|
|
new_repo.save()
|
2018-11-30 00:40:18 +00:00
|
|
|
|
|
|
|
profile = BackupProfileModel.get(id=1)
|
|
|
|
profile.repo = new_repo.id
|
2020-08-30 06:28:48 +00:00
|
|
|
profile.dont_run_on_metered_networks = False
|
2018-11-06 06:47:04 +00:00
|
|
|
profile.save()
|
|
|
|
|
2018-11-30 00:40:18 +00:00
|
|
|
test_archive = ArchiveModel(snapshot_id='99999', name='test-archive', time=dt(2000, 1, 1, 0, 0), repo=1)
|
|
|
|
test_archive.save()
|
|
|
|
|
2020-03-23 06:20:09 +00:00
|
|
|
test_archive1 = ArchiveModel(snapshot_id='99998', name='test-archive1', time=dt(2000, 1, 1, 0, 0), repo=1)
|
|
|
|
test_archive1.save()
|
|
|
|
|
2020-10-30 08:30:52 +00:00
|
|
|
source_dir = SourceFileModel(dir='/tmp/another', repo=new_repo, dir_size=100, dir_files_count=18, path_isdir=True)
|
2018-11-06 06:47:04 +00:00
|
|
|
source_dir.save()
|
2018-11-30 00:40:18 +00:00
|
|
|
|
2020-10-03 06:27:31 +00:00
|
|
|
qapp.main_window = MainWindow(qapp) # Re-open main window to apply mock data in UI
|
2020-03-03 05:19:36 +00:00
|
|
|
|
|
|
|
|
2020-09-13 04:55:10 +00:00
|
|
|
@pytest.fixture(scope='function', autouse=True)
|
|
|
|
def cleanup(request, qapp, qtbot):
|
|
|
|
"""
|
2021-02-17 01:58:42 +00:00
|
|
|
Cleanup after each test
|
2020-09-13 04:55:10 +00:00
|
|
|
"""
|
|
|
|
def ensure_borg_thread_stopped():
|
|
|
|
qapp.backup_cancelled_event.emit()
|
|
|
|
qtbot.waitUntil(lambda: not vorta.borg.borg_thread.BorgThread.is_running())
|
|
|
|
request.addfinalizer(ensure_borg_thread_stopped)
|
|
|
|
|
|
|
|
|
2018-11-30 00:40:18 +00:00
|
|
|
@pytest.fixture
|
2018-12-05 09:05:47 +00:00
|
|
|
def choose_file_dialog(*args):
|
2018-11-30 00:40:18 +00:00
|
|
|
class MockFileDialog:
|
2018-12-04 02:58:12 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2018-11-30 00:40:18 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def open(self, func):
|
|
|
|
func()
|
|
|
|
|
|
|
|
def selectedFiles(self):
|
|
|
|
return ['/tmp']
|
|
|
|
|
|
|
|
return MockFileDialog
|
|
|
|
|
|
|
|
|
2018-11-06 06:47:04 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def borg_json_output():
|
|
|
|
def _read_json(subcommand):
|
2018-11-27 11:33:16 +00:00
|
|
|
stdout = open(f'tests/borg_json_output/{subcommand}_stdout.json')
|
|
|
|
stderr = open(f'tests/borg_json_output/{subcommand}_stderr.json')
|
|
|
|
return stdout, stderr
|
2018-11-06 06:47:04 +00:00
|
|
|
return _read_json
|
2020-11-19 07:44:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def rootdir():
|
|
|
|
return os.path.dirname(os.path.abspath(__file__))
|