bmc_hub/tests/test_workflow_dismissed_keywords.py

56 lines
2.3 KiB
Python

"""Manual dismissal wins over keyword rediscovery, including case time entries."""
import pytest
from app.workflows.classification import enrich_entity_context, apply_entity_keyword_matches
class Cursor:
def __init__(self, dismissed):
self.dismissed = dismissed
self.queries = []
self.rows = []
def execute(self, sql, args=None):
self.queries.append((sql, args))
if 'SELECT id,parent_id' in sql:
self.rows = [{'id': 1, 'parent_id': None}, {'id': 2, 'parent_id': 1}]
elif 'workflow_tag_keywords' in sql:
self.rows = [{'tag_id': 2, 'alternatives': ['ABA']}]
elif "source='manual_dismissal'" in sql:
self.rows = [{'tag_id': tag} for tag in self.dismissed]
elif 'SELECT t.id,t.tag_group_id' in sql:
self.rows = []
else:
self.rows = []
def fetchall(self):
return self.rows
@pytest.mark.parametrize('entity_type,entity_id', [('case', 20), ('time_entry', 30)])
def test_dismissed_keyword_not_reintroduced(entity_type, entity_id):
cursor = Cursor({2})
context = {'case_id': 20, 'entity_type': entity_type, 'description': 'ABA service', 'concrete_tag_ids': []}
result = enrich_entity_context(cursor, context, entity_type, entity_id)
apply_entity_keyword_matches(cursor, entity_type, entity_id, result)
assert result['keyword_matches'] == []
assert result['tag_ids'] == []
assert not any('INSERT INTO entity_tags' in sql for sql, _ in cursor.queries)
dismissal_args = next(args for sql, args in cursor.queries if "source='manual_dismissal'" in sql)
assert dismissal_args == (entity_type, entity_id, 20)
def test_explicit_manual_readdition_still_counts_for_billing():
cursor = Cursor({2})
result = enrich_entity_context(cursor, {'case_id': 20, 'entity_type': 'case', 'subject': 'ABA service', 'concrete_tag_ids': [2]}, 'case', 20)
assert result['keyword_matches'] == []
assert result['concrete_tag_ids'] == [2]
assert result['tag_ids'] == [1, 2]
def test_undismissed_keyword_still_added():
cursor = Cursor(set())
result = enrich_entity_context(cursor, {'case_id': 20, 'entity_type': 'case', 'subject': 'ABA service', 'concrete_tag_ids': []}, 'case', 20)
assert result['keyword_matches'][0]['tag_id'] == 2
assert result['tag_ids'] == [1, 2]