Skip to content

Commit

Permalink
test case to validate with two level in json object
Browse files Browse the repository at this point in the history
  • Loading branch information
msaipraneeth committed Dec 14, 2023
1 parent e77e2e6 commit 9d0ae54
Showing 1 changed file with 71 additions and 3 deletions.
74 changes: 71 additions & 3 deletions tests/test_utils_build_entities_from_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def test_single_object():
"""test write to single object"""
"""test generation of entities and schema for a simple JSON object."""
test_data = """
{
"name": "sai",
Expand All @@ -29,7 +29,8 @@ def test_single_object():


def test_single_object_one_level():
"""test write to single object with one level"""
"""test generation of entities and schema for a JSON object with one level of
hierarchy"""
test_data = """
{
"name": "sai",
Expand Down Expand Up @@ -70,7 +71,8 @@ def test_single_object_one_level():


def test_single_object_one_level_array():
"""test write to single object with array in first level"""
"""test generation of entities and schema for a JSON object with array object in
first level of hierarchy"""
test_data = """
{
"name": "sai",
Expand Down Expand Up @@ -112,3 +114,69 @@ def test_single_object_one_level_array():
EntityPath("country", False, is_attribute=True)
]
)


def test_single_object_two_level_array():
"""test generation of entities and schema for a JSON object with two levels of
hierarchy"""
test_data = """
{
"name": "sai",
"email": "[email protected]",
"city": [
{
"name": "San Francisco",
"country": "United States",
"geo_location": {
"lat": "37.773972",
"long": "-122.431297"
}
},
{
"name": "New York",
"country": "United States",
"geo_location": {
"lat": "40.730610",
"long": "-73.935242"
}
}
]
}"""
data = json.loads(test_data)
entities = build_entities_from_data(data)
assert len(list(entities.entities)) == 1
for _ in entities.entities:
assert len(_.values) == 3
assert _.values[0:2] == [["sai"], ["[email protected]"]]
assert _.values[2][0].startswith("urn:x-ulid:")
assert len(entities.schema.paths) == 3
assert entities.schema == EntitySchema(
type_uri="",
paths=[
EntityPath("name", False, is_attribute=True),
EntityPath("email", False, is_attribute=True),
EntityPath("city", True, is_attribute=False),
]
)
# Validate sub entities
location_entities = entities.sub_entities[0]
city_entities = entities.sub_entities[1]
assert len(list(city_entities.entities)) == 2
assert len(list(location_entities.entities)) == 2

assert city_entities.schema == EntitySchema(
type_uri="",
paths=[
EntityPath("name", False, is_attribute=True),
EntityPath("country", False, is_attribute=True),
EntityPath("geo_location", True, is_attribute=True),
]
)

assert location_entities.schema == EntitySchema(
type_uri="",
paths=[
EntityPath("lat", False, is_attribute=True),
EntityPath("long", False, is_attribute=True),
]
)

0 comments on commit 9d0ae54

Please sign in to comment.