From fc0da52f5b469798dcadf6fedb6f90f8d5646589 Mon Sep 17 00:00:00 2001 From: Nicolas Harraudeau Date: Fri, 20 Nov 2015 10:11:06 +0100 Subject: [PATCH] tests: Additional mapping generation tests * Adds mapping generation tests. They test more features provided by jsonschema. Signed-off-by: Nicolas Harraudeau --- tests/test_generate_mapping.py | 204 +++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) diff --git a/tests/test_generate_mapping.py b/tests/test_generate_mapping.py index db90dc1..ae22262 100644 --- a/tests/test_generate_mapping.py +++ b/tests/test_generate_mapping.py @@ -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 = {