1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-01-22 22:03:54 +03:00

Added tests for jobs factory

This commit is contained in:
Adolfo Gómez García 2024-08-29 18:35:21 +02:00
parent 206947cbe3
commit c69d880c40
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
2 changed files with 62 additions and 10 deletions

View File

@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2024 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
Author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from unittest import mock
from django.test import TransactionTestCase
from uds.core.jobs import jobs_factory
class JobsFactoryTest(TransactionTestCase):
def test_ensure_workers_initialized(self) -> None:
with mock.patch('uds.workers.initialize') as mock_initialize:
jobs_factory.JobsFactory.factory().ensure_jobs_registered()
mock_initialize.assert_called_once()
def test_jobs_stores_on_db(self) -> None:
with mock.patch(
'uds.models.Scheduler'
) as mock_scheduler:
jobs_factory.JobsFactory.factory().ensure_jobs_registered()
# Currently there are 34 jobs registered. If this changes to a lower number, fail
self.assertGreaterEqual(mock_scheduler.objects.create.call_count, 34)

View File

@ -47,10 +47,10 @@ class JobsFactory(factory.Factory['Job']):
"""
Ensures that uds core workers are correctly registered in database and in factory
"""
from uds.models import Scheduler # pylint: disable=import-outside-toplevel
from uds.core.util.model import sql_now # pylint: disable=import-outside-toplevel
from uds.core.types.states import State # pylint: disable=import-outside-toplevel
from uds import workers # pylint: disable=import-outside-toplevel
from uds.models import Scheduler # pylint: disable=import-outside-toplevel
from uds.core.util.model import sql_now # pylint: disable=import-outside-toplevel
from uds.core.types.states import State # pylint: disable=import-outside-toplevel
from uds import workers # pylint: disable=import-outside-toplevel
try:
logger.debug('Ensuring that jobs are registered inside database')
@ -74,12 +74,8 @@ class JobsFactory(factory.Factory['Job']):
logger.debug('Already added %s', name)
job = Scheduler.objects.get(name=name)
job.frecuency = type_.frecuency
if job.next_execution > job.last_execution + datetime.timedelta(
seconds=type_.frecuency
):
job.next_execution = job.last_execution + datetime.timedelta(
seconds=type_.frecuency
)
if job.next_execution > job.last_execution + datetime.timedelta(seconds=type_.frecuency):
job.next_execution = job.last_execution + datetime.timedelta(seconds=type_.frecuency)
job.save()
except Exception as e:
logger.debug(
@ -87,3 +83,7 @@ class JobsFactory(factory.Factory['Job']):
e.__class__,
e,
)
@staticmethod
def factory() -> 'JobsFactory':
return JobsFactory()