Skip to content

Commit

Permalink
Merge pull request #532 from drdavella/test-nested-array
Browse files Browse the repository at this point in the history
Add tests for validating nested arrays
  • Loading branch information
drdavella authored Aug 16, 2018
2 parents 3c9591d + 4896714 commit 2994900
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions asdf/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,82 @@ def test_large_literals():
print(buff.getvalue())


def test_nested_array():
s = {
'type': 'object',
'properties': {
'stuff': {
'type': 'array',
'items': {
'type': 'array',
'items': [
{ 'type': 'integer' },
{ 'type': 'string' },
{ 'type': 'number' },
],
'minItems': 3,
'maxItems': 3
}
}
}
}

good = dict(stuff=[[1, 'hello', 2], [4, 'world', 9.7]])
schema.validate(good, schema=s)

bads = [
dict(stuff=[[1, 2, 3]]),
dict(stuff=[12,'dldl']),
dict(stuff=[[12, 'dldl']]),
dict(stuff=[[1, 'hello', 2], [4, 5]]),
dict(stuff=[[1, 'hello', 2], [4, 5, 6]])
]

for b in bads:
with pytest.raises(ValidationError):
schema.validate(b, schema=s)


def test_nested_array_yaml(tmpdir):
schema_def = """
%YAML 1.1
---
type: object
properties:
stuff:
type: array
items:
type: array
items:
- type: integer
- type: string
- type: number
minItems: 3
maxItems: 3
...
"""
schema_path = tmpdir.join('nested.yaml')
schema_path.write(schema_def.encode())

schema_tree = schema.load_schema(str(schema_path))
schema.check_schema(schema_tree)

good = dict(stuff=[[1, 'hello', 2], [4, 'world', 9.7]])
schema.validate(good, schema=schema_tree)

bads = [
dict(stuff=[[1, 2, 3]]),
dict(stuff=[12,'dldl']),
dict(stuff=[[12, 'dldl']]),
dict(stuff=[[1, 'hello', 2], [4, 5]]),
dict(stuff=[[1, 'hello', 2], [4, 5, 6]])
]

for b in bads:
with pytest.raises(ValidationError):
schema.validate(b, schema=schema_tree)


@pytest.mark.importorskip('astropy')
def test_type_missing_dependencies():

Expand Down

0 comments on commit 2994900

Please sign in to comment.