def test_should_extract_inherited_fields_with_bases(sub_model): spec = model_to_spec(sub_model, inheritance=True) assert ":bases" in spec["field"] assert spec["field"][":bases"] == {'basic': 'implements'} assert "field" in spec assert spec["field"]["type"] == 'string'
def test_should_extract_overridden_fields_with_overrides(override_model): spec = model_to_spec(override_model, inheritance=False) assert ":bases" not in spec["field"] assert ":overrides" not in spec["field"] assert "field" in spec assert spec["field"] == {'type': 'string', 'required': True} assert "other_field" in spec
def test_should_resolve_compond_types(blog_module): spec = model_to_spec(blog_module.BlogModel, inheritance=False) assert "posts" in spec assert "type" in spec["posts"] assert spec["posts"]["type"] == 'list' assert spec["posts"]["list"] == { 'type': 'model', 'model': 'post', }
def test_should_extract_overridden_fields(override_model): for inheriance_option in ["overrides", True]: spec = model_to_spec(override_model, inheritance=inheriance_option) assert ":overrides" in spec["field"] assert spec["field"][":overrides"] == ['basic'] assert "field" in spec assert spec["field"]["type"] == 'string' assert spec["field"]["required"] is True
def extract_specs(): parser = argparse.ArgumentParser( description='Script to extract the acspec spec from schematis models') parser.add_argument('input', help='specify the package or module that contains ' 'the schematcs models') parser.add_argument('-o', '--output', help='specify the output file name (default: STDOUT)', default=sys.stdout) parser.add_argument('-d', '--output-directory', help='specify the output directoy to dump every ' 'model in a separate file') parser.add_argument('-n', '--normalize', default=False, action='store_true', help='normalize field names') parser.add_argument('-i', '--inheritance', default="overrides", choices=["false", "overrides", "true"], help='deactivate to print :overrides, print inherited ' 'fields, too') args = parser.parse_args() root = __import__(args.input) module = root for level in args.input.split(".")[1:]: module = getattr(module, level) classes = find_model_classes(module) specs = {} for cls in classes: model_name = get_short_name(cls.__name__) specs[model_name] = model_to_spec(cls) if args.output_directory: mkdir_p(args.output_directory) print("Write output to: {}".format(args.output_directory)) for model_name, model_spec in iteritems(specs): path = os.path.join(args.output_directory, model_name + ".yml") stream = open(path, "w") yaml.safe_dump(model_spec, stream, default_flow_style=False) else: stream = args.output if isinstance(stream, string_types): stream = open(stream, "w") print("Write output to: {}".format(args.output)) yaml.safe_dump(specs, stream, default_flow_style=False)
def test_should_extract_bases_for_overridden_fields(sub_override_model): spec = model_to_spec(sub_override_model, inheritance=True) assert "field" in spec assert ":overrides" not in spec["field"] assert ":bases" in spec["field"] # override is the model's name (implied from OverrideModel) assert "override" in spec["field"][":bases"] assert spec["field"][":bases"]["override"] == {':overrides': ['basic']} assert "other_field" in spec assert ":overrides" in spec["other_field"] assert spec["other_field"][":overrides"] == ['override']
def test_should_exted_inherited_fields_without_bases(sub_model): spec = model_to_spec(sub_model, inheritance=False) assert ":bases" not in spec["field"] assert "field" in spec assert spec["field"] == {'type': 'string'}
def test_should_extract_spec(basic_model): spec = model_to_spec(basic_model) assert "field" in spec assert spec["field"] == {'type': 'string'}
def test_should_not_extract_inherited_fields_by_default(sub_model): spec = model_to_spec(sub_model) assert "field" not in spec
def test_should_extract_bases(sub_model): spec = model_to_spec(sub_model) assert ":bases" in spec assert spec[":bases"] == ['basic']