-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use regex pattern to get library name from fy_code #1476
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", "[email protected]") | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = "[email protected]" | ||
|
||
case "EMAIL_ENC_SUL": | ||
value = "[email protected]" | ||
|
||
case "EMAIL_ENC_LAW": | ||
value = "[email protected]" | ||
|
||
case "EMAIL_ENC_LANE": | ||
value = "[email protected]" | ||
|
||
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 == ['[email protected]', '[email protected]'] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! Very useful approach for using regex in the match statement.