1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-03-12 04:58:34 +03:00

Added factory tester

This commit is contained in:
Adolfo Gómez García 2024-01-28 13:47:51 +01:00
parent db082109bd
commit cf34718cf4
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
3 changed files with 48 additions and 8 deletions

View File

@ -36,12 +36,16 @@ import typing
import collections.abc
from .job import Job
from .delayed_task import DelayedTask
from .jobs_factory import JobsFactory
# Imports for type checking
if typing.TYPE_CHECKING:
from .jobs_factory import JobsFactory
def factory() -> 'JobsFactory':
"""
Returns a singleton to a jobs factory
"""
from .jobs_factory import JobsFactory # pylint: disable=import-outside-toplevel
return JobsFactory()

View File

@ -38,7 +38,7 @@ class Factory(typing.Generic[V], metaclass=singleton.Singleton):
self._objects[type_name.lower()] = type_
def get(self, type_name: str) -> typing.Optional[type[V]]:
def get_type(self, type_name: str) -> typing.Optional[type[V]]:
'''
Returns an object from the factory.
'''
@ -69,8 +69,8 @@ class Factory(typing.Generic[V], metaclass=singleton.Singleton):
return self._objects.values()
# aliases for get
lookup = get
__getitem__ = get
lookup = get_type
__getitem__ = get_type
__setitem__ = register
__contains__ = has

View File

@ -29,10 +29,15 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from ast import Sub
import typing
import collections.abc
import logging
from unittest import mock
from py import test
from ...utils.test import UDSTestCase
@ -40,14 +45,45 @@ from uds.core.util import factory
logger = logging.getLogger(__name__)
class FactoryObject:
def __init__(self, name: str, value: int):
self.name = name
self.value = value
class Subclass1(FactoryObject):
pass
class Subclass2(FactoryObject):
pass
class FactoryTest(UDSTestCase):
def test_factory(self) -> None:
f = factory.Factory[FactoryObject]()
pass
test_factory = factory.Factory[FactoryObject]()
test_factory.register('first', Subclass1)
test_factory.register('second', Subclass2)
self.assertIn('first', test_factory.objects())
self.assertIn('second', test_factory.objects())
self.assertTrue('first' in test_factory.objects())
self.assertTrue('second' in test_factory.objects())
self.assertCountEqual(test_factory.objects(), ['first', 'second'])
self.assertEqual(test_factory.objects()['first'], Subclass1)
self.assertEqual(test_factory['first'], Subclass1)
self.assertEqual(test_factory.get_type('first'), Subclass1)
self.assertEqual(test_factory.objects()['second'], Subclass2)
self.assertEqual(test_factory['second'], Subclass2)
self.assertEqual(test_factory.get_type('second'), Subclass2)
# This should not raise an exception, but call to its logger.debug
with mock.patch.object(factory.logger, 'debug') as mock_debug:
test_factory.register('first', Subclass1)
mock_debug.assert_called_once()