def test_writing_reading_test_data(): column_names = ('text', 'input_text_languages_str', 'input_int1_int', 'input_arg_bool', 'expected1', 'expected2') texts = (None, 'text2', 'text3', '###text4') values = ((('l1', '11', 'False', 'e11', 'e12'), ), (('l2', '22', 'True', 'e21', 'e22'), ), (('l3', '33', 'False', 'e31', 'e32'), ('l3', '33', 'False', 'e31', 'e32'), 'string'), ()) file_name = None try: file_name = lexnlp_tests.write_test_data_text_and_tuple( texts, values, column_names) actual = [ (i, text, input_args, expected) for i, text, input_args, expected in lexnlp_tests.iter_test_data_text_and_tuple(call_stack_offset=1) ] a2 = actual[0] e2 = (2, 'text2', { 'text_languages': 'l2', 'arg': True, 'int1': 22 }, [('e21', 'e22')]) print('Actual: {0}'.format(a2)) print('Expected: {0}'.format(e2)) assert_equals(a2[0], e2[0]) assert_equals(a2[1], e2[1]) assert_dict_equal(a2[2], e2[2]) assert_equals(len(a2[3]), len(e2[3])) assert_tuple_equal(a2[3][0], e2[3][0]) finally: if file_name: os.remove(file_name)
def test_get_regulations(): """ Test default get regulations behavior. :return: """ lexnlp_tests.test_extraction_func_on_test_data(get_regulations, expected_data_converter=lambda d: [(reg_type, reg_code) for reg_type, reg_code, _reg_str in d], return_source=False, as_dict=False) lexnlp_tests.test_extraction_func_on_test_data(get_regulations, expected_data_converter=lambda d: [(reg_type, reg_code, reg_str) for reg_type, reg_code, reg_str in d], return_source=True, as_dict=False) # TODO Impl test_extraction_func_on_test_data() comparing lists of dicts for (_i, text, _input_args, expected) in lexnlp_tests.iter_test_data_text_and_tuple(): expected_no_source_dict = [{'regulation_type': reg_type, 'regulation_code': reg_code} for reg_type, reg_code, _reg_str in expected] expected_source_dict = [{'regulation_type': reg_type, 'regulation_code': reg_code, 'regulation_str': reg_str} for reg_type, reg_code, reg_str in expected] assert_list_equal( list(lexnlp_tests.benchmark_extraction_func(get_regulations, text, return_source=False, as_dict=True)), expected_no_source_dict) assert_list_equal( list(lexnlp_tests.benchmark_extraction_func(get_regulations, text, return_source=True, as_dict=True)), expected_source_dict)
def test_page_examples(): for (_i, text, _input_args, expected) in lexnlp_tests.iter_test_data_text_and_tuple(): def remove_whitespace(r): return r.replace(" ", "").replace("\n", "").replace("\r", "").replace("\t", "") # Get list of pages page_list = list(lexnlp_tests.benchmark_extraction_func(get_pages, text)) assert len(page_list) == len(expected) clean_result = [remove_whitespace(p) for p in expected] for page in page_list: assert remove_whitespace(page) in clean_result
def test_get_regulations_csv(self): """ Test default get regulations behavior. :return: """ test_data_path = os.path.join( lexnlp_test_path, 'lexnlp/extract/en/tests/test_regulations/test_get_regulations.csv' ) lexnlp_tests.test_extraction_func_on_test_data( get_regulations, expected_data_converter=lambda d: [ (reg_type, reg_code) for reg_type, reg_code, _reg_str in d ], return_source=False, as_dict=False, test_data_path=test_data_path) lexnlp_tests.test_extraction_func_on_test_data( get_regulations, expected_data_converter=lambda d: [(reg_type, reg_code, reg_str) for reg_type, reg_code, reg_str in d], return_source=True, as_dict=False, test_data_path=test_data_path) cmp = DictionaryComparer(check_order=True) errors = [] for (i, text, _input_args, expected) in \ lexnlp_tests.iter_test_data_text_and_tuple(file_name=test_data_path): expected = [{ 'regulation_type': reg_type, 'regulation_code': reg_code, 'regulation_text': reg_str } for reg_type, reg_code, reg_str in expected] actual = list( lexnlp_tests.benchmark_extraction_func(get_regulations, text, return_source=True, as_dict=True)) line_errors = cmp.compare_list_of_dicts(expected, actual) if line_errors: line_errors_str = '\n'.join(line_errors) errors.append(f'Regulation tests, line [{i + 1}] errors:\n' + line_errors_str) if errors: raise Exception('\n\n'.join(errors))
def test_page_examples(self): file_path = os.path.join(self.TEST_PATH, 'test_page_examples.csv') for (_i, text, _input_args, expected) in lexnlp_tests.iter_test_data_text_and_tuple( file_name=file_path): def remove_blankspace(r): return r.replace(" ", "").replace("\n", "").replace("\r", "").replace("\t", "") # Get list of pages page_list = list( lexnlp_tests.benchmark_extraction_func(get_pages, text)) assert len(page_list) == len(expected) clean_result = [remove_blankspace(p) for p in expected] for page in page_list: assert remove_blankspace(page) in clean_result
def test_get_citations(): """ Test default get citation behavior. :return: """ for (_i, text, _input_args, expected) in lexnlp_tests.iter_test_data_text_and_tuple(): expected = [(int(volume) if volume else None, reporter, reporter_full_name, int(page) if page else None, page2, court, int(year) if year else None, source_text) for volume, reporter, reporter_full_name, page, page2, court, year, source_text in expected] expected_without_sources = [v[:-1] for v in expected] expected_with_sources = expected lexnlp_tests.test_extraction_func(expected_without_sources, get_citations, text, return_source=False) lexnlp_tests.test_extraction_func(expected_with_sources, get_citations, text, return_source=True)
def test_paragraph_examples(): for (_i, text, _input_args, expected) in lexnlp_tests.iter_test_data_text_and_tuple(): run_paragraph_test(text, expected)
def test_paragraph_examples(self): file_path = os.path.join(self.TEST_PATH, 'test_paragraph_examples.csv') for (_i, text, _input_args, expected) in lexnlp_tests.iter_test_data_text_and_tuple( file_name=file_path): self.run_paragraph_test(text, expected)