Files
doczyai-pipelines/fieldExtraction/tests/test_tin_npi_funcs.py
T
Alex Galarce c8e64bcd4b Merged in feature/align-tin-npi (pull request #485)
Feature/align tin npi

* Add merge_provider_info function to consolidate provider data into one-to-one results

* Merge remote-tracking branch 'origin/main' into feature/align-tin-npi

* Remove debug print statement from run_one_to_one_prompts function

* turn run_regex_fields into an orchestrator function by breaking parts of it into separate functions

* Remove unused import of get_matches function and add docstring to chunk_on_matches for clarity

* Add docstring to run_regex_fields for improved clarity and documentation

* fix provider name keys in merge_provider_info

* Remove unused functions get_matches, tin_npi_prompt, and clean_tin_npi from tin_npi_funcs.py

* Set provider name to "UNKNOWN" if cleaned name is empty in clean_provider_info

* Add unit tests for clean_provider_info function to validate data processing

* Enhance docstring for get_all_matches function

* Add tests for get_all_matches and chunk_on_matches functions

* Merge remote-tracking branch 'origin/main' into feature/align-tin-npi

* Implement merge_provider_info function to consolidate provider data into one-to-one results

* Refactor merge_provider_info to simplify list conversion for group and other provider information

* Remove unnecessary TIN and NPI postprocessing step from postprocess function

* Remove clean_tin_npi_other function and its invocation from postprocess function

* Use json prompts for TIN/NPI/Name and inject them into TIN_NPI_TEMPLATE

* Rename run_regex_fields to run_provider_info_fields

* Enhance TIN_NPI_TEMPLATE to include detailed instructions for extracting provider entities and their identifying information, including IS_GROUP logic for main contracting parties.

* moved TIN/NPI regexes to regex_patterns.py

* Remove unnecessary blank line in investment_values.py

* Remove debug print statements from run_provider_info_fields and get_provider_info functions

* Enhance clean_provider_info to handle various representations of IS_GROUP as boolean

* Enhance get_provider_info to include error handling for JSON parsing and ensure consistent return format


Approved-by: Katon Minhas
2025-04-18 20:50:25 +00:00

155 lines
5.6 KiB
Python

import pytest
from src.investment.tin_npi_funcs import clean_provider_info, get_all_matches, chunk_on_matches
def test_clean_provider_info_valid_data():
provider_info = [
{"TIN": "12-3456789", "NPI": "1234567890", "NAME": "John Doe", "IS_GROUP": True},
{"TIN": "98.7654321", "NPI": "0987654321", "NAME": " Jane Smith ", "IS_GROUP": False}
]
expected_output = [
{"TIN": "123456789", "NPI": "1234567890", "NAME": "John Doe", "IS_GROUP": True},
{"TIN": "987654321", "NPI": "0987654321", "NAME": "Jane Smith", "IS_GROUP": False}
]
assert clean_provider_info(provider_info) == expected_output
def test_clean_provider_info_invalid_tin_npi():
provider_info = [
{"TIN": "12345", "NPI": "12345", "NAME": "John Doe", "IS_GROUP": True},
{"TIN": None, "NPI": None, "NAME": "Jane Smith", "IS_GROUP": False}
]
expected_output = [
{"TIN": "UNKNOWN", "NPI": "UNKNOWN", "NAME": "John Doe", "IS_GROUP": True},
{"TIN": "UNKNOWN", "NPI": "UNKNOWN", "NAME": "Jane Smith", "IS_GROUP": False}
]
assert clean_provider_info(provider_info) == expected_output
def test_clean_provider_info_missing_fields():
provider_info = [
{"TIN": "12-3456789", "NAME": "John Doe"},
{"NPI": "1234567890", "IS_GROUP": True}
]
expected_output = [
{"TIN": "123456789", "NPI": "UNKNOWN", "NAME": "John Doe", "IS_GROUP": False},
{"TIN": "UNKNOWN", "NPI": "1234567890", "NAME": "UNKNOWN", "IS_GROUP": True}
]
assert clean_provider_info(provider_info) == expected_output
def test_clean_provider_info_empty_input():
provider_info = []
expected_output = []
assert clean_provider_info(provider_info) == expected_output
def test_clean_provider_info_whitespace_name():
provider_info = [
{"TIN": "12-3456789", "NPI": "1234567890", "NAME": " ", "IS_GROUP": True}
]
expected_output = [
{"TIN": "123456789", "NPI": "1234567890", "NAME": "UNKNOWN", "IS_GROUP": True}
]
assert clean_provider_info(provider_info) == expected_output
def test_get_all_matches_single_match():
text = "The TIN is 123456789."
pattern = r"\b\d{9}\b"
filename = "test_file.txt"
expected_output = ["123456789"]
assert get_all_matches(text, pattern, filename) == expected_output
def test_get_all_matches_multiple_matches():
text = "TINs: 123456789, 987654321, and 123456789."
pattern = r"\b\d{9}\b"
filename = "test_file.txt"
expected_output = ["123456789", "987654321"]
assert sorted(get_all_matches(text, pattern, filename)) == sorted(expected_output)
def test_get_all_matches_no_matches():
text = "No valid TINs here."
pattern = r"\b\d{9}\b"
filename = "test_file.txt"
expected_output = []
assert get_all_matches(text, pattern, filename) == expected_output
def test_get_all_matches_special_characters():
text = "TINs: 123-45-6789 and 987.65.4321."
pattern = r"\b\d{3}-\d{2}-\d{4}\b|\b\d{3}\.\d{2}\.\d{4}\b"
filename = "test_file.txt"
expected_output = ["123-45-6789", "987.65.4321"]
assert sorted(get_all_matches(text, pattern, filename)) == sorted(expected_output)
def test_get_all_matches_empty_text():
text = ""
pattern = r"\b\d{9}\b"
filename = "test_file.txt"
expected_output = []
assert get_all_matches(text, pattern, filename) == expected_output
def test_get_all_matches_empty_pattern():
text = "The TIN is 123456789."
pattern = ""
filename = "test_file.txt"
expected_output = []
assert get_all_matches(text, pattern, filename) == expected_output
def test_chunk_on_matches_single_match():
matches = ["match1"]
text_dict = {
"page1": "This is a text with match1.",
"page2": "This is another text without matches."
}
expected_output = "This is a text with match1."
assert chunk_on_matches(matches, text_dict) == expected_output
def test_chunk_on_matches_multiple_matches():
matches = ["match1", "match2"]
text_dict = {
"page1": "This is a text with match1.",
"page2": "This is another text with match2.",
"page3": "No matches here."
}
expected_output = "This is a text with match1.\nThis is another text with match2."
assert chunk_on_matches(matches, text_dict) == expected_output
def test_chunk_on_matches_no_matches():
matches = ["match1"]
text_dict = {
"page1": "No relevant text here.",
"page2": "Still no matches."
}
expected_output = ""
assert chunk_on_matches(matches, text_dict) == expected_output
def test_chunk_on_matches_empty_matches():
matches = []
text_dict = {
"page1": "This is some text.",
"page2": "This is more text."
}
expected_output = ""
assert chunk_on_matches(matches, text_dict) == expected_output
def test_chunk_on_matches_empty_text_dict():
matches = ["match1"]
text_dict = {}
expected_output = ""
assert chunk_on_matches(matches, text_dict) == expected_output
def test_chunk_on_matches_partial_match():
matches = ["match"]
text_dict = {
"page1": "This is a text with match1.",
"page2": "This is another text with match2.",
"page3": "But this page you won't find one."
}
expected_output = "This is a text with match1.\nThis is another text with match2."
assert chunk_on_matches(matches, text_dict) == expected_output
def test_chunk_on_matches_whitespace_handling():
matches = ["match1"]
text_dict = {
"page1": " This is a text with match1. ",
"page2": "This is another text without matches."
}
expected_output = " This is a text with match1. "
assert chunk_on_matches(matches, text_dict) == expected_output