def UpdateEntity(self, entityType, entityData, entityId): """Update an entity or multiple entities :param entityType: type of entity to update :type entityType: str :param entityData: data of entity(s) to update :type entityData: dict :param entityId: id of entity to update if updating multiple instances at once :type entityId: int | str :return: content of updated data :rtype: dict """ url = "{url}{entity}/{id}".format(url=self.url, entity=entityType, id=entityId) data = utils.DictToXml(entityData, re.sub(r's$', '', entityType)) request = self.session.put(url, data=data, headers={ 'Accept': 'application/json', 'Content-Type': 'application/xml' }) if request.status_code == 401: raise qc_exceptions.QCAuthenticationError("Not logged in") content = json.loads(request.content) if request.status_code == 200: return content raise qc_exceptions.QCError("Failed to update entity. {0}".format( content['Title']))
def GetEntity(self, entityType, entityId=None, query=None): """Get an entity :param entityType: type of entity to get: tests/test-sets/test-configs/test-set-folders/test-instances/runs/release-cycles/defects :type entityType: str :param entityId: id of entity to get. If None returns all instances of entity :type entityId: str | int :param query: query string to filter data. e.g: {name[Basic]} :type query: str :return: requested entity(s) :rtype: dict """ url = "{url}{entityType}".format(url=self.url, entityType=entityType) if entityId: url += "/{0}".format(entityId) if query: url += "?page-size=max&query={0}".format(query) request = self.session.get(url, headers={'Accept': 'application/json'}) if request.status_code == 401: raise qc_exceptions.QCAuthenticationError("Not logged in") content = json.loads(request.content) if request.status_code == 200: data = utils.ConvertJsonToDict(content) return data raise qc_exceptions.QCError( "Failed to get data of type '{0}'. [{1}] {2}. {3}".format( entityType, request.status_code, content['Title'], url))
def inner(self, *args, **kwargs): """wrapped inner function""" try: return func(self, *args, **kwargs) except qc_exceptions.QCAuthenticationError: request = self.session.post(self.baseUrl + 'rest/is-authenticated') # check if session is active if request.status_code == 401: self.Login() elif request.status_code == 503: raise qc_exceptions.QCError("Could not connect to Quality Center. {0}".format(request.reason)) return func(self, *args, **kwargs)
def __exit__(self, exc_type, exc_val, exc_tb): """logout when leaving context :param exc_type: Exception type :type exc_type: Exception :param exc_val: exception message :type exc_val: str :param exc_tb: Exception traceback :type exc_tb: traceback :return: None """ self.Logout() if exc_type: raise qc_exceptions.QCError( "An unexpected exception occurred. {0}".format(exc_val))
def GetFields(self, entityType): """Get entity's fields :param entityType: type of entity to get fields for :type entityType: str :return: requested fields for entity :rtype: list """ url = self.url + "customization/entities/{0}/fields".format(entityType) request = self.session.get(url, headers={'Accept': 'application/json'}) if request.status_code == 401: raise qc_exceptions.QCAuthenticationError("Not logged in") content = json.loads(request.content) if request.status_code == 200: return content["Fields"]["Field"] raise qc_exceptions.QCError( "Failed to get data of type '{0}'. [{1}] {2}. {3}".format( entityType, request.status_code, content['Title'], url))
def UpdateTestInstance(self, testInstanceId, paramsToUpdate): """update fields in test instance :param testInstanceId: id of test instance to update :type testInstanceId: int | str :param paramsToUpdate: dict contains params to update in test instance :type paramsToUpdate: dict :return: dict representing the updated test instance :rtype: dict """ testInstance = self.GetTestInstances(id=testInstanceId) if not testInstance: raise qc_exceptions.QCError( "Could not find test instance matching {0}".format( testInstanceId)) return self.UpdateEntity(constants.Entities.TEST_INSTANCES, entityData=paramsToUpdate, entityId=testInstance[0]["id"])
def CreateEntity(self, entityType, entityData): """Create an entity :param entityType: type of entity to create. e.g: defect, run, test :type entityType: str :param entityData: data to update. Class must inherit from base_entity.BaseEntity :type entityData: BaseEntity :return: created object data :rtype: dict """ url = "{url}{entity}".format(url=self.url, entity=entityType) request = self.session.post(url, data=entityData.ToXml(), headers={ 'Content-Type': 'application/xml', 'Accept': 'application/json' }) if request.status_code == 401: raise qc_exceptions.QCAuthenticationError("Not logged in") content = json.loads(request.content) if request.status_code in [200, 201]: return utils.ConvertJsonToDict(content) raise qc_exceptions.QCError("Failed to create entity. {0}".format( content['Title']))