1
1
import json
2
2
3
- from candore .modules .variatons import Variations
4
- from candore .utils import last_index_of_element
3
+ from candore .modules .variations import Variations , Constants
4
+ from candore .utils import last_index_of_element , is_list_contains_dict
5
5
6
6
7
7
class Comparator :
8
8
def __init__ (self , settings ):
9
9
self .big_key = []
10
- self .big_compare = {}
10
+ self .big_diff = {}
11
+ self .big_constant = {}
11
12
self .record_evs = False
12
13
self .variations = Variations (settings )
14
+ self .constants = Constants (settings )
13
15
self .expected_variations = self .variations .expected_variations
14
16
self .skipped_variations = self .variations .skipped_variations
17
+ self .expected_constants = self .constants .expected_constants
18
+ self .skipped_constants = self .constants .skipped_constants
15
19
16
- def remove_non_variant_key (self , key ):
20
+
21
+
22
+ def remove_verifed_key (self , key ):
17
23
reversed_bk = self .big_key [::- 1 ]
18
24
if key in reversed_bk :
19
25
reversed_bk .remove (key )
@@ -36,13 +42,32 @@ def record_variation(self, pre, post, var_details=None):
36
42
"post" : post ,
37
43
"variation" : var_details or "Expected(A)" ,
38
44
}
39
- self .big_compare .update ({full_path : variation })
45
+ self .big_diff .update ({full_path : variation })
40
46
elif (
41
47
var_full_path not in self .expected_variations
42
48
and var_full_path not in self .skipped_variations
43
49
):
44
50
variation = {"pre" : pre , "post" : post , "variation" : var_details or "" }
45
- self .big_compare .update ({full_path : variation })
51
+ self .big_diff .update ({full_path : variation })
52
+
53
+ def record_constants (self , pre , post , var_details = None ):
54
+ big_key = [str (itm ) for itm in self .big_key ]
55
+ full_path = "/" .join (big_key )
56
+ var_full_path = "/" .join ([itm for itm in self .big_key if not isinstance (itm , int )])
57
+ if var_full_path in self .expected_constants or var_full_path in self .skipped_constants :
58
+ if self .record_evs :
59
+ variation = {
60
+ "pre" : pre ,
61
+ "post" : post ,
62
+ "constant" : var_details or "Expected(A)" ,
63
+ }
64
+ self .big_constant .update ({full_path : variation })
65
+ elif (
66
+ var_full_path not in self .expected_constants
67
+ and var_full_path not in self .skipped_constants
68
+ ):
69
+ variation = {"pre" : pre , "post" : post , "constant" : var_details or "" }
70
+ self .big_constant .update ({full_path : variation })
46
71
47
72
def _is_data_type_dict (self , pre , post , unique_key = "" ):
48
73
if (pre and 'id' in pre ) and (post and 'id' in post ):
@@ -62,32 +87,41 @@ def _is_data_type_dict(self, pre, post, unique_key=""):
62
87
)
63
88
self .remove_path (unique_key )
64
89
65
- def _is_data_type_list (self , pre , post , unique_key = "" ):
90
+ def _is_data_type_list_contains_dict (self , pre , post ):
66
91
for pre_entity in pre :
67
92
if not pre_entity :
68
93
continue
69
- if type (pre_entity ) is dict :
70
- for post_entity in post :
71
- if not post_entity :
72
- continue
73
- if "id" in pre_entity :
74
- if pre_entity ["id" ] == post_entity ["id" ]:
75
- self .compare_all_pres_with_posts (
76
- pre_entity , post_entity , unique_key = pre_entity ["id" ]
77
- )
78
- else :
79
- key = list (pre_entity .keys ())[0 ]
80
- if pre_entity [key ] == post_entity [key ]:
81
- self .compare_all_pres_with_posts (
82
- pre_entity [key ], post_entity [key ], unique_key = key
83
- )
94
+ for post_entity in post :
95
+ if not post_entity :
96
+ continue
84
97
if "id" in pre_entity :
85
- self .remove_path (pre_entity ["id" ])
98
+ if pre_entity ["id" ] == post_entity ["id" ]:
99
+ self .compare_all_pres_with_posts (
100
+ pre_entity , post_entity , unique_key = pre_entity ["id" ]
101
+ )
86
102
else :
87
- self .remove_path (pre_entity [list (pre_entity .keys ())[0 ]])
103
+ key = list (pre_entity .keys ())[0 ]
104
+ if pre_entity [key ] == post_entity [key ]:
105
+ self .compare_all_pres_with_posts (
106
+ pre_entity [key ], post_entity [key ], unique_key = key
107
+ )
108
+ if "id" in pre_entity :
109
+ self .remove_path (pre_entity ["id" ])
88
110
else :
89
- if pre_entity not in post :
90
- self .record_variation (pre , post )
111
+ self .remove_path (pre_entity [list (pre_entity .keys ())[0 ]])
112
+
113
+ def _is_data_type_list (self , pre , post , unique_key = "" ):
114
+
115
+ def custom_key (elem ):
116
+ return 'None' if elem is None else str (elem )
117
+
118
+ if not is_list_contains_dict (pre ):
119
+ if sorted (pre , key = custom_key ) != sorted (post , key = custom_key ):
120
+ self .record_variation (pre , post )
121
+ else :
122
+ self .record_constants (pre , post )
123
+ else :
124
+ self ._is_data_type_list_contains_dict (pre , post )
91
125
self .remove_path (unique_key )
92
126
93
127
def compare_all_pres_with_posts (self , pre_data , post_data , unique_key = "" , var_details = None ):
@@ -100,9 +134,11 @@ def compare_all_pres_with_posts(self, pre_data, post_data, unique_key="", var_de
100
134
else :
101
135
if pre_data != post_data :
102
136
self .record_variation (pre_data , post_data , var_details )
103
- self .remove_non_variant_key (unique_key )
137
+ else :
138
+ self .record_constants (pre_data , post_data , var_details )
139
+ self .remove_verifed_key (unique_key )
104
140
105
- def compare_json (self , pre_file , post_file ):
141
+ def compare_json (self , pre_file , post_file , inverse ):
106
142
pre_data = post_data = None
107
143
108
144
with open (pre_file , "r" ) as fpre :
@@ -112,4 +148,7 @@ def compare_json(self, pre_file, post_file):
112
148
post_data = json .load (fpost )
113
149
114
150
self .compare_all_pres_with_posts (pre_data , post_data )
115
- return self .big_compare
151
+ if not inverse :
152
+ return self .big_diff
153
+ else :
154
+ return self .big_constant
0 commit comments