Skip to content

Commit

Permalink
feat: fix datetime parse error (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
exaby73 committed Jun 17, 2024
1 parent 187c968 commit c1c801c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,5 @@ doc/dist

# IDE files
.idea
.vscode
.vscode/*
!.vscode/settings.json
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
"python.linting.lintOnSave": true,
"python.linting.mypyEnabled": true,
"mypy.dmypyExecutable": "${workspaceFolder}/venv/bin/dmypy",
"files.autoSave": "afterDelay"
"files.autoSave": "afterDelay",
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
7 changes: 7 additions & 0 deletions src/firebase_functions/pubsub_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ def _message_handler(
data = event_dict["data"]
message_dict = data["message"]

# if no microseconds are present, we should set them to 0 to prevent parsing from failing
if "." not in event_dict["time"]:
event_dict["time"] = event_dict["time"].replace("Z", ".000000Z")
if "." not in message_dict["publish_time"]:
message_dict["publish_time"] = message_dict["publish_time"].replace(
"Z", ".000000Z")

time = _dt.datetime.strptime(
event_dict["time"],
"%Y-%m-%dT%H:%M:%S.%f%z",
Expand Down
29 changes: 29 additions & 0 deletions tests/test_pubsub_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,32 @@ def init():
_message_handler(func, raw_event)

self.assertEqual("world", hello)

def test_datetime_without_mircroseconds_doesnt_throw(self):
time = "2023-03-11T13:25:37Z"
raw_event = _CloudEvent(
attributes={
"id": "test-message",
"source": "https://example.com/pubsub",
"specversion": "1.0",
"time": time,
"type": "com.example.pubsub.message",
},
data={
"message": {
"attributes": {
"key": "value"
},
"data": "eyJ0ZXN0IjogInZhbHVlIn0=",
"message_id": "message-id-123",
"publish_time": time,
},
"subscription": "my-subscription",
},
)
try:
_message_handler(lambda _: None, raw_event)
# pylint: disable=broad-except
except Exception:
self.fail(
"Datetime without microseconds should not throw an exception")

0 comments on commit c1c801c

Please sign in to comment.