コード例 #1
0
    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")
コード例 #2
0
    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)
コード例 #3
0
    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")
コード例 #4
0
    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)
コード例 #5
0
ファイル: entity_manager.py プロジェクト: hund030/fastAPIDemo
 def delete_entity(client: TableClient, row_key: str, partition_key: str):
     try:
         client.delete_entity(row_key=row_key, partition_key=partition_key)
     except ResourceNotFoundError:
         raise EntityNotFoundError()