Skip to content

Commit

Permalink
Sy/update metadata validation (#17521)
Browse files Browse the repository at this point in the history
* add sample tags to validation

* changelog

* make validation optional

* fix tests

* lint

* add tests and comments

* rename tests and comments
  • Loading branch information
steveny91 authored May 10, 2024
1 parent a877d95 commit 98a519e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
1 change: 1 addition & 0 deletions ddev/changelog.d/17521.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add sample_tags to metadata validation
9 changes: 7 additions & 2 deletions ddev/src/ddev/cli/validate/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,17 @@ def metadata(app: Application, integrations: tuple[str, ...], check_duplicates:

error_message += f"{current_check.name}:{line} Invalid column {invalid_headers}.\n"

missing_headers = metadata_utils.ALL_HEADERS.difference(all_keys)
missing_headers = metadata_utils.HEADERS_TO_CHECK.difference(all_keys)
if missing_headers:
errors = True

error_message += f"{current_check.name}:{line} Missing columns {missing_headers}.\n"
continue

if errors:
# There's now an optional sample_tag column that isn't added yet to the existing metadata.csv
# all_keys will not be same as ALL_HEADERS. But since that sample_tag column is optional and not
# inside HEADERS_TO_CHECK, we should only continue if there's an invalid header or missing_header.
continue

# check duplicate metric name
duplicate_metric_name = check_duplicate_values(
Expand Down
14 changes: 12 additions & 2 deletions ddev/src/ddev/cli/validate/metadata_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@

REQUIRED_HEADERS = {'metric_name', 'metric_type', 'orientation', 'integration'}

OPTIONAL_HEADERS = {'description', 'interval', 'unit_name', 'per_unit_name', 'short_name', 'curated_metric'}
OPTIONAL_HEADERS = {
'description',
'interval',
'unit_name',
'per_unit_name',
'short_name',
'curated_metric',
}

ALL_HEADERS = REQUIRED_HEADERS | OPTIONAL_HEADERS
EXPERIMENTAL_HEADER = {"sample_tags"}
ALL_HEADERS = REQUIRED_HEADERS | OPTIONAL_HEADERS | EXPERIMENTAL_HEADER
HEADERS_TO_CHECK = REQUIRED_HEADERS | OPTIONAL_HEADERS

ORDERED_HEADERS = [
"metric_name",
Expand All @@ -20,6 +29,7 @@
"integration",
"short_name",
"curated_metric",
"sample_tags",
]

VALID_METRIC_TYPE = {'count', 'gauge', 'rate'}
Expand Down
52 changes: 52 additions & 0 deletions ddev/tests/cli/validate/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,58 @@ def test_metrics_ordered(fake_repo, ddev, helpers):
)


def test_passing_with_experimental_column(fake_repo, ddev, helpers):
# Testing to ensure that experimental header sample_tags is allowed
write_file(
fake_repo.path / "metadata_integration",
'metadata.csv',
"""metric_name,metric_type,interval,unit_name,per_unit_name,description,orientation,integration,short_name,curated_metric,sample_tags
metadata_integration.metric_a,gauge,,,,My metric A,0,metadata_integration,,,
metadata_integration.metric_b,gauge,,,,My metric B,0,metadata_integration,,,
""",
)

result = ddev('validate', 'metadata', 'metadata_integration')

assert result.exit_code == 0, result.output
assert helpers.remove_trailing_spaces(result.output) == helpers.dedent(
"""
Metrics validation
Passed: 1
"""
)


def test_passing_invalid_experimental_column(fake_repo, ddev, helpers):
# Testing to ensure that experimental header sample_tags is allowed. But if other tags are added,
# it will be flagged as an error
write_file(
fake_repo.path / "metadata_integration",
'metadata.csv',
"""metric_name,metric_type,interval,unit_name,per_unit_name,description,orientation,integration,short_name,curated_metric,sample_tags,foo
metadata_integration.metric_a,gauge,,,,My metric A,0,metadata_integration,,,,
metadata_integration.metric_b,gauge,,,,My metric B,0,metadata_integration,,,,
""",
)
outfile = os.path.join('metadata_integration', 'metadata.csv')
result = ddev('validate', 'metadata', 'metadata_integration')

assert result.exit_code == 1, result.output
assert helpers.remove_trailing_spaces(result.output) == helpers.dedent(
f"""
Metrics validation
└── metadata_integration
└── {outfile}
metadata_integration:2 Invalid column {{'foo'}}.
metadata_integration:3 Invalid column {{'foo'}}.
Errors: 1
"""
)


def test_metrics_not_ordered(fake_repo, ddev, helpers):
outfile = os.path.join('metadata_integration', 'metadata.csv')
result = ddev('validate', 'metadata', 'metadata_integration')
Expand Down

0 comments on commit 98a519e

Please sign in to comment.