From bb33c98705f58dade65a2c3bdda0cb469e608211 Mon Sep 17 00:00:00 2001 From: seankane-msft Date: Fri, 7 Aug 2020 09:02:59 -0700 Subject: [PATCH 01/10] adding type hinting, changed tests to have INT64 as default for integers, not INT32 --- .../azure/data/tables/_deserialize.py | 4 +- .../azure/data/tables/_entity.py | 30 +++++++++++++-- .../tests/test_table_batch.py | 38 +++++++++---------- .../tests/test_table_encryption.py | 34 ++++++++--------- .../tests/test_table_entity.py | 22 +++++------ .../tests/test_table_entity_async.py | 22 +++++------ 6 files changed, 86 insertions(+), 64 deletions(-) diff --git a/sdk/tables/azure-data-tables/azure/data/tables/_deserialize.py b/sdk/tables/azure-data-tables/azure/data/tables/_deserialize.py index 4555dad01241..95078e91a643 100644 --- a/sdk/tables/azure-data-tables/azure/data/tables/_deserialize.py +++ b/sdk/tables/azure-data-tables/azure/data/tables/_deserialize.py @@ -46,11 +46,11 @@ def deserialize_table_creation(response, _, headers): def _from_entity_binary(value): - return EntityProperty(EdmType.BINARY, _decode_base64_to_bytes(value)) + return EntityProperty(_decode_base64_to_bytes(value)) def _from_entity_int32(value): - return EntityProperty(EdmType.INT32, int(value)) + return EntityProperty(int(value)) zero = datetime.timedelta(0) # same as 00:00 diff --git a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py index cc3265c2860c..ca6e501fe0d0 100644 --- a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py +++ b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py @@ -4,6 +4,7 @@ # license information. # -------------------------------------------------------------------------- from enum import Enum +from datetime import datetime from ._shared._error import _ERROR_ATTRIBUTE_MISSING @@ -75,17 +76,40 @@ class EntityProperty(object): """ def __init__(self, + value=None, # type: any type=None, # type: Union[str,EdmType] # pylint:disable=W0622 - value=None # type: Any - ): + # value=None # type: Any + ): """ Represents an Azure Table. Returned by list_tables. :param Union[str, EdmType] type: The type of the property. :param Any value: The value of the property. """ - self.type = type + # self.type = type self.value = value + if type is not None: + self.type = type + elif isinstance(value, bytes): + self.type = EdmType.BINARY + elif isinstance(value, int): + self.type = EdmType.INT64 + elif isinstance(value, uuid.UUID): + self.type = EdmType.GUID + elif isinstance(value, datetime): + self.type = EdmType.DATETIME + elif isinstance(value, str): + self.type = EdmType.STRING + elif isinstance(value, float): + self.type = EdmType.DOUBLE + elif isinstance(value, bool): + self.type = EdmType.BOOLEAN + else: + return ValueError( + """Type of {} could not be inferred. Acceptable types are bytes, int, uuid.UUID, + datetime, string, int32, float, and boolean. + """.format(value) + ) class EdmType(str, Enum): diff --git a/sdk/tables/azure-data-tables/tests/test_table_batch.py b/sdk/tables/azure-data-tables/tests/test_table_batch.py index 62d20bd42bd3..5830739ba0ba 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_batch.py +++ b/sdk/tables/azure-data-tables/tests/test_table_batch.py @@ -78,7 +78,7 @@ def _create_random_entity_dict(self, pk=None, rk=None): 'Birthday': datetime(1973, 10, 4, tzinfo=tzutc()), 'birthday': datetime(1970, 10, 4, tzinfo=tzutc()), 'binary': b'binary', - 'other': EntityProperty(EdmType.INT32, 20), + 'other': EntityProperty(20, EdmType.INT32), 'clsid': uuid.UUID('c9da6455-213d-42c9-9a79-3e9149a57833') } return Entity(**properties) @@ -158,10 +158,10 @@ def test_batch_insert(self, resource_group, location, storage_account, storage_a entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert' - entity.test = EntityProperty(EdmType.BOOLEAN, 'true') + entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 - entity.test4 = EntityProperty(EdmType.INT64, '1234567890') + entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() batch = self.table.create_batch() @@ -185,10 +185,10 @@ def test_batch_update(self, resource_group, location, storage_account, storage_a entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_update' - entity.test = EntityProperty(EdmType.BOOLEAN, 'true') + entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 - entity.test4 = EntityProperty(EdmType.INT64, '1234567890') + entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() self.table.create_item(entity) @@ -218,10 +218,10 @@ def test_batch_merge(self, resource_group, location, storage_account, storage_ac entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_merge' - entity.test = EntityProperty(EdmType.BOOLEAN, 'true') + entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 - entity.test4 = EntityProperty(EdmType.INT64, '1234567890') + entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() self.table.create_item(entity) @@ -313,7 +313,7 @@ def test_batch_insert_replace(self, resource_group, location, storage_account, s entity.test = True entity.test2 = 'value' entity.test3 = 3 - entity.test4 = EntityProperty(EdmType.INT64, '1234567890') + entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() batch = self.table.create_batch() @@ -343,7 +343,7 @@ def test_batch_insert_merge(self, resource_group, location, storage_account, sto entity.test = True entity.test2 = 'value' entity.test3 = 3 - entity.test4 = EntityProperty(EdmType.INT64, '1234567890') + entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() batch = self.table.create_batch() @@ -370,10 +370,10 @@ def test_batch_delete(self, resource_group, location, storage_account, storage_a entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_delete' - entity.test = EntityProperty(EdmType.BOOLEAN, 'true') + entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 - entity.test4 = EntityProperty(EdmType.INT64, '1234567890') + entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() self.table.create_item(entity) @@ -399,10 +399,10 @@ def test_batch_inserts(self, resource_group, location, storage_account, storage_ # Act entity = Entity() entity.PartitionKey = 'batch_inserts' - entity.test = EntityProperty(EdmType.BOOLEAN, 'true') + entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 - entity.test4 = EntityProperty(EdmType.INT64, '1234567890') + entity.test4 = EntityProperty(1234567890) batch = self.table.create_batch() for i in range(100): @@ -428,10 +428,10 @@ def test_batch_all_operations_together(self, resource_group, location, storage_a entity = Entity() entity.PartitionKey = '003' entity.RowKey = 'batch_all_operations_together-1' - entity.test = EntityProperty(EdmType.BOOLEAN, 'true') + entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 - entity.test4 = EntityProperty(EdmType.INT64, '1234567890') + entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() self.table.create_item(entity) entity.RowKey = 'batch_all_operations_together-2' @@ -476,10 +476,10 @@ def test_batch_all_operations_together_context_manager(self, resource_group, loc entity = Entity() entity.PartitionKey = '003' entity.RowKey = 'batch_all_operations_together-1' - entity.test = EntityProperty(EdmType.BOOLEAN, 'true') + entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 - entity.test4 = EntityProperty(EdmType.INT64, '1234567890') + entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() self.table.create_item(entity) entity.RowKey = 'batch_all_operations_together-2' @@ -525,10 +525,10 @@ def test_batch_reuse(self, resource_group, location, storage_account, storage_ac entity = Entity() entity.PartitionKey = '003' entity.RowKey = 'batch_all_operations_together-1' - entity.test = EntityProperty(EdmType.BOOLEAN, 'true') + entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 - entity.test4 = EntityProperty(EdmType.INT64, '1234567890') + entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() batch = TableBatchClient() diff --git a/sdk/tables/azure-data-tables/tests/test_table_encryption.py b/sdk/tables/azure-data-tables/tests/test_table_encryption.py index 008f0ea3743c..f6d43889da91 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_encryption.py +++ b/sdk/tables/azure-data-tables/tests/test_table_encryption.py @@ -164,16 +164,16 @@ # entity.large = 933311100 # entity.Birthday = datetime(1973, 10, 4) # entity.birthday = datetime(1970, 10, 4) -# entity.binary = EntityProperty(EdmType.BINARY, b'binary') -# entity.other = EntityProperty(EdmType.INT32, 20) +# entity.binary = EntityProperty(b'binary') +# entity.other = EntityProperty(20, EdmType.INT32) # entity.clsid = EntityProperty( -# EdmType.GUID, 'c9da6455-213d-42c9-9a79-3e9149a57833') +# 'c9da6455-213d-42c9-9a79-3e9149a57833') # return entity # # def _create_default_entity_for_encryption(self): # entity = self._create_random_entity_class() -# entity['sex'] = EntityProperty(EdmType.STRING, entity['sex'], True) -# entity['name'] = EntityProperty(EdmType.STRING, entity['name'], True) +# entity['sex'] = EntityProperty(entity['sex']) +# entity['name'] = EntityProperty(entity['name']) # return entity # # def _create_default_entity_dict(self, pk=None, rk=None): @@ -196,11 +196,9 @@ # 'large': 933311100, # 'Birthday': datetime(1973, 10, 4), # 'birthday': datetime(1970, 10, 4), -# 'binary': EntityProperty(EdmType.BINARY, b'binary'), -# 'other': EntityProperty(EdmType.INT32, 20), -# 'clsid': EntityProperty( -# EdmType.GUID, -# 'c9da6455-213d-42c9-9a79-3e9149a57833')} +# 'binary': EntityProperty(b'binary'), +# 'other': EntityProperty(20, EdmType.INT32), +# 'clsid': EntityProperty('c9da6455-213d-42c9-9a79-3e9149a57833')} # # def _assert_default_entity(self, entity): # ''' @@ -265,7 +263,7 @@ # # Arrange # self.ts.require_encryption = True # entity = self._create_default_entity_dict() -# entity['sex'] = EntityProperty(EdmType.STRING, entity['sex'], True) +# entity['sex'] = EntityProperty(entity['sex']) # self.ts.key_encryption_key = KeyWrapper('key1') # self.table.create_entity(table_entity_properties=entity) # @@ -547,7 +545,7 @@ # # Arrange # entity = self._create_random_entity_class() # self.ts.insert_entity(self.table_name, entity) -# entity['sex'] = EntityProperty(EdmType.STRING, 'female', True) +# entity['sex'] = EntityProperty('female') # self.ts.key_encryption_key = KeyWrapper('key1') # # # Act @@ -764,22 +762,22 @@ # # Arrange # self.ts.require_encryption = True # entity_binary = self._create_random_entity_class() -# entity_binary['bytes'] = EntityProperty(EdmType.BINARY, urandom(10), True) +# entity_binary['bytes'] = EntityProperty(b'urandom(10)') # entity_boolean = self._create_random_entity_class() -# entity_boolean['married'] = EntityProperty(EdmType.BOOLEAN, True, True) +# entity_boolean['married'] = EntityProperty(True) # entity_date_time = self._create_random_entity_class() -# entity_date_time['birthday'] = EntityProperty(EdmType.DATETIME, entity_date_time['birthday'], True) +# entity_date_time['birthday'] = EntityProperty(ntity_date_time['birthday']) # entity_double = self._create_random_entity_class() -# entity_double['ratio'] = EntityProperty(EdmType.DATETIME, entity_double['ratio'], True) +# entity_double['ratio'] = EntityProperty(entity_double['ratio']) # entity_guid = self._create_random_entity_class() # entity_guid['clsid'].encrypt = True # entity_int32 = self._create_random_entity_class() # entity_int32['other'].encrypt = True # entity_int64 = self._create_random_entity_class() -# entity_int64['large'] = EntityProperty(EdmType.INT64, entity_int64['large'], True) +# entity_int64['large'] = EntityProperty(entity_int64['large']) # self.ts.key_encryption_key = KeyWrapper('key1') # entity_none_str = self._create_random_entity_class() -# entity_none_str['none_str'] = EntityProperty(EdmType.STRING, None, True) +# entity_none_str['none_str'] = EntityProperty(None) # # # Act # 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 c199a57d506f..7009161fdfff 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_entity.py +++ b/sdk/tables/azure-data-tables/tests/test_table_entity.py @@ -115,7 +115,7 @@ def _create_random_entity_dict(self, pk=None, rk=None): 'Birthday': datetime(1973, 10, 4, tzinfo=tzutc()), 'birthday': datetime(1970, 10, 4, tzinfo=tzutc()), 'binary': b'binary', - 'other': EntityProperty(type=EdmType.INT32, value=20), + 'other': EntityProperty(value=20, type=EdmType.INT32), 'clsid': uuid.UUID('c9da6455-213d-42c9-9a79-3e9149a57833') } return TableEntity(**properties) @@ -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.INT32) + self.assertEqual(entity['other'].type, EdmType.INT64) self.assertEqual(entity['other'].value, 20) self.assertEqual(entity['clsid'], uuid.UUID('c9da6455-213d-42c9-9a79-3e9149a57833')) # self.assertTrue('metadata' in entity.odata) @@ -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.INT32) + self.assertEqual(entity['other'].type, EdmType.INT64) self.assertEqual(entity['other'].value, 20) self.assertEqual(entity['clsid'], uuid.UUID('c9da6455-213d-42c9-9a79-3e9149a57833')) # self.assertTrue('metadata' in entity.odata) @@ -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.INT32) + self.assertEqual(entity['other'].type, EdmType.INT64) self.assertEqual(entity['other'].value, 20) self.assertEqual(entity['clsid'], 'c9da6455-213d-42c9-9a79-3e9149a57833') # self.assertIsNone(entity.odata) @@ -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.INT32) + self.assertEqual(entity.other.type, EdmType.INT64) self.assertEqual(entity.other.value, 20) self.assertIsInstance(entity.clsid, uuid.UUID) self.assertEqual(str(entity.clsid), 'c9da6455-213d-42c9-9a79-3e9149a57833') @@ -417,13 +417,13 @@ def test_insert_entity_with_large_int32_value_throws(self, resource_group, locat try: # Act dict32 = self._create_random_base_entity_dict() - dict32['large'] = EntityProperty(EdmType.INT32, 2 ** 31) + dict32['large'] = EntityProperty(2 ** 31, EdmType.INT32) # Assert with self.assertRaises(TypeError): self.table.create_entity(entity=dict32) - dict32['large'] = EntityProperty(EdmType.INT32, -(2 ** 31 + 1)) + dict32['large'] = EntityProperty(-(2 ** 31 + 1), EdmType.INT32) with self.assertRaises(TypeError): self.table.create_entity(entity=dict32) finally: @@ -438,13 +438,13 @@ def test_insert_entity_with_large_int64_value_throws(self, resource_group, locat try: # Act dict64 = self._create_random_base_entity_dict() - dict64['large'] = EntityProperty(EdmType.INT64, 2 ** 63) + dict64['large'] = EntityProperty(2 ** 63) # Assert with self.assertRaises(TypeError): self.table.create_entity(entity=dict64) - dict64['large'] = EntityProperty(EdmType.INT64, -(2 ** 63 + 1)) + dict64['large'] = EntityProperty(-(2 ** 63 + 1)) with self.assertRaises(TypeError): self.table.create_entity(entity=dict64) finally: @@ -1304,10 +1304,10 @@ def test_query_entities_large(self, resource_group, location, storage_account, s entity = Entity() entity.PartitionKey = 'large' entity.RowKey = 'batch{0}-item{1}'.format(j, i) - entity.test = EntityProperty(EdmType.BOOLEAN, 'true') + entity.test = EntityProperty(True) entity.test2 = 'hello world;' * 100 entity.test3 = 3 - entity.test4 = EntityProperty(EdmType.INT64, '1234567890') + entity.test4 = EntityProperty(1234567890) entity.test5 = datetime(2016, 12, 31, 11, 59, 59, 0) batch.create_entity(entity) self.ts.commit_batch(table_name, batch) diff --git a/sdk/tables/azure-data-tables/tests/test_table_entity_async.py b/sdk/tables/azure-data-tables/tests/test_table_entity_async.py index e6b6272e7250..df61b825def8 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_entity_async.py +++ b/sdk/tables/azure-data-tables/tests/test_table_entity_async.py @@ -117,7 +117,7 @@ def _create_random_entity_dict(self, pk=None, rk=None): 'Birthday': datetime(1973, 10, 4, tzinfo=tzutc()), 'birthday': datetime(1970, 10, 4, tzinfo=tzutc()), 'binary': b'binary', - 'other': EntityProperty(type=EdmType.INT32, value=20), + 'other': EntityProperty(value=20, type=EdmType.INT32), 'clsid': uuid.UUID('c9da6455-213d-42c9-9a79-3e9149a57833') } return TableEntity(**properties) @@ -163,7 +163,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.INT32) + self.assertEqual(entity['other'].type, EdmType.INT64) self.assertEqual(entity['other'].value, 20) self.assertEqual(entity['clsid'], uuid.UUID('c9da6455-213d-42c9-9a79-3e9149a57833')) # self.assertTrue('metadata' in entity.odata) @@ -192,7 +192,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.INT32) + self.assertEqual(entity['other'].type, EdmType.INT64) self.assertEqual(entity['other'].value, 20) self.assertEqual(entity['clsid'], uuid.UUID('c9da6455-213d-42c9-9a79-3e9149a57833')) # self.assertTrue('metadata' in entity.odata) @@ -227,7 +227,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.INT32) + self.assertEqual(entity['other'].type, EdmType.INT64) self.assertEqual(entity['other'].value, 20) self.assertEqual(entity['clsid'], 'c9da6455-213d-42c9-9a79-3e9149a57833') # self.assertIsNone(entity.odata) @@ -277,7 +277,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.INT32) + self.assertEqual(entity.other.type, EdmType.INT64) self.assertEqual(entity.other.value, 20) self.assertIsInstance(entity.clsid, uuid.UUID) self.assertEqual(str(entity.clsid), 'c9da6455-213d-42c9-9a79-3e9149a57833') @@ -391,13 +391,13 @@ async def test_insert_entity_with_large_int32_value_throws(self, resource_group, try: # Act dict32 = self._create_random_base_entity_dict() - dict32['large'] = EntityProperty(EdmType.INT32, 2 ** 31) + dict32['large'] = EntityProperty(2 ** 31, EdmType.INT32) # TODO: this is outside the range of int32 # Assert with self.assertRaises(TypeError): await self.table.create_entity(entity=dict32) - dict32['large'] = EntityProperty(EdmType.INT32, -(2 ** 31 + 1)) + 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) finally: @@ -412,13 +412,13 @@ async def test_insert_entity_with_large_int64_value_throws(self, resource_group, try: # Act dict64 = self._create_random_base_entity_dict() - dict64['large'] = EntityProperty(EdmType.INT64, 2 ** 63) + dict64['large'] = EntityProperty(2 ** 63) # Assert with self.assertRaises(TypeError): await self.table.create_entity(entity=dict64) - dict64['large'] = EntityProperty(EdmType.INT64, -(2 ** 63 + 1)) + dict64['large'] = EntityProperty(-(2 ** 63 + 1)) with self.assertRaises(TypeError): await self.table.create_entity(entity=dict64) finally: @@ -1305,10 +1305,10 @@ def test_query_entities_large(self, resource_group, location, storage_account, s entity = TableEntity() entity.PartitionKey = 'large' entity.RowKey = 'batch{0}-item{1}'.format(j, i) - entity.test = EntityProperty(EdmType.BOOLEAN, 'true') + entity.test = EntityProperty(True) entity.test2 = 'hello world;' * 100 entity.test3 = 3 - entity.test4 = EntityProperty(EdmType.INT64, '1234567890') + entity.test4 = EntityProperty(1234567890) entity.test5 = datetime(2016, 12, 31, 11, 59, 59, 0) batch.create_entity(entity) self.ts.commit_batch(table_name, batch) From 9e1b298d5cb45c5f891a91731098c10bf3eab9d3 Mon Sep 17 00:00:00 2001 From: seankane-msft Date: Wed, 12 Aug 2020 14:43:16 -0700 Subject: [PATCH 02/10] added test for entity type, need to verify this works in python2 --- .../azure/data/tables/_entity.py | 31 +++++++++++++------ .../tests/test_table_batch.py | 28 +++++++++++++++++ 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py index 995a8542b680..3db5028025be 100644 --- a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py +++ b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py @@ -5,6 +5,7 @@ # -------------------------------------------------------------------------- from enum import Enum from datetime import datetime +from uuid import UUID from ._error import _ERROR_ATTRIBUTE_MISSING @@ -76,9 +77,9 @@ class EntityProperty(object): """ def __init__(self, - value=None, # type: any - type=None, # type: Union[str,EdmType] # pylint:disable=W0622 - # value=None # type: Any + value=None, # type: Any + type=None, # type: Union[str,EdmType] # pylint:disable=W0622 + # value=None # type: Any ): """ Represents an Azure Table. Returned by list_tables. @@ -87,29 +88,39 @@ def __init__(self, :param Any value: The value of the property. """ # self.type = type - self.value = value if type is not None: + self.value = value self.type = type elif isinstance(value, bytes): + self.value = value self.type = EdmType.BINARY + elif isinstance(value, bool): + self.value = value + self.type = EdmType.BOOLEAN elif isinstance(value, int): + self.value = value self.type = EdmType.INT64 - elif isinstance(value, uuid.UUID): - self.type = EdmType.GUID elif isinstance(value, datetime): + self.value = value self.type = EdmType.DATETIME elif isinstance(value, str): - self.type = EdmType.STRING + try: + self.value = UUID(value) + self.type = EdmType.GUID + except ValueError: + self.value = value + self.type = EdmType.STRING elif isinstance(value, float): + self.value = value self.type = EdmType.DOUBLE - elif isinstance(value, bool): - self.type = EdmType.BOOLEAN else: return ValueError( """Type of {} could not be inferred. Acceptable types are bytes, int, uuid.UUID, - datetime, string, int32, float, and boolean. + datetime, string, int32, int64, float, and boolean. Refer to + azure.data.tables.EdmType for more information. """.format(value) ) + class EdmType(str, Enum): diff --git a/sdk/tables/azure-data-tables/tests/test_table_batch.py b/sdk/tables/azure-data-tables/tests/test_table_batch.py index 5830739ba0ba..a3eda594aeb4 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_batch.py +++ b/sdk/tables/azure-data-tables/tests/test_table_batch.py @@ -16,6 +16,7 @@ from azure.core import MatchConditions from azure.core.exceptions import ( ResourceExistsError) +from azure.data.tables import EdmType, TableEntity, EntityProperty from _shared.testcase import GlobalStorageAccountPreparer, TableTestCase, LogCaptured @@ -148,6 +149,33 @@ def _assert_updated_entity(self, entity): self.assertIsInstance(entity.timestamp, datetime) #--Test cases for batch --------------------------------------------- + + def test_inferred_types(self): + # Arrange + # Act + entity = TableEntity() + entity.PartitionKey = '003' + entity.RowKey = 'batch_all_operations_together-1' + entity.test = EntityProperty(True) + entity.test2 = EntityProperty(b'abcdef') + entity.test3 = EntityProperty('c9da6455-213d-42c9-9a79-3e9149a57833') + entity.test4 = EntityProperty(datetime(1973, 10, 4, tzinfo=tzutc())) + entity.test5 = EntityProperty("stringystring") + entity.test6 = EntityProperty(3.14159) + entity.test7 = EntityProperty(100) + entity.test8 = EntityProperty(10, EdmType.INT32) + + # Assert + self.assertEqual(entity.test.type, EdmType.BOOLEAN) + self.assertEqual(entity.test2.type, EdmType.BINARY) + 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.test3.type, EdmType.GUID) + + @pytest.mark.skip("pending") @GlobalStorageAccountPreparer() def test_batch_insert(self, resource_group, location, storage_account, storage_account_key): From 1eb81e385cce8abb32da5f8a8f349d96b1224a1b Mon Sep 17 00:00:00 2001 From: seankane-msft Date: Wed, 12 Aug 2020 14:54:10 -0700 Subject: [PATCH 03/10] fixed some silly linting issues --- .../azure-data-tables/azure/data/tables/_deserialize.py | 1 - .../azure-data-tables/azure/data/tables/_entity.py | 9 ++++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/sdk/tables/azure-data-tables/azure/data/tables/_deserialize.py b/sdk/tables/azure-data-tables/azure/data/tables/_deserialize.py index f965bbd9fbef..8adf6db3fac2 100644 --- a/sdk/tables/azure-data-tables/azure/data/tables/_deserialize.py +++ b/sdk/tables/azure-data-tables/azure/data/tables/_deserialize.py @@ -16,7 +16,6 @@ from ._entity import EntityProperty, EdmType, TableEntity from ._common_conversion import _decode_base64_to_bytes -from ._generated.models import TableProperties from ._error import TableErrorCode if TYPE_CHECKING: diff --git a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py index 3db5028025be..d8fd49d7e5ce 100644 --- a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py +++ b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py @@ -108,19 +108,18 @@ def __init__(self, self.value = UUID(value) self.type = EdmType.GUID except ValueError: - self.value = value + self.value = value self.type = EdmType.STRING elif isinstance(value, float): self.value = value self.type = EdmType.DOUBLE else: - return ValueError( - """Type of {} could not be inferred. Acceptable types are bytes, int, uuid.UUID, - datetime, string, int32, int64, float, and boolean. Refer to + raise ValueError( + """Type of {} could not be inferred. Acceptable types are bytes, int, uuid.UUID, + datetime, string, int32, int64, float, and boolean. Refer to azure.data.tables.EdmType for more information. """.format(value) ) - class EdmType(str, Enum): From 13f5463f9d2e313680d016515f024504794edc20 Mon Sep 17 00:00:00 2001 From: seankane-msft Date: Thu, 13 Aug 2020 07:33:24 -0700 Subject: [PATCH 04/10] using six to check for string and binary types --- sdk/tables/azure-data-tables/azure/data/tables/_entity.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py index d8fd49d7e5ce..2ff2f9a3c74e 100644 --- a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py +++ b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py @@ -6,6 +6,7 @@ from enum import Enum from datetime import datetime from uuid import UUID +import six from ._error import _ERROR_ATTRIBUTE_MISSING @@ -91,7 +92,7 @@ def __init__(self, if type is not None: self.value = value self.type = type - elif isinstance(value, bytes): + elif isinstance(value, six.binary_type): self.value = value self.type = EdmType.BINARY elif isinstance(value, bool): @@ -103,7 +104,7 @@ def __init__(self, elif isinstance(value, datetime): self.value = value self.type = EdmType.DATETIME - elif isinstance(value, str): + elif isinstance(value, six.string_types): try: self.value = UUID(value) self.type = EdmType.GUID From a7d6fc9f039cc62d55f58890cdb58790a806e900 Mon Sep 17 00:00:00 2001 From: seankane-msft Date: Thu, 13 Aug 2020 08:15:49 -0700 Subject: [PATCH 05/10] adding six.integer_types as well --- sdk/tables/azure-data-tables/azure/data/tables/_entity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py index 2ff2f9a3c74e..1e62c6076db7 100644 --- a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py +++ b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py @@ -98,7 +98,7 @@ def __init__(self, elif isinstance(value, bool): self.value = value self.type = EdmType.BOOLEAN - elif isinstance(value, int): + elif isinstance(value, six.integer_types): self.value = value self.type = EdmType.INT64 elif isinstance(value, datetime): From 2503683d7df9b863111525b03931e87160d9333c Mon Sep 17 00:00:00 2001 From: seankane-msft Date: Thu, 13 Aug 2020 10:46:59 -0700 Subject: [PATCH 06/10] modifying entity inference --- .../azure-data-tables/azure/data/tables/_entity.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py index 1e62c6076db7..da00240f78f9 100644 --- a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py +++ b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py @@ -92,6 +92,13 @@ def __init__(self, if type is not None: self.value = value self.type = type + elif isinstance(value, six.text_type): + try: + self.value = UUID(value) + self.type = EdmType.GUID + except ValueError: + self.value = value + self.type = EdmType.STRING elif isinstance(value, six.binary_type): self.value = value self.type = EdmType.BINARY @@ -104,13 +111,6 @@ def __init__(self, elif isinstance(value, datetime): self.value = value self.type = EdmType.DATETIME - elif isinstance(value, six.string_types): - try: - self.value = UUID(value) - self.type = EdmType.GUID - except ValueError: - self.value = value - self.type = EdmType.STRING elif isinstance(value, float): self.value = value self.type = EdmType.DOUBLE From 2b11da038205e73a16f89c5bd463e122c85c18f5 Mon Sep 17 00:00:00 2001 From: seankane-msft Date: Thu, 13 Aug 2020 10:58:23 -0700 Subject: [PATCH 07/10] prefixing string with `u` to ensure string type matching --- sdk/tables/azure-data-tables/tests/test_table_batch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/tables/azure-data-tables/tests/test_table_batch.py b/sdk/tables/azure-data-tables/tests/test_table_batch.py index a3eda594aeb4..067311283be7 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_batch.py +++ b/sdk/tables/azure-data-tables/tests/test_table_batch.py @@ -160,7 +160,7 @@ def test_inferred_types(self): entity.test2 = EntityProperty(b'abcdef') entity.test3 = EntityProperty('c9da6455-213d-42c9-9a79-3e9149a57833') entity.test4 = EntityProperty(datetime(1973, 10, 4, tzinfo=tzutc())) - entity.test5 = EntityProperty("stringystring") + entity.test5 = EntityProperty(u"stringystring") entity.test6 = EntityProperty(3.14159) entity.test7 = EntityProperty(100) entity.test8 = EntityProperty(10, EdmType.INT32) From 694a964886fdc7e4db11c65e848e1ffc63a0a7a5 Mon Sep 17 00:00:00 2001 From: seankane-msft Date: Thu, 13 Aug 2020 11:15:43 -0700 Subject: [PATCH 08/10] unicoded strings in tests --- sdk/tables/azure-data-tables/tests/test_table_batch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/tables/azure-data-tables/tests/test_table_batch.py b/sdk/tables/azure-data-tables/tests/test_table_batch.py index 067311283be7..59a34d14a69f 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_batch.py +++ b/sdk/tables/azure-data-tables/tests/test_table_batch.py @@ -158,7 +158,7 @@ def test_inferred_types(self): entity.RowKey = 'batch_all_operations_together-1' entity.test = EntityProperty(True) entity.test2 = EntityProperty(b'abcdef') - entity.test3 = EntityProperty('c9da6455-213d-42c9-9a79-3e9149a57833') + entity.test3 = EntityProperty(u'c9da6455-213d-42c9-9a79-3e9149a57833') entity.test4 = EntityProperty(datetime(1973, 10, 4, tzinfo=tzutc())) entity.test5 = EntityProperty(u"stringystring") entity.test6 = EntityProperty(3.14159) From ae07795c3ddefef28a52f9985881616e2b0e3b02 Mon Sep 17 00:00:00 2001 From: seankane-msft Date: Thu, 13 Aug 2020 11:42:24 -0700 Subject: [PATCH 09/10] linting fixes --- sdk/tables/azure-data-tables/azure/data/tables/_entity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py index da00240f78f9..ae14ce305dca 100644 --- a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py +++ b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py @@ -98,7 +98,7 @@ def __init__(self, self.type = EdmType.GUID except ValueError: self.value = value - self.type = EdmType.STRING + self.type = EdmType.STRING elif isinstance(value, six.binary_type): self.value = value self.type = EdmType.BINARY From a20775a18b520282200786f237f793a7c957e501 Mon Sep 17 00:00:00 2001 From: seankane-msft Date: Mon, 17 Aug 2020 09:32:47 -0700 Subject: [PATCH 10/10] addressing izzys nits --- .../azure-data-tables/azure/data/tables/_entity.py | 10 +--------- sdk/tables/azure-data-tables/tests/test_table_batch.py | 2 +- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py index ae14ce305dca..230eb402e16d 100644 --- a/sdk/tables/azure-data-tables/azure/data/tables/_entity.py +++ b/sdk/tables/azure-data-tables/azure/data/tables/_entity.py @@ -80,7 +80,6 @@ class EntityProperty(object): def __init__(self, value=None, # type: Any type=None, # type: Union[str,EdmType] # pylint:disable=W0622 - # value=None # type: Any ): """ Represents an Azure Table. Returned by list_tables. @@ -88,31 +87,24 @@ def __init__(self, :param Union[str, EdmType] type: The type of the property. :param Any value: The value of the property. """ - # self.type = type + self.value = value if type is not None: - self.value = value self.type = type elif isinstance(value, six.text_type): try: self.value = UUID(value) self.type = EdmType.GUID except ValueError: - self.value = value self.type = EdmType.STRING elif isinstance(value, six.binary_type): - self.value = value self.type = EdmType.BINARY elif isinstance(value, bool): - self.value = value self.type = EdmType.BOOLEAN elif isinstance(value, six.integer_types): - self.value = value self.type = EdmType.INT64 elif isinstance(value, datetime): - self.value = value self.type = EdmType.DATETIME elif isinstance(value, float): - self.value = value self.type = EdmType.DOUBLE else: raise ValueError( diff --git a/sdk/tables/azure-data-tables/tests/test_table_batch.py b/sdk/tables/azure-data-tables/tests/test_table_batch.py index 59a34d14a69f..ba8603f9703f 100644 --- a/sdk/tables/azure-data-tables/tests/test_table_batch.py +++ b/sdk/tables/azure-data-tables/tests/test_table_batch.py @@ -168,12 +168,12 @@ def test_inferred_types(self): # Assert self.assertEqual(entity.test.type, EdmType.BOOLEAN) self.assertEqual(entity.test2.type, EdmType.BINARY) + self.assertEqual(entity.test3.type, EdmType.GUID) 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.test3.type, EdmType.GUID) @pytest.mark.skip("pending")