Skip to content

Commit

Permalink
Implement schema function
Browse files Browse the repository at this point in the history
  • Loading branch information
jtv8 committed May 22, 2020
1 parent 636ef19 commit 7b03ee4
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 2 deletions.
83 changes: 83 additions & 0 deletions features/dict.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
Feature: Test JSON DOM objects

Scenario: Test good input string

Given the Python module dict_module.py
When we execute the following python code:
"""
example_dict_input = {
"first_name": "Marge",
"last_name": "Simpson",
"current_address": {
"first_line": "123 Fake Street",
"second_line": "",
"city": "Springfield",
"postal_code": 58008
},
"previous_addresses": [{
"first_line": "742 Evergreen Terrace",
"second_line": "",
"city": "Springfield",
"postal_code": 58008
}],
"vehicles": {
"eabf04": {
"color": "orange",
"description": "Station Wagon"
}
}
}
example = dict_module.Person(example_dict_input)
example_dict_output = example.to_builtin()
"""
Then the following statements are true:
"""
example.first_name == "Marge"
example.last_name == "Simpson"
example.current_address.first_line == "123 Fake Street"
example.current_address.second_line == ""
example.current_address.city == "Springfield"
example.current_address.postal_code == 58008
example["current_address"].first_line == "123 Fake Street"
example["current_address"].second_line == ""
example["current_address"].city == "Springfield"
example["current_address"].postal_code == 58008
example.previous_addresses[0].first_line == "742 Evergreen Terrace"
example.previous_addresses[0].second_line == ""
example.previous_addresses[0].city == "Springfield"
example.previous_addresses[0].postal_code == 58008
example.vehicles["eabf04"].color == "orange"
example.vehicles["eabf04"].description == "Station Wagon"
example.vehicles["eabf04"].license == "eabf04"
len(example) == 5
len(example.current_address) == 4
len(example.previous_addresses) == 1
len(example.vehicles) == 1
parent(example.current_address) is example
document(example.current_address) is example
key(example.current_address) == "current_address"
parent(example.vehicles["eabf04"]) is example.vehicles
document(example.vehicles["eabf04"]) is example
key(example.vehicles["eabf04"]) == "eabf04"
parent(example["vehicles"]["eabf04"]) is example.vehicles
document(example["vehicles"]["eabf04"]) is example
key(example["vehicles"]["eabf04"]) == "eabf04"
schema(example).is_valid(example_dict_input)
example_dict_output == example_dict_input
"""

Scenario: Test bad input string

Given the Python module dict_module.py
When we execute the following python code:
"""
example_dict_input = {"foo": "bar"}
"""
Then the following statements are true:
"""
not(schema(example).is_valid(example_dict_input))
"""
And the following statement raises ValidationError
"""
dict_module.Person(example_dict_input)
"""
27 changes: 27 additions & 0 deletions features/examples/modules/dict_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Dict, List

from wysdom import UserObject, UserProperty, SchemaArray, SchemaDict, key


class Vehicle(UserObject):
color: str = UserProperty(str)
description: str = UserProperty(str)

@property
def license(self):
return key(self)


class Address(UserObject):
first_line: str = UserProperty(str)
second_line: str = UserProperty(str)
city: str = UserProperty(str)
postal_code: str = UserProperty(int)


class Person(UserObject):
first_name: str = UserProperty(str)
last_name: str = UserProperty(str)
current_address: Address = UserProperty(Address)
previous_addresses: List[Address] = UserProperty(SchemaArray(Address))
vehicles: Dict[str, Vehicle] = UserProperty(SchemaDict(Vehicle))
3 changes: 2 additions & 1 deletion features/steps/steps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from behave import *

from wysdom import document, parent, key
from wysdom import document, parent, key, schema

import os
import importlib.util
Expand Down Expand Up @@ -38,6 +38,7 @@ def step_impl(context):
assert callable(document)
assert callable(parent)
assert callable(key)
assert callable(schema)
for line in context.text.splitlines():
result = eval(line)
if not result:
Expand Down
2 changes: 1 addition & 1 deletion wysdom/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .__version__ import __version__
from . import mixins
from .exceptions import ValidationError
from .dom.functions import dom, document, parent, key
from .dom.functions import dom, document, parent, key, schema
from .dom import DOMInfo as DOMInfo
from .dom import DOMElement as Element
from .base_schema import Schema, SchemaAnything, SchemaConst
Expand Down
5 changes: 5 additions & 0 deletions wysdom/dom/functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Optional

from ..base_schema import Schema
from .DOMElement import DOMElement
from . import DOMInfo

Expand All @@ -18,3 +19,7 @@ def parent(element: DOMElement) -> Optional[DOMElement]:

def key(element: DOMElement) -> Optional[str]:
return dom(element).element_key


def schema(element: DOMElement) -> Schema:
return element.__json_schema__()

0 comments on commit 7b03ee4

Please sign in to comment.