-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from malinkinsa/0.0.2
Now, we correctly distinguish between root models and child models by checking if a model is only referenced but never itself a reference. And now child models correctly recognize nested models and process them as properties in Elasticsearch mappings.
- Loading branch information
Showing
6 changed files
with
254 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
import pytest | ||
|
||
from pydantic2es.converters.dict2mapping import dict_to_mapping | ||
|
||
|
||
@pytest.fixture | ||
def first_sample(): | ||
return { | ||
'address': { | ||
'city': 'str', 'street': 'str', 'zip_code': 'str' | ||
}, | ||
'age': 'int', | ||
'hobbies': | ||
'list[str]', | ||
'name': 'str' | ||
} | ||
|
||
@pytest.fixture | ||
def second_sample(): | ||
return { | ||
'user': { | ||
'name': 'str', | ||
'age': 'int', | ||
'address': { | ||
'city': 'str', | ||
'street': 'str', | ||
'zip_code': 'str', | ||
'geo': { | ||
'latitude': 'float', | ||
'longitude': 'float' | ||
} | ||
}, | ||
'hobbies': 'list[str]' | ||
} | ||
} | ||
|
||
def test_dict_to_mapping(first_sample, second_sample): | ||
first_mapping = dict_to_mapping(first_sample, 'nested', text_fields=[]) | ||
second_mapping = dict_to_mapping(second_sample, 'nested', text_fields=[]) | ||
|
||
expected_first_mapping = { | ||
"mappings": { | ||
"properties": { | ||
"name": { | ||
"type": "keyword" | ||
}, | ||
"age": { | ||
"type": "integer" | ||
}, | ||
"address": { | ||
"type": "nested", | ||
"properties": { | ||
"street": { | ||
"type": "keyword" | ||
}, | ||
"city": { | ||
"type": "keyword" | ||
}, | ||
"zip_code": { | ||
"type": "keyword" | ||
} | ||
} | ||
}, | ||
"hobbies": { | ||
"type": "keyword" | ||
} | ||
} | ||
} | ||
} | ||
|
||
expected_second_mapping = { | ||
"mappings": { | ||
"properties": { | ||
"user": { | ||
"type": "nested", | ||
"properties": { | ||
"name": { | ||
"type": "keyword" | ||
}, | ||
"age": { | ||
"type": "integer" | ||
}, | ||
"address": { | ||
"type": "nested", | ||
"properties": { | ||
"city": { | ||
"type": "keyword" | ||
}, | ||
"street": { | ||
"type": "keyword" | ||
}, | ||
"zip_code": { | ||
"type": "keyword" | ||
}, | ||
"geo": { | ||
"type": "nested", | ||
"properties": { | ||
"latitude": { | ||
"type": "float" | ||
}, | ||
"longitude": { | ||
"type": "float" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"hobbies": { | ||
"type": "keyword" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
assert second_mapping == expected_second_mapping | ||
assert first_mapping == expected_first_mapping | ||
|
||
def test_dict_to_mapping_with_test_and_object(first_sample, second_sample): | ||
first_mapping = dict_to_mapping(first_sample, 'object', text_fields=['name']) | ||
second_mapping = dict_to_mapping(second_sample, 'object', text_fields=['name']) | ||
|
||
first_expected_mapping = { | ||
"mappings": { | ||
"properties": { | ||
"name": { | ||
"type": "text" | ||
}, | ||
"age": { | ||
"type": "integer" | ||
}, | ||
"address": { | ||
"type": "object", | ||
"properties": { | ||
"street": { | ||
"type": "keyword" | ||
}, | ||
"city": { | ||
"type": "keyword" | ||
}, | ||
"zip_code": { | ||
"type": "keyword" | ||
} | ||
} | ||
}, | ||
"hobbies": { | ||
"type": "keyword" | ||
} | ||
} | ||
} | ||
} | ||
|
||
expected_second_mapping = { | ||
"mappings": { | ||
"properties": { | ||
"user": { | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "text" | ||
}, | ||
"age": { | ||
"type": "integer" | ||
}, | ||
"address": { | ||
"type": "object", | ||
"properties": { | ||
"city": { | ||
"type": "keyword" | ||
}, | ||
"street": { | ||
"type": "keyword" | ||
}, | ||
"zip_code": { | ||
"type": "keyword" | ||
}, | ||
"geo": { | ||
"type": "object", | ||
"properties": { | ||
"latitude": { | ||
"type": "float" | ||
}, | ||
"longitude": { | ||
"type": "float" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"hobbies": { | ||
"type": "keyword" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
assert second_mapping == expected_second_mapping | ||
assert first_mapping == first_expected_mapping |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import pytest | ||
|
||
from pydantic2es.helpers.helpers import struct_dict | ||
|
||
|
||
@pytest.fixture | ||
def converted_models(): | ||
return { | ||
'Address': { | ||
'city': 'str', | ||
'street': 'str', | ||
'zip_code': 'str' | ||
}, | ||
'User': { | ||
'name': 'str', | ||
'age': 'int', | ||
'address': 'Address', | ||
'hobbies': 'list[str]' | ||
} | ||
} | ||
|
||
|
||
def test_struct_dict_user_address(converted_models): | ||
result = struct_dict(converted_models) | ||
|
||
assert isinstance(result, dict) | ||
assert isinstance(result["address"], dict) | ||
|
||
assert 'User' not in result | ||
assert "address" in result | ||
assert "city" in result["address"] | ||
assert "street" in result["address"] | ||
assert "zip_code" in result["address"] | ||
assert result["name"] == "str" | ||
assert result["age"] == "int" | ||
assert result["hobbies"] == "list[str]" |