From e2dae3cda1cf37cb4f6c5a4591136e3948d50b02 Mon Sep 17 00:00:00 2001 From: IanCa Date: Fri, 27 Oct 2023 18:48:09 -0500 Subject: [PATCH] Enable word cloud tests. Improve error reporter tests --- hed/errors/error_reporter.py | 17 +++++------- tests/errors/test_error_reporter.py | 38 +++++++++++++++++++++++++++ tests/tools/visualization/__init__.py | 0 3 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 tests/tools/visualization/__init__.py diff --git a/hed/errors/error_reporter.py b/hed/errors/error_reporter.py index 409656235..e67c40bc6 100644 --- a/hed/errors/error_reporter.py +++ b/hed/errors/error_reporter.py @@ -298,10 +298,9 @@ def format_error_from_context(error_type, error_context, *args, actual_error=Non if not error_func: error_object = ErrorHandler.val_error_unknown(*args, **kwargs) error_object['code'] = error_type - ErrorHandler._add_context_to_errors(error_object, error_context) - return [error_object] + else: + error_object = error_func(*args, **kwargs) - error_object = error_func(*args, **kwargs) if actual_error: error_object['code'] = actual_error @@ -321,8 +320,6 @@ def _add_context_to_errors(error_object, error_context_to_add): list: A list of dict with needed context strings added at the beginning of the list. """ - if error_object is None: - error_object = {} for (context_type, context) in error_context_to_add: error_object[context_type] = context @@ -345,11 +342,7 @@ def _get_tag_span_to_error_object(error_object): if ErrorContext.HED_STRING not in error_object: return None, None - if 'char_index' in error_object: - char_index = error_object['char_index'] - char_index_end = error_object.get('char_index_end', char_index + 1) - return char_index, char_index_end - elif 'source_tag' in error_object: + if 'source_tag' in error_object: source_tag = error_object['source_tag'] if isinstance(source_tag, int): return None, None @@ -364,7 +357,9 @@ def _get_tag_span_to_error_object(error_object): def _update_error_with_char_pos(error_object): # This part is optional as you can always generate these as needed. start, end = ErrorHandler._get_tag_span_to_error_object(error_object) - if start is not None and end is not None: + if start is not None: + # silence warning in pycharm + start = int(start) source_tag = error_object.get('source_tag', None) # Todo: Move this functionality somewhere more centralized. # If the tag has been modified from the original, don't try to use sub indexing. diff --git a/tests/errors/test_error_reporter.py b/tests/errors/test_error_reporter.py index d4482314c..9c27274e6 100644 --- a/tests/errors/test_error_reporter.py +++ b/tests/errors/test_error_reporter.py @@ -1,6 +1,7 @@ import unittest from hed.errors import ErrorHandler, ErrorContext, ErrorSeverity, ValidationErrors, SchemaWarnings, \ get_printable_issue_string, sort_issues, replace_tag_references +from hed.errors.error_reporter import hed_tag_error, get_printable_issue_string_html from hed import HedString from hed import load_schema_version @@ -33,6 +34,9 @@ def test_push_error_context(self): self.assertTrue(column_name == error_list[0][ErrorContext.COLUMN]) self.assertTrue(len(error_list) == 1) self.error_handler.reset_error_context() + self.error_handler.push_error_context(ErrorContext.ROW, None) + self.assertTrue(self.error_handler.error_context[0][1] == 0) + self.error_handler.reset_error_context() def test_pop_error_context(self): error_list = self.error_handler.format_error_with_context(ValidationErrors.TAG_NOT_UNIQUE, "") @@ -115,6 +119,18 @@ def test_printable_issue_string_with_filenames(self): self.assertTrue(len(printable_issues3) > len(printable_issues2)) self.assertEqual(printable_issues3.count(myfile), 1) + printable_issues = get_printable_issue_string_html(error_list, skip_filename=False) + self.assertTrue(len(printable_issues) > 10) + self.assertEqual(printable_issues.count(myfile), 1) + + printable_issues2 = get_printable_issue_string_html(error_list, severity=ErrorSeverity.ERROR, skip_filename=False) + self.assertTrue(len(printable_issues) > len(printable_issues2)) + self.assertEqual(printable_issues2.count(myfile), 1) + printable_issues3 = get_printable_issue_string_html(error_list, severity=ErrorSeverity.ERROR, skip_filename=False, + title="Later added custom title that is longer") + self.assertTrue(len(printable_issues3) > len(printable_issues2)) + self.assertEqual(printable_issues3.count(myfile), 1) + self.error_handler.reset_error_context() def test_sort_issues(self): @@ -160,3 +176,25 @@ def test_replace_tag_references(self): mixed = {'a': HedString('Hed1', self._schema), 'b': [2, 3, {'c': HedString('Hed2', self._schema)}, 4]} replace_tag_references(mixed) self.assertEqual(mixed, {'a': 'Hed1', 'b': [2, 3, {'c': 'Hed2'}, 4]}) + + + def test_register_error_twice(self): + test_code = "test_error_code" + @hed_tag_error(test_code) + def test_error_code(tag): + pass + + with self.assertRaises(KeyError): + @hed_tag_error(test_code) + def test_error_code(tag): + pass + + def test_format_unknown_error(self): + error_code = "Unknown error type" + error_list = self.error_handler.format_error(error_code, "param1", param2=0) + self.assertEqual(error_list[0]['code'], error_code) + + actual_code = "Actual unknown error type" + error_list = self.error_handler.format_error_from_context(error_code, self.error_handler.error_context, "param1", param2=0, + actual_error=actual_code) + self.assertEqual(error_list[0]['code'], actual_code) diff --git a/tests/tools/visualization/__init__.py b/tests/tools/visualization/__init__.py new file mode 100644 index 000000000..e69de29bb