Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
changes from int64 to int32 serialization. client side will try to se…
…rialize to int32, then int64, and raise a TypeError if it cannot
  • Loading branch information
seankane-msft committed Aug 31, 2020
commit 189d1e5a30dbbd4dfaf3999d9f2f82e9248e3ceb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ def _convert_to_entity(entry_element):

# Add type for Int32
if type(value) is int: # pylint:disable=C0123
mtype = EdmType.INT32
if value.bit_length() <= 32:
mtype = EdmType.INT32
elif value.bit_length() <= 64:
mtype = EdmType.INT64
else:
mtype = EdmType.STRING

# no type info, property should parse automatically
if not mtype:
Expand Down
7 changes: 6 additions & 1 deletion sdk/tables/azure-data-tables/azure/data/tables/_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ def __init__(self,
elif isinstance(value, bool):
self.type = EdmType.BOOLEAN
elif isinstance(value, six.integer_types):
self.type = EdmType.INT64
if value.bit_length() <= 32:
self.type = EdmType.INT32
elif value.bit_length() <= 64:
self.type = EdmType.INT64
else:
self.type = EdmType.STRING
elif isinstance(value, datetime):
self.type = EdmType.DATETIME
elif isinstance(value, float):
Expand Down
11 changes: 10 additions & 1 deletion sdk/tables/azure-data-tables/azure/data/tables/_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ def _to_entity_int64(value):
return EdmType.INT64, str(value)


def _to_entity_int(value):
ivalue = int(value)
if ivalue.bit_length() <= 32:
return _to_entity_int32(value)
elif ivalue.bit_length() <= 64:
return _to_entity_int64(value)
raise TypeError(_ERROR_VALUE_TOO_LARGE.format(str(value), EdmType.INT64))


def _to_entity_str(value):
return None, value

Expand All @@ -144,7 +153,7 @@ def _to_entity_none(value): # pylint:disable=W0613
# Conversion from Python type to a function which returns a tuple of the
# type string and content string.
_PYTHON_TO_ENTITY_CONVERSIONS = {
int: _to_entity_int64,
int: _to_entity_int,
bool: _to_entity_bool,
datetime: _to_entity_datetime,
float: _to_entity_float,
Expand Down
6 changes: 3 additions & 3 deletions sdk/tables/azure-data-tables/tests/test_table_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def test_inferred_types(self):
entity.test5 = EntityProperty(u"stringystring")
entity.test6 = EntityProperty(3.14159)
entity.test7 = EntityProperty(100)
entity.test8 = EntityProperty(10, EdmType.INT32)
entity.test8 = EntityProperty(2 ** 33)

# Assert
self.assertEqual(entity.test.type, EdmType.BOOLEAN)
Expand All @@ -172,8 +172,8 @@ def test_inferred_types(self):
self.assertEqual(entity.test4.type, EdmType.DATETIME)
self.assertEqual(entity.test5.type, EdmType.STRING)
self.assertEqual(entity.test6.type, EdmType.DOUBLE)
self.assertEqual(entity.test7.type, EdmType.INT64)
self.assertEqual(entity.test8.type, EdmType.INT32)
self.assertEqual(entity.test7.type, EdmType.INT32)
self.assertEqual(entity.test8.type, EdmType.INT64)


@pytest.mark.skip("pending")
Expand Down
12 changes: 8 additions & 4 deletions sdk/tables/azure-data-tables/tests/test_table_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def _assert_default_entity(self, entity, headers=None):
self.assertEqual(entity['birthday'], datetime(1970, 10, 4, tzinfo=tzutc()))
self.assertEqual(entity['binary'].value, b'binary')
self.assertIsInstance(entity['other'], EntityProperty)
self.assertEqual(entity['other'].type, EdmType.INT64)
self.assertEqual(entity['other'].type, EdmType.INT32)
self.assertEqual(entity['other'].value, 20)
self.assertEqual(entity['clsid'], uuid.UUID('c9da6455-213d-42c9-9a79-3e9149a57833'))
# self.assertTrue('metadata' in entity.odata)
Expand Down Expand Up @@ -188,7 +188,7 @@ def _assert_default_entity_json_full_metadata(self, entity, headers=None):
self.assertEqual(entity['birthday'], datetime(1970, 10, 4, tzinfo=tzutc()))
self.assertEqual(entity['binary'].value, b'binary')
self.assertIsInstance(entity['other'], EntityProperty)
self.assertEqual(entity['other'].type, EdmType.INT64)
self.assertEqual(entity['other'].type, EdmType.INT32)
self.assertEqual(entity['other'].value, 20)
self.assertEqual(entity['clsid'], uuid.UUID('c9da6455-213d-42c9-9a79-3e9149a57833'))
# self.assertTrue('metadata' in entity.odata)
Expand Down Expand Up @@ -222,7 +222,7 @@ def _assert_default_entity_json_no_metadata(self, entity, headers=None):
self.assertTrue(entity['birthday'].endswith('00Z'))
self.assertEqual(entity['binary'], b64encode(b'binary').decode('utf-8'))
self.assertIsInstance(entity['other'], EntityProperty)
self.assertEqual(entity['other'].type, EdmType.INT64)
self.assertEqual(entity['other'].type, EdmType.INT32)
self.assertEqual(entity['other'].value, 20)
self.assertEqual(entity['clsid'], 'c9da6455-213d-42c9-9a79-3e9149a57833')
# self.assertIsNone(entity.odata)
Expand Down Expand Up @@ -273,7 +273,7 @@ def _assert_merged_entity(self, entity):
self.assertEqual(entity.Birthday, datetime(1973, 10, 4, tzinfo=tzutc()))
self.assertEqual(entity.birthday, datetime(1991, 10, 4, tzinfo=tzutc()))
self.assertIsInstance(entity.other, EntityProperty)
self.assertEqual(entity.other.type, EdmType.INT64)
self.assertEqual(entity.other.type, EdmType.INT32)
self.assertEqual(entity.other.value, 20)
self.assertIsInstance(entity.clsid, uuid.UUID)
self.assertEqual(str(entity.clsid), 'c9da6455-213d-42c9-9a79-3e9149a57833')
Expand Down Expand Up @@ -446,6 +446,10 @@ def test_insert_entity_with_large_int32_value_throws(self, resource_group, locat
dict32['large'] = EntityProperty(-(2 ** 31 + 1), EdmType.INT32)
with self.assertRaises(TypeError):
self.table.create_entity(entity=dict32)

dict32['large'] = 2**100
with self.assertRaises(TypeError):
self.table.create_entity(entity=dict32)
finally:
self._tear_down()

Expand Down
12 changes: 8 additions & 4 deletions sdk/tables/azure-data-tables/tests/test_table_entity_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def _assert_default_entity(self, entity, headers=None):
self.assertEqual(entity['birthday'], datetime(1970, 10, 4, tzinfo=tzutc()))
self.assertEqual(entity['binary'].value, b'binary') # TODO: added the ".value" portion, verify this is correct
self.assertIsInstance(entity['other'], EntityProperty)
self.assertEqual(entity['other'].type, EdmType.INT64)
self.assertEqual(entity['other'].type, EdmType.INT32)
self.assertEqual(entity['other'].value, 20)
self.assertEqual(entity['clsid'], uuid.UUID('c9da6455-213d-42c9-9a79-3e9149a57833'))
# self.assertTrue('metadata' in entity.odata)
Expand Down Expand Up @@ -190,7 +190,7 @@ def _assert_default_entity_json_full_metadata(self, entity, headers=None):
self.assertEqual(entity['birthday'], datetime(1970, 10, 4, tzinfo=tzutc()))
self.assertEqual(entity['binary'].value, b'binary')
self.assertIsInstance(entity['other'], EntityProperty)
self.assertEqual(entity['other'].type, EdmType.INT64)
self.assertEqual(entity['other'].type, EdmType.INT32)
self.assertEqual(entity['other'].value, 20)
self.assertEqual(entity['clsid'], uuid.UUID('c9da6455-213d-42c9-9a79-3e9149a57833'))
# self.assertTrue('metadata' in entity.odata)
Expand Down Expand Up @@ -225,7 +225,7 @@ def _assert_default_entity_json_no_metadata(self, entity, headers=None):
self.assertTrue(entity['birthday'].endswith('00Z'))
self.assertEqual(entity['binary'], b64encode(b'binary').decode('utf-8'))
self.assertIsInstance(entity['other'], EntityProperty)
self.assertEqual(entity['other'].type, EdmType.INT64)
self.assertEqual(entity['other'].type, EdmType.INT32)
self.assertEqual(entity['other'].value, 20)
self.assertEqual(entity['clsid'], 'c9da6455-213d-42c9-9a79-3e9149a57833')
# self.assertIsNone(entity.odata)
Expand Down Expand Up @@ -275,7 +275,7 @@ def _assert_merged_entity(self, entity):
self.assertEqual(entity.Birthday, datetime(1973, 10, 4, tzinfo=tzutc()))
self.assertEqual(entity.birthday, datetime(1991, 10, 4, tzinfo=tzutc()))
self.assertIsInstance(entity.other, EntityProperty)
self.assertEqual(entity.other.type, EdmType.INT64)
self.assertEqual(entity.other.type, EdmType.INT32)
self.assertEqual(entity.other.value, 20)
self.assertIsInstance(entity.clsid, uuid.UUID)
self.assertEqual(str(entity.clsid), 'c9da6455-213d-42c9-9a79-3e9149a57833')
Expand Down Expand Up @@ -412,6 +412,10 @@ async def test_insert_entity_with_large_int32_value_throws(self, resource_group,
dict32['large'] = EntityProperty(-(2 ** 31 + 1), EdmType.INT32) # TODO: this is outside the range of int32
with self.assertRaises(TypeError):
await self.table.create_entity(entity=dict32)

dict32['large'] = 2**100
with self.assertRaises(TypeError):
await self.table.create_entity(entity=dict32)
finally:
await self._tear_down()

Expand Down