Skip to content

Commit

Permalink
Adding expected constants support and better var file examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jyejare committed Apr 22, 2024
1 parent 0c7018d commit a22513b
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 72 deletions.
17 changes: 0 additions & 17 deletions 6_14.yaml

This file was deleted.

10 changes: 10 additions & 0 deletions 6_14_constants.yaml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
expected_constants:
# These are just examples, please replace these with some real expected constants
hosts:
content_facet_something: content_source_something_id

skipped_constants:
# These are just examples, please replace these with some real skip-able constants
content_view_versions:
environments:
permissions_something: something here
10 changes: 10 additions & 0 deletions 6_14_variations.yaml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
expected_variations:
# These are just examples, please replace these with some real expected variations
hosts:
content_facet_somevar: content_source_ssomevar_id

skipped_variations:
# These are just examples, please replace these with some real skip-able variations
content_view_versions:
environments:
permissions_somevar: somevar_here
37 changes: 27 additions & 10 deletions candore/modules/comparator.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import json

from candore.modules.variatons import Variations
from candore.modules.variations import Variations, Constants
from candore.utils import last_index_of_element, is_list_contains_dict


class Comparator:
def __init__(self, settings):
self.big_key = []
self.big_compare = {}
self.big_diff = {}
self.big_constant = {}
self.record_evs = False
self.variations = Variations(settings)
self.constants = Constants(settings)
self.expected_variations = self.variations.expected_variations
self.skipped_variations = self.variations.skipped_variations
self.expected_constants = self.constants.expected_constants
self.skipped_constants = self.constants.skipped_constants



def remove_verifed_key(self, key):
reversed_bk = self.big_key[::-1]
Expand All @@ -37,20 +42,32 @@ def record_variation(self, pre, post, var_details=None):
"post": post,
"variation": var_details or "Expected(A)",
}
self.big_compare.update({full_path: variation})
self.big_diff.update({full_path: variation})
elif (
var_full_path not in self.expected_variations
and var_full_path not in self.skipped_variations
):
variation = {"pre": pre, "post": post, "variation": var_details or ""}
self.big_compare.update({full_path: variation})
self.big_diff.update({full_path: variation})

def record_constants(self, pre, post):
def record_constants(self, pre, post, var_details=None):
big_key = [str(itm) for itm in self.big_key]
full_path = "/".join(big_key)
# var_full_path = "/".join([itm for itm in self.big_key if not isinstance(itm, int)])
variation = {"pre": pre, "post": post}
self.big_constant.update({full_path: variation})
var_full_path = "/".join([itm for itm in self.big_key if not isinstance(itm, int)])
if var_full_path in self.expected_constants or var_full_path in self.skipped_constants:
if self.record_evs:
variation = {
"pre": pre,
"post": post,
"constant": var_details or "Expected(A)",
}
self.big_constant.update({full_path: variation})
elif (
var_full_path not in self.expected_constants
and var_full_path not in self.skipped_constants
):
variation = {"pre": pre, "post": post, "constant": var_details or ""}
self.big_constant.update({full_path: variation})

def _is_data_type_dict(self, pre, post, unique_key=""):
if (pre and 'id' in pre) and (post and 'id' in post):
Expand Down Expand Up @@ -118,7 +135,7 @@ def compare_all_pres_with_posts(self, pre_data, post_data, unique_key="", var_de
if pre_data != post_data:
self.record_variation(pre_data, post_data, var_details)
else:
self.record_constants(pre_data, post_data)
self.record_constants(pre_data, post_data, var_details)
self.remove_verifed_key(unique_key)

def compare_json(self, pre_file, post_file, inverse):
Expand All @@ -132,6 +149,6 @@ def compare_json(self, pre_file, post_file, inverse):

self.compare_all_pres_with_posts(pre_data, post_data)
if not inverse:
return self.big_compare
return self.big_diff
else:
return self.big_constant
4 changes: 3 additions & 1 deletion candore/modules/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def _generate_csv_report(self, output_file, inverse):
csv_writer.writerow(columns)
# Writing Rows
for var_path, vals in self.results.items():
csv_writer.writerow([var_path, vals["pre"], vals["post"], vals.get('variation', None)])
csv_writer.writerow([
var_path, vals["pre"], vals["post"],
vals["variation" if not inverse else "constant"]])
print("Wrote CSV report to {}".format(output_file))
print("CSV report contains {} results".format(len(self.results)))
44 changes: 44 additions & 0 deletions candore/modules/variations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
A module responsible for calculating expected and skipped variations from
`conf/variations` yaml file and convert them into processable list
"""
from functools import cached_property
from candore.utils import yaml_reader, get_yaml_paths

import yaml


class Variations:
def __init__(self, settings):
self.settings = settings

@cached_property
def variations(self):
yaml_data = yaml_reader(file_path=self.settings.candore.var_file)
return yaml_data

@cached_property
def expected_variations(self):
return get_yaml_paths(yaml_data=self.variations.get("expected_variations"))

@cached_property
def skipped_variations(self):
return get_yaml_paths(yaml_data=self.variations.get("skipped_variations"))


class Constants:
def __init__(self, settings):
self.settings = settings

@cached_property
def constants(self):
yaml_data = yaml_reader(file_path=self.settings.candore.constant_file)
return yaml_data

@cached_property
def expected_constants(self):
return get_yaml_paths(yaml_data=self.constants.get("expected_constants"))

@cached_property
def skipped_constants(self):
return get_yaml_paths(yaml_data=self.constants.get("skipped_constants"))
43 changes: 0 additions & 43 deletions candore/modules/variatons.py

This file was deleted.

24 changes: 24 additions & 0 deletions candore/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
An utility helpers module
"""
from pathlib import Path
import yaml


def last_index_of_element(arr, element):
Expand All @@ -13,3 +15,25 @@ def last_index_of_element(arr, element):
def is_list_contains_dict(_list):
contains_dict = any(isinstance(element, dict) for element in _list)
return contains_dict


def yaml_reader(file_path=None):
templates_path = Path(file_path)
if not templates_path.exists():
print(f"The file {templates_path} does not exist.")
with templates_path.open() as yaml_file:
yaml_data = yaml.safe_load(yaml_file)
return yaml_data


def get_yaml_paths(yaml_data, prefix="", separator="/"):
paths = []
if isinstance(yaml_data, dict):
for key, value in yaml_data.items():
paths.extend(get_yaml_paths(value, f"{prefix}{key}{separator}"))
elif isinstance(yaml_data, list):
for item in yaml_data:
paths.extend(get_yaml_paths(item, prefix, separator))
else:
paths.append(f"{prefix}{yaml_data}")
return paths
3 changes: 2 additions & 1 deletion settings.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ candore:
product_version: 6.14
docpath: /apidoc
parser: apipie
var_file: "@jinja conf/variations/{{this.candore.version | replace('.', '_')}}.yaml"
var_file: "@jinja {{this.candore.product_version | replace('.', '_')}}_variations.yaml"
constant_File: "@jinja {{this.candore.product_version | replace('.', '_')}}_constants.yaml"

0 comments on commit a22513b

Please sign in to comment.