Skip to content

feat: fix datetime parse error #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Loading