Skip to content

Commit

Permalink
tests: Additional mapping generation tests
Browse files Browse the repository at this point in the history
* Adds mapping generation tests. They test more features provided by
  jsonschema.

Signed-off-by: Nicolas Harraudeau <nicolas.harraudeau@cern.ch>
  • Loading branch information
Nicolas Harraudeau committed Nov 20, 2015
1 parent 3c3a0b6 commit fc0da52
Showing 1 changed file with 204 additions and 0 deletions.
204 changes: 204 additions & 0 deletions tests/test_generate_mapping.py
Expand Up @@ -58,6 +58,210 @@ def test_simple_properties(self):
ElasticMappingGeneratorConfig())
assert result_mapping == es_mapping

def test_references(self):
"""Test mapping generation from a schema containing references."""
json_schema = {
'id': 'http://some.site/root_schema#',
'type': 'object',
'properties': {
'orig': {'type': 'string'},
'local_ref': {'$ref': '#/properties/orig'},
'ext_ref': {
'$ref':
'http://some.site/external_schema#/definitions/ext_def'
},
},
}
external_json_schema = {
'id': 'http://some.site/external_schema#',
'definitions': {
'ext_def': {'type': 'boolean'},
},
}
es_mapping = {
'_all': {'enable': True},
'numeric_detection': True,
'date_detection': True,
'properties': {
'orig': {'type': 'string'},
'local_ref': {'type': 'string'},
'ext_ref': {'type': 'boolean'},
},
}
result_mapping = schema_to_mapping(
json_schema, json_schema['id'], {
external_json_schema['id']: external_json_schema
}, ElasticMappingGeneratorConfig())
assert result_mapping == es_mapping

def test_allOf_anyOf_oneOf(self):
"""Test mapping generation from a schema containing (all|any|one)Of"""
json_schema = {
'id': 'http://some.site/root_schema#',
'anyOf': [{
'type': 'object',
'properties': {
'defined_twice1': {'type': 'string'},
'allof_attr': {
'allOf': [{
'type': 'object',
'properties': {
'attr1': {'type': 'string'},
'defined_twice2': {'type': 'string'},
},
}, {
'type': 'object',
'properties': {
'attr2': {'type': 'boolean'},
'defined_twice2': {'type': 'string'},
},
}]
},
},
}, {
'type': 'object',
'properties': {
'defined_twice1': {'type': 'string'},
'oneof_attr': {
'oneOf': [{
'type': 'object',
'properties': {
'attr3': {'type': 'string'},
'defined_twice3': {'type': 'string'},
},
}, {
'type': 'object',
'properties': {
'attr4': {'type': 'boolean'},
'defined_twice3': {'type': 'string'},
},
}]
},
},
}]
}
es_mapping = {
'_all': {'enable': True},
'numeric_detection': True,
'date_detection': True,
'properties': {
'defined_twice1': {'type': 'string'},
'allof_attr': {
'type': 'object',
'properties': {
'attr1': {'type': 'string'},
'attr2': {'type': 'boolean'},
'defined_twice2': {'type': 'string'},
},
},
'oneof_attr': {
'type': 'object',
'properties': {
'attr3': {'type': 'string'},
'attr4': {'type': 'boolean'},
'defined_twice3': {'type': 'string'},
},
},
},
}
result_mapping = schema_to_mapping(json_schema, json_schema['id'],
{},
ElasticMappingGeneratorConfig())
assert result_mapping == es_mapping

def test_complex_array(self):
"""Test mapping generation from schema containing a complex array."""
json_schema = {
'id': 'http://some.site/root_schema#',
'type': 'object',
'properties': {
'myarray': {
"type": "array",
"items": [{
"type": "object",
"properties": {
"first_attr": {
"type": "string",
},
},
}, {
"type": "object",
"properties": {
"second_attr": {
"type": "boolean",
},
},
}]
},
'myarray2': {
"type": "array",
"items": {
"type": "object",
"properties": {
"third_attr": {
"type": "string",
},
},
},
},
},
}
es_mapping = {
'_all': {'enable': True},
'numeric_detection': True,
'date_detection': True,
'properties': {
'myarray': {
'type': 'object',
'properties': {
'first_attr': {'type': 'string'},
'second_attr': {'type': 'boolean'},
},
},
'myarray2': {
'type': 'object',
'properties': {
'third_attr': {'type': 'string'},
},
},
},
}
result_mapping = schema_to_mapping(json_schema, json_schema['id'],
{},
ElasticMappingGeneratorConfig())
assert result_mapping == es_mapping

def test_depencency_extension(self):
"""Test ampping generation from schema containing a dependency"""
json_schema = {
'id': 'http://some.site/root_schema#',
'type': 'object',
'properties': {
"name": {"type": "string"},
},
"dependencies": {
"name": {
"properties": {
"address": {"type": "boolean"}
},
"required": ["address"]
}
}
}
es_mapping = {
'_all': {'enable': True},
'numeric_detection': True,
'date_detection': True,
'properties': {
'name': {'type': 'string'},
'address': {'type': 'boolean'},
},
}
result_mapping = schema_to_mapping(json_schema, json_schema['id'],
{},
ElasticMappingGeneratorConfig())
assert result_mapping == es_mapping

def test_type_mapping(self):
"""Test mapping a json type to another elasticsearch type."""
json_schema = {
Expand Down

0 comments on commit fc0da52

Please sign in to comment.