Skip to content

Commit

Permalink
made all breaking rules snake case (datacontract#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
torbenkeller authored Mar 6, 2024
1 parent 92b443f commit a85164a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
16 changes: 10 additions & 6 deletions datacontract/breaking/breaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,26 +264,26 @@ def field_breaking_changes(
# logic for enum, tags and other arrays
if type(old_value) is list and type(new_value) is list:
if not old_value and new_value:
rule_name = f"field_{field_definition_field}_added"
rule_name = f"field_{_camel_to_snake(field_definition_field)}_added"
description = f"added with value: `{new_value}`"
elif old_value and not new_value:
rule_name = f"field_{field_definition_field}_removed"
rule_name = f"field_{_camel_to_snake(field_definition_field)}_removed"
description = "removed field property"
elif sorted(old_value) != sorted(new_value):
rule_name = f"field_{field_definition_field}_updated"
rule_name = f"field_{_camel_to_snake(field_definition_field)}_updated"
description = f"changed from `{old_value}` to `{new_value}`"

# logic for normal fields
elif old_value is None and new_value is not None:
rule_name = f"field_{field_definition_field}_added"
rule_name = f"field_{_camel_to_snake(field_definition_field)}_added"
description = f"added with value: `{str(new_value).lower() if type(new_value) is bool else new_value}`"

elif old_value is not None and new_value is None:
rule_name = f"field_{field_definition_field}_removed"
rule_name = f"field_{_camel_to_snake(field_definition_field)}_removed"
description = "removed field property"

elif old_value != new_value:
rule_name = f"field_{field_definition_field}_updated"
rule_name = f"field_{_camel_to_snake(field_definition_field)}_updated"
description = (f"changed from `{str(old_value).lower() if type(old_value) is bool else old_value}` "
f"to `{str(new_value).lower() if type(new_value) is bool else new_value}`")

Expand All @@ -310,3 +310,7 @@ def _get_rule(rule_name):
except AttributeError:
print(f'WARNING: Breaking Rule not found for {rule_name}!')
return 'error'

def _camel_to_snake(s):
return ''.join(['_'+c.lower() if c.isupper() else c for c in s]).lstrip('_')

24 changes: 12 additions & 12 deletions datacontract/breaking/breaking_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,29 @@ class BreakingRules:
field_pattern_removed = 'error'
field_pattern_updated = 'error'

field_minLength_added = 'warning'
field_minLength_removed = 'warning'
field_minLength_updated = 'error'
field_min_length_added = 'warning'
field_min_length_removed = 'warning'
field_min_length_updated = 'error'

field_maxLength_added = 'warning'
field_maxLength_removed = 'warning'
field_maxLength_updated = 'error'
field_max_length_added = 'warning'
field_max_length_removed = 'warning'
field_max_length_updated = 'error'

field_minimum_added = 'warning'
field_minimum_removed = 'warning'
field_minimum_updated = 'error'

field_exclusiveMinimum_added = 'warning'
field_exclusiveMinimum_removed = 'warning'
field_exclusiveMinimum_updated = 'error'
field_exclusive_minimum_added = 'warning'
field_exclusive_minimum_removed = 'warning'
field_exclusive_minimum_updated = 'error'

field_maximum_added = 'warning'
field_maximum_removed = 'warning'
field_maximum_updated = 'error'

field_exclusiveMaximum_added = 'warning'
field_exclusiveMaximum_removed = 'warning'
field_exclusiveMaximum_updated = 'error'
field_exclusive_maximum_added = 'warning'
field_exclusive_maximum_removed = 'warning'
field_exclusive_maximum_updated = 'error'

field_enum_added = 'warning'
field_enum_removed = 'info'
Expand Down
48 changes: 24 additions & 24 deletions tests/test_breaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ def test_pattern_added(self, output):
added with value: `^[A-Za-z0-9]{8,14}$`""" in output

def test_min_length_added(self, output):
assert r"""warning [field_minLength_added] at
assert r"""warning [field_min_length_added] at
./examples/breaking/datacontract-fields-v2.yaml
in models.my_table.fields.field_minLength.minLength
added with value: `8`""" in output

def test_max_length_added(self, output):
assert r"""warning [field_maxLength_added] at
assert r"""warning [field_max_length_added] at
./examples/breaking/datacontract-fields-v2.yaml
in models.my_table.fields.field_maxLength.maxLength
added with value: `14`""" in output
Expand All @@ -200,7 +200,7 @@ def test_minimum_added(self, output):
added with value: `8`""" in output

def test_minimum_exclusive_added(self, output):
assert r"""warning [field_exclusiveMinimum_added] at
assert r"""warning [field_exclusive_minimum_added] at
./examples/breaking/datacontract-fields-v2.yaml
in models.my_table.fields.field_exclusiveMinimum.exclusiveMinimum
added with value: `8`""" in output
Expand All @@ -211,7 +211,7 @@ def test_maximum_added(self, output):
added with value: `14`""" in output

def test_maximum_exclusive_added(self, output):
assert r"""warning [field_exclusiveMaximum_added] at
assert r"""warning [field_exclusive_maximum_added] at
./examples/breaking/datacontract-fields-v2.yaml
in models.my_table.fields.field_exclusiveMaximum.exclusiveMaximum
added with value: `14`""" in output
Expand Down Expand Up @@ -279,13 +279,13 @@ def test_pattern_removed(self, output):
removed field property""" in output

def test_min_length_removed(self, output):
assert r"""warning [field_minLength_removed] at
assert r"""warning [field_min_length_removed] at
./examples/breaking/datacontract-fields-v1.yaml
in models.my_table.fields.field_minLength.minLength
removed field property""" in output

def test_max_length_removed(self, output):
assert r"""warning [field_maxLength_removed] at
assert r"""warning [field_max_length_removed] at
./examples/breaking/datacontract-fields-v1.yaml
in models.my_table.fields.field_maxLength.maxLength
removed field property""" in output
Expand All @@ -297,7 +297,7 @@ def test_minimum_removed(self, output):
removed field property""" in output

def test_minimum_exclusive_removed(self, output):
assert r"""warning [field_exclusiveMinimum_removed] at
assert r"""warning [field_exclusive_minimum_removed] at
./examples/breaking/datacontract-fields-v1.yaml
in models.my_table.fields.field_exclusiveMinimum.exclusiveMinimum
removed field property""" in output
Expand All @@ -309,7 +309,7 @@ def test_maximum_removed(self, output):
removed field property""" in output

def test_maximum_exclusive_removed(self, output):
assert r"""warning [field_exclusiveMaximum_removed] at
assert r"""warning [field_exclusive_maximum_removed] at
./examples/breaking/datacontract-fields-v1.yaml
in models.my_table.fields.field_exclusiveMaximum.exclusiveMaximum
removed field property""" in output
Expand Down Expand Up @@ -404,13 +404,13 @@ def test_pattern_updated(self, output):
changed from `^[A-Za-z0-9]{8,14}$` to `^[A-Za-z0-9]$`""" in output

def test_min_length_updated(self, output):
assert r"""error [field_minLength_updated] at
assert r"""error [field_min_length_updated] at
./examples/breaking/datacontract-fields-v3.yaml
in models.my_table.fields.field_minLength.minLength
changed from `8` to `10`""" in output

def test_max_length_updated(self, output):
assert r"""error [field_maxLength_updated] at
assert r"""error [field_max_length_updated] at
./examples/breaking/datacontract-fields-v3.yaml
in models.my_table.fields.field_maxLength.maxLength
changed from `14` to `20`""" in output
Expand All @@ -422,7 +422,7 @@ def test_minimum_updated(self, output):
changed from `8` to `10`""" in output

def test_minimum_exclusive_updated(self, output):
assert r"""error [field_exclusiveMinimum_updated] at
assert r"""error [field_exclusive_minimum_updated] at
./examples/breaking/datacontract-fields-v3.yaml
in models.my_table.fields.field_exclusiveMinimum.exclusiveMinimum
changed from `8` to `10`""" in output
Expand All @@ -434,7 +434,7 @@ def test_maximum_updated(self, output):
changed from `14` to `20`""" in output

def test_maximum_exclusive_updated(self, output):
assert r"""error [field_exclusiveMaximum_updated] at
assert r"""error [field_exclusive_maximum_updated] at
./examples/breaking/datacontract-fields-v3.yaml
in models.my_table.fields.field_exclusiveMaximum.exclusiveMaximum
changed from `14` to `20`""" in output
Expand Down Expand Up @@ -511,13 +511,13 @@ def test_pattern_added(self, output):
added with value: `.*`""" in output

def test_min_length_added(self, output):
assert r"""warning [field_minLength_added] at
assert r"""warning [field_min_length_added] at
./examples/breaking/datacontract-definitions-v2.yaml
in models.my_table.fields.my_field.minLength
added with value: `8`""" in output

def test_max_length_added(self, output):
assert r"""warning [field_maxLength_added] at
assert r"""warning [field_max_length_added] at
./examples/breaking/datacontract-definitions-v2.yaml
in models.my_table.fields.my_field.maxLength
added with value: `14`""" in output
Expand All @@ -529,7 +529,7 @@ def test_minimum_added(self, output):
added with value: `8`""" in output

def test_minimum_exclusive_added(self, output):
assert r"""warning [field_exclusiveMinimum_added] at
assert r"""warning [field_exclusive_minimum_added] at
./examples/breaking/datacontract-definitions-v2.yaml
in models.my_table.fields.my_field.exclusiveMinimum
added with value: `14`""" in output
Expand All @@ -541,7 +541,7 @@ def test_maximum_added(self, output):
added with value: `14`""" in output

def test_maximum_exclusive_added(self, output):
assert r"""warning [field_exclusiveMaximum_added] at
assert r"""warning [field_exclusive_maximum_added] at
./examples/breaking/datacontract-definitions-v2.yaml
in models.my_table.fields.my_field.exclusiveMaximum
added with value: `8`""" in output
Expand Down Expand Up @@ -608,13 +608,13 @@ def test_pattern_removed(self, output):
removed field property""" in output

def test_min_length_removed(self, output):
assert r"""warning [field_minLength_removed] at
assert r"""warning [field_min_length_removed] at
./examples/breaking/datacontract-definitions-v1.yaml
in models.my_table.fields.my_field.minLength
removed field property""" in output

def test_max_length_removed(self, output):
assert r"""warning [field_maxLength_removed] at
assert r"""warning [field_max_length_removed] at
./examples/breaking/datacontract-definitions-v1.yaml
in models.my_table.fields.my_field.maxLength
removed field property""" in output
Expand All @@ -626,7 +626,7 @@ def test_minimum_removed(self, output):
removed field property""" in output

def test_minimum_exclusive_removed(self, output):
assert r"""warning [field_exclusiveMinimum_removed] at
assert r"""warning [field_exclusive_minimum_removed] at
./examples/breaking/datacontract-definitions-v1.yaml
in models.my_table.fields.my_field.exclusiveMinimum
removed field property""" in output
Expand All @@ -638,7 +638,7 @@ def test_maximum_removed(self, output):
removed field property""" in output

def test_maximum_exclusive_removed(self, output):
assert r"""warning [field_exclusiveMaximum_removed] at
assert r"""warning [field_exclusive_maximum_removed] at
./examples/breaking/datacontract-definitions-v1.yaml
in models.my_table.fields.my_field.exclusiveMaximum
removed field property""" in output
Expand Down Expand Up @@ -703,13 +703,13 @@ def test_pattern_updated(self, output):
changed from `.*` to `.*.*`""" in output

def test_min_length_updated(self, output):
assert r"""error [field_minLength_updated] at
assert r"""error [field_min_length_updated] at
./examples/breaking/datacontract-definitions-v3.yaml
in models.my_table.fields.my_field.minLength
changed from `8` to `10`""" in output

def test_max_length_updated(self, output):
assert r"""error [field_maxLength_updated] at
assert r"""error [field_max_length_updated] at
./examples/breaking/datacontract-definitions-v3.yaml
in models.my_table.fields.my_field.maxLength
changed from `14` to `20`""" in output
Expand All @@ -721,7 +721,7 @@ def test_minimum_updated(self, output):
changed from `8` to `10`""" in output

def test_minimum_exclusive_updated(self, output):
assert r"""error [field_exclusiveMinimum_updated] at
assert r"""error [field_exclusive_minimum_updated] at
./examples/breaking/datacontract-definitions-v3.yaml
in models.my_table.fields.my_field.exclusiveMinimum
changed from `14` to `10`""" in output
Expand All @@ -733,7 +733,7 @@ def test_maximum_updated(self, output):
changed from `14` to `20`""" in output

def test_maximum_exclusive_updated(self, output):
assert r"""error [field_exclusiveMaximum_updated] at
assert r"""error [field_exclusive_maximum_updated] at
./examples/breaking/datacontract-definitions-v3.yaml
in models.my_table.fields.my_field.exclusiveMaximum
changed from `8` to `20`""" in output
Expand Down

0 comments on commit a85164a

Please sign in to comment.