Skip to content

Commit

Permalink
Fix seeding bugs add missing tests (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
whabanks authored Nov 13, 2024
1 parent 42ded1e commit acfde00
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/actions/waffles/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
docopt==0.6.2
Flask==2.3.3
markupsafe==2.1.5
git+https://github.com/cds-snc/[email protected].7#egg=notifications-utils
git+https://github.com/cds-snc/[email protected].8#egg=notifications-utils
5 changes: 3 additions & 2 deletions notifications_utils/clients/redis/annual_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def decode_byte_dict(dict: dict, value_type=str):
# Check if expected_value_type is one of the allowed types
if value_type not in {int, float, str}:
raise ValueError("expected_value_type must be int, float, or str")
return {key.decode("utf-8"): value_type(value.decode("utf-8")) for key, value in dict.items()}
return {key.decode("utf-8"): value_type(value.decode("utf-8")) for key, value in dict.items() if dict.items()}


class RedisAnnualLimit:
Expand Down Expand Up @@ -110,7 +110,8 @@ def was_seeded_today(self, service_id):
return last_seeded_time == datetime.utcnow().strftime("%Y-%m-%d") if last_seeded_time else False

def get_seeded_at(self, service_id: str):
return self._redis_client.get_hash_field(annual_limit_status_key(service_id), SEEDED_AT).decode("utf-8")
seeded_at = self._redis_client.get_hash_field(annual_limit_status_key(service_id), SEEDED_AT)
return seeded_at and seeded_at.decode("utf-8")

def set_seeded_at(self, service_id):
self._redis_client.set_hash_value(annual_limit_status_key(service_id), SEEDED_AT, datetime.utcnow().strftime("%Y-%m-%d"))
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "notifications-utils"
version = "52.3.7"
version = "52.3.8"
description = "Shared python code for Notification - Provides logging utils etc."
authors = ["Canadian Digital Service"]
license = "MIT license"
Expand Down
23 changes: 23 additions & 0 deletions tests/test_annual_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,29 @@ def test_clear_annual_limit_statuses(mock_annual_limit_client, mock_annual_limit
assert len(mock_annual_limit_client.get_all_annual_limit_statuses(mocked_service_id)) == 0


@freeze_time("2024-10-25 12:00:00.000000")
@pytest.mark.parametrize("seeded_at_value, expected_value", [(b"2024-10-25", True), (None, False)])
def test_was_seeded_today(mock_annual_limit_client, seeded_at_value, expected_value, mocked_service_id, mocker):
mocker.patch.object(mock_annual_limit_client._redis_client, "get_hash_field", return_value=seeded_at_value)
result = mock_annual_limit_client.was_seeded_today(mocked_service_id)
assert result == expected_value


@freeze_time("2024-10-25 12:00:00.000000")
def test_set_seeded_at(mock_annual_limit_client, mocked_service_id):
mock_annual_limit_client.set_seeded_at(mocked_service_id)
result = mock_annual_limit_client.get_seeded_at(mocked_service_id)
assert result == datetime.utcnow().strftime("%Y-%m-%d")


@freeze_time("2024-10-25 12:00:00.000000")
@pytest.mark.parametrize("seeded_at_value, expected_value", [(b"2024-10-25", "2024-10-25"), (None, None)])
def test_get_seeded_at(mock_annual_limit_client, seeded_at_value, expected_value, mocked_service_id, mocker):
mocker.patch.object(mock_annual_limit_client._redis_client, "get_hash_field", return_value=seeded_at_value)
result = mock_annual_limit_client.get_seeded_at(mocked_service_id)
assert result == expected_value


@freeze_time("2024-10-25 12:00:00.000000")
def test_set_nearing_sms_limit(mock_annual_limit_client, mocked_service_id):
mock_annual_limit_client.set_nearing_sms_limit(mocked_service_id)
Expand Down

0 comments on commit acfde00

Please sign in to comment.