From b4c01fbe5177c34c744d09f56ffad035e8d2f678 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Mon, 14 Jun 2021 18:22:44 -0700 Subject: [PATCH 1/6] Cloud event should parse smalled ms precisions --- sdk/core/azure-core/azure/core/_utils.py | 3 + .../tests/test_messaging_cloud_event.py | 102 ++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/sdk/core/azure-core/azure/core/_utils.py b/sdk/core/azure-core/azure/core/_utils.py index 5f3133e02955..2ae6f5bb1110 100644 --- a/sdk/core/azure-core/azure/core/_utils.py +++ b/sdk/core/azure-core/azure/core/_utils.py @@ -63,6 +63,9 @@ def _convert_to_isoformat(date_time): break if len(decimal_str) > 6: timestamp = timestamp.replace(decimal_str, decimal_str[0:6]) + elif len(decimal_str) < 6: + new_dec = "0" * (6 - len(decimal_str)) + decimal_str + timestamp = timestamp.replace(decimal_str, new_dec) if delta == 0: tzinfo = TZ_UTC diff --git a/sdk/core/azure-core/tests/test_messaging_cloud_event.py b/sdk/core/azure-core/tests/test_messaging_cloud_event.py index 9dd8b80a7fbc..7383410b04cc 100644 --- a/sdk/core/azure-core/tests/test_messaging_cloud_event.py +++ b/sdk/core/azure-core/tests/test_messaging_cloud_event.py @@ -146,6 +146,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 + +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 == 123 + +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 == 123 + +def test_cloud_custom_dict_ms_precision_is_eq_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.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 + def test_cloud_custom_dict_blank_data(): cloud_custom_dict_with_extensions = { "id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033", From 4c3bd934da0157ecb212a9600941b45042fa1ede Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Tue, 15 Jun 2021 00:48:19 -0700 Subject: [PATCH 2/6] changelog --- sdk/core/azure-core/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 10e242697b12..96f588da4d7b 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -2,6 +2,9 @@ ## 1.15.1 (Unreleased) +### Bug Fixes + +- The `from_dict` methhod in the `CloudEvent` can now convert a datetime string to datetime object when microsecond precision is less than six in length. ## 1.15.0 (2021-06-04) From bacbbb649932b667c3b82de3a52d426b940d0d5d Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Tue, 15 Jun 2021 09:29:24 -0700 Subject: [PATCH 3/6] Update sdk/core/azure-core/tests/test_messaging_cloud_event.py --- sdk/core/azure-core/tests/test_messaging_cloud_event.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/azure-core/tests/test_messaging_cloud_event.py b/sdk/core/azure-core/tests/test_messaging_cloud_event.py index 7383410b04cc..2fa893079b4e 100644 --- a/sdk/core/azure-core/tests/test_messaging_cloud_event.py +++ b/sdk/core/azure-core/tests/test_messaging_cloud_event.py @@ -233,7 +233,7 @@ def test_cloud_custom_dict_ms_precision_is_lt_six_z_not(): def test_cloud_custom_dict_ms_precision_is_eq_six_z_not(): cloud_custom_dict_with_extensions = { - "id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033", + "id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e034", "source":"https://egtest.dev/cloudcustomevent", "data":{"team": "event grid squad"}, "type":"Azure.Sdk.Sample", From 91dae3a4bae0aed218e5125b38e01b8ca4160bd1 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Tue, 22 Jun 2021 02:06:16 -0700 Subject: [PATCH 4/6] add tests --- sdk/core/azure-core/CHANGELOG.md | 2 - sdk/core/azure-core/azure/core/_utils.py | 3 - .../tests/test_messaging_cloud_event.py | 62 +++++++++++++++++-- 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 96f588da4d7b..a3fc6b713748 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -4,8 +4,6 @@ ### Bug Fixes -- The `from_dict` methhod in the `CloudEvent` can now convert a datetime string to datetime object when microsecond precision is less than six in length. - ## 1.15.0 (2021-06-04) ### New Features diff --git a/sdk/core/azure-core/azure/core/_utils.py b/sdk/core/azure-core/azure/core/_utils.py index 2ae6f5bb1110..5f3133e02955 100644 --- a/sdk/core/azure-core/azure/core/_utils.py +++ b/sdk/core/azure-core/azure/core/_utils.py @@ -63,9 +63,6 @@ def _convert_to_isoformat(date_time): break if len(decimal_str) > 6: timestamp = timestamp.replace(decimal_str, decimal_str[0:6]) - elif len(decimal_str) < 6: - new_dec = "0" * (6 - len(decimal_str)) + decimal_str - timestamp = timestamp.replace(decimal_str, new_dec) if delta == 0: tzinfo = TZ_UTC diff --git a/sdk/core/azure-core/tests/test_messaging_cloud_event.py b/sdk/core/azure-core/tests/test_messaging_cloud_event.py index 2fa893079b4e..c42bf1079778 100644 --- a/sdk/core/azure-core/tests/test_messaging_cloud_event.py +++ b/sdk/core/azure-core/tests/test_messaging_cloud_event.py @@ -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 @@ -178,7 +176,7 @@ def test_cloud_custom_dict_ms_precision_is_lt_six(): assert event.time.month == 2 assert event.time.day == 18 assert event.time.hour == 20 - assert event.time.microsecond == 123 + assert event.time.microsecond == 123000 def test_cloud_custom_dict_ms_precision_is_eq_six(): cloud_custom_dict_with_extensions = { @@ -229,7 +227,7 @@ def test_cloud_custom_dict_ms_precision_is_lt_six_z_not(): assert event.time.month == 2 assert event.time.day == 18 assert event.time.hour == 20 - assert event.time.microsecond == 123 + assert event.time.microsecond == 123000 def test_cloud_custom_dict_ms_precision_is_eq_six_z_not(): cloud_custom_dict_with_extensions = { @@ -384,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" + 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" + 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(): + 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 \ No newline at end of file From 92b0aff3fdcfa394c175a7292fd570f3d1aa7365 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Tue, 22 Jun 2021 02:07:07 -0700 Subject: [PATCH 5/6] Update sdk/core/azure-core/CHANGELOG.md --- sdk/core/azure-core/CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index a3fc6b713748..10e242697b12 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -2,7 +2,6 @@ ## 1.15.1 (Unreleased) -### Bug Fixes ## 1.15.0 (2021-06-04) From bcf66caf25118748332676495e1c8a803f35ad3c Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Tue, 22 Jun 2021 14:46:05 -0700 Subject: [PATCH 6/6] Apply suggestions from code review --- sdk/core/azure-core/tests/test_messaging_cloud_event.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/core/azure-core/tests/test_messaging_cloud_event.py b/sdk/core/azure-core/tests/test_messaging_cloud_event.py index c42bf1079778..c82fa4f55370 100644 --- a/sdk/core/azure-core/tests/test_messaging_cloud_event.py +++ b/sdk/core/azure-core/tests/test_messaging_cloud_event.py @@ -411,7 +411,7 @@ def test_cloud_custom_dict_ms_precision_is_eq_six(): 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" + time ="2021-02-18T20:18:10.539861122Z" date_obj = _convert_to_isoformat(time) assert date_obj.month == 2 @@ -420,7 +420,7 @@ def test_cloud_custom_dict_ms_precision_is_gt_six_z_not(): 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" + time ="2021-02-18T20:18:10.123Z" date_obj = _convert_to_isoformat(time) assert date_obj.month == 2 @@ -429,10 +429,10 @@ def test_cloud_custom_dict_ms_precision_is_lt_six_z_not(): assert date_obj.microsecond == 123000 def test_cloud_custom_dict_ms_precision_is_eq_six_z_not(): - time ="2021-02-18T20:18:10.123456+00:00" + time ="2021-02-18T20:18:10.123456Z" 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 \ No newline at end of file + assert date_obj.microsecond == 123456