Skip to content

Commit b9c01bc

Browse files
committed
Add meta field to entity ref and to asserter
1 parent 23b45bb commit b9c01bc

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/shipchain_common/test_utils/json_asserter.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121

2222

2323
class EntityReferenceClass:
24-
def __init__(self, resource=None, pk=None, attributes=None, relationships=None):
24+
def __init__(self, resource=None, pk=None, attributes=None, relationships=None, meta=None):
2525
self.resource = resource
2626
self.pk = pk
2727
self.attributes = attributes
2828
self.relationships = relationships
29+
self.meta = meta
2930

3031
def __str__(self):
3132
return f'Type: {self.resource}; ID: {self.pk}; ' \
@@ -158,6 +159,21 @@ def _vnd_assert_relationships(response_data, relationships):
158159
f'EntityRef ID `{relationship_ref.pk}` does not match {response_relationships}'
159160

160161

162+
def _vnd_assert_meta(response_data, meta_data):
163+
"""
164+
Scan response data for meta data
165+
"""
166+
assert 'meta' in response_data, f'Meta missing in {response_data}'
167+
assert isinstance(meta_data, dict), f'Invalid format for meta data {type(meta_data)}, must be dict'
168+
169+
response_meta = response_data['meta']
170+
171+
for key, value in meta_data.items():
172+
assert key in response_meta, f'Meta field `{key}` not found in {response_meta}'
173+
assert response_meta[key] == value,\
174+
f'Meta field `{key}` had value `{response_meta[key]}` not `{value}` as expected.'
175+
176+
161177
def _vnd_assert_include(response, included):
162178
"""
163179
Scan a response for all included resources
@@ -187,6 +203,9 @@ def _vnd_assertions(response_data, entity_ref):
187203
if entity_ref.relationships:
188204
_vnd_assert_relationships(response_data, entity_ref.relationships)
189205

206+
if entity_ref.meta:
207+
_vnd_assert_meta(response_data, entity_ref.meta)
208+
190209

191210
def _plain_assert_attributes_in_response(response, attributes):
192211
for key, value in attributes.items():

tests/test_json_asserter.py

+59
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ def vnd_single():
105105
}
106106
]
107107
}
108+
},
109+
'meta': {
110+
'key': 'value',
111+
'other_key': 'other_value',
108112
}
109113
},
110114
'included': [
@@ -1080,3 +1084,58 @@ def test_plain_json_single_count(self, vnd_single):
10801084
with pytest.raises(AssertionError) as err:
10811085
AssertionHelper.HTTP_200(response, count=1)
10821086
assert f'Count is only checked when response is list' in str(err.value)
1087+
1088+
def test_vnd_meta(self, vnd_single):
1089+
response = self.build_response(vnd_single)
1090+
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
1091+
resource=EXAMPLE_RESOURCE['type'],
1092+
meta={
1093+
'key': 'value',
1094+
'other_key': 'other_value'
1095+
},
1096+
))
1097+
1098+
def test_vnd_meta_mismatch(self, vnd_single):
1099+
response = self.build_response(vnd_single)
1100+
with pytest.raises(AssertionError) as err:
1101+
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
1102+
resource=EXAMPLE_RESOURCE['type'],
1103+
meta={
1104+
'key': 'different value'
1105+
},
1106+
))
1107+
assert f'Meta field `key` had value `value` not `different value` as expected.' in str(err.value)
1108+
1109+
def test_vnd_meta_invalid_key(self, vnd_single):
1110+
response = self.build_response(vnd_single)
1111+
with pytest.raises(AssertionError) as err:
1112+
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
1113+
resource=EXAMPLE_RESOURCE['type'],
1114+
meta={
1115+
'invalid_key': 'value'
1116+
},
1117+
))
1118+
assert f'Meta field `invalid_key` not found' in str(err.value)
1119+
1120+
def test_vnd_no_meta(self, vnd_single):
1121+
vnd_single['data'].pop('meta')
1122+
response = self.build_response(vnd_single)
1123+
with pytest.raises(AssertionError) as err:
1124+
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
1125+
resource=EXAMPLE_RESOURCE['type'],
1126+
meta={
1127+
'key': 'value'
1128+
},
1129+
))
1130+
assert 'Meta missing' in str(err.value)
1131+
1132+
def test_vnd_invalid_meta_format(self, vnd_single):
1133+
response = self.build_response(vnd_single)
1134+
with pytest.raises(AssertionError) as err:
1135+
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
1136+
resource=EXAMPLE_RESOURCE['type'],
1137+
meta=[{
1138+
'key': 'value'
1139+
}],
1140+
))
1141+
assert 'Invalid format for meta data <class \'list\'>, must be dict' in str(err.value)

0 commit comments

Comments
 (0)