-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from usegalaxy-au/slack-alert-throttle
Slack alert throttle
- Loading branch information
Showing
4 changed files
with
152 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Virtual Environments | ||
venv/ | ||
.venv/ | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
psycopg2-binary>=2.9,<3.0 | ||
PyYAML>=6.0,<7.0 | ||
requests>=2.32,<3.0 |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
from datetime import datetime, timedelta | ||
import pathlib | ||
import tempfile | ||
from walle import NotificationHistory | ||
|
||
SLACK_NOTIFY_PERIOD_DAYS = 7 | ||
|
||
|
||
class TestNotificationHistory(unittest.TestCase): | ||
def setUp(self): | ||
self.temp_file = tempfile.NamedTemporaryFile(delete=False) | ||
self.record = NotificationHistory(self.temp_file.name) | ||
|
||
def tearDown(self): | ||
pathlib.Path(self.temp_file.name).unlink(missing_ok=True) | ||
|
||
def test_contains_new_entry(self): | ||
jwd = "unique_id_1" | ||
self.assertFalse( | ||
self.record.contains(jwd), "New entry should initially return False" | ||
) | ||
self.assertTrue(self.record.contains(jwd), "Duplicate entry should return True") | ||
|
||
def test_contains_existing_entry(self): | ||
jwd = "existing_id" | ||
self.record._write_record(jwd) | ||
self.assertTrue(self.record.contains(jwd), "Existing entry should return True") | ||
|
||
@patch("walle.SLACK_NOTIFY_PERIOD_DAYS", new=SLACK_NOTIFY_PERIOD_DAYS) | ||
def test_truncate_old_records(self): | ||
old_jwd = "old_entry" | ||
recent_jwd = "recent_entry" | ||
old_date = datetime.now() - timedelta(days=SLACK_NOTIFY_PERIOD_DAYS + 1) | ||
recent_date = datetime.now() | ||
|
||
with open(self.temp_file.name, "a") as f: | ||
f.write(f"{old_date.isoformat()}\t{old_jwd}\n") | ||
f.write(f"{recent_date.isoformat()}\t{recent_jwd}\n") | ||
|
||
self.record._truncate_records() | ||
self.assertFalse(self.record.contains(old_jwd), "Old entry should be purged") | ||
self.assertTrue(self.record.contains(recent_jwd), "Recent entry should remain") | ||
|
||
def test_purge_invalid_records(self): | ||
with open(self.temp_file.name, "w") as f: | ||
f.write("invalid_date\tinvalid_path\n") | ||
|
||
with patch("walle.logger.warning") as mock_warning: | ||
self.record._read_records() | ||
mock_warning.assert_called() | ||
|
||
self.assertFalse(self.record._get_jwds(), "Invalid records should be purged") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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