Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Aug 24, 2023
1 parent 73752ac commit c25b55e
Show file tree
Hide file tree
Showing 54 changed files with 1,327 additions and 130 deletions.
44 changes: 0 additions & 44 deletions app/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,47 +304,3 @@ def extract_clustering_setting(cluster_id):
return False
last_bit = cluster_id % 10
return (last_bit % 2) == 1


def compare_different_types_equality(a, b, epsilon=0.0001) -> bool:
"""Function is supposed to compare different object types such as dict(), list(), set(), tuple()
on equality taking into consideration that it can contain float
which should be compared with epsilon approximation.
Args:
a: The first object.
b: The second object.
epsilon: Approximation parameter to compare floats.
Returns:
True for the equality, False otherwise.
"""
if isinstance(a, (int, str, bool)):
return a == b
elif isinstance(a, float):
return abs(a - b) < epsilon
elif isinstance(a, (list, tuple)):
if len(a) != len(b):
return False
for i in range(len(a)):
if not compare_different_types_equality(a[i], b[i]):
return False
return True
elif isinstance(a, dict):
if set(a.keys()) != set(b.keys()):
return False
for key in a:
if not compare_different_types_equality(a[key], b[key]):
return False
return True
elif isinstance(a, set):
if len(a) != len(b):
return False
for item in a:
if item not in b:
return False
return True
elif a is None:
return b is None
else:
return False
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ httpretty==1.0.5
pytest==7.4.0
pytest-cov==4.1.0
flake8==5.0.4
sure==2.0.1
49 changes: 17 additions & 32 deletions test/boosting_decision_making/test_boosting_featurizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import logging
import unittest

import sure

from app.boosting_decision_making import weighted_similarity_calculator
from app.boosting_decision_making.boosting_featurizer import BoostingFeaturizer
from app.boosting_decision_making.suggest_boosting_featurizer import SuggestBoostingFeaturizer
Expand Down Expand Up @@ -59,7 +61,7 @@ def get_default_config(
filter_fields_any = []
return {
"max_query_terms": 50,
"min_should_match": 0.8,
"min_should_match": 0.47,
"min_word_length": 0,
"filter_min_should_match": filter_fields,
"filter_min_should_match_any": filter_fields_any,
Expand Down Expand Up @@ -98,41 +100,33 @@ def test_normalize_results(self):
weight_log_sim = weighted_similarity_calculator. \
WeightedSimilarityCalculator(folder=self.weights_folder)
for idx, test in enumerate(tests):
try:
with sure.ensure('Error in the test case index: {0}', idx):
_boosting_featurizer = BoostingFeaturizer(
test["elastic_results"],
test["config"],
[],
weighted_log_similarity_calculator=weight_log_sim)
assert len(_boosting_featurizer.all_results) == len(test["result"])
_boosting_featurizer.all_results.should.have.length_of(len(test["result"]))
for i in range(len(test["result"])):
for j in range(len(test["result"][i])):
for field in test["result"][i][j]:
elastic_res = _boosting_featurizer.all_results[i][1][j]
assert utils.compare_different_types_equality(elastic_res[field],
test["result"][i][j][field],
self.epsilon)
except AssertionError as err:
raise AssertionError(f'Error in the test case number: {idx}'). \
with_traceback(err.__traceback__)
elastic_res[field].should.equal(test["result"][i][j][field],
epsilon=self.epsilon)

def assert_scores_by_issue_type(self, boosting_featurizer, test):
scores_by_issue_type = boosting_featurizer.find_most_relevant_by_type()
assert len(scores_by_issue_type) == len(test["result"])
scores_by_issue_type.should.have.length_of(len(test["result"]))
for issue_type in test["result"]:
assert issue_type in scores_by_issue_type.keys()
scores_by_issue_type.keys().should.contain(issue_type)
elastic_res = scores_by_issue_type[issue_type]
for field in test["result"][issue_type]:
if type(test["result"][issue_type][field]) != dict:
assert utils.compare_different_types_equality(elastic_res[field],
test["result"][issue_type][field],
self.epsilon)
elastic_res[field].should.equal(test["result"][issue_type][field], epsilon=self.epsilon)
else:
for field_dict in test["result"][issue_type][field]:
result_field_dict = test["result"][issue_type][field][field_dict]
assert utils.compare_different_types_equality(elastic_res[field][field_dict],
result_field_dict,
self.epsilon)
elastic_res[field][field_dict].should.equal(result_field_dict, epsilon=self.epsilon)

@utils.ignore_warnings
def test_find_most_relevant_by_type(self):
Expand Down Expand Up @@ -192,23 +186,20 @@ def test_find_most_relevant_by_type(self):
weight_log_sim = weighted_similarity_calculator. \
WeightedSimilarityCalculator(folder=self.weights_folder)
for idx, test in enumerate(tests):
try:
with sure.ensure('Error in the test case index: {0}', idx):
_boosting_featurizer = BoostingFeaturizer(
test["elastic_results"],
test["config"],
[],
weighted_log_similarity_calculator=weight_log_sim)
self.assert_scores_by_issue_type(_boosting_featurizer, test)
except AssertionError as err:
raise AssertionError(f'Error in the test case number: {idx}'). \
with_traceback(err.__traceback__)

def assert_elastic_results(self, results, test):
assert len(results) == len(test["result"])
results.should.have.length_of(len(test["result"]))
for idx_res, (log, hits) in enumerate(results):
assert log["_id"] == test["result"][idx_res][0]["_id"]
log["_id"].should.equal(test["result"][idx_res][0]["_id"])
for i, hit in enumerate(hits["hits"]["hits"]):
assert hit["_id"] == test["result"][idx_res][1]["hits"]["hits"][i]["_id"]
hit["_id"].should.equal(test["result"][idx_res][1]["hits"]["hits"][i]["_id"])

@utils.ignore_warnings
def test_filter_by_min_should_match(self):
Expand Down Expand Up @@ -354,16 +345,13 @@ def test_find_most_relevant_by_type_for_suggests(self):
weight_log_sim = weighted_similarity_calculator. \
WeightedSimilarityCalculator(folder=self.weights_folder)
for idx, test in enumerate(tests):
try:
with sure.ensure('Error in the test case index: {0}', idx):
_boosting_featurizer = SuggestBoostingFeaturizer(
test["elastic_results"],
test["config"],
[],
weighted_log_similarity_calculator=weight_log_sim)
self.assert_scores_by_issue_type(_boosting_featurizer, test)
except AssertionError as err:
raise AssertionError(f'Error in the test case number: {idx}'). \
with_traceback(err.__traceback__)

@utils.ignore_warnings
def test_filter_by_min_should_match_any(self):
Expand Down Expand Up @@ -445,7 +433,7 @@ def test_filter_by_min_should_match_any(self):
weight_log_sim = weighted_similarity_calculator. \
WeightedSimilarityCalculator(folder=self.weights_folder)
for idx, test in enumerate(tests):
try:
with sure.ensure('Error in the test case index: {0}', idx):
_boosting_featurizer = SuggestBoostingFeaturizer(
test["elastic_results"],
test["config"],
Expand All @@ -456,6 +444,3 @@ def test_filter_by_min_should_match_any(self):
all_results,
fields=test["config"]["filter_min_should_match_any"])
self.assert_elastic_results(all_results, test)
except AssertionError as err:
raise AssertionError(f'Error in the test case number: {idx}'). \
with_traceback(err.__traceback__)
40 changes: 14 additions & 26 deletions test/boosting_decision_making/test_boosting_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import unittest

import numpy as np
import sure

from app.boosting_decision_making import defect_type_model
from app.boosting_decision_making import weighted_similarity_calculator
Expand Down Expand Up @@ -57,7 +58,7 @@ def get_default_config(
number_of_log_lines,
filter_fields=["detected_message", "stacktrace"],
filter_fields_any=[],
min_should_match=0.8):
min_should_match=0.0):
"""Get default config"""
return {
"max_query_terms": 50,
Expand Down Expand Up @@ -150,21 +151,14 @@ def test_full_data_check(self):
if self.global_defect_type_model_folder.strip():
_boosting_featurizer.set_defect_type_model(
defect_type_model.DefectTypeModel(folder=self.global_defect_type_model_folder))
try:
with sure.ensure('Error in the test case index: {0}', idx):
gathered_data, issue_type_names = _boosting_featurizer.gather_features_info()
assert utils.compare_different_types_equality(gathered_data, boost_model_results[str(idx)][0],
epsilon=self.epsilon)
predict_label, predict_probability = test["decision_maker"].predict(
gathered_data)
assert utils.compare_different_types_equality(predict_label.tolist(),
boost_model_results[str(idx)][1],
epsilon=self.epsilon)
assert utils.compare_different_types_equality(predict_probability.tolist(),
boost_model_results[str(idx)][2],
epsilon=self.epsilon)
except AssertionError as err:
raise AssertionError(f'Error in the test case number: {idx}'). \
with_traceback(err.__traceback__)
gathered_data.should.equal(boost_model_results[str(idx)][0], epsilon=self.epsilon)
predict_label.tolist().should.equal(boost_model_results[str(idx)][1], epsilon=self.epsilon)
predict_probability.tolist().should.equal(boost_model_results[str(idx)][2],
epsilon=self.epsilon)

@utils.ignore_warnings
def test_full_data_check_suggests(self):
Expand Down Expand Up @@ -248,18 +242,12 @@ def test_full_data_check_suggests(self):
if self.global_defect_type_model_folder.strip():
_boosting_featurizer.set_defect_type_model(
defect_type_model.DefectTypeModel(folder=self.global_defect_type_model_folder))
try:
with sure.ensure('Error in the test case index: {0}', idx):
gathered_data, test_item_ids = _boosting_featurizer.gather_features_info()
predict_label, predict_probability = test["decision_maker"].predict(gathered_data)
assert utils.compare_different_types_equality(gathered_data,
boost_model_results[str(idx)][0],
epsilon=self.epsilon)
assert utils.compare_different_types_equality(predict_label.tolist(),
boost_model_results[str(idx)][1],
epsilon=self.epsilon)
assert utils.compare_different_types_equality(predict_probability.tolist(),
boost_model_results[str(idx)][2],
epsilon=self.epsilon)
except AssertionError as err:
raise AssertionError(f'Error in the test case number: {idx}'). \
with_traceback(err.__traceback__)
gathered_data.should.equal(boost_model_results[str(idx)][0],
epsilon=self.epsilon)
predict_label.tolist().should.equal(boost_model_results[str(idx)][1],
epsilon=self.epsilon)
predict_probability.tolist().should.equal(boost_model_results[str(idx)][2],
epsilon=self.epsilon)
Loading

0 comments on commit c25b55e

Please sign in to comment.