File tree 4 files changed +44
-2
lines changed
4 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -137,4 +137,5 @@ doc/dist
137
137
138
138
# IDE files
139
139
.idea
140
- .vscode
140
+ .vscode /*
141
+ ! .vscode /settings.json
Original file line number Diff line number Diff line change 12
12
"python.linting.lintOnSave" : true ,
13
13
"python.linting.mypyEnabled" : true ,
14
14
"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
16
21
}
Original file line number Diff line number Diff line change @@ -105,6 +105,13 @@ def _message_handler(
105
105
data = event_dict ["data" ]
106
106
message_dict = data ["message" ]
107
107
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
+
108
115
time = _dt .datetime .strptime (
109
116
event_dict ["time" ],
110
117
"%Y-%m-%dT%H:%M:%S.%f%z" ,
Original file line number Diff line number Diff line change @@ -129,3 +129,32 @@ def init():
129
129
_message_handler (func , raw_event )
130
130
131
131
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" )
You can’t perform that action at this time.
0 commit comments