def json_should_not_match_schema(context, s, j): """Carrega o JSON {jdata} no atributo 'test_json' no context.""" schema = json_schema.loads(getattr(context, s)) json_string = getattr(context, j) logging.info(schema) logging.info(json_string) assert not schema == json_string
def i_have_schema(context, schema): """Carrega o schema {schema} no atributo 'test_schema' no context.""" try: s = json_schema.loads(getattr(context, schema)) except: logging.info("### Erro ao carregar o schema ###") logging.info(repr(schema)) raise context.__setattr__("test_schema", s)
def json_should_match_schema(context, s, j): """Carrega o JSON {jdata} no atributo 'test_json' no context.""" schema = json_schema.loads(getattr(context, s)) json_string = getattr(context, j) try: if not schema == json_string: schema.full_check(json_string) except: logging.info(schema) logging.info(json_string) pass assert schema == json_string
def VerifyExpected(actual, expected, global_dict, json_file=None, case_sensitive=True): result = True actual_flattened = "" #match json structure exported from api_functions try: response_schema = expected["response_schema"] except: response_schema = "none" if response_schema == "match": if os.path.isfile(json_file): schema = create_schema(json_file) schema_object = json_schema.loads(schema) full_match_output = full_match_schema(actual, schema_object) if "fail" in full_match_output: result = False global_dict["debuglog"].write( "Schema match unsuccessful. Check schema_file for details.\n" ) else: result = True global_dict["debuglog"].write("Schema match successful\n") #Test run on the same web session writes to the same schema comparison report. global_dict["schema"] = open( global_dict["schema_folder"] + global_dict["schema_filename"], "a") global_dict["schema"].write( "\n*************************************%s************************************\n" % (json_file)) global_dict["schema"].write(full_match_output + "\n") global_dict["schema"].close() #last schema (irrespective of pass or fail) is always accessible from the Schema tab in the main navigation bar. global_dict["schema"] = open( global_dict["test_folder"] + "schema.txt", "w") global_dict["schema"].write(full_match_output + "\n") global_dict["schema"].close() else: global_dict["debuglog"].write( "Schema file doesn't exist. Write expected schema before attempting to match! \n" ) actual = json.loads(actual) old_actual = actual #store, in order to restore later try: for exp in expected.keys(): actual = old_actual #before next iteration starts if (exp == "rowcount") or (exp == "specific") or ( exp == "filter") or (exp == "should_fail"): continue try: if exp[:5] == "call_": function = getattr(sys.modules[__name__], exp[5:]) for json_path in expected[exp].keys(): result_function = function(json_path, actual, expected[exp][json_path], global_dict) result = result and result_function except: pass except: result = False return result