Skip to content

Commit

Permalink
alphabetical order for init-test-data output (#3328)
Browse files Browse the repository at this point in the history
* add_sorting

* add_to_changelog

* CR comments

* Update init_test_data.py

* Update init_test_data.py

* CR fixes

* add_tests

* fixes

* fixes

* Update modeling_rule.py
  • Loading branch information
sapirshuker authored Jul 19, 2023
1 parent 65f6355 commit 8a67088
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Fixed an issue where the **format** command would change the value of the `unsearchable` key in fields.
* Added an ability to provide list of marketplace names as a credentials-type (type 9) param attribute.
* **doc-review** will run with the `--use-packs-known-words` default to true.
* Calling **modeling-rules init-test-data** will now return the XDM fields output in alphabetical order.

## 1.17.2
* Fixed an issue where **lint** and **validate** commands failed on integrations and scripts that use docker images that are not available in the Docker Hub but exist locally.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,17 @@ def datamodel(self, value):
self._datamodel = value

@property
def fields(self):
def fields(self) -> List[str]:
if not self._fields:
uniq_fields = set(re.findall(self.RULE_FIELDS_REGEX, self.rule_text))
uniq_fields.add(self.TIME_FIELD) # The '_time' field is always required.
self.fields = uniq_fields
if not self._fields:
uniq_fields = list(set(re.findall(self.RULE_FIELDS_REGEX, self.rule_text)))
if not uniq_fields:
raise ValueError(
f'could not parse datamodel fields from the rule text: "{self.rule_text}"'
)

uniq_fields.append(self.TIME_FIELD) # The '_time' field is always required.
self.fields = sorted(uniq_fields)

return self._fields

@fields.setter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import traceback
from io import StringIO
from pathlib import Path
from typing import List, Set
from typing import List

import typer

Expand Down Expand Up @@ -76,9 +76,6 @@ def init_test_data(
for fp in input:
try:
mr_entity = ModelingRule(fp.as_posix())
all_mr_entity_fields: Set[str] = set()
for mr in mr_entity.rules:
all_mr_entity_fields = all_mr_entity_fields.union(mr.fields)

operation_mode = "create"
test_data_file = mr_entity.testdata_path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def test_init_test_data_create(pack):
Then:
- Ensure the test data file is created.
- Ensure the test data file contains the correct number of events.
- Ensure the event fields are sorted.
"""
from demisto_sdk.commands.test_content.test_modeling_rule.init_test_data import (
app as init_test_data_app,
Expand All @@ -106,7 +107,9 @@ def test_init_test_data_create(pack):
assert result.exit_code == 0
assert test_data_file.exists() is True
test_data = TestData.parse_file(test_data_file.as_posix())
event_fields: dict = test_data.data[0].expected_values or {}
assert len(test_data.data) == count
assert dict(sorted(event_fields.items())) == test_data.data[0].expected_values


def test_init_test_data_update_with_unchanged_modeling_rule(pack):
Expand All @@ -119,6 +122,7 @@ def test_init_test_data_update_with_unchanged_modeling_rule(pack):
- The test data file exists.
Then:
- Ensure the test data file contains the correct number of events.
- Ensure the event fields are sorted.
"""
from demisto_sdk.commands.test_content.test_modeling_rule.init_test_data import (
app as init_test_data_app,
Expand All @@ -143,7 +147,9 @@ def test_init_test_data_update_with_unchanged_modeling_rule(pack):
assert result.exit_code == 0
assert test_data_file.exists() is True
test_data = TestData.parse_file(test_data_file.as_posix())
first_event_fields: dict = test_data.data[0].expected_values or {}
assert len(test_data.data) == count
assert dict(sorted(first_event_fields.items())) == test_data.data[0].expected_values


def test_init_test_data_update_with_reduced_modeling_rule(pack):
Expand Down

0 comments on commit 8a67088

Please sign in to comment.