Files
doczyai-pipelines/src/tests/test_fieldset.py
T
Katon Minhas afb6d5185d Merged in feature/lesser-table-caching-refactor-hybrid (pull request #847)
Feature/lesser table caching refactor hybrid

* chore: Remove unused duplicate main.py from shared pipeline

* fix: Correct crosswalk paths in aarete_derived.py

* chore: Remove unused documentation files from fieldExtraction

* docs: Add documentation files to documentation folder

* docs: Update README with uv setup, expanded project structure, and branching conventions

* docs: Add uv installation steps with Ubuntu/WSL emphasis

* Enable prompt caching for all remaining LLM calls

- Add _INSTRUCTION() functions for: EXHIBIT_HEADER, EXHIBIT_LINKAGE,
  EXHIBIT_TITLE_MATCH, DATE_FIX, DERIVED_TERM_DATE, CHECK_PROVIDER_NAME_MATCH,
  SPECIAL_CASE_ASSIGNMENT
- Update all invoke_claude() calls in saas and clover pipelines to use
  cache=True with corresponding _INSTRUCTION() functions
- Add new instructions to get_cacheable_instructions() for cache warming
- Update tests for new instruction functions

Functions now using caching:
- prompt_exhibit_level
- prompt_exhibit_lesser (EXHIBIT_LEVEL_LESSER_OF)
- prompt_fee_schedule_breakout
- prompt_grouper_breakout
- prompt_special_case_assignment
- prompt_exhibit_linkage
- prompt_exhibit_header
- prompt_smart_chunked (ONE_TO_ONE templates)
- prompt_date_fix
- prompt_derived_term_date
- prompt_exhibit_title_match
- provider_name_match_check

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Reorder

* feat: Add bcbs_promise client pipeline with OFFSET_TERM extraction

- Add new bcbs_promise client with HSC-based OFFSET_TERM field extraction
- Extract full paragraph text of offset/recoupment provisions from contracts
- Derive OFFSET_INDICATOR (Y/N) from OFFSET_TERM presence
- Fix reorder_columns to preserve extra columns not in COLUMN_ORDER
- Update QC/QA output path to outputs/qc_qa/

* fix: Update dev deps and test assertions for QC/QA output path

- Add pytest/pytest-mock to dev dependencies for mypy type checking
- Update test assertions to expect outputs/qc_qa instead of qa_qc_output

* style: Apply black formatting to prompt_templates.py

* Merge main, move scripts

* Archive some scripts

* update py version

* remove .py version file

* Remove ASCII characters

* Restore testbed code

* restore tracking

* Update testbed metrics

* Enable prompt caching for CODE_LAST_CHECK, FILL_BILL_TYPE, DUAL_LOB_CHECK, and GROUPER_BREAKOUT

- Add CODE_LAST_CHECK_INSTRUCTION() for service specificity classification
- Add FILL_BILL_TYPE_INSTRUCTION() for bill type code determination
- Add DUAL_LOB_CHECK_INSTRUCTION() for Medicare/Medicaid classification
- Update code_funcs.py to use caching for CODE_LAST_CHECK, FILL_BILL_TYPE, GROUPER_BREAKOUT
- Update postprocessing_funcs.py to use caching for DUAL_LOB_CHECK
- Add new instructions to get_cacheable_instructions() for cache warming
- Add unit tests for new instruction functions

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Fix postprocessing_funcs to remove invalid columns

* Merge branch 'main' into feature/lesser-table-caching-refactor-hybrid

* Revert prompt caching changes from aed1b73c

* update formatting

* Update imports


Approved-by: Sha Brown
Approved-by: Praneel Panchigar
2026-01-26 16:52:55 +00:00

449 lines
17 KiB
Python

import json
import os
import unittest
from unittest.mock import mock_open, patch
from src.constants.constants import Constants
from src.prompts.fieldset import Field, FieldSet, _cache_lock, _field_data_cache
class TestFieldWithRealConstants(unittest.TestCase):
"""Tests for the Field class using real Constants values."""
def setUp(self):
"""Set up test fixtures before each test method."""
# Create test field with a real VALID_BILL_TYPE placeholder
self.test_field_dict = {
"field_name": "TEST_FIELD",
"relationship": "one-to-one",
"field_type": "string",
"prompt": "What is the test field? Valid values: {valid_values}",
"valid_values": "VALID_BILL_TYPE",
"base_field": "base_test_field",
"crosswalk": "test_crosswalk",
"keywords": ["test", "field"],
"methodology": "LLM",
"case_sensitive": True,
"regex_pattern": r"\d+",
"format": "text",
"allow_na": False,
}
self.field = Field(self.test_field_dict)
# Create a real Constants object - this will load actual mappings
self.constants = Constants()
def test_init(self):
"""Test Field initialization with all parameters."""
# Test all fields are correctly set
self.assertEqual(self.field.field_name, "TEST_FIELD")
self.assertEqual(self.field.relationship, "one-to-one")
self.assertEqual(self.field.field_type, "string")
self.assertEqual(
self.field.prompt, "What is the test field? Valid values: {valid_values}"
)
self.assertEqual(self.field.valid_values, "VALID_BILL_TYPE")
self.assertEqual(self.field.base_field, "base_test_field")
self.assertEqual(self.field.crosswalk, "test_crosswalk")
self.assertEqual(self.field.keywords, ["test", "field"])
self.assertEqual(self.field.methodology, "LLM")
self.assertTrue(self.field.case_sensitive)
self.assertEqual(self.field.regex_pattern, r"\d+")
self.assertEqual(self.field.format, "text")
self.assertFalse(self.field.allow_na)
self.assertIsNone(self.field.retrieved_text)
def test_init_minimal(self):
"""Test Field initialization with minimal parameters."""
minimal_field_dict = {
"field_name": "MINIMAL_FIELD",
"relationship": "one-to-one",
"field_type": "string",
}
field = Field(minimal_field_dict)
# Check required fields
self.assertEqual(field.field_name, "MINIMAL_FIELD")
self.assertEqual(field.relationship, "one-to-one")
self.assertEqual(field.field_type, "string")
# Check defaults for optional fields
self.assertIsNone(field.prompt)
self.assertIsNone(field.valid_values)
self.assertIsNone(field.base_field)
self.assertIsNone(field.crosswalk)
self.assertIsNone(field.keywords)
self.assertIsNone(field.methodology)
self.assertFalse(field.case_sensitive)
self.assertIsNone(field.regex_pattern)
self.assertIsNone(field.format)
self.assertTrue(field.allow_na)
def test_repr(self):
"""Test the string representation of a Field object."""
self.assertEqual(repr(self.field), "TEST_FIELD")
@patch(
"builtins.open",
new_callable=mock_open,
read_data=json.dumps(
[
{
"field_name": "FIELD1",
"relationship": "one-to-one",
"field_type": "string",
},
{
"field_name": "FIELD2",
"relationship": "one-to-many",
"field_type": "number",
},
]
),
)
def test_load_from_file(self, mock_file):
"""Test loading a Field object from a file."""
field = Field.load_from_file("dummy_path.json", "FIELD2")
self.assertEqual(field.field_name, "FIELD2")
self.assertEqual(field.relationship, "one-to-many")
self.assertEqual(field.field_type, "number")
def test_from_values(self):
"""Test creating a Field using the from_values class method."""
field = Field.from_values(
field_name="TEST_FIELD2",
relationship="one-to-many",
field_type="number",
prompt="Test prompt",
)
self.assertEqual(field.field_name, "TEST_FIELD2")
self.assertEqual(field.relationship, "one-to-many")
self.assertEqual(field.field_type, "number")
self.assertEqual(field.prompt, "Test prompt")
def test_to_dict(self):
"""Test converting a Field to a dictionary."""
field_dict = self.field.to_dict()
self.assertEqual(field_dict["field_name"], "TEST_FIELD")
self.assertEqual(field_dict["relationship"], "one-to-one")
self.assertEqual(field_dict["field_type"], "string")
self.assertEqual(
field_dict["prompt"], "What is the test field? Valid values: {valid_values}"
)
self.assertEqual(field_dict["valid_values"], "VALID_BILL_TYPE")
self.assertEqual(field_dict["base_field"], "base_test_field")
self.assertEqual(field_dict["keywords"], ["test", "field"])
self.assertEqual(field_dict["methodology"], "LLM")
self.assertTrue(field_dict["case_sensitive"])
self.assertEqual(field_dict["regex_pattern"], r"\d+")
self.assertEqual(field_dict["format"], "text")
@patch("builtins.print")
def test_print(self, mock_print):
"""Test printing a Field object."""
self.field.print()
# Verify print was called the expected number of times
self.assertEqual(mock_print.call_count, 9)
def test_get_prompt_dict_with_real_constants(self):
"""Test getting a prompt dictionary from a Field with real Constants."""
prompt_dict = self.field.get_prompt_dict(self.constants)
# Get the actual list of bill types from Constants
bill_types = self.constants.VALID_BILL_TYPE
# Verify the prompt contains the actual bill types from Constants
prompt = prompt_dict["TEST_FIELD"]
self.assertTrue(prompt.startswith("What is the test field? Valid values: "))
# Check if the output contains the real values
for bill_type in bill_types[:3]: # Check first few to avoid overly long test
self.assertIn(bill_type, prompt)
def test_get_prompt_no_valid_values(self):
"""Test getting a prompt with no valid_values to resolve."""
field = Field(
{
"field_name": "SIMPLE_FIELD",
"relationship": "one-to-one",
"field_type": "string",
"prompt": "What is the simple field?",
}
)
prompt = field.get_prompt()
self.assertEqual(prompt, "What is the simple field?")
def test_get_prompt_with_real_constants(self):
"""Test resolving valid values in a prompt using real Constants."""
prompt = self.field.get_prompt(self.constants)
# Check that the prompt includes the actual bill types
self.assertTrue(prompt.startswith("What is the test field? Valid values: "))
# Verify that the real constants values are used
bill_types = self.constants.VALID_BILL_TYPE
bill_types_str = str(bill_types)
self.assertIn(bill_types_str, prompt)
def test_update_valid_values(self):
"""Test updating valid values for a Field."""
self.field.update_valid_values("NEW_VALID_VALUES")
self.assertEqual(self.field.valid_values, "NEW_VALID_VALUES")
class TestFieldSetWithRealConstants(unittest.TestCase):
"""Tests for the FieldSet class using real Constants values."""
def setUp(self):
"""Set up test fixtures before each test method."""
# Create fields with real Constants placeholders
self.test_fields = [
Field(
{
"field_name": "FIELD1",
"relationship": "one-to-one",
"field_type": "string",
"prompt": "What is field 1?",
}
),
Field(
{
"field_name": "FIELD2",
"relationship": "one-to-many",
"field_type": "number",
"prompt": "What is field 2? Valid values: {valid_values}",
"valid_values": "VALID_BILL_TYPE",
}
),
Field(
{
"field_name": "FIELD3",
"relationship": "one-to-one",
"field_type": "string",
"prompt": "What is field 3? Valid values: {valid_values}",
"valid_values": "VALID_LOBS",
}
),
]
self.fieldset = FieldSet(fields=self.test_fields)
# Create a real Constants object
self.constants = Constants()
@patch(
"builtins.open",
new_callable=mock_open,
read_data=json.dumps(
[
{
"field_name": "FIELD1",
"relationship": "one-to-one",
"field_type": "string",
},
{
"field_name": "FIELD2",
"relationship": "one-to-many",
"field_type": "number",
},
{
"field_name": "FIELD3",
"relationship": "one-to-one",
"field_type": "boolean",
},
]
),
)
def test_init_with_file(self, mock_file):
"""Test FieldSet initialization with a file."""
# Clear the cache so that our mock will be used
with _cache_lock:
_field_data_cache.clear()
fieldset = FieldSet(file_path="dummy_path.json")
self.assertEqual(len(fieldset.fields), 3)
self.assertEqual(fieldset.fields[0].field_name, "FIELD1")
self.assertEqual(fieldset.fields[1].field_name, "FIELD2")
self.assertEqual(fieldset.fields[2].field_name, "FIELD3")
def test_add_field(self):
"""Test adding a field to a FieldSet."""
new_field = Field(
{
"field_name": "FIELD4",
"relationship": "one-to-one",
"field_type": "string",
}
)
self.fieldset.add_field(new_field)
self.assertEqual(len(self.fieldset.fields), 4)
self.assertEqual(self.fieldset.fields[3].field_name, "FIELD4")
# Test adding a duplicate field (should not add)
duplicate_field = Field(
{
"field_name": "FIELD1",
"relationship": "one-to-one",
"field_type": "string",
}
)
self.fieldset.add_field(duplicate_field)
self.assertEqual(len(self.fieldset.fields), 4) # No change in length
def test_get_field(self):
"""Test getting a field by name."""
field = self.fieldset.get_field("FIELD2")
self.assertEqual(field.field_name, "FIELD2")
self.assertEqual(field.relationship, "one-to-many")
# Test getting a field that doesn't exist
with self.assertRaises(ValueError):
self.fieldset.get_field("NONEXISTENT_FIELD")
def test_list_fields(self):
"""Test listing all field names."""
field_names = self.fieldset.list_fields()
self.assertEqual(field_names, ["FIELD1", "FIELD2", "FIELD3"])
def test_remove_field(self):
"""Test removing a field."""
self.fieldset.remove_field("FIELD2")
self.assertEqual(len(self.fieldset.fields), 2)
self.assertEqual(self.fieldset.fields[0].field_name, "FIELD1")
self.assertEqual(self.fieldset.fields[1].field_name, "FIELD3")
def test_update_field_attr(self):
"""Test updating a field attribute."""
self.fieldset.update_field_attr("FIELD1", "prompt", "Updated prompt")
self.assertEqual(self.fieldset.get_field("FIELD1").prompt, "Updated prompt")
def test_get_prompt_dict_with_real_constants(self):
"""Test getting prompt dictionaries with real Constants values."""
prompt_dict = self.fieldset.get_prompt_dict(self.constants)
self.assertEqual(len(prompt_dict), 3)
self.assertEqual(prompt_dict["FIELD1"], "What is field 1?")
# Check that FIELD2 has the actual bill types
self.assertTrue(
prompt_dict["FIELD2"].startswith("What is field 2? Valid values: ")
)
for bill_type in self.constants.VALID_BILL_TYPE[:3]: # Check first few
self.assertIn(bill_type, str(prompt_dict["FIELD2"]))
# Check that FIELD3 has the actual LOBs
self.assertTrue(
prompt_dict["FIELD3"].startswith("What is field 3? Valid values: ")
)
for lob in self.constants.VALID_LOBS[:3]: # Check first few
self.assertIn(lob, str(prompt_dict["FIELD3"]))
# Test filtering by field_type
prompt_dict = self.fieldset.get_prompt_dict(self.constants, field_type="string")
self.assertEqual(len(prompt_dict), 2)
self.assertEqual(prompt_dict["FIELD1"], "What is field 1?")
self.assertIn("FIELD3", prompt_dict)
# Test filtering by relationship
prompt_dict = self.fieldset.get_prompt_dict(
self.constants, relationship="one-to-many"
)
self.assertEqual(len(prompt_dict), 1)
self.assertIn("FIELD2", prompt_dict)
def test_print_prompt_dict_with_real_constants(self):
"""Test printing all prompts as a string with real Constants."""
prompt_str = self.fieldset.print_prompt_dict(self.constants)
# Verify each field is in the output
self.assertIn("FIELD1 : What is field 1?", prompt_str)
self.assertIn("FIELD2 : What is field 2? Valid values: ", prompt_str)
self.assertIn("FIELD3 : What is field 3? Valid values: ", prompt_str)
# Verify real constants values are used
for bill_type in self.constants.VALID_BILL_TYPE[:2]: # Check first couple
self.assertIn(bill_type, prompt_str)
for lob in self.constants.VALID_LOBS[:2]: # Check first couple
self.assertIn(lob, prompt_str)
@patch("builtins.print")
def test_print(self, mock_print):
"""Test printing all fields."""
self.fieldset.print()
# Each field calls print multiple times
self.assertTrue(mock_print.call_count > 20)
def test_repr(self):
"""Test the string representation of a FieldSet."""
self.assertEqual(repr(self.fieldset), "['FIELD1', 'FIELD2', 'FIELD3']")
# Test empty FieldSet
empty_fieldset = FieldSet()
self.assertEqual(repr(empty_fieldset), "FieldSet: No fields loaded")
@patch("builtins.print")
def test_print_fields(self, mock_print):
"""Test printing field names."""
self.fieldset.print_fields()
mock_print.assert_called_with(["FIELD1", "FIELD2", "FIELD3"])
def test_filter(self):
"""Test filtering fields based on criteria."""
# Filter by relationship
filtered = self.fieldset.filter(relationship="one-to-one")
self.assertEqual(len(filtered.fields), 2)
self.assertEqual(filtered.fields[0].field_name, "FIELD1")
self.assertEqual(filtered.fields[1].field_name, "FIELD3")
# Filter by field_type
filtered = self.fieldset.filter(field_type="number")
self.assertEqual(len(filtered.fields), 1)
self.assertEqual(filtered.fields[0].field_name, "FIELD2")
def test_contains_fields(self):
"""Test checking if FieldSet contains fields."""
self.assertTrue(self.fieldset.contains_fields())
# Test empty FieldSet
empty_fieldset = FieldSet()
self.assertFalse(empty_fieldset.contains_fields())
def test_combine(self):
"""Test combining FieldSets."""
other_fields = [
Field(
{
"field_name": "FIELD4",
"relationship": "one-to-one",
"field_type": "string",
}
),
Field(
{
"field_name": "FIELD1", # Duplicate, should not be added
"relationship": "one-to-one",
"field_type": "string",
}
),
]
other_fieldset = FieldSet(fields=other_fields)
# Test combine with new FieldSet (not in-place)
combined = self.fieldset.combine(other_fieldset)
self.assertEqual(len(combined.fields), 4)
self.assertEqual(combined.fields[0].field_name, "FIELD1")
self.assertEqual(combined.fields[1].field_name, "FIELD2")
self.assertEqual(combined.fields[2].field_name, "FIELD3")
self.assertEqual(combined.fields[3].field_name, "FIELD4")
# Original should be unchanged
self.assertEqual(len(self.fieldset.fields), 3)
# Test combine in-place
self.fieldset.combine(other_fieldset, inplace=True)
self.assertEqual(len(self.fieldset.fields), 4)
self.assertEqual(self.fieldset.fields[3].field_name, "FIELD4")
if __name__ == "__main__":
unittest.main()