-
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.
add simple test case for build_entities_from_data
- Loading branch information
1 parent
96426af
commit 540dde2
Showing
1 changed file
with
68 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import json | ||
|
||
from dataintegration.entity import EntitySchema, EntityPath | ||
from dataintegration.utils import build_entities_from_data | ||
|
||
|
||
def test_single_object(): | ||
"""test write to single object""" | ||
test_data = """ | ||
{ | ||
"name": "sai", | ||
"email": "[email protected]" | ||
}""" | ||
data = json.loads(test_data) | ||
entities = build_entities_from_data(data) | ||
assert len(entities.entities) == 1 | ||
for _ in entities.entities: | ||
assert len(_.values) == 2 | ||
assert _.values == [["sai"], ["[email protected]"]] | ||
assert len(entities.schema.paths) == 2 | ||
assert str(entities.schema) == str(EntitySchema( | ||
type_uri="", | ||
paths=[ | ||
EntityPath("name", False, is_attribute=True), | ||
EntityPath("email", False, is_attribute=True), | ||
] | ||
)) | ||
|
||
|
||
def test_single_object_one_level(): | ||
"""test write to single object""" | ||
test_data = """ | ||
{ | ||
"name": "sai", | ||
"email": "[email protected]", | ||
"city": { | ||
"name": "San Francisco", | ||
"country": "United States" | ||
} | ||
}""" | ||
data = json.loads(test_data) | ||
entities = build_entities_from_data(data) | ||
assert len(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 str(entities.schema) == str(EntitySchema( | ||
type_uri="", | ||
paths=[ | ||
EntityPath("name", False, is_attribute=True), | ||
EntityPath("email", False, is_attribute=True), | ||
EntityPath("city", True, is_attribute=True), | ||
] | ||
)) | ||
# Validate sub entities | ||
for _ in entities.sub_entities: | ||
for _entity in _.entities: | ||
assert len(_entity.values) == 2 | ||
assert _entity.values == [["San Francisco"], ["United States"]] | ||
assert str(_.schema) == str(EntitySchema( | ||
type_uri="", | ||
paths=[ | ||
EntityPath("name", False, is_attribute=True), | ||
EntityPath("country", False, is_attribute=True) | ||
] | ||
)) |