def create_entity(client: TableClient, row_key: str, parameters=''): e = Entity.newEntity(parameters) try: client.create_entity(entity=e) except ResourceExistsError: raise EntityAlreadyExistsError()
def delete_entity(self): from azure.data.tables import TableClient from azure.core.exceptions import ResourceNotFoundError from azure.core import MatchConditions table_client = TableClient(account_url=self.account_url, credential=self.access_key, table_name=self.table_name) # Create entity to delete (to showcase etag) entity_created = table_client.create_entity(entity=self.entity) # show without calling metadata, cannot access etag try: entity_created.etag except AttributeError: print("Need to get metadata of entity") # In order to access etag as a part of the entity, need to call metadata on the entity metadata = entity_created.metadata() # Can now get etag etag = metadata['etag'] try: # will delete if match_condition and etag are satisfied table_client.delete_entity(entity=self.entity, etag=etag, match_condition=MatchConditions.IfNotModified) except ResourceNotFoundError: print("EntityDoesNotExists")
def test_table_name_errors_bad_chars(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): endpoint = self.account_url(tables_cosmos_account_name, "cosmos") # cosmos table names must be a non-empty string without chars '\', '/', '#', '?', and less than 255 chars. invalid_table_names = ["\\", "//", "#", "?", "- "] for invalid_name in invalid_table_names: client = TableClient(endpoint=endpoint, credential=tables_primary_cosmos_account_key, table_name=invalid_name) with pytest.raises(ValueError) as error: client.create_table() assert "Cosmos table names must contain from 1-255 characters" in str( error.value) try: with pytest.raises(ValueError) as error: client.delete_table() assert "Cosmos table names must contain from 1-255 characters" in str( error.value) except HttpResponseError as error: # Delete table returns a MethodNotAllowed for tablename == "\" if error.error_code != 'MethodNotAllowed': raise with pytest.raises(ValueError) as error: client.create_entity({'PartitionKey': 'foo', 'RowKey': 'foo'}) assert "Cosmos table names must contain from 1-255 characters" in str( error.value) with pytest.raises(ValueError) as error: client.upsert_entity({'PartitionKey': 'foo', 'RowKey': 'foo'}) assert "Cosmos table names must contain from 1-255 characters" in str( error.value) with pytest.raises(ValueError) as error: client.delete_entity("PK", "RK") assert "Cosmos table names must contain from 1-255 characters" in str( error.value) with pytest.raises(ValueError) as error: batch = [] batch.append(('upsert', {'PartitionKey': 'A', 'RowKey': 'B'})) client.submit_transaction(batch) assert "Cosmos table names must contain from 1-255 characters" in str( error.value)
def test_table_name_errors_bad_length(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): endpoint = self.account_url(tables_cosmos_account_name, "cosmos") # cosmos table names must be a non-empty string without chars '\', '/', '#', '?', and less than 255 chars. client = TableClient(endpoint=endpoint, credential=tables_primary_cosmos_account_key, table_name="-" * 255) with pytest.raises(ValueError) as error: client.create_table() assert "Cosmos table names must contain from 1-255 characters" in str( error.value) with pytest.raises(ResourceNotFoundError): client.create_entity({'PartitionKey': 'foo', 'RowKey': 'foo'}) with pytest.raises(ResourceNotFoundError): client.upsert_entity({'PartitionKey': 'foo', 'RowKey': 'foo'}) with pytest.raises(TableTransactionError) as error: batch = [] batch.append(('upsert', {'PartitionKey': 'A', 'RowKey': 'B'})) client.submit_transaction(batch) assert error.value.error_code == 'ResourceNotFound'
def create_entity(self): from azure.data.tables import TableClient from azure.core.exceptions import ResourceExistsError table_client = TableClient(account_url=self.account_url, credential=self.access_key, table_name=self.table_name) try: inserted_entity = table_client.create_entity(entity=self.entity) # inserted_entity type is dict[str,object] print(inserted_entity.items()) # print out key-value pair of entity except ResourceExistsError: print("EntityExists")
def test_table_name_errors(self, tables_storage_account_name, tables_primary_storage_account_key): endpoint = self.account_url(tables_storage_account_name, "table") # storage table names must be alphanumeric, cannot begin with a number, and must be between 3 and 63 chars long. invalid_table_names = ["1table", "a" * 2, "a" * 64, "a//", "my_table"] for invalid_name in invalid_table_names: client = TableClient(endpoint=endpoint, credential=tables_primary_storage_account_key, table_name=invalid_name) with pytest.raises(ValueError) as error: client.create_table() assert 'Storage table names must be alphanumeric' in str( error.value) with pytest.raises(ValueError) as error: client.create_entity({'PartitionKey': 'foo', 'RowKey': 'bar'}) assert 'Storage table names must be alphanumeric' in str( error.value) with pytest.raises(ValueError) as error: client.upsert_entity({'PartitionKey': 'foo', 'RowKey': 'foo'}) assert 'Storage table names must be alphanumeric' in str( error.value) with pytest.raises(ValueError) as error: client.delete_entity("PK", "RK") assert 'Storage table names must be alphanumeric' in str( error.value) with pytest.raises(ValueError) as error: client.get_table_access_policy() assert 'Storage table names must be alphanumeric' in str( error.value) with pytest.raises(ValueError): batch = [] batch.append(('upsert', {'PartitionKey': 'A', 'RowKey': 'B'})) client.submit_transaction(batch) assert 'Storage table names must be alphanumeric' in str( error.value)
def delete_entity(self): from azure.data.tables import TableClient from azure.core.exceptions import ResourceNotFoundError, ResourceExistsError from azure.core import MatchConditions table_client = TableClient(account_url=self.account_url, credential=self.access_key, table_name=self.table_name) # Create entity to delete (to showcase etag) try: resp = table_client.create_entity(entity=self.entity) except ResourceExistsError: print("Entity already exists!") # [START delete_entity] try: table_client.delete_entity( row_key=self.entity["RowKey"], partition_key=self.entity["PartitionKey"]) print("Successfully deleted!") except ResourceNotFoundError: print("Entity does not exists")