# Path: planb/factories.py
# class FilesetFactory(DjangoModelFactory):
#     host_prefix = factory.Faker('hostname', levels=0)
#     host_suffix = factory.Faker('domain_word')
#     tld = factory.Faker('tld')
# 
#     @factory.lazy_attribute
#     def friendly_name(self):
#         # Set friendly name as the full hostname within the hostgroup domain.
#         return '.'.join((
#             self.host_prefix, self.host_suffix, self.hostgroup.name, self.tld))
# 
#     storage_alias = 'dummy'
#     hostgroup = factory.SubFactory(HostGroupFactory)
# 
#     class Meta:
#         model = 'planb.Fileset'
#         exclude = ['host_prefix', 'host_suffix', 'tld']
# 
# Path: planb/tests/base.py
# class PlanbTestCase(TestCase):
#     def setUp(self):
#         super().setUp()
# 
#         # Reset storage_pools, otherwise we might get stale data from
#         # previous dummy's. This is a dict, that everyone has loaded
#         # already. Flush the contents of the dict.
#         for storage in storage_pools.values():
#             storage.close()
#         storage_pools.clear()
#         storage_pools.update(load_storage_pools())


import datetime
from unittest.mock import patch
from django.test import override_settings
from django.utils import timezone
from planb.factories import FilesetFactory
from planb.tests.base import PlanbTestCase
        self.assertFalse(fileset.should_backup())
        fileset.retention = '3d'
        del fileset._retention_map

        # Has recent backup.
        self.assertFalse(fileset.should_backup())

        # No recent backup.
        # 68400 seconds since last + 19172 avg duration > 86400 daily interval.
        fileset.last_ok = timezone.now() - datetime.timedelta(hours=19)
        self.assertTrue(fileset.should_backup())

        # Daily backups should start each day, hourly backups should start
        # each hour, regardless of the time since the previous backup.
        with patch('planb.models.timezone') as m:
            # Time since last backup is just 5 hours.
            fileset.last_ok = datetime.datetime(
                2020, 5, 19, 19, tzinfo=timezone.utc)
            m.now.return_value = datetime.datetime(
                2020, 5, 20, tzinfo=timezone.utc)
            self.assertTrue(fileset.should_backup())
            # Same day of the month, one month later at midnight.
            m.now.return_value = datetime.datetime(
                2020, 6, 19, tzinfo=timezone.utc)
            self.assertTrue(fileset.should_backup())

        # Backup is running.
        # The running status is refreshed from the database.
        fileset.is_running = True
        fileset.save()

# Path: planb/factories.py
# class FilesetFactory(DjangoModelFactory):
#     host_prefix = factory.Faker('hostname', levels=0)
#     host_suffix = factory.Faker('domain_word')
#     tld = factory.Faker('tld')
# 
#     @factory.lazy_attribute
#     def friendly_name(self):
#         # Set friendly name as the full hostname within the hostgroup domain.
#         return '.'.join((
#             self.host_prefix, self.host_suffix, self.hostgroup.name, self.tld))
# 
#     storage_alias = 'dummy'
#     hostgroup = factory.SubFactory(HostGroupFactory)
# 
#     class Meta:
#         model = 'planb.Fileset'
#         exclude = ['host_prefix', 'host_suffix', 'tld']
# 
# Path: planb/tests/base.py
# class PlanbTestCase(TestCase):
#     def setUp(self):
#         super().setUp()
# 
#         # Reset storage_pools, otherwise we might get stale data from
#         # previous dummy's. This is a dict, that everyone has loaded
#         # already. Flush the contents of the dict.
#         for storage in storage_pools.values():
#             storage.close()
#         storage_pools.clear()
#         storage_pools.update(load_storage_pools())


# Path: planb/tests/test_models.py
import datetime
from unittest.mock import patch
from django.test import override_settings
from django.utils import timezone
from planb.factories import FilesetFactory
from planb.tests.base import PlanbTestCase

        self.assertFalse(fileset.should_backup())
        fileset.retention = '3d'
        del fileset._retention_map

        # Has recent backup.
        self.assertFalse(fileset.should_backup())

        # No recent backup.
        # 68400 seconds since last + 19172 avg duration > 86400 daily interval.
        fileset.last_ok = timezone.now() - datetime.timedelta(hours=19)
        self.assertTrue(fileset.should_backup())

        # Daily backups should start each day, hourly backups should start
        # each hour, regardless of the time since the previous backup.
        with patch('planb.models.timezone') as m:
            # Time since last backup is just 5 hours.
            fileset.last_ok = datetime.datetime(
                2020, 5, 19, 19, tzinfo=timezone.utc)
            m.now.return_value = datetime.datetime(
                2020, 5, 20, tzinfo=timezone.utc)
            self.assertTrue(fileset.should_backup())
            # Same day of the month, one month later at midnight.
            m.now.return_value = datetime.datetime(
                2020, 6, 19, tzinfo=timezone.utc)
            self.assertTrue(fileset.should_backup())

        # Backup is running.
        # The running status is refreshed from the database.
        fileset.is_running = True
        fileset.save()

        self.assertFalse(fileset.should_backup())