pyral deleteAttachment to delete attachment from a Test Case definition not working

105 Views Asked by At

I'm trying to delete the attachments from a Test Case definition on Rally using pyral:

del_attachment = rally.deleteAttachment('TestCase',filename)

any suggestions, what is going wrong ?

2

There are 2 best solutions below

0
Jean-Francois T. On

If you look at the code of pyral, you get the following signature:

  def deleteAttachment(self, artifact, filename):
        """
            Still unclear for WSAPI v2.0 if Attachment items can be deleted.
            Apparently AttachmentContent items can be deleted.
        """
        art_type, artifact = self._realizeArtifact(artifact)
        if not art_type:
            return False

        current_attachments = [att for att in artifact.Attachments]
        hits = [att for att in current_attachments if att.Name == filename]
        if not hits:
            return False

        ...

So the first argument is an artifact (i.e. the test case object), not a string.

The could should be like this:

import logging

logging.basicConfig(format="%(levelname)s:%(module)s:%(lineno)d:%(msg)s")


try:
    # Get number of existing steps
    testcase = rally.get("TestCase", query="FormattedID = %s" % tcid, instance=True)
    has_been_deleted = rally.deleteAttachment(testcase, filename)
    if not has_been_deleted:
        msg = "Attachment '{0}' of Test Case {1} not deleted successfully"
        logging.warning(msg.format(filename, testcase.FormattedID))

except RallyRESTAPIError as e:
    logging.error("Error while deleting attachment '{0}': {1}".format(filename, e))
0
sem824 On

passing a string of the FormattedID of the artifact should work because pyral tries to identify the type of artifact and retrieve it for you in the call below..

        art_type, artifact = self._realizeArtifact(artifact)

have look at the code for _realizeArtifact...

    def _realizeArtifact(self, artifact):
    """
        Helper method to identify the artifact type and to retrieve it if the 
        artifact value is a FormattedID. If the artifact is already an instance
        of a Rally entity, then all that needs to be done is deduce the art_type
        from the class name.  If the artifact argument given is neither of those 
        two conditions, return back a 2 tuple of (False, None).
        Once you have a Rally instance of the artifact, return back a 
        2 tuple of (art_type, artifact)
    """
    art_type = False
    if 'pyral.entity.' in str(type(artifact)):
        # we've got the artifact already...
        art_type = artifact.__class__.__name__
    elif self.FORMATTED_ID_PATTERN.match(artifact): 
        # artifact is a potential FormattedID value
        prefix = artifact[:2]
        if prefix[1] in string.digits:
            prefix = prefix[0]
        art_type = self.ARTIFACT_TYPE[prefix]
        response = self.get(art_type, fetch=True, query='FormattedID = %s' % artifact)
        if response.resultCount == 1:
            artifact = response.next()
        else:
            art_type = False
    else: # the supplied artifact isn't anything we can deal with here...
        pass

    return art_type, artifact