Skip to content

Commit

Permalink
Fixed exception handling and changed log level
Browse files Browse the repository at this point in the history
  • Loading branch information
adityabharadwaj198 committed Dec 3, 2024
1 parent a834c9f commit 44d37f9
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def setUpClass(cls) -> None:
formatter = logging.Formatter('%(asctime)s | %(levelname)s | %(filename)s:%(lineno)d | %(message)s')
handler.setFormatter(formatter)
cls.logger.addHandler(handler)
cls.logger.setLevel(logging.DEBUG)
cls.logger.setLevel(logging.INFO)

@classmethod
def get_results_file_path(cls):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

def get_logger(name):
logging.basicConfig(format='%(asctime)s | %(levelname)s | %(filename)s:%(lineno)d | %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.DEBUG)
logging.basicConfig(format='%(asctime)s | %(levelname)s | %(filename)s:%(lineno)d | %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.INFO)
logger = logging.getLogger(name)

return logger
28 changes: 2 additions & 26 deletions tests/compatibility_tests/compatibility_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,16 @@ def load_all_subclasses(package_name):
Args:
package_name (str): The top-level package name to search for subclasses.
"""
# package_path = os.path.join(*package_name.split('.'))
# logger.debug(f"package_path = {package_path}")
# for root, dirs, files in os.walk(f"{package_path}"):
# if root.__contains__("venv"): #Skip the directory if it's a venv.
# continue
# logger.debug(f"root, dirs, files = {root}, {dirs}, {files}")
# logger.debug(f" root = {root}")
# for file in files:
# logger.debug(f"file = {file}")
# if file.endswith(".py") and not file.startswith("__init__"):
# full_package_name = root.replace("/", ".")
# logger.debug(f"full_package_name = {full_package_name}")
# module_name = file.split('.')[0]
# logger.debug(f"Importing this {full_package_name}.{module_name}")
# try:
# importlib.import_module(f"{full_package_name}.{module_name}")
# except ImportError as e:
# logger.error(f"Could not import {module_name}: {e}")
package = importlib.import_module(package_name)
for _, name, is_pkg in pkgutil.walk_packages(package.__path__, f"{package_name}."):
logger.debug(f"Processing this {name}, {is_pkg}")
if is_pkg:
continue
try:
importlib.import_module(name)
logger.debug(f"Imported the module with name {name}")
logger.debug(f"Imported module with name {name}")
except ImportError as e:
logger.debug(f"Could not import module with {name}")
logger.error(f"Could not import {name}: {e}")
logger.error(f"Could not import module with {name}")



Expand Down Expand Up @@ -440,14 +421,9 @@ def run_prepare_mode(version_to_test_against: str):

version_to_test_against = semver.VersionInfo.parse(version_to_test_against)
load_all_subclasses("tests.compatibility_tests")
# load_all_subclasses("tests.compatibility_tests.add_or_replace_documents")
# load_all_subclasses("tests.compatibility_tests.create_index")
# Get all subclasses of `BaseCompatibilityTestCase` that match the `version_to_test_against` criterion
# The below condition also checks if the test class is not marked to be skipped
logger.debug(f"Printing all subclasses {BaseCompatibilityTestCase.__subclasses__()}")
for test_class in BaseCompatibilityTestCase.__subclasses__():
logger.info(f"=========================================================")
logger.debug(f"Test class {test_class.__name__}")
markers = getattr(test_class, "pytestmark", [])
# Check for specific markers
marqo_version_marker = next( # Checks what version a compatibility test is marked with (ex: @pytest.mark.marqo_version('2.11.0')). If no version is marked, it will skip the test
Expand Down
7 changes: 5 additions & 2 deletions tests/compatibility_tests/create_index/test_create_index.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from sys import exc_info

import pytest

from tests.compatibility_tests.base_test_case.base_compatibility_test import BaseCompatibilityTestCase
Expand All @@ -19,14 +21,15 @@ def prepare(self):
all_results[self.index_name] = self.client.index(self.index_name).get_settings()
self.save_results_to_file(all_results)
except Exception as e:
raise Exception(f"Exception when creating index with name {self.index_name}")
raise Exception(f"Exception when creating index with name {self.index_name}") from e

def test_expected_settings(self):
try:
expected_settings = self.load_results_from_file()
actual_settings = self.client.index(self.index_name).get_settings()
except Exception as e:
raise Exception(f"Exception when getting index settings for index {self.index_name}")
raise Exception(f"Exception when getting index settings for index {self.index_name}") from e

self.logger.debug(f"Expected settings: {expected_settings}")
self.logger.debug(f"Actual settings: {actual_settings}")
self.assertEqual(expected_settings[self.index_name], actual_settings, f"Index settings do not match expected settings, expected {expected_settings}, but got {actual_settings}")
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,5 @@ def test_expected_settings(self):
self.logger.debug(f"Printing actual_settings {actual_settings}")
self.logger.debug(f"Printing expected_settings {expected_settings.get(index_name)}")
except Exception as e:
self.logger.error(f"Exception while getting index settings {e}")
raise e
raise Exception(f"Exception when getting index settings for index {index_name}") from e
self.assertEqual(expected_settings.get(index_name), actual_settings, f"Index settings do not match expected settings, expected {expected_settings}, but got {actual_settings}")
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def prepare(self):

self.logger.debug(f"Finished running prepare method for test case: {self.__class__.__name__}")
except Exception as e:
raise e
raise Exception(f"Exception occurred while adding documents to indexes {e}") from e

def test_delete_document(self):
self.logger.info(f"Running test_delete_document on {self.__class__.__name__}")
Expand Down
3 changes: 1 addition & 2 deletions tests/compatibility_tests/embed/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def prepare(self):
self.logger.debug(f"Ran prepare method for {self.indexes_to_test_on} inside test class {self.__class__.__name__}")
self.save_results_to_file(all_results)
except Exception as e: #TODO: This was called out as an antipattern last time - (logging & raising - fix it)
self.logger.error(f"Exception occurred while embedding documents {e}")
raise e
raise Exception(f"Exception occurred while embedding documents") from e

def test_embed(self):
self.logger.info(f"Running test_embed on {self.__class__.__name__}")
Expand Down
3 changes: 1 addition & 2 deletions tests/compatibility_tests/recommend/test_recommend.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def prepare(self):
tensor_fields=self.tensor_fields)
self.logger.debug(f'Ran prepare method for {self.indexes_to_test_on} inside test class {self.__class__.__name__}')
except Exception as e:
self.logger.error(f"Exception occurred while adding documents {e}")
raise e
raise Exception(f"Exception occurred while adding documents") from e

all_results = {}
# Loop through queries, search methods, and result keys to populate unstructured_results
Expand Down
5 changes: 2 additions & 3 deletions tests/compatibility_tests/search/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ def prepare(self):
all_results[index_name][result_key] = result
self.save_results_to_file(all_results)

except Exception as e: #TODO: This was called out as an antipattern last time - (logging & raising - fix it)
self.logger.error(f"Exception occurred while adding documents {e}", exc_info=True)
raise e
except Exception as e:
raise Exception(f"Exception occurred while running prepare method") from e


# store the result of search across all structured & unstructured indexes
Expand Down
3 changes: 1 addition & 2 deletions tests/compatibility_tests/test_vector_normalisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def prepare(self):
self.save_results_to_file(result)
self.logger.debug(f'Ran prepare mode test for {self.text_index_with_normalize_embeddings_true} inside test class {self.__class__.__name__}')
except Exception as e:
self.logger.error(f"Exception occurred while adding documents {e}")
raise e
raise Exception(f"Eception occurred while adding documents to index {self.text_index_with_normalize_embeddings_true}") from e

def test_custom_vector_doc_in_normalized_embedding_true(self):
# This runs on to_version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def prepare(self):
self.client.index(index_name = index['indexName']).add_documents(documents = self.text_docs, mappings = self.mappings, tensor_fields = self.tensor_fields)

except Exception as e:
raise e
raise Exception(f"Exception occurred while adding documents to index") from e

def test_update_doc(self):
self.logger.info(f"Running test_update_doc on {self.__class__.__name__}")
Expand Down

0 comments on commit 44d37f9

Please sign in to comment.