test(backup): break out dummy service fixture

This commit is contained in:
Houkime 2023-09-20 13:48:30 +00:00
parent 69a05de3d7
commit 6e9d86e844
4 changed files with 57 additions and 49 deletions

View file

@ -197,6 +197,7 @@ class ResticBackupper(AbstractBackupper):
output, output,
"parsed messages:", "parsed messages:",
messages, messages,
"command: ",
backup_command, backup_command,
) from error ) from error

View file

@ -6,6 +6,59 @@ import pytest
from selfprivacy_api.utils import WriteUserData, ReadUserData from selfprivacy_api.utils import WriteUserData, ReadUserData
from os import path
from os import makedirs
from typing import Generator
# import pickle
import selfprivacy_api.services as services
from selfprivacy_api.services import get_service_by_id, Service
from selfprivacy_api.services.test_service import DummyService
TESTFILE_BODY = "testytest!"
TESTFILE_2_BODY = "testissimo!"
@pytest.fixture()
def raw_dummy_service(tmpdir):
dirnames = ["test_service", "also_test_service"]
service_dirs = []
for d in dirnames:
service_dir = path.join(tmpdir, d)
makedirs(service_dir)
service_dirs.append(service_dir)
testfile_path_1 = path.join(service_dirs[0], "testfile.txt")
with open(testfile_path_1, "w") as file:
file.write(TESTFILE_BODY)
testfile_path_2 = path.join(service_dirs[1], "testfile2.txt")
with open(testfile_path_2, "w") as file:
file.write(TESTFILE_2_BODY)
# we need this to not change get_folders() much
class TestDummyService(DummyService, folders=service_dirs):
pass
service = TestDummyService()
# assert pickle.dumps(service) is not None
return service
@pytest.fixture()
def dummy_service(tmpdir, raw_dummy_service) -> Generator[Service, None, None]:
service = raw_dummy_service
# register our service
services.services.append(service)
assert get_service_by_id(service.get_id()) is not None
yield service
# cleanup because apparently it matters wrt tasks
services.services.remove(service)
def test_get_api_version(authorized_client): def test_get_api_version(authorized_client):
response = authorized_client.get("/api/version") response = authorized_client.get("/api/version")

View file

@ -2,7 +2,6 @@ import pytest
import os import os
import os.path as path import os.path as path
from os import makedirs
from os import remove from os import remove
from os import listdir from os import listdir
from os import urandom from os import urandom
@ -15,7 +14,8 @@ from selfprivacy_api.utils.huey import huey
import tempfile import tempfile
import selfprivacy_api.services as services from tests.test_common import dummy_service, raw_dummy_service
from selfprivacy_api.services import Service, get_all_services from selfprivacy_api.services import Service, get_all_services
from selfprivacy_api.services import get_service_by_id from selfprivacy_api.services import get_service_by_id
from selfprivacy_api.services.service import ServiceStatus from selfprivacy_api.services.service import ServiceStatus
@ -48,8 +48,6 @@ from selfprivacy_api.backup.tasks import (
from selfprivacy_api.backup.storage import Storage from selfprivacy_api.backup.storage import Storage
TESTFILE_BODY = "testytest!"
TESTFILE_2_BODY = "testissimo!"
REPO_NAME = "test_backup" REPO_NAME = "test_backup"
REPOFILE_NAME = "totallyunrelated" REPOFILE_NAME = "totallyunrelated"
@ -78,7 +76,6 @@ def backups(tmpdir):
else: else:
prepare_localfile_backups(tmpdir) prepare_localfile_backups(tmpdir)
Jobs.reset() Jobs.reset()
# assert not repo_path
Backups.init_repo() Backups.init_repo()
assert Backups.provider().location == str(tmpdir) + "/" + REPOFILE_NAME assert Backups.provider().location == str(tmpdir) + "/" + REPOFILE_NAME
@ -91,49 +88,6 @@ def backups_backblaze(generic_userdata):
Backups.reset(reset_json=False) Backups.reset(reset_json=False)
@pytest.fixture()
def raw_dummy_service(tmpdir):
dirnames = ["test_service", "also_test_service"]
service_dirs = []
for d in dirnames:
service_dir = path.join(tmpdir, d)
makedirs(service_dir)
service_dirs.append(service_dir)
testfile_path_1 = path.join(service_dirs[0], "testfile.txt")
with open(testfile_path_1, "w") as file:
file.write(TESTFILE_BODY)
testfile_path_2 = path.join(service_dirs[1], "testfile2.txt")
with open(testfile_path_2, "w") as file:
file.write(TESTFILE_2_BODY)
# we need this to not change get_folders() much
class TestDummyService(DummyService, folders=service_dirs):
pass
service = TestDummyService()
return service
@pytest.fixture()
def dummy_service(tmpdir, backups, raw_dummy_service) -> Service:
service = raw_dummy_service
# register our service
services.services.append(service)
# make sure we are in immediate mode because this thing is non pickleable to store on queue.
huey.immediate = True
assert huey.immediate is True
assert get_service_by_id(service.get_id()) is not None
yield service
# cleanup because apparently it matters wrt tasks
services.services.remove(service)
@pytest.fixture() @pytest.fixture()
def memory_backup() -> AbstractBackupProvider: def memory_backup() -> AbstractBackupProvider:
ProviderClass = providers.get_provider(BackupProvider.MEMORY) ProviderClass = providers.get_provider(BackupProvider.MEMORY)

View file

@ -12,7 +12,7 @@ from selfprivacy_api.services.test_service import DummyService
from selfprivacy_api.services.service import Service, ServiceStatus, StoppedService from selfprivacy_api.services.service import Service, ServiceStatus, StoppedService
from selfprivacy_api.utils.waitloop import wait_until_true from selfprivacy_api.utils.waitloop import wait_until_true
from tests.test_graphql.test_backup import raw_dummy_service from tests.test_common import raw_dummy_service
def test_unimplemented_folders_raises(): def test_unimplemented_folders_raises():