-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_entity_object.py
170 lines (131 loc) · 6.56 KB
/
test_entity_object.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import jsondesign.entity as ey
from sample_data import SCHEMA, DEREFERENCED_SCHEMA
def test_create_object():
"""
Create an object representation of an Address
"""
e = ey.Object(uri='las://schema/address')
simple_string = ey.String()
features = {'street_address': simple_string,
'city': simple_string, 'state': simple_string}
e.set_feature(**features)
e.add_required_features('street_address')
# entity.add_required_features prevents duplicates
e.add_required_features('street_address', 'city', 'state')
# Match the creaded object against a target schema (i.e., las://schema/address)
assert e.schema == next(
item for item in SCHEMA if item["$id"] == 'las://schema/address')
def test_create_object_with_custom_features_field():
"""
Create an object representation of an Address
"""
e = ey.Object(uri='las://schema/foo', features_key='foobarbaz')
simple_string = ey.String()
custom_features = {'bar': simple_string}
e.set_feature(**custom_features)
assert e.schema == {
"$id": "las://schema/foo",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"allOf": [
{
"properties": {
'foobarbaz': {
"type": "object",
"allOf": [
{
"properties": {
"bar": {"type": "string"}
},
"required": []
}
]
}
}
}
]
}
def test_append_reference():
"""
Create a reference and append it to an object
"""
e = ey.Object(uri='las://schema/person')
simple_string = ey.String()
address = ey.ObjectReference('las://schema/address')
features = {'firstName': simple_string,
'lastName': simple_string, 'address': address}
e.set_feature(**features)
e.add_required_features('firstName', 'lastName')
# Match the creaded object against a target schema (i.e., las://schema/address)
assert e.schema == next(
item for item in SCHEMA if item["$id"] == 'las://schema/person')
def test_extend_entity():
"""
Create a Student schema extending person schema (i.e., inhertiance)
"""
student = ey.Object(uri='las://schema/student')
student_id = ey.String()
student.set_feature(student_id=student_id)
student.add_required_features('student_id')
# Inheritance
r = ey.ObjectReference('las://schema/person')
student.extend(r)
# Redundant inheritance. entity.extend() is protected against this kind of redundancy
student.extend(r)
assert student.schema == next(
item for item in SCHEMA if item["$id"] == 'las://schema/student')
def test_dereference(schema_store):
"""
Get an object from the store and resolve all references
"""
student = schema_store.get_object('las://schema/student')
dereferenced_schema = student.dereference(schema_store)
# Match the creaded object against a dereferenced target schema (i.e., las://schema/address)
assert dereferenced_schema == next(
item for item in DEREFERENCED_SCHEMA if item["$id"] == 'las://schema/student')
"""
Get data
"""
def test_get_features(schema_store):
student = schema_store.get_object('las://schema/student')
assert student.get_features() == {"student_id": {"type": "string"}}
def test_get_required_features(schema_store):
student = schema_store.get_object('las://schema/student')
assert student.get_required_features() == ["student_id"]
def test_get_features_paths(schema_store):
student = schema_store.get_object('las://schema/student')
assert student.get_features_paths(schema_store) == [{'path': 'features', 'type': 'object'}, {'path': 'features.student_id', 'type': 'string', 'required': True}, {'path': 'features.firstName', 'type': 'string', 'required': True}, {'path': 'features.lastName', 'type': 'string', 'required': True}, {'path': 'features.address', 'type': 'object'}, {'path': 'features.address.features', 'type': 'object'}, {'path': 'features.address.features.street_address', 'type': 'string', 'required': True}, {'path': 'features.address.features.city', 'type': 'string', 'required': True}, {'path': 'features.address.features.state', 'type': 'string', 'required': True}]
"""
Modify existing objects
"""
def test_remove_feature(schema_store):
"""
Get an object an remove a required feature (i.e., city)
Besides, non-existing features are passed and ignored
"""
address = schema_store.get_object('las://schema/address')
address.remove_features('foo', 'city', 'bar')
assert address.schema == {'$id': 'las://schema/address',
'$schema': 'http://json-schema.org/draft-07/schema#',
'type': 'object',
'allOf': [{'properties': {'features': {'type': 'object',
"allOf": [{
'properties': {'street_address': {'type': 'string'},
'state': {'type': 'string'}},
'required': ['state', 'street_address']}]}}}]}
def test_remove_required_feature(schema_store):
"""
Get an object an remove a required feature (i.e., city)
Besides, non-existing features are passed and ignored
"""
address = schema_store.get_object('las://schema/address')
address.remove_required_features('foo', 'city', 'bar')
assert address.schema == {'$id': 'las://schema/address',
'$schema': 'http://json-schema.org/draft-07/schema#',
'type': 'object',
'allOf': [{'properties': {'features': {'type': 'object',
'allOf': [{
'properties': {'street_address': {'type': 'string'},
'city': {'type': 'string'},
'state': {'type': 'string'}},
'required': ['state', 'street_address']}]}}}]}