33from typing import Mapping , Tuple , List , Dict , Any , Union
44import jmespath
55
6- from netcompare .arguments import (
7- CheckArguments ,
8- CheckArgumentsExactMatch ,
9- CheckArgumentsRegexMatch ,
10- CheckArgumentsParameterMatch ,
11- CheckArgumentsToleranceMatch ,
12- )
136
147from .utils .jmespath_parsers import (
158 jmespath_value_parser ,
2720class CheckType :
2821 """Check Type Class."""
2922
30- class_args = CheckArguments
31-
3223 @staticmethod
3324 def init (check_type ):
3425 """Factory pattern to get the appropriate CheckType implementation.
@@ -108,29 +99,47 @@ def evaluate(self, value_to_compare: Any, **kwargs) -> Tuple[Dict, bool]:
10899 Returns:
109100 tuple: Dictionary representing check result, bool indicating if differences are found.
110101 """
102+ # This method should call before any other logic the validation of the arguments
103+ # self.validate(**kwargs)
104+ raise NotImplementedError
105+
106+ @staticmethod
107+ def validate (** kwargs ):
108+ """Method to validate arguments that raises proper exceptions."""
111109 raise NotImplementedError
112110
113111
114112class ExactMatchType (CheckType ):
115113 """Exact Match class docstring."""
116114
117- validator_class = CheckArgumentsExactMatch
115+ @staticmethod
116+ def validate (** kwargs ):
117+ """Method to validate arguments."""
118+ # reference_data = getattr(kwargs, "reference_data")
118119
119120 def evaluate (self , value_to_compare : Any , reference_data : Any ) -> Tuple [Dict , bool ]:
120121 """Returns the difference between values and the boolean."""
121- self .validator_class . validate (reference_data = reference_data )
122+ self .validate (reference_data = reference_data )
122123 evaluation_result = diff_generator (reference_data , value_to_compare )
123124 return evaluation_result , not evaluation_result
124125
125126
126127class ToleranceType (CheckType ):
127128 """Tolerance class docstring."""
128129
129- validator_class = CheckArgumentsToleranceMatch
130+ @staticmethod
131+ def validate (** kwargs ):
132+ """Method to validate arguments."""
133+ # reference_data = getattr(kwargs, "reference_data")
134+ tolerance = kwargs .get ("tolerance" )
135+ if not tolerance :
136+ raise ValueError ("Tolerance argument is mandatory for Tolerance Check Type." )
137+ if not isinstance (tolerance , int ):
138+ raise ValueError (f"Tolerance argument must be an integer, and it's { type (tolerance )} ." )
130139
131140 def evaluate (self , value_to_compare : Any , reference_data : Any , tolerance : int ) -> Tuple [Dict , bool ]:
132141 """Returns the difference between values and the boolean. Overwrites method in base class."""
133- self .validator_class . validate (reference_data = reference_data , tolerance = tolerance )
142+ self .validate (reference_data = reference_data , tolerance = tolerance )
134143 diff = diff_generator (reference_data , value_to_compare )
135144 self ._remove_within_tolerance (diff , tolerance )
136145 return diff , not diff
@@ -156,11 +165,27 @@ def _within_tolerance(*, old_value: float, new_value: float) -> bool:
156165class ParameterMatchType (CheckType ):
157166 """Parameter Match class implementation."""
158167
159- validator_class = CheckArgumentsParameterMatch
168+ @staticmethod
169+ def validate (** kwargs ):
170+ """Method to validate arguments."""
171+ mode_options = ["match" , "no-match" ]
172+ params = kwargs .get ("params" )
173+ if not params :
174+ raise ValueError ("Params argument is mandatory for ParameterMatch Check Type." )
175+ if not isinstance (params , dict ):
176+ raise ValueError (f"Params argument must be a dict, and it's { type (params )} ." )
177+
178+ mode = kwargs .get ("mode" )
179+ if not mode :
180+ raise ValueError ("Mode argument is mandatory for ParameterMatch Check Type." )
181+ if not isinstance (mode , str ):
182+ raise ValueError (f"Mode argument must be a string, and it's { type (mode )} ." )
183+ if mode not in mode_options :
184+ raise ValueError (f"Mode argument should be { mode_options } , and it's { mode } " )
160185
161186 def evaluate (self , value_to_compare : Mapping , params : Dict , mode : str ) -> Tuple [Dict , bool ]:
162187 """Parameter Match evaluator implementation."""
163- self .validator_class . validate (params = params , mode = mode )
188+ self .validate (params = params , mode = mode )
164189 # TODO: we don't use the mode?
165190 evaluation_result = parameter_evaluator (value_to_compare , params )
166191 return evaluation_result , not evaluation_result
@@ -169,10 +194,26 @@ def evaluate(self, value_to_compare: Mapping, params: Dict, mode: str) -> Tuple[
169194class RegexType (CheckType ):
170195 """Regex Match class implementation."""
171196
172- validator_class = CheckArgumentsRegexMatch
197+ @staticmethod
198+ def validate (** kwargs ):
199+ """Method to validate arguments."""
200+ mode_options = ["match" , "no-match" ]
201+ regex = kwargs .get ("regex" )
202+ if not regex :
203+ raise ValueError ("Params argument is mandatory for Regex Match Check Type." )
204+ if not isinstance (regex , str ):
205+ raise ValueError (f"Params argument must be a string, and it's { type (regex )} ." )
206+
207+ mode = kwargs .get ("mode" )
208+ if not mode :
209+ raise ValueError ("Mode argument is mandatory for Regex Match Check Type." )
210+ if not isinstance (mode , str ):
211+ raise ValueError (f"Mode argument must be a string, and it's { type (mode )} ." )
212+ if mode not in mode_options :
213+ raise ValueError (f"Mode argument should be { mode_options } , and it's { mode } " )
173214
174215 def evaluate (self , value_to_compare : Mapping , regex : str , mode : str ) -> Tuple [Mapping , bool ]:
175216 """Regex Match evaluator implementation."""
176- self .validator_class . validate (regex = regex , mode = mode )
217+ self .validate (regex = regex , mode = mode )
177218 diff = regex_evaluator (value_to_compare , regex , mode )
178219 return diff , not diff
0 commit comments