Skip to content

Commit

Permalink
Add test suite based on docs/serialization.md [changelog skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
kanghyojun committed May 16, 2018
1 parent 396b921 commit 39e91ac
Show file tree
Hide file tree
Showing 21 changed files with 217 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/nirum_fixture/fixture/foo.nrm
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,7 @@ record name-shadowing-field-record (
uuid uuid,
binary bytes,
);

union optional-union = foo ( int32? bar )
| baz ( int32 qux )
;
3 changes: 3 additions & 0 deletions test/nirum_fixture/fixture/name.nrm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
record foo (
int64 bar/baz,
);
4 changes: 4 additions & 0 deletions test/nirum_fixture/fixture/norm.nrm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
record Payload (
text FIELD_NAME,
float64 second-field-name,
);
4 changes: 4 additions & 0 deletions test/nirum_fixture/fixture/types.nrm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ enum month

unboxed enum-unboxed (month);

unboxed enum-set-unboxed ({month});

record product (
text name,
int32 stock,
Expand All @@ -46,3 +48,5 @@ union post
unboxed union-unboxed (post);

unboxed unboxed-unboxed (record-unboxed);

unboxed list-unboxed ([text]);
15 changes: 15 additions & 0 deletions test/python/serialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pprint
from typing import _type_repr

from jsonpath_ng import parse
from pytest import mark


Expand All @@ -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:
Expand Down Expand Up @@ -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']))
Expand Down
4 changes: 4 additions & 0 deletions test/python/setup_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/serialization/enums/enums-female.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"description": "enum",
"type": "fixture.foo.gender",
"input": "yeoseong",
"normal": "yeoseong"
}
6 changes: 6 additions & 0 deletions test/serialization/enums/enums-male.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"description": "enum",
"type": "fixture.foo.gender",
"input": "male",
"normal": "male"
}
15 changes: 15 additions & 0 deletions test/serialization/identifiers/normalize.json.ignore
Original file line number Diff line number Diff line change
@@ -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
}
}
21 changes: 21 additions & 0 deletions test/serialization/map-types/record.json
Original file line number Diff line number Diff line change
@@ -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"
}
12 changes: 12 additions & 0 deletions test/serialization/names/behind_name.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"description": "Name can have behind name.",
"type": "fixture.name.foo",
"input": {
"_type": "foo",
"baz": 1
},
"normal": {
"_type": "foo",
"baz": 1
}
}
10 changes: 10 additions & 0 deletions test/serialization/primitive-types/date.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
13 changes: 13 additions & 0 deletions test/serialization/records/emit-type.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
15 changes: 15 additions & 0 deletions test/serialization/records/extra-fields.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
11 changes: 11 additions & 0 deletions test/serialization/records/option-fields.json
Original file line number Diff line number Diff line change
@@ -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
}
}
12 changes: 12 additions & 0 deletions test/serialization/records/option-fields2.json
Original file line number Diff line number Diff line change
@@ -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
}
}
35 changes: 35 additions & 0 deletions test/serialization/sets/record.json
Original file line number Diff line number Diff line change
@@ -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"
}
6 changes: 6 additions & 0 deletions test/serialization/unboxed-types/list.json
Original file line number Diff line number Diff line change
@@ -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"]
}
7 changes: 7 additions & 0 deletions test/serialization/unboxed-types/set.json
Original file line number Diff line number Diff line change
@@ -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": "$"
}
13 changes: 13 additions & 0 deletions test/serialization/unions/optional-field.json
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ skip_install = true
deps =
six
flake8
jsonpath-ng
pytest
py27: python-dateutil
commands =
Expand Down

0 comments on commit 39e91ac

Please sign in to comment.