diff --git a/sdk/tables/azure-data-tables/azure/data/tables/_serialize.py b/sdk/tables/azure-data-tables/azure/data/tables/_serialize.py index eb31bb55d0d3..56d336dbdca6 100644 --- a/sdk/tables/azure-data-tables/azure/data/tables/_serialize.py +++ b/sdk/tables/azure-data-tables/azure/data/tables/_serialize.py @@ -7,6 +7,7 @@ from uuid import UUID from datetime import datetime from math import isnan +from enum import Enum import sys import uuid import isodate @@ -116,6 +117,7 @@ def _to_entity_guid(value): def _to_entity_int32(value): + # TODO: What the heck? below if sys.version_info < (3,): value = int(value) else: @@ -159,7 +161,8 @@ def _to_entity_none(value): # pylint:disable=W0613 bool: _to_entity_bool, datetime: _to_entity_datetime, float: _to_entity_float, - UUID: _to_entity_guid + UUID: _to_entity_guid, + Enum: _to_entity_str } try: _PYTHON_TO_ENTITY_CONVERSIONS.update({ @@ -213,7 +216,13 @@ def _add_entity_properties(source): for name, value in source.items(): mtype = '' - if isinstance(value, EntityProperty): + if isinstance(value, Enum): + try: + conv = _PYTHON_TO_ENTITY_CONVERSIONS.get(unicode) + except NameError: + conv = _PYTHON_TO_ENTITY_CONVERSIONS.get(str) + mtype, value = conv(value) + elif isinstance(value, EntityProperty): conv = _EDM_TO_ENTITY_CONVERSIONS.get(value.type) if conv is None: raise TypeError( diff --git a/sdk/tables/azure-data-tables/tests/recordings/test_table_entity.test_insert_entity_with_enums.yaml b/sdk/tables/azure-data-tables/tests/recordings/test_table_entity.test_insert_entity_with_enums.yaml new file mode 100644 index 000000000000..9c4e400cf416 --- /dev/null +++ b/sdk/tables/azure-data-tables/tests/recordings/test_table_entity.test_insert_entity_with_enums.yaml @@ -0,0 +1,187 @@ +interactions: +- request: + body: !!python/unicode '{"TableName": "uttabled43513a4"}' + headers: + Accept: + - application/json;odata=minimalmetadata + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '32' + Content-Type: + - application/json;odata=nometadata + DataServiceVersion: + - '3.0' + Date: + - Thu, 10 Sep 2020 23:51:25 GMT + User-Agent: + - azsdk-python-data-tables/12.0.0b2 Python/2.7.18 (Windows-10-10.0.19041) + x-ms-date: + - Thu, 10 Sep 2020 23:51:25 GMT + x-ms-version: + - '2019-02-02' + method: POST + uri: !!python/unicode https://storagename.table.core.windows.net/Tables + response: + body: + string: !!python/unicode '{"odata.metadata":"https://storagename.table.core.windows.net/$metadata#Tables/@Element","TableName":"uttabled43513a4"}' + headers: + cache-control: + - no-cache + content-type: + - application/json;odata=minimalmetadata;streaming=true;charset=utf-8 + date: + - Thu, 10 Sep 2020 23:51:24 GMT + location: + - !!python/unicode https://storagename.table.core.windows.net/Tables('uttabled43513a4') + server: + - Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0 + transfer-encoding: + - chunked + x-content-type-options: + - nosniff + x-ms-version: + - '2019-02-02' + status: + code: 201 + message: Created +- request: + body: !!python/unicode '{"test1": "Color.YELLOW", "test1@odata.type": "Edm.String", + "test3": "Color.RED", "test2": "Color.BLUE", "RowKey@odata.type": "Edm.String", + "PartitionKey@odata.type": "Edm.String", "PartitionKey": "pkd43513a4", "test2@odata.type": + "Edm.String", "test3@odata.type": "Edm.String", "RowKey": "rkd43513a4"}' + headers: + Accept: + - application/json;odata=minimalmetadata + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '302' + Content-Type: + - application/json;odata=nometadata + DataServiceVersion: + - '3.0' + Date: + - Thu, 10 Sep 2020 23:51:25 GMT + User-Agent: + - azsdk-python-data-tables/12.0.0b2 Python/2.7.18 (Windows-10-10.0.19041) + x-ms-date: + - Thu, 10 Sep 2020 23:51:25 GMT + x-ms-version: + - '2019-02-02' + method: POST + uri: !!python/unicode https://storagename.table.core.windows.net/uttabled43513a4 + response: + body: + string: !!python/unicode '{"odata.metadata":"https://storagename.table.core.windows.net/$metadata#uttabled43513a4/@Element","odata.etag":"W/\"datetime''2020-09-10T23%3A51%3A25.711904Z''\"","PartitionKey":"pkd43513a4","RowKey":"rkd43513a4","Timestamp":"2020-09-10T23:51:25.711904Z","test1":"Color.YELLOW","test3":"Color.RED","test2":"Color.BLUE"}' + headers: + cache-control: + - no-cache + content-type: + - application/json;odata=minimalmetadata;streaming=true;charset=utf-8 + date: + - Thu, 10 Sep 2020 23:51:25 GMT + etag: + - W/"datetime'2020-09-10T23%3A51%3A25.711904Z'" + location: + - !!python/unicode https://storagename.table.core.windows.net/uttabled43513a4(PartitionKey='pkd43513a4',RowKey='rkd43513a4') + server: + - Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0 + transfer-encoding: + - chunked + x-content-type-options: + - nosniff + x-ms-version: + - '2019-02-02' + status: + code: 201 + message: Created +- request: + body: null + headers: + Accept: + - application/json;odata=minimalmetadata + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + DataServiceVersion: + - '3.0' + Date: + - Thu, 10 Sep 2020 23:51:25 GMT + User-Agent: + - azsdk-python-data-tables/12.0.0b2 Python/2.7.18 (Windows-10-10.0.19041) + x-ms-date: + - Thu, 10 Sep 2020 23:51:25 GMT + x-ms-version: + - '2019-02-02' + method: GET + uri: !!python/unicode https://storagename.table.core.windows.net/uttabled43513a4(PartitionKey='pkd43513a4',RowKey='rkd43513a4') + response: + body: + string: !!python/unicode '{"odata.metadata":"https://storagename.table.core.windows.net/$metadata#uttabled43513a4/@Element","odata.etag":"W/\"datetime''2020-09-10T23%3A51%3A25.711904Z''\"","PartitionKey":"pkd43513a4","RowKey":"rkd43513a4","Timestamp":"2020-09-10T23:51:25.711904Z","test1":"Color.YELLOW","test3":"Color.RED","test2":"Color.BLUE"}' + headers: + cache-control: + - no-cache + content-type: + - application/json;odata=minimalmetadata;streaming=true;charset=utf-8 + date: + - Thu, 10 Sep 2020 23:51:25 GMT + etag: + - W/"datetime'2020-09-10T23%3A51%3A25.711904Z'" + server: + - Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0 + transfer-encoding: + - chunked + x-content-type-options: + - nosniff + x-ms-version: + - '2019-02-02' + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + Date: + - Thu, 10 Sep 2020 23:51:25 GMT + User-Agent: + - azsdk-python-data-tables/12.0.0b2 Python/2.7.18 (Windows-10-10.0.19041) + x-ms-date: + - Thu, 10 Sep 2020 23:51:25 GMT + x-ms-version: + - '2019-02-02' + method: DELETE + uri: !!python/unicode https://storagename.table.core.windows.net/Tables('uttabled43513a4') + response: + body: + string: !!python/unicode + headers: + cache-control: + - no-cache + content-length: + - '0' + date: + - Thu, 10 Sep 2020 23:51:25 GMT + server: + - Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0 + x-content-type-options: + - nosniff + x-ms-version: + - '2019-02-02' + status: + code: 204 + message: No Content +version: 1 diff --git a/sdk/tables/azure-data-tables/tests/test_table_entity.py b/sdk/tables/azure-data-tables/tests/test_table_entity.py index 9e0cb8cf6ed2..8bc2c08f5e3e 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_entity.py +++ b/sdk/tables/azure-data-tables/tests/test_table_entity.py @@ -14,6 +14,7 @@ import uuid from base64 import b64encode from datetime import datetime, timedelta +from enum import Enum from azure.data.tables import TableServiceClient, TableClient, generate_table_sas from dateutil.tz import tzutc, tzoffset @@ -303,7 +304,6 @@ def _assert_valid_metadata(self, metadata): self.assertIn("etag", keys) self.assertEqual(len(keys), 3) - # --Test cases for entities ------------------------------------------ @GlobalStorageAccountPreparer() def test_insert_etag(self, resource_group, location, storage_account, storage_account_key): @@ -592,6 +592,36 @@ def test_insert_entity_property_name_too_long(self, resource_group, location, st finally: self._tear_down() + @GlobalStorageAccountPreparer() + def test_insert_entity_with_enums(self, resource_group, location, storage_account, + storage_account_key): + # Arrange + self._set_up(storage_account, storage_account_key) + try: + # Act + class Color(Enum): + RED = 1 + BLUE = 2 + YELLOW = 3 + + pk, rk = self._create_pk_rk(None, None) + entity = TableEntity() + entity.PartitionKey = pk + entity.RowKey = rk + entity.test1 = Color.YELLOW + entity.test2 = Color.BLUE + entity.test3 = Color.RED + + + self.table.create_entity(entity=entity) + resp_entity = self.table.get_entity(partition_key=pk, row_key=rk) + assert str(entity.test1) == resp_entity.test1.value + assert str(entity.test2) == resp_entity.test2.value + assert str(entity.test3) == resp_entity.test3.value + + finally: + self._tear_down() + # @pytest.mark.skip("pending") @GlobalStorageAccountPreparer() def test_get_entity(self, resource_group, location, storage_account, storage_account_key):