def testcase_has_attachment(step, test_case, haveness, attachment): # fetch the test case's resource identity testcaseModel = TestcaseModel() testcase_id = testcaseModel.get_resid(test_case)[0] result_list = testcaseModel.get_attachment_list(testcase_id) #@todo: this should be abstracted into a helper method or in the model found_item = [x for x in result_list if x[ns("name")] == attachment] if (haveness == "has"): assert len(found_item) == 1, "Expected to find %s in:\n%s" % (attachment, jstr(result_list)) else: assert len(found_item) == 0, "Expected to NOT find %s in:\n%s" % (attachment, jstr(result_list))
def testcase_has_attachment(step, test_case, haveness, attachment): # fetch the test case's resource identity testcaseModel = TestcaseModel() testcase_id = testcaseModel.get_resid(test_case)[0] result_list = testcaseModel.get_attachment_list(testcase_id) #@todo: this should be abstracted into a helper method or in the model found_item = [x for x in result_list if x[ns("name")] == attachment] if (haveness == "has"): assert len(found_item) == 1, "Expected to find %s in:\n%s" % ( attachment, jstr(result_list)) else: assert len(found_item) == 0, "Expected to NOT find %s in:\n%s" % ( attachment, jstr(result_list))
def get_single_item_from_search(self, uri, tcm_type = None, params = {}, headers = get_json_headers()): ''' This will always return a single item or None type. May have many responses, so throw an error if there is more than one. It goes into the "searchResult" tcm_type of response Yeah, it's inefficient to create a list, then potentially return the first item as NOT a list. But this makes the coding easier and more uniform, so I chose to do that. ''' list = self.get_list_from_search(uri, tcm_type, params = params, headers = headers) # if the last attempt to get something returned None, we want to persist that in the last_id, # otherwise we may think we're referencing the LAST one, but we'd be getting the one from before if len(list) == 0: self.store_latest(None) return None assert len(list) < 2,\ '''More than one object returned from search. Don't know which one to return. uri: %s params: %s %s '''\ % (uri, params, jstr(list)) item = list[0] self.store_latest(item) return item
def get_single_item_from_endpoint(self, uri = None, tcm_type = None, params = {}, headers = get_json_headers()): ''' This hits an endpoint. No searchResult or ArrayOfXXXX part here ''' if uri == None: uri = self.root_path if tcm_type == None: tcm_type = self.singular response_txt = do_get(uri, params = params, headers = headers) tcm_obj = json_to_obj(response_txt) try: item = tcm_obj[ns(tcm_type)][0] # we only want to store this latest item, if it's an object of this type. If this # model is doing a search for a different type of item (like percent complete object # of "CategoryValueInfo" then we don't want to store it as "latest item" if uri == self.root_path: self.store_latest(item) return item except KeyError: assert False, "%s\nDidn't find %s in %s" % (str(KeyError), ns(tcm_type), jstr(tcm_obj))
def verify_testcase_steps(step, stored, name): tcModel = TestcaseModel() name = tcModel.get_stored_or_store_name(stored, name) testcasestep_list = tcModel.get_latest_steps_list(name) # compare the returned values with those passed in to verify match step_num = 0 try: for exp_step in step.hashes: act_step = testcasestep_list[step_num] eq_(act_step[ns("name")], exp_step["name"], "name match") step_num += 1 except KeyError: assert False, "Object field mismatch.\nExpected:\n" + jstr( step.hashes) + "\n\nActual:\n" + jstr(testcasestep_list)
def get_single_item_from_endpoint(self, uri=None, tcm_type=None, params={}, headers=get_json_headers()): ''' This hits an endpoint. No searchResult or ArrayOfXXXX part here ''' if uri == None: uri = self.root_path if tcm_type == None: tcm_type = self.singular response_txt = do_get(uri, params=params, headers=headers) tcm_obj = json_to_obj(response_txt) try: item = tcm_obj[ns(tcm_type)][0] # we only want to store this latest item, if it's an object of this type. If this # model is doing a search for a different type of item (like percent complete object # of "CategoryValueInfo" then we don't want to store it as "latest item" if uri == self.root_path: self.store_latest(item) return item except KeyError: assert False, "%s\nDidn't find %s in %s" % ( str(KeyError), ns(tcm_type), jstr(tcm_obj))
def search_and_verify(self, uri, search_args, expect_to_find, tcm_type=None): ''' This does a search based on the search_args passed in. So "expect_to_find" is really filtered based on those parameters. expect_to_find: If True, then we verify based on expecting to find something. If False, this will fail if we get a resultset greater than 0. ''' if tcm_type == None: tcm_type = self.singular resp_list = self.get_list_from_search(uri, params=search_args) if not expect_to_find: eq_(len(resp_list), 0, "expect result size zero:\n" + jstr(resp_list)) else: # we want to verify just ONE of the items returned. Indeed, we likely # expect only one. So we pick the first item returned verify_single_item_in_list(resp_list, params=search_args)
def get_single_item_from_search(self, uri, tcm_type=None, params={}, headers=get_json_headers()): ''' This will always return a single item or None type. May have many responses, so throw an error if there is more than one. It goes into the "searchResult" tcm_type of response Yeah, it's inefficient to create a list, then potentially return the first item as NOT a list. But this makes the coding easier and more uniform, so I chose to do that. ''' list = self.get_list_from_search(uri, tcm_type, params=params, headers=headers) # if the last attempt to get something returned None, we want to persist that in the last_id, # otherwise we may think we're referencing the LAST one, but we'd be getting the one from before if len(list) == 0: self.store_latest(None) return None assert len(list) < 2,\ '''More than one object returned from search. Don't know which one to return. uri: %s params: %s %s '''\ % (uri, params, jstr(list)) item = list[0] self.store_latest(item) return item
def find_role_by_id(step, stored_role, role_name): roleModel = RoleModel() role_name = roleModel.get_stored_or_store_name(stored_role, role_name) role = roleModel.get_by_name(role_name) assert role[ns("name")] == role_name, "Expected to find role with name %s in:\n%s" % (role_name, jstr(role))
def get_resource_identity(tcm_obj): try: resid = int(tcm_obj[ns("resourceIdentity")]["@id"]) version = tcm_obj[ns("resourceIdentity")]["@version"] return resid, version except KeyError: assert False, "didn't find expected tcm_type: %s -- %s or %s in:\n%s" % ( ns("resourceIdentity"), "@id", "@version", jstr(tcm_obj))
def testcycle_has_testrun(step, cycle_name, run_name): testcycleModel = TestcycleModel() testcycle_id = testcycleModel.get_resid(cycle_name)[0] testrun_list = testcycleModel.get_testrun_list(testcycle_id) found_run = [x for x in testrun_list if x[ns("name")] == run_name] assert len(found_run) == 1, "Expected to find name %s in:\n%s" % ( run_name, jstr(testrun_list))
def testcycle_has_testrun(step, cycle_name, run_name): testcycleModel = TestcycleModel() testcycle_id = testcycleModel.get_resid(cycle_name)[0] testrun_list = testcycleModel.get_testrun_list(testcycle_id) found_run = [x for x in testrun_list if x[ns("name")] == run_name] assert len(found_run) == 1, "Expected to find name %s in:\n%s" % (run_name, jstr(testrun_list))
def search_for_resid(self, params={}): ''' Return the id and version as a tuple ''' item = self.get_single_item_from_search(self.root_path, params=params) assert item != None, "%s not found in search with these params:\n%s" % \ (self.singular, jstr(params)) return get_resource_identity(item)
def get_resource_identity(tcm_obj): try: resid = int(tcm_obj[ns("resourceIdentity")]["@id"]) version = tcm_obj[ns("resourceIdentity")]["@version"] return resid, version except KeyError: assert False, "didn't find expected tcm_type: %s -- %s or %s in:\n%s" % (ns("resourceIdentity"), "@id", "@version", jstr(tcm_obj))
def search_for_resid(self, params = {}): ''' Return the id and version as a tuple ''' item = self.get_single_item_from_search(self.root_path, params = params) assert item != None, "%s not found in search with these params:\n%s" % \ (self.singular, jstr(params)) return get_resource_identity(item)
def check_roles_exist(hashes): roleModel = RoleModel() role_list = roleModel.get_all_list() # walk through all the expected roles and make sure it has them all # note, it doesn't check that ONLY these roles exist. That should be a different # method. for role in hashes: role_name = role["name"] found_perm = [x for x in role_list if x[ns("name")] == role_name] assert len(found_perm) == 1, "Expected to find role name %s in:\n%s" % (role_name, jstr(role_list))
def testcase_has_status_of_approved(step, stored, testcase_name): tcModel = TestcaseModel() testcase_name = tcModel.get_stored_or_store_name(stored, testcase_name) # fetch the steps for this testcase from the latestversion testcase_id = tcModel.get_resid(testcase_name)[0] testcaseversion = tcModel.get_latestversion(testcase_id) # should be just one try: eq_(testcaseversion[ns("approvalStatusId")], 2, "Testcase is approved: " + str(testcaseversion)) except KeyError: assert False, "Object field mismatch.\nExpected:\n" + ns( "approved") + "\n\nActual:\n" + jstr(testcaseversion)
def testcases_have_approval_statuses(step): tcModel = TestcaseModel() for tc in step.hashes: testcase_id = tcModel.get_resid(tc["name"])[0] testcaseversion = tcModel.get_latestversion(testcase_id) # should be just one try: eq_(testcaseversion[ns("approvalStatusId")], 2, "Testcase is approved: " + str(testcaseversion)) except KeyError: assert False, "Object field mismatch.\nExpected:\n" + ns( "approved") + "\n\nActual:\n" + jstr(testcaseversion)
def at_least_these_environments_exist(step): model = EnvironmentModel() env_list = model.get_all_list() # walk through all the expected roles and make sure it has them all # note, it doesn't check that ONLY these roles exist. That should be a different # method. for env in step.hashes: env_name = env["name"] envtype_id = EnvironmenttypeModel().get_resid(env["type"])[0] found_env = [x for x in env_list if ((x[ns("name")] == env_name) and (x[ns("environmentTypeId")] == envtype_id))] assert (len(found_env) == 1), \ "Expected to find environment with name %s and environmentTypeId of %s in:\n%s" % \ (env_name, envtype_id, jstr(env_list))
def verify_testcase_steps(step, stored, name): tcModel = TestcaseModel() name = tcModel.get_stored_or_store_name(stored, name) testcasestep_list = tcModel.get_latest_steps_list(name) # compare the returned values with those passed in to verify match step_num = 0 try: for exp_step in step.hashes: act_step = testcasestep_list[step_num] eq_(act_step[ns("name")], exp_step["name"], "name match") step_num += 1 except KeyError: assert False, "Object field mismatch.\nExpected:\n" + jstr(step.hashes) + "\n\nActual:\n" + jstr(testcasestep_list)
def foo_has_these_assignments(step, stored, name): userModel = UserModel() name = userModel.get_stored_or_store_name(stored, name) user_id = userModel.get_resid(name)[0] assignment_list = userModel.get_assignment_list(user_id) # walk through all the expected roles and make sure it has them all # note, it doesn't check that ONLY these roles exist. That should be a different # method. for role in step.hashes: role_name = role["name"] found_perm = [x for x in assignment_list if x[ns("name")] == role_name] assert len(found_perm) == 1, "Expected to find assignment name %s in:\n%s" % (role_name, jstr(assignment_list))
def foo_has_these_assignments(step, stored, name): userModel = UserModel() name = userModel.get_stored_or_store_name(stored, name) user_id = userModel.get_resid(name)[0] assignment_list = userModel.get_assignment_list(user_id) # walk through all the expected roles and make sure it has them all # note, it doesn't check that ONLY these roles exist. That should be a different # method. for role in step.hashes: role_name = role["name"] found_perm = [x for x in assignment_list if x[ns("name")] == role_name] assert len( found_perm) == 1, "Expected to find assignment name %s in:\n%s" % ( role_name, jstr(assignment_list))
def user_has_these_roles(step, stored_user, user_name, at_least_only): userModel = UserModel() user_name = userModel.get_stored_or_store_name(stored_user, user_name) role_list = userModel.get_role_list(user_name) list_size_check(at_least_only, step.hashes, role_list) # walk through all the expected roles and make sure it has them all # note, it doesn't check that ONLY these roles exist. That should be a different # method. for role in step.hashes: role_name = role["name"] found_perm = [x for x in role_list if x[ns("name")] == role_name] assert len(found_perm) == 1, "Expected to find role name %s in:\n%s" % (role_name, jstr(role_list))
def testrun_has_summary_counts(step, stored_testrun, testrun_name): trModel = TestrunModel() testrun = trModel.get_stored_or_store_obj(stored_testrun, testrun_name) # get the list of testcases for this testrun summary_list = trModel.get_summary_list(testrun) # walk through and verify that each testcase has the expected status for category in step.hashes: # find that in the list of testcases status_id = get_result_status_id(category["name"]) categoryInfo = verify_single_item_in_list(summary_list, "categoryName", status_id) assert str(categoryInfo[ns("categoryValue")]) == category["count"], \ "%s count was wrong. Expected categoryName: %s , categoryValue: %s:\n%s" % \ (category["name"], status_id, category["count"], jstr(categoryInfo))
def search_and_verify(self, uri, search_args, expect_to_find, tcm_type = None): ''' This does a search based on the search_args passed in. So "expect_to_find" is really filtered based on those parameters. expect_to_find: If True, then we verify based on expecting to find something. If False, this will fail if we get a resultset greater than 0. ''' if tcm_type == None: tcm_type = self.singular resp_list = self.get_list_from_search(uri, params = search_args) if not expect_to_find: eq_(len(resp_list), 0, "expect result size zero:\n" + jstr(resp_list)) else: # we want to verify just ONE of the items returned. Indeed, we likely # expect only one. So we pick the first item returned verify_single_item_in_list(resp_list, params = search_args)
def get_single_item_from_endpoint(self, uri = None, tcm_type = None, headers = get_json_headers()): ''' This hits an endpoint. No searchResult or ArrayOfXXXX part here ''' if uri == None: uri = self.root_path if tcm_type == None: tcm_type = self.singular response_txt = do_get(uri, headers = headers) tcm_obj = json_to_obj(response_txt) try: item = tcm_obj[ns(tcm_type)][0] self.store_latest(item) return item except KeyError: assert False, "%s\nDidn't find %s in %s" % (str(KeyError), ns(tcm_type), jstr(tcm_obj))
def testcase_has_status_of_approved(step, stored, testcase_name): tcModel = TestcaseModel() testcase_name = tcModel.get_stored_or_store_name(stored, testcase_name) # fetch the steps for this testcase from the latestversion testcase_id = tcModel.get_resid(testcase_name)[0] testcaseversion = tcModel.get_latestversion(testcase_id) # should be just one try: eq_(testcaseversion[ns("approvalStatusId")], 2, "Testcase is approved: " + str(testcaseversion)) except KeyError: assert False, "Object field mismatch.\nExpected:\n" + ns("approved") + "\n\nActual:\n" + jstr(testcaseversion)
def testcases_have_approval_statuses(step): tcModel = TestcaseModel() for tc in step.hashes: testcase_id = tcModel.get_resid(tc["name"])[0] testcaseversion = tcModel.get_latestversion(testcase_id) # should be just one try: eq_(testcaseversion[ns("approvalStatusId")], 2, "Testcase is approved: " + str(testcaseversion)) except KeyError: assert False, "Object field mismatch.\nExpected:\n" + ns("approved") + "\n\nActual:\n" + jstr(testcaseversion)