Skip to content

Commit

Permalink
Merge pull request #6 from joe-mccarthy/r-2.1.0
Browse files Browse the repository at this point in the history
closes #4 and closes #5
  • Loading branch information
joe-mccarthy authored Sep 20, 2024
2 parents 9463e15 + 43cac3b commit 84f7d2c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
34 changes: 26 additions & 8 deletions src/app/bindicator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from .api import Api
from datetime import date
from datetime import date, timedelta
import requests


Expand All @@ -20,18 +20,36 @@ def run(self):
logging.debug(
f"{len(collections)} found publishing first collection date information"
)
if date.today() == collections[0].date:
message = (
f"Bin collection is today for {collections[0].wheelie.bin_type}"
today = date.today()
tomorrow = today + timedelta(days=1)
collection_date = collections[0].date
bin_type = collections[0].wheelie.bin_type
if today == collection_date:
message = f"Bin collection is today for {bin_type}"
logging.info(message)
logging.info("Publishing message to ntfy.sh")
requests.post(
f"https://ntfy.sh/{self.topic}",
data=message.encode(encoding="utf-8"),
headers={
"Title": "Binday Today",
"Priority": "3",
"Tags": "rotating_light",
},
)
elif tomorrow == collection_date:
message = f"Bin collection is tomorrow for {bin_type}"
logging.info(message)
logging.info(f"Publishing message to ntfy.sh")
logging.info("Publishing message to ntfy.sh")
requests.post(
f"https://ntfy.sh/{self.topic}",
data=message.encode(encoding="utf-8"),
headers={
"Title": "Binday Tomorrow",
"Priority": "3",
"Tags": "warning",
},
)
else:
logging.info("No bin collection today")
logging.info(
f"Next bin collection is {collections[0].date}, {collections[0].wheelie.bin_type}"
)
logging.info(f"Next bin collection is {collection_date}, {bin_type}")
36 changes: 34 additions & 2 deletions tests/test_bindicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,40 @@ def test_bindicator_run_collections(mock_requests, mock_api):
mock_requests.post.assert_called_once_with(
"https://ntfy.sh/topic",
data=f"Bin collection is today for {collection_date.wheelie.bin_type}".encode(
encoding="utf-8"
encoding="utf-8",
),
headers={
"Title": "Binday Today",
"Priority": "3",
"Tags": "rotating_light",
},
)


@patch("src.app.bindicator.Api")
@patch("src.app.bindicator.requests")
def test_bincollection_tomorrow(mock_requests, mock_api):
bindicator = Bindicator("uprn", "topic")
today = date.today()
collection = Collection(
"recycling", (today + timedelta(days=1)).strftime("%d/%m/%Y")
)
collection_date = CollectionDate(collection)
mock_api().get_data.return_value = [collection_date]

bindicator.run()

mock_api().get_data.assert_called_once()
mock_requests.post.assert_called_once_with(
"https://ntfy.sh/topic",
data=f"Bin collection is tomorrow for {collection_date.wheelie.bin_type}".encode(
encoding="utf-8",
),
headers={
"Title": "Binday Tomorrow",
"Priority": "3",
"Tags": "warning",
},
)


Expand All @@ -46,7 +78,7 @@ def test_bindicator_run_collections(mock_requests, mock_api):
def test_no_collection_today(mock_requests, mock_api):
bindicator = Bindicator("uprn", "topic")

future_date = (date.today() + timedelta(days=1)).strftime("%d/%m/%Y")
future_date = (date.today() + timedelta(days=2)).strftime("%d/%m/%Y")
collection = Collection("recycling", future_date)
collection_date = CollectionDate(collection)
mock_api().get_data.return_value = [collection_date]
Expand Down

0 comments on commit 84f7d2c

Please sign in to comment.