Skip to content

Commit

Permalink
Added stateful test to verify PrimaryIdentifier order (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
ammokhov committed Apr 23, 2024
1 parent ddc78bb commit 0eb761f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ pylint>=2.15.10
pytest-cov>=4.0.0
pytest-random-order>=1.1.0
pytest>=7.2.0
rich==13.7.1
StrEnum==0.4.10
wheel==0.38.1
27 changes: 21 additions & 6 deletions src/rpdk/guard_rail/core/stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
schema_v2 = ...
schema_meta_diff = schema_diff(schema_v1, schema_v2)
"""
import json
import re
from copy import copy
from enum import auto
Expand All @@ -27,8 +26,11 @@

import strenum
from deepdiff import DeepDiff
from rich.console import Console
from rpdk.guard_rail.utils.schema_utils import resolve_schema

console = Console()


class METADIFF(strenum.LowercaseStrEnum):
ITERABLE_ITEM_ADDED = auto()
Expand Down Expand Up @@ -91,14 +93,27 @@ class DIFFKEYS:

def schema_diff(previous_json: Dict[str, Any], current_json: Dict[str, Any]):
"""schema diff function to get formatted schema diff from deep diff"""

previous_schema = resolve_schema(previous_json)
current_schema = resolve_schema(current_json)

deep_diff = DeepDiff(
resolve_schema(previous_json),
resolve_schema(current_json),
ignore_order=True,
previous_schema,
current_schema,
ignore_order_func=lambda level: "primaryIdentifier" not in level.path(),
verbose_level=2,
)
print(json.dumps(_translate_meta_diff(deep_diff.to_dict())))
return _translate_meta_diff(deep_diff.to_dict())

meta_diff = _translate_meta_diff(deep_diff.to_dict())
console.rule("[bold red][GENERATED DIFF BETWEEN SCHEMAS]")
console.print(
meta_diff,
style="link https://google.com",
highlight=True,
justify="left",
soft_wrap=True,
)
return meta_diff


def _is_combiner_property(path_list):
Expand Down
2 changes: 0 additions & 2 deletions src/rpdk/guard_rail/core/templates/guard-result-pojo.output
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
message: {{check.message}}
{%- endfor -%}
{% endfor %}


{% if failed_rules%}
{{failed_header}}
{% for rule, checks in failed_rules.items() %}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/core/test_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ def test_success_result_str():
}
)
)
== "---------\n[SKIPPED]:\n\n\n\x1b[32m[PASSED]:\x1b[39m\n\n\n\x1b[33m[WARNING]:\x1b[39m\n\n\n\n\n\x1b[31m[FAILED]:\x1b[39m\n\nENSURE_OLD_PROPERTY_NOT_TURNED_IMMUTABLE:\n check-id: MI007\n message: cannot remove minimum from properties\n path: /minimum/removed\n \n" # pylint: disable=C0301
== "---------\n[SKIPPED]:\n\n\n\x1b[32m[PASSED]:\x1b[39m\n\n\n\x1b[33m[WARNING]:\x1b[39m\n\n\n\x1b[31m[FAILED]:\x1b[39m\n\nENSURE_OLD_PROPERTY_NOT_TURNED_IMMUTABLE:\n check-id: MI007\n message: cannot remove minimum from properties\n path: /minimum/removed\n \n" # pylint: disable=C0301
)
46 changes: 46 additions & 0 deletions tests/unit/core/test_stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,52 @@ def test_schema_diff_complex_json_semantics_mutations(
assert expected_diff_negative == schema_diff(schema_variant2, schema_variant1)


@pytest.mark.parametrize(
"schema_variant1, schema_variant2, expected_diff, expected_diff_negative",
[
(
{
"primaryIdentifier": [
"/properties/A",
"/properties/B",
]
},
{
"primaryIdentifier": [
"/properties/B",
"/properties/A",
]
},
{
"primaryIdentifier": {
"added": ["/properties/B", "/properties/A"],
"removed": ["/properties/A", "/properties/B"],
}
},
{
"primaryIdentifier": {
"added": ["/properties/A", "/properties/B"],
"removed": ["/properties/B", "/properties/A"],
}
},
)
],
)
def test_schema_diff_primary_identifier_order_change(
schema_variant1, schema_variant2, expected_diff, expected_diff_negative
):
"""
Args:
schema_variant1:
schema_variant2:
expected_diff:
expected_diff_negative:
"""
assert expected_diff == schema_diff(schema_variant1, schema_variant2)
assert expected_diff_negative == schema_diff(schema_variant2, schema_variant1)


@pytest.mark.parametrize(
"schema_variant1, schema_variant2, expected_diff, expected_diff_negative",
[
Expand Down

0 comments on commit 0eb761f

Please sign in to comment.