vendredi 5 juin 2020

How to perform complex range testing with Python?

I'm trying to make a process method inside a class, the method uses 5 variables from a passed Request object like this :

class BaseAction(ABC):
    def __init__(self, request):
        self.value      = request.value
        self.min_tr     = request.min_treshold
        self.warn_tr    = request.warn_treshold
        self.crit_tr    = request.crit_treshold
        self.fatal_tr   = request.fatal_treshold

    def process(self):

There are other methods inside the class :

    @abstractmethod
    def onMinimum(self):
        pass

    @abstractmethod
    def onWarning(self):
        pass

    @abstractmethod
    def onCritical(self):
        pass

    @abstractmethod
    def onFatal(self):
        pass

The access to those methods are depending on the self.value and it is position between the other treshold variables, for example :

  • If value between min and warn => onMinimun
  • If value between warn and crit => onWarning
  • If value between crit and fatal => onCritical
  • If value grater than fatal => onFatal

But the problem is : The request object may not have all the values, which means the user can send a request with all possibilities :

  • min only
  • min and warn
  • ... (4*4=16 possibilities)

I tried this basic solution:

if self.min_tr <= self.value < self.warn_tr:
            self.onMinimum()
elif self.warn_tr <= self.value < self.crit_tr:
            self.onWarning()
elif self.crit_tr <= self.value < self.fatal_tr:
            self.onCritical()
elif self.fatal_tr <= self.value:
            self.onFatal()

But when I passed a Request object with:

  • request.value = 5.2
  • request.min_treshold = 4
  • No warn, crit, and fatal values

Expected: onMinimum execution

But I got : onFatal execution, because self.fatal_tr = 0 and it is less than 5.2.

How can I solve this ?

Aucun commentaire:

Enregistrer un commentaire