11"""CheckType Implementation."""
2- from typing import Mapping , Iterable , Tuple
2+ from typing import Mapping , Iterable , Tuple , Union , List
33from .evaluator import diff_generator
4+ from .runner import extract_values_from_output
45
56
67class CheckType :
@@ -21,24 +22,28 @@ def init(*args):
2122 else :
2223 raise NotImplementedError
2324
24- def evaluate (self , pre_value : Mapping , post_value : Mapping , path : Mapping ) -> Tuple [Mapping , bool ]:
25- """Return a diff of the comparison and a boolean True if it passes or False otherwise."""
26- self .diff = diff_generator (pre_value , post_value , path )
27- # self.diff may get modified by a child class when self.check_logic is called.
28- logic_check = self .check_logic ()
29- return (self .diff , logic_check )
25+ @staticmethod
26+ def extract_value_from_json_path (
27+ value : Mapping , path : Mapping , exclude : List = list ()
28+ ) -> Union [Mapping , List , int , str , bool ]:
29+ """Return the value contained into a Mapping for a defined path."""
30+ return extract_values_from_output (value , path , exclude )
31+
32+ def evaluate (self , reference_value : Mapping , value_to_compare : Mapping ) -> Tuple [Mapping , bool ]:
33+ """Return the result of the evaluation and a boolean True if it passes it or False otherwise.
3034
31- def check_logic ( self ) -> bool :
32- """docstring placeholder."""
35+ This method is the one that each CheckType has to implement.
36+ """
3337 raise NotImplementedError
3438
3539
3640class ExactMatchType (CheckType ):
3741 """Exact Match class docstring."""
3842
39- def check_logic (self ) -> bool :
40- """Return True if diff is empty indicating an exact match."""
41- return not self .diff
43+ def evaluate (self , reference_value : Mapping , value_to_compare : Mapping ) -> Tuple [Mapping , bool ]:
44+ """Returns the difference between values and the boolean."""
45+ diff = diff_generator (reference_value , value_to_compare )
46+ return diff , not diff
4247
4348
4449class ToleranceType (CheckType ):
@@ -49,16 +54,17 @@ def __init__(self, *args):
4954 self .tolerance_factor = float (args [1 ]) / 100
5055 super ().__init__ ()
5156
52- def check_logic (self ) -> bool :
53- """Return True if the changed values are within tolerance."""
54- self .diff = self ._get_outliers ()
55- return not self .diff
57+ def evaluate (self , reference_value : Mapping , value_to_compare : Mapping ) -> Tuple [Mapping , bool ]:
58+ """Returns the difference between values and the boolean."""
59+ diff = diff_generator (reference_value , value_to_compare )
60+ diff = self ._get_outliers (diff )
61+ return diff , not diff
5662
57- def _get_outliers (self ) -> Mapping :
63+ def _get_outliers (self , diff : Mapping ) -> Mapping :
5864 """Return a mapping of values outside the tolerance threshold."""
5965 result = {
6066 key : {sub_key : values for sub_key , values in obj .items () if not self ._within_tolerance (** values )}
61- for key , obj in self . diff .items ()
67+ for key , obj in diff .items ()
6268 }
6369 return {key : obj for key , obj in result .items () if obj }
6470
@@ -68,20 +74,26 @@ def _within_tolerance(self, *, old_value: float, new_value: float) -> bool:
6874 return (old_value - max_diff ) < new_value < (old_value + max_diff )
6975
7076
71- def compare (
72- pre_obj : Mapping , post_obj : Mapping , path : Mapping , type_info : Iterable , options : Mapping
73- ) -> Tuple [Mapping , bool ]:
74- """Entry point function.
77+ # TODO: compare is no longer the entry point, we should use the libary as:
78+ # netcompare_check = CheckType.init(check_type_info, options)
79+ # pre_result = netcompare_check.extract_value_from_json_path(pre_obj, path)
80+ # post_result = netcompare_check.extract_value_from_json_path(post_obj, path)
81+ # netcompare_check.evaluate(pre_result, post_result)
82+ #
83+ # def compare(
84+ # pre_obj: Mapping, post_obj: Mapping, path: Mapping, type_info: Iterable, options: Mapping
85+ # ) -> Tuple[Mapping, bool]:
86+ # """Entry point function.
7587
76- Returns a diff object and the boolean of the comparison.
77- """
88+ # Returns a diff object and the boolean of the comparison.
89+ # """
7890
79- type_info = type_info .lower ()
91+ # type_info = type_info.lower()
8092
81- try :
82- type_obj = CheckType .init (type_info , options )
83- except Exception :
84- # We will be here if we can't infer the type_obj
85- raise
93+ # try:
94+ # type_obj = CheckType.init(type_info, options)
95+ # except Exception:
96+ # # We will be here if we can't infer the type_obj
97+ # raise
8698
87- return type_obj .evaluate (pre_obj , post_obj , path )
99+ # return type_obj.evaluate(pre_obj, post_obj, path)
0 commit comments