Skip to content
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: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ChangeLog
master (unreleased)
===================

Nothing here yet.
- Added tests against the ``formidable.yml`` schema definition of Forms (#295).

Release 1.3.0 (2018-02-14)
==========================
Expand Down
3 changes: 3 additions & 0 deletions docs/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool:pytest]
; intentionally left blank
; it voids the defaults that live in the setup.cfg file at the root of the repository.
52 changes: 52 additions & 0 deletions docs/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from os.path import abspath, join, dirname
import json
import collections

from jsonschema import Draft4Validator
import yaml

ROOT_PATH = dirname(abspath(__file__))


def yaml2json():
"""
Builds a dictionary of the BuilderForm definition out of the Swagger YAML.
"""
# Setup support for ordered dicts so we do not lose ordering
# when importing from YAML
_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG

def dict_representer(dumper, data):
return dumper.represent_mapping(_mapping_tag, data.iteritems())

def dict_constructor(loader, node):
return collections.OrderedDict(loader.construct_pairs(node))

yaml.add_representer(collections.OrderedDict, dict_representer)
yaml.add_constructor(_mapping_tag, dict_constructor)

# Building raw data out of the Swagger Formidable definition
swagger_file = join(ROOT_PATH, '..', 'swagger', 'formidable.yml')
with open(swagger_file) as fd:
data = yaml.load(fd)

to_exclude = (
'BuilderForm', 'InputError', 'BuilderError', 'InputForm', 'InputField'
)
definitions = (item for item in data['definitions'].items())
definitions = filter(lambda item: item[0] not in to_exclude, definitions)

new_data = {}
new_data.update(data['definitions']['BuilderForm'])
new_data.update({"definitions": dict(definitions)})
return new_data


def _load_fixture(path):
if not path.startswith('fixtures/'):
path = 'fixtures/{}'.format(path)
return json.load(open(join(ROOT_PATH, path)))


schema = yaml2json()
validator = Draft4Validator(schema)
1 change: 1 addition & 0 deletions docs/tests/fixtures/0000_empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions docs/tests/fixtures/0001_just_id.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": 1
}
4 changes: 4 additions & 0 deletions docs/tests/fixtures/0002_id_label.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": 1,
"label": "This is my form title"
}
5 changes: 5 additions & 0 deletions docs/tests/fixtures/0003_id_not_integer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "hello",
"label": "This is my form title",
"description": "Here's the description of my form."
}
5 changes: 5 additions & 0 deletions docs/tests/fixtures/0004_id_label_description.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": 1,
"label": "This is my form title",
"description": "Here's the description of my form."
}
6 changes: 6 additions & 0 deletions docs/tests/fixtures/0010_wrong_type_field_dict.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason the number in the file name goes from 0004 to 0010? (also some numbers are similar)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to work using "blocks" of similar tests and "space" them to make sure I could squeeze more tests in the blocks without having to rename all my fixtures.
The numbers are following the schema definition logic, from the most basic to the most complicated.
On top of that, they haven't been written in order, I've jumped from one idea to another, so I've put "spaces" when I was starting to write a test that was a bit ahead, so sometimes there's a gap between fixture numbers.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the answer :)

"id": 1,
"label": "This is my form title",
"description": "Here's the description of my form.",
"fields": {"hello": "world"}
}
6 changes: 6 additions & 0 deletions docs/tests/fixtures/0010_wrong_type_field_int.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": 1,
"label": "This is my form title",
"description": "Here's the description of my form.",
"fields": 42
}
6 changes: 6 additions & 0 deletions docs/tests/fixtures/0011_empty_fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": 1,
"label": "This is my form title",
"description": "Here's the description of my form.",
"fields": []
}
8 changes: 8 additions & 0 deletions docs/tests/fixtures/0012_fields_empty_object.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": 1,
"label": "This is my form title",
"description": "Here's the description of my form.",
"fields": [
{}
]
}
15 changes: 15 additions & 0 deletions docs/tests/fixtures/0012_fields_ok.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": 1,
"label": "This is my form title",
"description": "Here's the description of my form.",
"fields": [
{
"id": 1,
"label": "Name",
"slug": "name",
"type_id": "text",
"description": "please put your name here",
"accesses": []
}
]
}
16 changes: 16 additions & 0 deletions docs/tests/fixtures/0013_fields_placeholder.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": 1,
"label": "This is my form title",
"description": "Here's the description of my form.",
"fields": [
{
"id": 1,
"label": "Name",
"slug": "name",
"type_id": "text",
"description": "please put your name here",
"accesses": [],
"placeholder": "This is my placeholder"
}
]
}
17 changes: 17 additions & 0 deletions docs/tests/fixtures/0014_fields_multiple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": 1,
"label": "This is my form title",
"description": "Here's the description of my form.",
"fields": [
{
"id": 1,
"label": "Name",
"slug": "name",
"type_id": "text",
"description": "please put your name here",
"accesses": [],
"placeholder": "This is my placeholder",
"multiple": true
}
]
}
21 changes: 21 additions & 0 deletions docs/tests/fixtures/0015_fields_items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"id": 1,
"label": "This is my form title",
"description": "Here's the description of my form.",
"fields": [
{
"id": 1,
"label": "Name",
"slug": "name",
"type_id": "text",
"description": "please put your name here",
"accesses": [],
"placeholder": "This is my placeholder",
"multiple": true,
"items": [
{"label": "first value", "value": "FIRST", "description": "This is my first value"},
{"label": "second value", "value": "SECOND", "description": "This is the last value"}
]
}
]
}
20 changes: 20 additions & 0 deletions docs/tests/fixtures/0016_fields_accesses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"id": 1,
"label": "This is my form title",
"description": "Here's the description of my form.",
"fields": [
{
"id": 1,
"label": "Name",
"slug": "name",
"type_id": "text",
"description": "please put your name here",
"accesses": [],
"placeholder": "This is my placeholder",
"accesses": [
{"access_id": "JEDI", "level": "REQUIRED"},
{"access_id": "PADAWAN", "level": "EDITABLE"}
]
}
]
}
20 changes: 20 additions & 0 deletions docs/tests/fixtures/0017_fields_validations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"id": 1,
"label": "This is my form title",
"description": "Here's the description of my form.",
"fields": [
{
"id": 1,
"label": "Name",
"slug": "name",
"type_id": "text",
"description": "please put your name here",
"accesses": [],
"placeholder": "This is my placeholder",
"validations": [
{"type": "MINLENGTH", "value": "5", "message": "Your name is too short"},
{"type": "MAXLENGTH", "value": "100", "message": "Your name is too long"}
]
}
]
}
17 changes: 17 additions & 0 deletions docs/tests/fixtures/0018_fields_defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": 1,
"label": "This is my form title",
"description": "Here's the description of my form.",
"fields": [
{
"id": 1,
"label": "Name",
"slug": "name",
"type_id": "text",
"description": "please put your name here",
"accesses": [],
"placeholder": "This is my placeholder",
"defaults": ["This is your default"]
}
]
}
60 changes: 60 additions & 0 deletions docs/tests/fixtures/0020_simple_condition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"id": 1,
"label": "The Game Form",
"description": "A form to pick a cool game",
"fields": [
{
"id": 1,
"slug": "do-you-want-to-play-games",
"label": "Do you want to play games?",
"type_id": "checkbox",
"description": "",
"accesses": []
},
{
"id": 2,
"slug": "favorite-game",
"label": "Favorite game",
"type_id": "dropdown",
"description": "",
"accesses": [],
"items": [
{
"value": "BORING",
"label": "Monopoly"
},
{
"value": "YES",
"label": "Magic Maze"
}
],
"multiple": false
},
{
"id": 3,
"slug": "please-explain",
"label": "Please, explain...",
"type_id": "text",
"description": "Please explain why it matters...",
"accesses": []
}
],
"conditions": [
{
"name": "Jeux",
"field_ids": [
"favorite-game"
],
"action": "display_iff",
"tests": [
{
"field_id": "do-you-want-to-play-games",
"operator": "eq",
"values": [
"true"
]
}
]
}
]
}
7 changes: 7 additions & 0 deletions docs/tests/test_check_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from jsonschema import Draft4Validator
from . import schema


def test_validate_schema():
# First, check Schema
assert Draft4Validator.check_schema(schema) is None
Loading