-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Validate] Refactor some IF validations #4144
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
minor comments.
demisto_sdk/commands/validate/validators/IF_validators/IF115_unsearchable_key.py
Outdated
Show resolved
Hide resolved
demisto_sdk/commands/validate/validators/IF_validators/IF111_is_field_type_changed.py
Outdated
Show resolved
Hide resolved
demisto_sdk/commands/validate/validators/IF_validators/IF111_is_field_type_changed.py
Outdated
Show resolved
Hide resolved
demisto_sdk/commands/validate/validators/IF_validators/IF113_name_field_prefix.py
Outdated
Show resolved
Hide resolved
item_prefix: Optional[Union[List[str], str]], valid_prefix: str | ||
): | ||
""" | ||
Given: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please explain the differences between the cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm using pytest.param
with id
parameter instead docstring
# not valid | ||
content_item = create_incident_field_object(["type"], ["html"]) | ||
old_content_items = [create_incident_field_object(["type"], ["short text"])] | ||
create_old_file_pointers([content_item], old_content_items) | ||
assert IsFieldTypeChangedValidator().is_valid([content_item]) | ||
|
||
# valid | ||
content_item.field_type = "short text" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# not valid | |
content_item = create_incident_field_object(["type"], ["html"]) | |
old_content_items = [create_incident_field_object(["type"], ["short text"])] | |
create_old_file_pointers([content_item], old_content_items) | |
assert IsFieldTypeChangedValidator().is_valid([content_item]) | |
# valid | |
content_item.field_type = "short text" | |
old_type = "short text" | |
new_type = "html" # simulates modification | |
# not valid | |
content_item = create_incident_field_object(["type"], [new_type]) | |
old_content_items = [create_incident_field_object(["type"], [old_type])] | |
create_old_file_pointers([content_item], old_content_items) | |
assert IsFieldTypeChangedValidator().is_valid([content_item]) | |
# valid | |
content_item.field_type = old_type |
with ChangeCWD(REPO.path): | ||
content_item = create_incident_field_object(pack_info={"name": "Foo"}) | ||
results = NameFieldPrefixValidator().is_valid([content_item]) | ||
assert results | ||
assert results[0].message == ( | ||
"Field name must start with the relevant pack name or one of the item prefixes found in pack metadata." | ||
"\nFollowing prefixes are allowed for this IncidentField:" | ||
"\nFoo" | ||
) | ||
|
||
# valid | ||
content_item.name = "Foo CVE" | ||
assert not NameFieldPrefixValidator().is_valid([content_item]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with ChangeCWD(REPO.path): | |
content_item = create_incident_field_object(pack_info={"name": "Foo"}) | |
results = NameFieldPrefixValidator().is_valid([content_item]) | |
assert results | |
assert results[0].message == ( | |
"Field name must start with the relevant pack name or one of the item prefixes found in pack metadata." | |
"\nFollowing prefixes are allowed for this IncidentField:" | |
"\nFoo" | |
) | |
# valid | |
content_item.name = "Foo CVE" | |
assert not NameFieldPrefixValidator().is_valid([content_item]) | |
pack_name = "Foo" | |
with ChangeCWD(REPO.path): | |
content_item = create_incident_field_object(pack_info={"name": pack_name}) | |
results = NameFieldPrefixValidator().is_valid([content_item]) | |
assert results | |
assert results[0].message == ( | |
"Field name must start with the relevant pack name or one of the item prefixes found in pack metadata." | |
"\nFollowing prefixes are allowed for this IncidentField:" | |
f"\n{pack_name}" | |
) | |
# valid | |
content_item.name = "Foo CVE" | |
assert not NameFieldPrefixValidator().is_valid([content_item]) |
def test_NameFieldPrefixValidator_is_valid_with_item_prefix( | ||
item_prefix: Optional[Union[List[str], str]], | ||
valid_prefix: str, | ||
expected_allowed_prefixes: str, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected_allowed_prefixes: str, | |
expected_allowed_prefixes: List[str], |
content_item = create_incident_field_object(["type"], ["html"]) | ||
old_content_items = [create_incident_field_object(["type"], ["short text"])] | ||
create_old_file_pointers([content_item], old_content_items) | ||
results = IsFieldTypeChangedValidator().fix(content_item) | ||
assert content_item.field_type == "short text" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
content_item = create_incident_field_object(["type"], ["html"]) | |
old_content_items = [create_incident_field_object(["type"], ["short text"])] | |
create_old_file_pointers([content_item], old_content_items) | |
results = IsFieldTypeChangedValidator().fix(content_item) | |
assert content_item.field_type == "short text" | |
old_type = "short text" | |
new_type = "html" | |
content_item = create_incident_field_object(["type"], [new_type]) | |
old_content_items = [create_incident_field_object(["type"], [old_type])] | |
create_old_file_pointers([content_item], old_content_items) | |
results = IsFieldTypeChangedValidator().fix(content_item) | |
assert content_item.field_type == old_type |
create_old_file_pointers([content_item], old_content_items) | ||
results = IsFieldTypeChangedValidator().fix(content_item) | ||
assert content_item.field_type == "short text" | ||
assert results.message == "Changed the `type` field back to `short text`." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert results.message == "Changed the `type` field back to `short text`." | |
assert results.message == f"Changed the `type` field back to `{old_type}`." |
for prefix in allowed_prefixes(content_item): | ||
if content_item.name.startswith(prefix): | ||
return True | ||
return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for prefix in allowed_prefixes(content_item): | |
if content_item.name.startswith(prefix): | |
return True | |
return False | |
return any((content_item.name.startswith(prefix) for prefix in allowed_prefixes(content_item)) |
] | ||
|
||
|
||
def name_include_allowed_prefix(content_item: ContentTypes) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def name_include_allowed_prefix(content_item: ContentTypes) -> bool: | |
def is_name_using_allowed_prefix(content_item: ContentTypes) -> bool: |
elif isinstance(item_prefix, list): | ||
return prefixes | set(item_prefix) | ||
else: | ||
return prefixes | {item_prefix} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do from more_itertools import always_iterable
, and then
elif isinstance(item_prefix, list): | |
return prefixes | set(item_prefix) | |
else: | |
return prefixes | {item_prefix} | |
return prefixes | set(always_iterable(item_prefix)) |
🪄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please link to a jira issue
Related Issues
fixes:
Description