Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow types to be defined by bare type expressions #145

Merged
merged 1 commit into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"


@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"