From aa9eaa001ddd16201c247092ff460cfbb58820a5 Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Wed, 24 May 2017 18:00:35 +0000 Subject: [PATCH] allow types to be defined by bare type expressions --- ramlfications/parser/parser.py | 2 + .../data_types/data_type_expressions.raml | 53 ++++++++++++++++ .../data_types/test_data_type_expressions.py | 62 +++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 tests/data/raml_10/data_types/data_type_expressions.raml create mode 100644 tests/integration/data_types/test_data_type_expressions.py diff --git a/ramlfications/parser/parser.py b/ramlfications/parser/parser.py index bed1699..5d91497 100644 --- a/ramlfications/parser/parser.py +++ b/ramlfications/parser/parser.py @@ -155,6 +155,8 @@ def __init__(self, data, root, config): self.resolve_from = ["method", "resource", "types", "traits", "root"] def create_node(self, name, raw): + if not isinstance(raw, dict): + raw = dict(type=raw) raw["errors"] = self.root.errors raw["config"] = self.root.config return parse_type(name, raw, self.root) diff --git a/tests/data/raml_10/data_types/data_type_expressions.raml b/tests/data/raml_10/data_types/data_type_expressions.raml new file mode 100644 index 0000000..434132d --- /dev/null +++ b/tests/data/raml_10/data_types/data_type_expressions.raml @@ -0,0 +1,53 @@ +#%RAML 1.0 + +types: + Org: + type: object + properties: + name: string + phone_number: + type: string + pattern: "\\d(-?\\d)+" + + Organization: Org + +# TODO: Arrays using [] notations not yet implemented. +# Organizations: Organization[] + + Snake: + type: object + properties: + length: + type: integer + description: Length in centimeters. + + Python: + type: Snake + properties: + kind: + type: string + enum: + - Aspidites + - Ball + - Burmese + - Carpet + - Green tree + - Liasis + - Morelia + - Reticulated + - Spotted + + Boa: + type: Snake + properties: + kind: + type: string + enum: + - Argentine + - Common northern + - Ecuadorian + - Red-tailed + - Tumbes Peru + +# TODO: Union types not yet implemented. +# FunPet: Python | Boa diff --git a/tests/integration/data_types/test_data_type_expressions.py b/tests/integration/data_types/test_data_type_expressions.py new file mode 100644 index 0000000..5b9d18b --- /dev/null +++ b/tests/integration/data_types/test_data_type_expressions.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function +""" +Tests for handling of data type expressions in RAML 1.0. + +""" + +import os +import pytest + +from ramlfications.parser import parse_raml +from ramlfications.config import setup_config +from ramlfications.utils import load_file + +from tests.base import RAML_10 + + +@pytest.fixture(scope="session") +def api(): + ramlfile = os.path.join(RAML_10, "data_types/data_type_expressions.raml") + loaded_raml = load_file(ramlfile) + conffile = os.path.join(RAML_10, "test-config.ini") + config = setup_config(conffile) + return parse_raml(loaded_raml, config) + + +def get_named_type(api, name): + t, = api.types.filter_by(name=name) + return t + + +def test_definition_by_expression(api): + typeobj = get_named_type(api, "Organization") + assert typeobj.example is None + assert typeobj.examples is None + assert typeobj.name == "Organization" + assert typeobj.type == "Org" + assert typeobj.description.raw == "" + assert typeobj.display_name == "Organization" + + +@pytest.mark.skip(reason="Union types not yet implemented.") +def test_array_definition_by_expression(api): + typeobj = get_named_type(api, "Organizations") + assert typeobj.example is None + assert typeobj.examples is None + assert typeobj.name == "Organizations" + assert typeobj.description.raw == "" + assert typeobj.display_name == "Organizations" + # TODO: Figure out what this should look like. + # assert typeobj.type == "Organization[]" + + +@pytest.mark.skip(reason="Union types not yet implemented.") +def test_definition_by_union_expression(api): + typeobj = get_named_type(api, "FunPet") + assert typeobj.example is None + assert typeobj.examples is None + assert typeobj.name == "FunPet" + assert typeobj.type == "Python | Boa" + assert typeobj.description.raw == "" + assert typeobj.display_name == "FunPet"