Commit 5d0e2199 authored by Gianluca Marotta's avatar Gianluca Marotta
Browse files

CT-205 Final implementation of unit test for transaction id decorator; removed...

CT-205 Final implementation of unit test for transaction id decorator; removed unusued decorators in CSPSubarray code
parent 8db40239
Loading
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -40,7 +40,6 @@ from ska.base import SKASubarray, SKASubarrayStateModel
from ska.base.commands import ActionCommand, ResultCode
from ska.base.faults import CapabilityValidationError
from ska.base.control_model import HealthState, AdminMode, ObsState, ObsMode
from .utils.decorators import AdminModeCheck, ObsStateCheck, SubarrayRejectCmd
from .utils.cspcommons import CmdExecState
from . import release
# PROTECTED REGION END #    //  CspSubarray.additionnal_import
+3 −3
Original line number Diff line number Diff line
@@ -9,16 +9,16 @@ from .cspcommons import CmdExecState

def transaction_id(func):
    """
    This decorator add a transaction id to the input of the decorated method. 
    Add a transaction id to the input of the decorated method. 
    The input of the decorated method is a json string.
    
    """
    @functools.wraps(func)
    def wrap(*args, **kwargs):
        
        # Argument of the decorated method can be either positional or named. 
        # Argument of the decorated method can be either positional or keyword. 
        # Following code manage both cases.
        # A named argument occurs when applied to "do" method of SKA Base Command Classes.
        # A keyword argument occurs when applied to "do" method of SKA Base Command Classes.
        # argument must be single.
        # note that args[0] is the "self" object of the class of the decorated method.
        if kwargs:
+38 −12
Original line number Diff line number Diff line
from csp_lmc_common.utils.decorators import transaction_id
import json

def test_transaction_id():

class class_test:
    """ A test class for the transaction id. 
    It has the few essential attributes
    and the decorated method called in tests. 
    """
    def __init__(self):
        self.name = 'name_test'
        self.logger = None
    @transaction_id
    def test_func(self, argin):
        """ A method to be tested with the transaction id decorator.
        it returns the input "argin" modified by the decorator
        """
        return argin

def test_transaction_id_with_positional_arguments():
    """ A test that shows that the transaction id is added to the argin
    when a positional arguments is passed to the decorated function
    """
    argin = '{"id":"test_id"}'
    c = class_test()
    argin = c.test_func(argin)
    assert 'transaction_id' in argin

    out = c.test_func(argin)
    assert 'transaction_id' in out
def test_transaction_id_with_keyword_arguments():
    """ The transaction id is added to the argin
    when a keyword argument is passed to the decorated function.
    The key of the argument is irrelevant
    """
    argin = '{"id":"test_id"}'
    c = class_test()
    argin = c.test_func(argin1=argin)
    assert 'transaction_id' in argin

    out = c.test_func(argin=argin)
    assert 'transaction_id' in out
    argin = c.test_func(argin2=argin)
    assert 'transaction_id' in argin

def test_transaction_id_if_present():
    """ If the transaction id is already present in argin
    it is not changed by the decorator
    """
    argin = '{"transaction_id":"test_id"}'
    c = class_test()
    out = c.test_func(argin)
    out = json.loads(out)
    assert out['transaction_id'] == 'test_id'