def homologate(args): template_file_path = os.path.join( os.path.dirname(json_schema_generator.__file__), 'test_template.py.tmpl') json_schemas_dir = os.path.join(args.path, 'json_schemas') json_schema_file_name = '%s.json_schema' % args.homologation_name json_schema_file_path = os.path.join(json_schemas_dir, json_schema_file_name) test_file_path = os.path.join( args.path, 'test_%s_json_schema.py' % args.homologation_name) with open(template_file_path) as template_file: tmpl = string.Template(template_file.read()) if not os.path.exists(json_schemas_dir): os.mkdir(json_schemas_dir) if not os.path.exists(json_schema_file_path): rec = Recorder.from_url(args.json_source) rec.save_json_schema(json_schema_file_path, indent=4) rendered = tmpl.substitute(homologation_name=args.homologation_name, service_url=args.json_source, json_schema_file_name=json_schema_file_name, json_schemas_dir=json_schemas_dir) with open(test_file_path, 'w') as test_file: test_file.write(rendered)
def homologate(args): template_file_path = os.path.join(os.path.dirname(json_schema_generator.__file__), 'test_template.py.tmpl') json_schemas_dir = os.path.join(args.path, 'json_schemas') json_schema_file_name = '%s.json_schema' % args.homologation_name json_schema_file_path = os.path.join(json_schemas_dir, json_schema_file_name) test_file_path = os.path.join(args.path, 'test_%s_json_schema.py' % args.homologation_name) with open(template_file_path) as template_file: tmpl = string.Template(template_file.read()) if not os.path.exists(json_schemas_dir): os.mkdir(json_schemas_dir) if not os.path.exists(json_schema_file_path): rec = Recorder.from_url(args.json_source) rec.save_json_schema(json_schema_file_path, indent=4) rendered = tmpl.substitute( homologation_name=args.homologation_name, service_url=args.json_source, json_schema_file_name=json_schema_file_name, json_schemas_dir=json_schemas_dir ) with open(test_file_path, 'w') as test_file: test_file.write(rendered)
def test_recorder_should_get_json_from_url(self, fake_urlopen=None): fake_urlopen.is_callable().expects_call().returns_fake() \ .provides('read').returns(fixtures.json_1) rec = Recorder.from_url(self.service_url) rec.save_json_schema(self.json_schema_file_path) expected = json.loads(fixtures.json_schema_1) gotten = json.load(open(self.json_schema_file_path)) self.assertEqual(gotten, expected)
def record(rest_url, json_schema_file_path): """ The function generates the JSON schema from the json response returned by given REST API """ rec = Recorder.from_url(rest_url) rec.save_json_schema(json_schema_file_path, indent=4) # To check file is empty or not if (os.stat(json_schema_file_path).st_size == 0): raise Exception("REST API %s not returned JSON, file %s is empty " % (rest_url, json_schema_file_path))
def validate(args): from six.moves.urllib.request import urlopen json_data = Recorder.open_with_basic_auth(args.json_source, args.auth).read() validator = Validator.from_path(args.json_schema_file_path) is_valid = validator.assert_json(json_data) if is_valid: print(" * JSON is valid") else: print(" ! JSON is broken ") print(validator.error_message)
def record(args): rec = Recorder.from_url(args.json_source) rec.save_json_schema(args.json_schema_file_path, indent=4)
def record(args): if (os.path.isfile(args.json_source)): rec = Recorder.from_file(args.json_source) else: rec = Recorder.from_url(args.json_source) rec.save_json_schema(args.json_schema_file_path, indent=4)