Skip to content

Commit

Permalink
allow types to be defined by bare type expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
freddrake committed May 24, 2017
1 parent 890396e commit 8e6e195
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ramlfications/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
53 changes: 53 additions & 0 deletions tests/data/raml_10/data_types/data_type_expressions.raml
Original file line number Diff line number Diff line change
@@ -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
62 changes: 62 additions & 0 deletions tests/integration/data_types/test_data_type_expressions.py
Original file line number Diff line number Diff line change
@@ -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"


def test_array_definition_by_expression(api):
pytest.skip("Union types not yet implemented.")
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[]"


def test_definition_by_union_expression(api):
pytest.skip("Union types not yet implemented.")
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"

0 comments on commit 8e6e195

Please sign in to comment.