Skip to content

Commit

Permalink
Test generator finalized
Browse files Browse the repository at this point in the history
  • Loading branch information
perenecabuto committed Feb 7, 2013
1 parent 3bbba69 commit 169c6d6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ It validates if the json validates with file.json_schema

It should **create (or replace)**
a fixture file called **json_schemas/immutable.json_schema**
and a test file called **immutable_jsonschema.py**
and a test file called **test_immutable_jsonschema.py**
in the current dir.

If you want to create it in another path use **--path /wanted/path/dir/**
Expand Down
35 changes: 31 additions & 4 deletions bin/jsonschema_generator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#import re
import os
import argparse
import re
import string

import json_schema_generator
from json_schema_generator import Recorder, Validator


Expand All @@ -27,15 +30,39 @@ def validate(args):


def homologate(args):
print "homolotate"
template_file_path = os.path.join(os.path.dirname(json_schema_generator.__file__), 'test_template.py.txt')
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 main():
parser = argparse.ArgumentParser()

default_parser = argparse.ArgumentParser(add_help=False)
default_parser.add_argument('json_source', type=str, help='url or file')
default_parser.add_argument('--path', dest='path', help='set path')
default_parser.add_argument('--path', dest='path', default='', help='set path')

subparsers = parser.add_subparsers(help='sub-command help')

Expand All @@ -48,7 +75,7 @@ def main():
parser_validate.set_defaults(func=validate)

parser_homologate = subparsers.add_parser('homologate', parents=[default_parser])
parser_homologate.add_argument('homolation_name', type=str, help='json schema file path')
parser_homologate.add_argument('homologation_name', type=str, help='json schema file path')
parser_homologate.set_defaults(func=homologate)

args = parser.parse_args()
Expand Down
19 changes: 18 additions & 1 deletion json_schema_generator/test_template.py.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
# -*- coding: utf-8 -*-

import os

from unittest import TestCase
from json_certifier.recorder import Recorder
from json_schema_generator import Validator
from urllib2 import urlopen

SERVICE_URL = "${service_url}"


class TestHomogate(TestCase):

def test_${homologation_name}_should_match_json_schema(self):
json_data = urlopen(SERVICE_URL).read()
json_schema_file_path = os.path.join("${json_schemas_dir}", "${json_schema_file_name}")
validator = Validator.from_path(json_schema_file_path)
is_valid = validator.assert_json(json_data)

self.assertTrue(is_valid, validator.error_message)

0 comments on commit 169c6d6

Please sign in to comment.