diff --git a/.coveragerc b/.coveragerc index 6dd2b310..1a943292 100644 --- a/.coveragerc +++ b/.coveragerc @@ -3,4 +3,4 @@ omit = tests/* vendor_loads_migration/* libsys_airflow/plugins/folio/encumbrances/fix_encumbrances.py - \ No newline at end of file + libsys_airflow/dags/* \ No newline at end of file diff --git a/libsys_airflow/dags/folio_finance/lane_fix_encumbrances.py b/libsys_airflow/dags/folio_finance/lane_fix_encumbrances.py index b57e20e4..3b13253e 100644 --- a/libsys_airflow/dags/folio_finance/lane_fix_encumbrances.py +++ b/libsys_airflow/dags/folio_finance/lane_fix_encumbrances.py @@ -67,7 +67,7 @@ trigger_rule='all_done', op_kwargs={ "log_file": "{{ ti.xcom_pull('run_fix_encumbrances_script') }}", - "library": FY_CODE, + "fy_code": FY_CODE, }, ) diff --git a/libsys_airflow/dags/folio_finance/law_fix_encumbrances.py b/libsys_airflow/dags/folio_finance/law_fix_encumbrances.py index da2f8d8a..64c27d2b 100644 --- a/libsys_airflow/dags/folio_finance/law_fix_encumbrances.py +++ b/libsys_airflow/dags/folio_finance/law_fix_encumbrances.py @@ -66,7 +66,7 @@ trigger_rule='all_done', op_kwargs={ "log_file": "{{ ti.xcom_pull('run_fix_encumbrances_script') }}", - "library": FY_CODE, + "fy_code": FY_CODE, }, ) diff --git a/libsys_airflow/dags/folio_finance/sul_fix_encumbrances.py b/libsys_airflow/dags/folio_finance/sul_fix_encumbrances.py index dd8a23c9..2835b12e 100644 --- a/libsys_airflow/dags/folio_finance/sul_fix_encumbrances.py +++ b/libsys_airflow/dags/folio_finance/sul_fix_encumbrances.py @@ -67,7 +67,7 @@ trigger_rule='all_done', op_kwargs={ "log_file": "{{ ti.xcom_pull('run_fix_encumbrances_script') }}", - "library": FY_CODE, + "fy_code": FY_CODE, }, ) diff --git a/libsys_airflow/plugins/folio/encumbrances/email.py b/libsys_airflow/plugins/folio/encumbrances/email.py index 4b67e1c4..48b956b4 100644 --- a/libsys_airflow/plugins/folio/encumbrances/email.py +++ b/libsys_airflow/plugins/folio/encumbrances/email.py @@ -1,12 +1,13 @@ +import re + from airflow.models import Variable from jinja2 import Template from libsys_airflow.plugins.shared.utils import send_email_with_server_name -def email_log(**kwargs): - library = kwargs.get("library", "") - log_file = kwargs.get("log_file") +def email_to(**kwargs): + fy_code = kwargs.get("fy_code", "") devs_email = Variable.get("EMAIL_DEVS", "sul-unicorn-devs@lists.stanford.edu") sul_email = Variable.get("EMAIL_ENC_SUL", "") law_email = Variable.get("EMAIL_ENC_LAW", "") @@ -16,18 +17,26 @@ def email_log(**kwargs): devs_email, ] - match library: - case "sul": + match Regex_equals(fy_code): + case r'SUL+': to_addresses.append(sul_email) - case "law": + case r'LAW+': to_addresses.append(law_email) - case "lane": + case r'LANE+': to_addresses.append(lane_email) + return to_addresses + + +def email_log(**kwargs): + fy_code = kwargs.get("fy_code", "") + log_file = kwargs.get("log_file") + to_addresses = email_to(fy_code=fy_code) + with open(log_file, 'r') as fo: send_email_with_server_name( to=to_addresses, - subject=subject(library=library), + subject=subject(fy_code=fy_code), html_content=_email_body(fo), ) @@ -44,5 +53,12 @@ def _email_body(log): def subject(**kwargs): - library = kwargs.get("library", "") - return f"Fix Encumbrances for {library}" + fy_code = kwargs.get("fy_code", "") + return f"Fix Encumbrances for {fy_code}" + + +class Regex_equals(str): + "Override str.__eq__ to match a regex pattern." + + def __eq__(self, pattern): + return re.match(pattern, self) diff --git a/tests/test_fix_encumbrances_run.py b/tests/encumbrances/test_fix_encumbrances_run.py similarity index 54% rename from tests/test_fix_encumbrances_run.py rename to tests/encumbrances/test_fix_encumbrances_run.py index 4b3610bd..5062af52 100644 --- a/tests/test_fix_encumbrances_run.py +++ b/tests/encumbrances/test_fix_encumbrances_run.py @@ -14,19 +14,41 @@ def mock_task_instance(mocker): @pytest.fixture -def mock_okapi(monkeypatch): - def mock_get(*args): - return "http://okapi-test" +def mock_folio_variables(monkeypatch): + def mock_get(key, *args): + value = None + match key: + case "EMAIL_DEVS": + value = "test@stanford.edu" + + case "EMAIL_ENC_SUL": + value = "sul@example.com" + + case "EMAIL_ENC_LAW": + value = "law@example.com" + + case "EMAIL_ENC_LANE": + value = "lane@example.com" + + case "OKAPI_URL": + value = "okapi-test" + + case "FOLIO_URL": + value = "okapi-test" + + case _: + raise ValueError("") + return value monkeypatch.setattr(Variable, "get", mock_get) def test_fix_encumbrances_log_file_params( - mocker, tmp_path, mock_task_instance, mock_okapi, monkeypatch + mocker, tmp_path, mock_task_instance, mock_folio_variables, monkeypatch ): mocker.patch( 'libsys_airflow.plugins.folio.encumbrances.fix_encumbrances.Variable.get', - return_value=mock_okapi, + return_value=mock_folio_variables, ) async_mock = AsyncMock() @@ -58,5 +80,17 @@ def test_fix_encumbrances_email_subject(): from libsys_airflow.plugins.shared.utils import _subject_with_server_name from libsys_airflow.plugins.folio.encumbrances.email import subject - subj = _subject_with_server_name(subject=subject(library="SUL2024")) + subj = _subject_with_server_name(subject=subject(fy_code="SUL2024")) assert subj == "okapi-test - Fix Encumbrances for SUL2024" + + +def test_email_to(mocker): + mocker.patch( + 'libsys_airflow.plugins.shared.utils.send_email_with_server_name', + return_value=None, + ) + + from libsys_airflow.plugins.folio.encumbrances.email import email_to + + to_addresses = email_to(fy_code='SUL2025') + assert to_addresses == ['test@stanford.edu', 'sul@example.com']