Skip to content
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

Cloud event should parse smaller ms precisions #19259

Merged
merged 6 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.15.1 (Unreleased)

### Bug Fixes
rakshith91 marked this conversation as resolved.
Show resolved Hide resolved

## 1.15.0 (2021-06-04)

Expand Down
160 changes: 157 additions & 3 deletions sdk/core/azure-core/tests/test_messaging_cloud_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import logging
import sys
import os
import pytest
import json
import datetime

from azure.core.messaging import CloudEvent
from azure.core._utils import _convert_to_isoformat
from azure.core.serialization import NULL

# Cloud Event tests
Expand Down Expand Up @@ -146,6 +144,108 @@ def test_cloud_custom_dict_with_extensions():
assert event.time.microsecond == 539861
assert event.extensions == {"ext1": "example", "ext2": "example2"}

def test_cloud_custom_dict_ms_precision_is_gt_six():
cloud_custom_dict_with_extensions = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
"source":"https://egtest.dev/cloudcustomevent",
"data":{"team": "event grid squad"},
"type":"Azure.Sdk.Sample",
"time":"2021-02-18T20:18:10.539861122+00:00",
"specversion":"1.0",
}
event = CloudEvent.from_dict(cloud_custom_dict_with_extensions)
assert event.data == {"team": "event grid squad"}
assert event.__class__ == CloudEvent
assert event.time.month == 2
assert event.time.day == 18
assert event.time.hour == 20
assert event.time.microsecond == 539861
swathipil marked this conversation as resolved.
Show resolved Hide resolved

def test_cloud_custom_dict_ms_precision_is_lt_six():
cloud_custom_dict_with_extensions = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
"source":"https://egtest.dev/cloudcustomevent",
"data":{"team": "event grid squad"},
"type":"Azure.Sdk.Sample",
"time":"2021-02-18T20:18:10.123+00:00",
"specversion":"1.0",
}
event = CloudEvent.from_dict(cloud_custom_dict_with_extensions)
assert event.data == {"team": "event grid squad"}
assert event.__class__ == CloudEvent
assert event.time.month == 2
assert event.time.day == 18
assert event.time.hour == 20
assert event.time.microsecond == 123000

def test_cloud_custom_dict_ms_precision_is_eq_six():
cloud_custom_dict_with_extensions = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
"source":"https://egtest.dev/cloudcustomevent",
"data":{"team": "event grid squad"},
"type":"Azure.Sdk.Sample",
"time":"2021-02-18T20:18:10.123456+00:00",
"specversion":"1.0",
}
event = CloudEvent.from_dict(cloud_custom_dict_with_extensions)
assert event.data == {"team": "event grid squad"}
assert event.__class__ == CloudEvent
assert event.time.month == 2
assert event.time.day == 18
assert event.time.hour == 20
assert event.time.microsecond == 123456

def test_cloud_custom_dict_ms_precision_is_gt_six_z_not():
cloud_custom_dict_with_extensions = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
"source":"https://egtest.dev/cloudcustomevent",
"data":{"team": "event grid squad"},
"type":"Azure.Sdk.Sample",
"time":"2021-02-18T20:18:10.539861122Z",
"specversion":"1.0",
}
event = CloudEvent.from_dict(cloud_custom_dict_with_extensions)
assert event.data == {"team": "event grid squad"}
assert event.__class__ == CloudEvent
assert event.time.month == 2
assert event.time.day == 18
assert event.time.hour == 20
assert event.time.microsecond == 539861

def test_cloud_custom_dict_ms_precision_is_lt_six_z_not():
cloud_custom_dict_with_extensions = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
"source":"https://egtest.dev/cloudcustomevent",
"data":{"team": "event grid squad"},
"type":"Azure.Sdk.Sample",
"time":"2021-02-18T20:18:10.123Z",
"specversion":"1.0",
}
event = CloudEvent.from_dict(cloud_custom_dict_with_extensions)
assert event.data == {"team": "event grid squad"}
assert event.__class__ == CloudEvent
assert event.time.month == 2
assert event.time.day == 18
assert event.time.hour == 20
assert event.time.microsecond == 123000

def test_cloud_custom_dict_ms_precision_is_eq_six_z_not():
cloud_custom_dict_with_extensions = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e034",
"source":"https://egtest.dev/cloudcustomevent",
"data":{"team": "event grid squad"},
"type":"Azure.Sdk.Sample",
"time":"2021-02-18T20:18:10.123456Z",
"specversion":"1.0",
}
event = CloudEvent.from_dict(cloud_custom_dict_with_extensions)
assert event.data == {"team": "event grid squad"}
assert event.__class__ == CloudEvent
assert event.time.month == 2
assert event.time.day == 18
assert event.time.hour == 20
assert event.time.microsecond == 123456
swathipil marked this conversation as resolved.
Show resolved Hide resolved

def test_cloud_custom_dict_blank_data():
cloud_custom_dict_with_extensions = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
Expand Down Expand Up @@ -282,3 +382,57 @@ def test_cloud_from_dict_with_invalid_extensions():
}
with pytest.raises(ValueError):
event = CloudEvent.from_dict(cloud_custom_dict_with_extensions)

def test_cloud_custom_dict_ms_precision_is_gt_six():
time ="2021-02-18T20:18:10.539861122+00:00"
date_obj = _convert_to_isoformat(time)

assert date_obj.month == 2
assert date_obj.day == 18
assert date_obj.hour == 20
assert date_obj.microsecond == 539861

def test_cloud_custom_dict_ms_precision_is_lt_six():
time ="2021-02-18T20:18:10.123+00:00"
date_obj = _convert_to_isoformat(time)

assert date_obj.month == 2
assert date_obj.day == 18
assert date_obj.hour == 20
assert date_obj.microsecond == 123000

def test_cloud_custom_dict_ms_precision_is_eq_six():
time ="2021-02-18T20:18:10.123456+00:00"
date_obj = _convert_to_isoformat(time)

assert date_obj.month == 2
assert date_obj.day == 18
assert date_obj.hour == 20
assert date_obj.microsecond == 123456

def test_cloud_custom_dict_ms_precision_is_gt_six_z_not():
time ="2021-02-18T20:18:10.539861122+00:00"
rakshith91 marked this conversation as resolved.
Show resolved Hide resolved
date_obj = _convert_to_isoformat(time)

assert date_obj.month == 2
assert date_obj.day == 18
assert date_obj.hour == 20
assert date_obj.microsecond == 539861

def test_cloud_custom_dict_ms_precision_is_lt_six_z_not():
time ="2021-02-18T20:18:10.123+00:00"
rakshith91 marked this conversation as resolved.
Show resolved Hide resolved
date_obj = _convert_to_isoformat(time)

assert date_obj.month == 2
assert date_obj.day == 18
assert date_obj.hour == 20
assert date_obj.microsecond == 123000

def test_cloud_custom_dict_ms_precision_is_eq_six_z_not():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like times in these don't end with the Z?

time ="2021-02-18T20:18:10.123456+00:00"
rakshith91 marked this conversation as resolved.
Show resolved Hide resolved
date_obj = _convert_to_isoformat(time)

assert date_obj.month == 2
assert date_obj.day == 18
assert date_obj.hour == 20
assert date_obj.microsecond == 123456