Skip to content

Commit

Permalink
Move to v2 model
Browse files Browse the repository at this point in the history
  • Loading branch information
Iain-S committed Jan 3, 2025
1 parent f6d698d commit ea7a3be
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 29 deletions.
1 change: 0 additions & 1 deletion usage_function/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ COPY utils/*.py /home/site/wwwroot/utils/

RUN mkdir -p /home/site/wwwroot/monthly_usage
COPY monthly_usage/*.py /home/site/wwwroot/monthly_usage/
COPY monthly_usage/function.json /home/site/wwwroot/monthly_usage/

WORKDIR /home/site/wwwroot

Expand Down
11 changes: 0 additions & 11 deletions usage_function/monthly_usage/function.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

MAX_ATTEMPTS = 5

app = func.FunctionApp()


def get_dates() -> Union[None, Tuple[date], Tuple[date, date]]:
"""Get up to two dates to process.
Expand Down Expand Up @@ -45,7 +47,10 @@ def get_dates() -> Union[None, Tuple[date], Tuple[date, date]]:
return day1, day2


def main(mytimer: func.TimerRequest) -> None:
@app.timer_trigger(
schedule="0 10 */2 7,8 * *", arg_name="my_timer", run_on_startup=False
)
def main(my_timer: func.TimerRequest) -> None:
"""Collect usage information for the previous month."""
# If incorrect settings have been given,
# better to find out sooner rather than later.
Expand Down
2 changes: 1 addition & 1 deletion usage_function/run_monthly_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import types

from monthly_usage import main
from monthly_usage.function_app import main

if __name__ == "__main__":
main(types.SimpleNamespace(past_due=True))
32 changes: 17 additions & 15 deletions usage_function/tests/test_function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,18 @@ def test_main(self) -> None:
_env_file=None,
)

with patch("monthly_usage.get_all_usage") as mock_get_all, patch(
"monthly_usage.retrieve_usage"
) as mock_retrieve, patch("monthly_usage.get_dates") as mock_get_dates, patch(
"monthly_usage.send_usage"
with patch("monthly_usage.function_app.get_all_usage") as mock_get_all, patch(
"monthly_usage.function_app.retrieve_usage"
) as mock_retrieve, patch(
"monthly_usage.function_app.get_dates"
) as mock_get_dates, patch(
"monthly_usage.function_app.send_usage"
) as mock_send, patch(
"utils.settings.get_settings"
) as mock_get_settings:
mock_get_dates.return_value = date(2024, 1, 1), date(2024, 1, 2)
mock_get_settings.return_value = settings
monthly_usage.main(mock_timer)
monthly_usage.function_app.main(mock_timer)

mock_get_all.assert_called_once_with(
datetime(2024, 1, 1),
Expand All @@ -139,54 +141,54 @@ def test_main(self) -> None:
def test_get_date_range(self) -> None:
"""Test that the get_date_range function returns the expected dates."""

with patch("monthly_usage.datetime") as mock_datetime:
with patch("monthly_usage.function_app.datetime") as mock_datetime:
# On hour 0 of the 7th day, we expect to get dates 1 and 2.
mock_datetime.now.return_value = datetime(2024, 2, 7, 0, 4, 56)

expected_dates: tuple[date, ...] = (date(2024, 1, 1), date(2024, 1, 2))

actual_dates = monthly_usage.get_dates()
actual_dates = monthly_usage.function_app.get_dates()
assert actual_dates is not None

self.assertTupleEqual(expected_dates, actual_dates)

with patch("monthly_usage.datetime") as mock_datetime:
with patch("monthly_usage.function_app.datetime") as mock_datetime:
# On hour 2 of the 7th day, we expect to get dates 3 and 4.
mock_datetime.now.return_value = datetime(2024, 2, 7, 2, 6, 0)

expected_dates = (date(2024, 1, 3), date(2024, 1, 4))

actual_dates = monthly_usage.get_dates()
actual_dates = monthly_usage.function_app.get_dates()
assert actual_dates is not None

self.assertTupleEqual(expected_dates, actual_dates)

with patch("monthly_usage.datetime") as mock_datetime:
with patch("monthly_usage.function_app.datetime") as mock_datetime:
# Some hours of the 8th day don't map to valid dates.
mock_datetime.now.return_value = datetime(2024, 2, 8, 22, 0, 0)

actual_dates = monthly_usage.get_dates()
actual_dates = monthly_usage.function_app.get_dates()

self.assertIsNone(actual_dates)

with patch("monthly_usage.datetime") as mock_datetime:
with patch("monthly_usage.function_app.datetime") as mock_datetime:
# For leap year February, we only expect one final date.
mock_datetime.now.return_value = datetime(2024, 3, 8, 4, 0, 0)

expected_dates = (date(2024, 2, 29),)

actual_dates = monthly_usage.get_dates()
actual_dates = monthly_usage.function_app.get_dates()
assert actual_dates is not None

self.assertTupleEqual(expected_dates, actual_dates)

with patch("monthly_usage.datetime") as mock_datetime:
with patch("monthly_usage.function_app.datetime") as mock_datetime:
# For months with 31 days, we only expect one final date.
mock_datetime.now.return_value = datetime(2024, 2, 8, 6, 0, 0)

expected_dates = (date(2024, 1, 31),)

actual_dates = monthly_usage.get_dates()
actual_dates = monthly_usage.function_app.get_dates()
assert actual_dates is not None
self.assertTupleEqual(expected_dates, actual_dates)

Expand Down

0 comments on commit ea7a3be

Please sign in to comment.