From d7316f8e79402e683bdee5229fa11c48833668fd Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Mon, 10 Apr 2023 15:51:54 +0000 Subject: [PATCH] test(backups): test autobackup timing --- selfprivacy_api/backup/__init__.py | 2 +- selfprivacy_api/backup/storage.py | 2 +- tests/test_graphql/test_backup.py | 75 ++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/selfprivacy_api/backup/__init__.py b/selfprivacy_api/backup/__init__.py index c931e57..1676ca5 100644 --- a/selfprivacy_api/backup/__init__.py +++ b/selfprivacy_api/backup/__init__.py @@ -69,7 +69,7 @@ class Backups: period = Backups.autobackup_period_minutes() if period is None: return False - if not Storage.is_autobackup_set_by_name(service_id) is None: + if not Storage.is_autobackup_set(service_id): return False last_backup = Storage.get_last_backup_time(service_id) diff --git a/selfprivacy_api/backup/storage.py b/selfprivacy_api/backup/storage.py index cf7d93a..7ca5f18 100644 --- a/selfprivacy_api/backup/storage.py +++ b/selfprivacy_api/backup/storage.py @@ -122,7 +122,7 @@ class Storage: redis.delete(Storage.__autobackup_key(service.get_id())) @staticmethod - def is_autobackup_set(service_name: str): + def is_autobackup_set(service_name: str) -> bool: return redis.exists(Storage.__autobackup_key(service_name)) @staticmethod diff --git a/tests/test_graphql/test_backup.py b/tests/test_graphql/test_backup.py index 54701b1..710e7b8 100644 --- a/tests/test_graphql/test_backup.py +++ b/tests/test_graphql/test_backup.py @@ -198,6 +198,17 @@ def test_autobackup_enable_service(backups, dummy_service): assert not Backups.is_autobackup_enabled(dummy_service) +def test_autobackup_enable_service_storage(backups, dummy_service): + assert len(Storage.services_with_autobackup()) == 0 + + Backups.enable_autobackup(dummy_service) + assert len(Storage.services_with_autobackup()) == 1 + assert Storage.services_with_autobackup()[0] == dummy_service.get_id() + + Backups.disable_autobackup(dummy_service) + assert len(Storage.services_with_autobackup()) == 0 + + def test_set_autobackup_period(backups): assert Backups.autobackup_period_minutes() is None @@ -220,6 +231,70 @@ def test_set_autobackup_period(backups): assert Backups.autobackup_period_minutes() is None +def test_no_default_autobackup(backups, dummy_service): + now = datetime.now(timezone.utc) + assert not Backups.is_time_to_backup_service(dummy_service.get_id(), now) + assert not Backups.is_time_to_backup(now) + + +def test_autobackup_timer_periods(backups, dummy_service): + now = datetime.now(timezone.utc) + backup_period = 13 # minutes + + Backups.enable_autobackup(dummy_service) + assert not Backups.is_time_to_backup_service(dummy_service.get_id(), now) + assert not Backups.is_time_to_backup(now) + + Backups.set_autobackup_period_minutes(backup_period) + assert Backups.is_time_to_backup_service(dummy_service.get_id(), now) + assert Backups.is_time_to_backup(now) + + Backups.set_autobackup_period_minutes(0) + assert not Backups.is_time_to_backup_service(dummy_service.get_id(), now) + assert not Backups.is_time_to_backup(now) + + +def test_autobackup_timer_enabling(backups, dummy_service): + now = datetime.now(timezone.utc) + backup_period = 13 # minutes + + Backups.set_autobackup_period_minutes(backup_period) + assert not Backups.is_time_to_backup_service(dummy_service.get_id(), now) + assert not Backups.is_time_to_backup(now) + + Backups.enable_autobackup(dummy_service) + assert Backups.is_time_to_backup_service(dummy_service.get_id(), now) + assert Backups.is_time_to_backup(now) + + Backups.disable_autobackup(dummy_service) + assert not Backups.is_time_to_backup_service(dummy_service.get_id(), now) + assert not Backups.is_time_to_backup(now) + + +def test_autobackup_timing(backups, dummy_service): + backup_period = 13 # minutes + now = datetime.now(timezone.utc) + + Backups.enable_autobackup(dummy_service) + Backups.set_autobackup_period_minutes(backup_period) + assert Backups.is_time_to_backup_service(dummy_service.get_id(), now) + assert Backups.is_time_to_backup(now) + + Backups.back_up(dummy_service) + + now = datetime.now(timezone.utc) + assert not Backups.is_time_to_backup_service(dummy_service.get_id(), now) + assert not Backups.is_time_to_backup(now) + + past = datetime.now(timezone.utc) - timedelta(minutes=1) + assert not Backups.is_time_to_backup_service(dummy_service.get_id(), past) + assert not Backups.is_time_to_backup(past) + + future = datetime.now(timezone.utc) + timedelta(minutes=backup_period + 2) + assert Backups.is_time_to_backup_service(dummy_service.get_id(), future) + assert Backups.is_time_to_backup(future) + + # Storage def test_snapshots_caching(backups, dummy_service): Backups.back_up(dummy_service)