Skip to content

Commit

Permalink
Allow specifying timezone suffix in submit_time
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Nov 23, 2022
1 parent 90ca389 commit 5cb7df4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
11 changes: 6 additions & 5 deletions resultsdb/parsers/api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ def parse_submit_time(cls, v):
if isinstance(v, Number):
return time_from_milliseconds(v)
if isinstance(v, str):
try:
return datetime.strptime(v, '%Y-%m-%dT%H:%M:%S.%f')
except ValueError:
pass
for suffix in ('Z', '', '%z', '+00'):
try:
return datetime.strptime(v, f'%Y-%m-%dT%H:%M:%S.%f{suffix}')
except ValueError:
pass

try:
return time_from_milliseconds(int(v))
Expand All @@ -136,7 +137,7 @@ def parse_submit_time(cls, v):
raise ValueError(
"Expected timestamp in milliseconds or datetime"
" (in format YYYY-MM-DDTHH:MM:SS.ffffff),"
" got %r" % type(v)
" got %r" % v
)

@validator('testcase')
Expand Down
22 changes: 12 additions & 10 deletions testing/functest_api_v20.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,17 +624,19 @@ def test_create_result_submit_time_as_number_string(self):
assert data['submit_time'] == '2022-08-24T06:54:57.123000'

def test_create_result_submit_time_as_datetime(self):
ref_data = json.dumps(dict(
outcome=self.ref_result_outcome,
testcase=self.ref_testcase,
submit_time='2022-08-24T06:54:57.123456',
))
for suffix in ('', 'Z', '+00:00', '+0000', '+00'):
ref_data = json.dumps(dict(
outcome=self.ref_result_outcome,
testcase=self.ref_testcase,
submit_time=f'2022-08-24T06:54:57.123456{suffix}',
))

r = self.app.post('/api/v2.0/results', data=ref_data, content_type='application/json')
data = json.loads(r.data)
r = self.app.post(
'/api/v2.0/results', data=ref_data, content_type='application/json')
data = json.loads(r.data)

assert r.status_code == 201, data
assert data['submit_time'] == '2022-08-24T06:54:57.123456'
assert r.status_code == 201, data
assert data['submit_time'] == '2022-08-24T06:54:57.123456'

def test_create_result_submit_time_as_invalid(self):
ref_data = json.dumps(dict(
Expand All @@ -653,7 +655,7 @@ def test_create_result_submit_time_as_invalid(self):
"loc": ["submit_time"],
"msg": (
"Expected timestamp in milliseconds or datetime"
" (in format YYYY-MM-DDTHH:MM:SS.ffffff), got <class 'str'>"
" (in format YYYY-MM-DDTHH:MM:SS.ffffff), got 'now'"
),
"type": "value_error"
}]
Expand Down

0 comments on commit 5cb7df4

Please sign in to comment.