Permalink
Browse files

Add test suite based on docs/serialization.md [changelog skip]

  • Loading branch information...
kanghyojun committed May 16, 2018
1 parent 396b921 commit 39e91ac685d8cee46ab37a407c8a598109c5ec96
@@ -138,3 +138,7 @@ record name-shadowing-field-record (
uuid uuid,
binary bytes,
);
union optional-union = foo ( int32? bar )
| baz ( int32 qux )
;
@@ -0,0 +1,3 @@
record foo (
int64 bar/baz,
);
@@ -0,0 +1,4 @@
record Payload (
text FIELD_NAME,
float64 second-field-name,
);
@@ -29,6 +29,8 @@ enum month
unboxed enum-unboxed (month);
unboxed enum-set-unboxed ({month});
record product (
text name,
int32 stock,
@@ -46,3 +48,5 @@ union post
unboxed union-unboxed (post);
unboxed unboxed-unboxed (record-unboxed);
unboxed list-unboxed ([text]);
@@ -9,6 +9,7 @@
import pprint
from typing import _type_repr
from jsonpath_ng import parse
from pytest import mark
@@ -35,6 +36,17 @@ def list_specs(path):
)
def normalize(path, value):
parser = parse(path)
for match in parser.find(value):
try:
match.value.sort(key=lambda x: json.dumps(x, sort_keys=True))
except AttributeError:
raise AssertionError('$.{!s} is not an array.'.format(
match.full_path
))
@mark.parametrize('spec_file', list_specs(test_suite_dir))
def test_serializer_deserializer(spec_file):
with io.open(spec_file, 'r', encoding='utf-8') as f:
@@ -66,6 +78,9 @@ def test_serializer_deserializer(spec_file):
print('Normal:', dump_json(spec['normal']))
serialized = deserialized.__nirum_serialize__()
print('Serialized:', dump_json(serialized))
if 'ignoreOrder' in spec:
normalize(spec['ignoreOrder'], spec['normal'])
normalize(spec['ignoreOrder'], serialized)
assert serialized == spec['normal']
else:
print('Expected errors:', dump_json(spec['errors']))
@@ -25,6 +25,8 @@ def test_setup_metadata():
'fixture.types', 'fixture.alias',
'renamed', 'renamed.foo', 'renamed.foo.bar',
'fixture.datetime',
'fixture.name',
'fixture.norm',
}
assert ['0.3.0'] == pkg['Version']
assert ['Package description'] == pkg['Summary']
@@ -46,6 +48,8 @@ def test_module_entry_points():
'fixture.alias',
'renames.test.foo', 'renames.test.foo.bar',
'fixture.datetime',
'fixture.name',
'fixture.norm',
}
import fixture.foo
assert map_['fixture.foo'].resolve() is fixture.foo
@@ -0,0 +1,6 @@
{
"description": "enum",
"type": "fixture.foo.gender",
"input": "yeoseong",
"normal": "yeoseong"
}
@@ -0,0 +1,6 @@
{
"description": "enum",
"type": "fixture.foo.gender",
"input": "male",
"normal": "male"
}
@@ -0,0 +1,15 @@
FIXME: To follow a general principle of robustness, this test should be successful.
{
"description": "All identifiers has to be normalized.",
"type": "fixture.norm.payload",
"input": {
"_type": "payload",
"FIELD_NAME": "FIELD_NAME becomes to field_name",
"second-field-name": 3.14
},
"normal": {
"_type": "payload",
"field_name": "FIELD_NAME becomes to field_name",
"second_field_name": 3.14
}
}
@@ -0,0 +1,21 @@
{
"description": "Parse a map type.",
"type": "fixture.foo.record-with-map",
"input": {
"_type": "record_with_map",
"text_to_text": [
{"key": "a", "value": "foo"},
{"key": "b", "value": "bar"},
{"key": "c", "value": "baz"}
]
},
"normal": {
"_type": "record_with_map",
"text_to_text": [
{"key": "a", "value": "foo"},
{"key": "b", "value": "bar"},
{"key": "c", "value": "baz"}
]
},
"ignoreOrder": "$.text_to_text"
}
@@ -0,0 +1,12 @@
{
"description": "Name can have behind name.",
"type": "fixture.name.foo",
"input": {
"_type": "foo",
"baz": 1
},
"normal": {
"_type": "foo",
"baz": 1
}
}
@@ -0,0 +1,10 @@
{
"description": "Nirum date type is represented as RFC 3339 date in JSON.",
"type": "fixture.types.date-list",
"input": [
"2018-05-10"
],
"normal": [
"2018-05-10"
]
}
@@ -0,0 +1,13 @@
{
"description": "_type can be emitted.",
"type": "fixture.foo.person",
"input": {
"first_name": "foo",
"last_name": "bar"
},
"normal": {
"_type": "person",
"first_name": "foo",
"last_name": "bar"
}
}
@@ -0,0 +1,15 @@
{
"description": "Parse object has more fields than definition.",
"type": "fixture.foo.person",
"input": {
"_type": "person",
"first_name": "foo",
"last_name": "bar",
"extra": "some"
},
"normal": {
"_type": "person",
"first_name": "foo",
"last_name": "bar"
}
}
@@ -0,0 +1,11 @@
{
"description": "If optional field isn't given, it should treat value of optional field is null.",
"type": "fixture.foo.record-with-optional-record-field",
"input": {
"_type": "record_with_optional_record_field"
},
"normal": {
"_type": "record_with_optional_record_field",
"f": null
}
}
@@ -0,0 +1,12 @@
{
"description": "If a field is optional, it is ok to give null.",
"type": "fixture.foo.record-with-optional-record-field",
"input": {
"_type": "record_with_optional_record_field",
"f": null
},
"normal": {
"_type": "record_with_optional_record_field",
"f": null
}
}
@@ -0,0 +1,35 @@
{
"description": "Parse a set of record.",
"type": "fixture.foo.people",
"input": {
"_type": "people",
"people": [
{
"_type": "person",
"first_name": "John",
"last_name": "Doe"
},
{
"_type": "person",
"first_name": "John",
"last_name": "Smith"
}
]
},
"normal": {
"_type": "people",
"people": [
{
"_type": "person",
"first_name": "John",
"last_name": "Doe"
},
{
"_type": "person",
"first_name": "John",
"last_name": "Smith"
}
]
},
"ignoreOrder": "$.people"
}
@@ -0,0 +1,6 @@
{
"description": "If an unboxed type consists of a list type it also has to be represented in the same way to its inner type.",
"type": "fixture.types.list-unboxed",
"input": ["september", "april"],
"normal": ["september", "april"]
}
@@ -0,0 +1,7 @@
{
"description": "If an unboxed type consists of a set type it also has to be represented in the same way to its inner type.",
"type": "fixture.types.enum-set-unboxed",
"input": ["february", "september"],
"normal": ["february", "september"],
"ignoreOrder": "$"
}
@@ -0,0 +1,13 @@
{
"description": "If optional field isn't given, it should treat value of optional field is null.",
"type": "fixture.foo.optional-union",
"input": {
"_tag": "foo",
"_type": "optional_union"
},
"normal": {
"_tag": "foo",
"_type": "optional_union",
"bar": null
}
}
View
@@ -15,6 +15,7 @@ skip_install = true
deps =
six
flake8
jsonpath-ng
pytest
py27: python-dateutil
commands =

0 comments on commit 39e91ac

Please sign in to comment.