Skip to content

Commit c1c801c

Browse files
authored
feat: fix datetime parse error (#204)
1 parent 187c968 commit c1c801c

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,5 @@ doc/dist
137137

138138
# IDE files
139139
.idea
140-
.vscode
140+
.vscode/*
141+
!.vscode/settings.json

Diff for: .vscode/settings.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@
1212
"python.linting.lintOnSave": true,
1313
"python.linting.mypyEnabled": true,
1414
"mypy.dmypyExecutable": "${workspaceFolder}/venv/bin/dmypy",
15-
"files.autoSave": "afterDelay"
15+
"files.autoSave": "afterDelay",
16+
"python.testing.pytestArgs": [
17+
"tests"
18+
],
19+
"python.testing.unittestEnabled": false,
20+
"python.testing.pytestEnabled": true
1621
}

Diff for: src/firebase_functions/pubsub_fn.py

+7
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ def _message_handler(
105105
data = event_dict["data"]
106106
message_dict = data["message"]
107107

108+
# if no microseconds are present, we should set them to 0 to prevent parsing from failing
109+
if "." not in event_dict["time"]:
110+
event_dict["time"] = event_dict["time"].replace("Z", ".000000Z")
111+
if "." not in message_dict["publish_time"]:
112+
message_dict["publish_time"] = message_dict["publish_time"].replace(
113+
"Z", ".000000Z")
114+
108115
time = _dt.datetime.strptime(
109116
event_dict["time"],
110117
"%Y-%m-%dT%H:%M:%S.%f%z",

Diff for: tests/test_pubsub_fn.py

+29
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,32 @@ def init():
129129
_message_handler(func, raw_event)
130130

131131
self.assertEqual("world", hello)
132+
133+
def test_datetime_without_mircroseconds_doesnt_throw(self):
134+
time = "2023-03-11T13:25:37Z"
135+
raw_event = _CloudEvent(
136+
attributes={
137+
"id": "test-message",
138+
"source": "https://example.com/pubsub",
139+
"specversion": "1.0",
140+
"time": time,
141+
"type": "com.example.pubsub.message",
142+
},
143+
data={
144+
"message": {
145+
"attributes": {
146+
"key": "value"
147+
},
148+
"data": "eyJ0ZXN0IjogInZhbHVlIn0=",
149+
"message_id": "message-id-123",
150+
"publish_time": time,
151+
},
152+
"subscription": "my-subscription",
153+
},
154+
)
155+
try:
156+
_message_handler(lambda _: None, raw_event)
157+
# pylint: disable=broad-except
158+
except Exception:
159+
self.fail(
160+
"Datetime without microseconds should not throw an exception")

0 commit comments

Comments
 (0)