Skip to content

Commit

Permalink
Add comparison for primitive type (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
anshikg authored May 24, 2021
1 parent f510d16 commit d113786
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
23 changes: 13 additions & 10 deletions src/rpdk/core/contract/resource_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,16 +355,19 @@ def compare(self, inputs, outputs):
" defined as writeOnlyProperties in the resource schema"
)
try:
for key in inputs:
if isinstance(inputs[key], dict):
self.compare(inputs[key], outputs[key])
elif isinstance(inputs[key], list):
assert len(inputs[key]) == len(outputs[key])
self.compare_list(inputs[key], outputs[key])
else:
assert inputs[key] == outputs[key], assertion_error_message
except KeyError as e:
raise AssertionError(assertion_error_message) from e
if isinstance(inputs, dict):
for key in inputs:
if isinstance(inputs[key], dict):
self.compare(inputs[key], outputs[key])
elif isinstance(inputs[key], list):
assert len(inputs[key]) == len(outputs[key])
self.compare_list(inputs[key], outputs[key])
else:
assert inputs[key] == outputs[key], assertion_error_message
else:
assert inputs == outputs, assertion_error_message
except Exception as exception:
raise AssertionError(assertion_error_message) from exception

def compare_list(self, inputs, outputs):
for index in range(len(inputs)): # pylint: disable=C0200
Expand Down
21 changes: 18 additions & 3 deletions tests/contract/test_resource_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
"insertionOrder": "false",
"items": {"$ref": "#/definitions/c"},
},
"i": {
"type": "array",
"insertionOrder": "false",
"items": "string",
},
},
"definitions": {
"c": {
Expand Down Expand Up @@ -1275,12 +1280,18 @@ def test_generate_update_example_with_composite_key(

def test_compare_should_pass(resource_client):
resource_client._update_schema(SCHEMA_WITH_NESTED_PROPERTIES)
inputs = {"b": {"d": 1}, "f": [{"d": 1}], "h": [{"d": 1}, {"d": 2}]}
inputs = {
"b": {"d": 1},
"f": [{"d": 1}],
"h": [{"d": 1}, {"d": 2}],
"i": ["abc", "ghi"],
}

outputs = {
"b": {"d": 1, "e": 3},
"f": [{"d": 1, "e": 2}],
"h": [{"d": 1, "e": 3}, {"d": 2}],
"i": ["abc", "ghi"],
}
resource_client.compare(inputs, outputs)

Expand Down Expand Up @@ -1313,9 +1324,13 @@ def test_compare_should_throw_key_error(resource_client):

def test_compare_ordered_list_throws_assertion_exception(resource_client):
resource_client._update_schema(SCHEMA_WITH_NESTED_PROPERTIES)
inputs = {"b": {"d": 1}, "f": [{"d": 1}], "h": [{"d": 1}]}
inputs = {"b": {"d": 1}, "f": [{"d": 1}], "h": [{"d": 1}], "i": ["abc", "ghi"]}

outputs = {"b": {"d": 1, "e": 2}, "f": [{"e": 2}, {"d": 2, "e": 3}]}
outputs = {
"b": {"d": 1, "e": 2},
"f": [{"e": 2}, {"d": 2, "e": 3}],
"i": ["abc", "ghi", "tt"],
}
try:
resource_client.compare(inputs, outputs)
except AssertionError:
Expand Down

0 comments on commit d113786

Please sign in to comment.