Skip to content

Commit 30c771e

Browse files
adds support for enums by converting to string before sending on the … (#13726)
* adds support for enums by converting to string before sending on the wire * forgot about considerations for python2 strings/unicode stuff
1 parent 55a1f90 commit 30c771e

File tree

3 files changed

+229
-3
lines changed

3 files changed

+229
-3
lines changed

sdk/tables/azure-data-tables/azure/data/tables/_serialize.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from uuid import UUID
88
from datetime import datetime
99
from math import isnan
10+
from enum import Enum
1011
import sys
1112
import uuid
1213
import isodate
@@ -116,6 +117,7 @@ def _to_entity_guid(value):
116117

117118

118119
def _to_entity_int32(value):
120+
# TODO: What the heck? below
119121
if sys.version_info < (3,):
120122
value = int(value)
121123
else:
@@ -159,7 +161,8 @@ def _to_entity_none(value): # pylint:disable=W0613
159161
bool: _to_entity_bool,
160162
datetime: _to_entity_datetime,
161163
float: _to_entity_float,
162-
UUID: _to_entity_guid
164+
UUID: _to_entity_guid,
165+
Enum: _to_entity_str
163166
}
164167
try:
165168
_PYTHON_TO_ENTITY_CONVERSIONS.update({
@@ -213,7 +216,13 @@ def _add_entity_properties(source):
213216
for name, value in source.items():
214217
mtype = ''
215218

216-
if isinstance(value, EntityProperty):
219+
if isinstance(value, Enum):
220+
try:
221+
conv = _PYTHON_TO_ENTITY_CONVERSIONS.get(unicode)
222+
except NameError:
223+
conv = _PYTHON_TO_ENTITY_CONVERSIONS.get(str)
224+
mtype, value = conv(value)
225+
elif isinstance(value, EntityProperty):
217226
conv = _EDM_TO_ENTITY_CONVERSIONS.get(value.type)
218227
if conv is None:
219228
raise TypeError(
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
interactions:
2+
- request:
3+
body: !!python/unicode '{"TableName": "uttabled43513a4"}'
4+
headers:
5+
Accept:
6+
- application/json;odata=minimalmetadata
7+
Accept-Encoding:
8+
- gzip, deflate
9+
Connection:
10+
- keep-alive
11+
Content-Length:
12+
- '32'
13+
Content-Type:
14+
- application/json;odata=nometadata
15+
DataServiceVersion:
16+
- '3.0'
17+
Date:
18+
- Thu, 10 Sep 2020 23:51:25 GMT
19+
User-Agent:
20+
- azsdk-python-data-tables/12.0.0b2 Python/2.7.18 (Windows-10-10.0.19041)
21+
x-ms-date:
22+
- Thu, 10 Sep 2020 23:51:25 GMT
23+
x-ms-version:
24+
- '2019-02-02'
25+
method: POST
26+
uri: !!python/unicode https://storagename.table.core.windows.net/Tables
27+
response:
28+
body:
29+
string: !!python/unicode '{"odata.metadata":"https://storagename.table.core.windows.net/$metadata#Tables/@Element","TableName":"uttabled43513a4"}'
30+
headers:
31+
cache-control:
32+
- no-cache
33+
content-type:
34+
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
35+
date:
36+
- Thu, 10 Sep 2020 23:51:24 GMT
37+
location:
38+
- !!python/unicode https://storagename.table.core.windows.net/Tables('uttabled43513a4')
39+
server:
40+
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
41+
transfer-encoding:
42+
- chunked
43+
x-content-type-options:
44+
- nosniff
45+
x-ms-version:
46+
- '2019-02-02'
47+
status:
48+
code: 201
49+
message: Created
50+
- request:
51+
body: !!python/unicode '{"test1": "Color.YELLOW", "[email protected]": "Edm.String",
52+
"test3": "Color.RED", "test2": "Color.BLUE", "[email protected]": "Edm.String",
53+
"[email protected]": "Edm.String", "PartitionKey": "pkd43513a4", "[email protected]":
54+
"Edm.String", "[email protected]": "Edm.String", "RowKey": "rkd43513a4"}'
55+
headers:
56+
Accept:
57+
- application/json;odata=minimalmetadata
58+
Accept-Encoding:
59+
- gzip, deflate
60+
Connection:
61+
- keep-alive
62+
Content-Length:
63+
- '302'
64+
Content-Type:
65+
- application/json;odata=nometadata
66+
DataServiceVersion:
67+
- '3.0'
68+
Date:
69+
- Thu, 10 Sep 2020 23:51:25 GMT
70+
User-Agent:
71+
- azsdk-python-data-tables/12.0.0b2 Python/2.7.18 (Windows-10-10.0.19041)
72+
x-ms-date:
73+
- Thu, 10 Sep 2020 23:51:25 GMT
74+
x-ms-version:
75+
- '2019-02-02'
76+
method: POST
77+
uri: !!python/unicode https://storagename.table.core.windows.net/uttabled43513a4
78+
response:
79+
body:
80+
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"}'
81+
headers:
82+
cache-control:
83+
- no-cache
84+
content-type:
85+
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
86+
date:
87+
- Thu, 10 Sep 2020 23:51:25 GMT
88+
etag:
89+
- W/"datetime'2020-09-10T23%3A51%3A25.711904Z'"
90+
location:
91+
- !!python/unicode https://storagename.table.core.windows.net/uttabled43513a4(PartitionKey='pkd43513a4',RowKey='rkd43513a4')
92+
server:
93+
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
94+
transfer-encoding:
95+
- chunked
96+
x-content-type-options:
97+
- nosniff
98+
x-ms-version:
99+
- '2019-02-02'
100+
status:
101+
code: 201
102+
message: Created
103+
- request:
104+
body: null
105+
headers:
106+
Accept:
107+
- application/json;odata=minimalmetadata
108+
Accept-Encoding:
109+
- gzip, deflate
110+
Connection:
111+
- keep-alive
112+
DataServiceVersion:
113+
- '3.0'
114+
Date:
115+
- Thu, 10 Sep 2020 23:51:25 GMT
116+
User-Agent:
117+
- azsdk-python-data-tables/12.0.0b2 Python/2.7.18 (Windows-10-10.0.19041)
118+
x-ms-date:
119+
- Thu, 10 Sep 2020 23:51:25 GMT
120+
x-ms-version:
121+
- '2019-02-02'
122+
method: GET
123+
uri: !!python/unicode https://storagename.table.core.windows.net/uttabled43513a4(PartitionKey='pkd43513a4',RowKey='rkd43513a4')
124+
response:
125+
body:
126+
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"}'
127+
headers:
128+
cache-control:
129+
- no-cache
130+
content-type:
131+
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
132+
date:
133+
- Thu, 10 Sep 2020 23:51:25 GMT
134+
etag:
135+
- W/"datetime'2020-09-10T23%3A51%3A25.711904Z'"
136+
server:
137+
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
138+
transfer-encoding:
139+
- chunked
140+
x-content-type-options:
141+
- nosniff
142+
x-ms-version:
143+
- '2019-02-02'
144+
status:
145+
code: 200
146+
message: OK
147+
- request:
148+
body: null
149+
headers:
150+
Accept:
151+
- application/json
152+
Accept-Encoding:
153+
- gzip, deflate
154+
Connection:
155+
- keep-alive
156+
Content-Length:
157+
- '0'
158+
Date:
159+
- Thu, 10 Sep 2020 23:51:25 GMT
160+
User-Agent:
161+
- azsdk-python-data-tables/12.0.0b2 Python/2.7.18 (Windows-10-10.0.19041)
162+
x-ms-date:
163+
- Thu, 10 Sep 2020 23:51:25 GMT
164+
x-ms-version:
165+
- '2019-02-02'
166+
method: DELETE
167+
uri: !!python/unicode https://storagename.table.core.windows.net/Tables('uttabled43513a4')
168+
response:
169+
body:
170+
string: !!python/unicode
171+
headers:
172+
cache-control:
173+
- no-cache
174+
content-length:
175+
- '0'
176+
date:
177+
- Thu, 10 Sep 2020 23:51:25 GMT
178+
server:
179+
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
180+
x-content-type-options:
181+
- nosniff
182+
x-ms-version:
183+
- '2019-02-02'
184+
status:
185+
code: 204
186+
message: No Content
187+
version: 1

sdk/tables/azure-data-tables/tests/test_table_entity.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import uuid
1515
from base64 import b64encode
1616
from datetime import datetime, timedelta
17+
from enum import Enum
1718

1819
from azure.data.tables import TableServiceClient, TableClient, generate_table_sas
1920
from dateutil.tz import tzutc, tzoffset
@@ -303,7 +304,6 @@ def _assert_valid_metadata(self, metadata):
303304
self.assertIn("etag", keys)
304305
self.assertEqual(len(keys), 3)
305306

306-
307307
# --Test cases for entities ------------------------------------------
308308
@GlobalStorageAccountPreparer()
309309
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
592592
finally:
593593
self._tear_down()
594594

595+
@GlobalStorageAccountPreparer()
596+
def test_insert_entity_with_enums(self, resource_group, location, storage_account,
597+
storage_account_key):
598+
# Arrange
599+
self._set_up(storage_account, storage_account_key)
600+
try:
601+
# Act
602+
class Color(Enum):
603+
RED = 1
604+
BLUE = 2
605+
YELLOW = 3
606+
607+
pk, rk = self._create_pk_rk(None, None)
608+
entity = TableEntity()
609+
entity.PartitionKey = pk
610+
entity.RowKey = rk
611+
entity.test1 = Color.YELLOW
612+
entity.test2 = Color.BLUE
613+
entity.test3 = Color.RED
614+
615+
616+
self.table.create_entity(entity=entity)
617+
resp_entity = self.table.get_entity(partition_key=pk, row_key=rk)
618+
assert str(entity.test1) == resp_entity.test1.value
619+
assert str(entity.test2) == resp_entity.test2.value
620+
assert str(entity.test3) == resp_entity.test3.value
621+
622+
finally:
623+
self._tear_down()
624+
595625
# @pytest.mark.skip("pending")
596626
@GlobalStorageAccountPreparer()
597627
def test_get_entity(self, resource_group, location, storage_account, storage_account_key):

0 commit comments

Comments
 (0)