Skip to content

Commit 98a519e

Browse files
authored
Sy/update metadata validation (#17521)
* add sample tags to validation * changelog * make validation optional * fix tests * lint * add tests and comments * rename tests and comments
1 parent a877d95 commit 98a519e

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

ddev/changelog.d/17521.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add sample_tags to metadata validation

ddev/src/ddev/cli/validate/metadata.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,17 @@ def metadata(app: Application, integrations: tuple[str, ...], check_duplicates:
130130

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

133-
missing_headers = metadata_utils.ALL_HEADERS.difference(all_keys)
133+
missing_headers = metadata_utils.HEADERS_TO_CHECK.difference(all_keys)
134134
if missing_headers:
135135
errors = True
136136

137137
error_message += f"{current_check.name}:{line} Missing columns {missing_headers}.\n"
138-
continue
138+
139+
if errors:
140+
# There's now an optional sample_tag column that isn't added yet to the existing metadata.csv
141+
# all_keys will not be same as ALL_HEADERS. But since that sample_tag column is optional and not
142+
# inside HEADERS_TO_CHECK, we should only continue if there's an invalid header or missing_header.
143+
continue
139144

140145
# check duplicate metric name
141146
duplicate_metric_name = check_duplicate_values(

ddev/src/ddev/cli/validate/metadata_utils.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@
55

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

8-
OPTIONAL_HEADERS = {'description', 'interval', 'unit_name', 'per_unit_name', 'short_name', 'curated_metric'}
8+
OPTIONAL_HEADERS = {
9+
'description',
10+
'interval',
11+
'unit_name',
12+
'per_unit_name',
13+
'short_name',
14+
'curated_metric',
15+
}
916

10-
ALL_HEADERS = REQUIRED_HEADERS | OPTIONAL_HEADERS
17+
EXPERIMENTAL_HEADER = {"sample_tags"}
18+
ALL_HEADERS = REQUIRED_HEADERS | OPTIONAL_HEADERS | EXPERIMENTAL_HEADER
19+
HEADERS_TO_CHECK = REQUIRED_HEADERS | OPTIONAL_HEADERS
1120

1221
ORDERED_HEADERS = [
1322
"metric_name",
@@ -20,6 +29,7 @@
2029
"integration",
2130
"short_name",
2231
"curated_metric",
32+
"sample_tags",
2333
]
2434

2535
VALID_METRIC_TYPE = {'count', 'gauge', 'rate'}

ddev/tests/cli/validate/test_metrics.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,58 @@ def test_metrics_ordered(fake_repo, ddev, helpers):
715715
)
716716

717717

718+
def test_passing_with_experimental_column(fake_repo, ddev, helpers):
719+
# Testing to ensure that experimental header sample_tags is allowed
720+
write_file(
721+
fake_repo.path / "metadata_integration",
722+
'metadata.csv',
723+
"""metric_name,metric_type,interval,unit_name,per_unit_name,description,orientation,integration,short_name,curated_metric,sample_tags
724+
metadata_integration.metric_a,gauge,,,,My metric A,0,metadata_integration,,,
725+
metadata_integration.metric_b,gauge,,,,My metric B,0,metadata_integration,,,
726+
""",
727+
)
728+
729+
result = ddev('validate', 'metadata', 'metadata_integration')
730+
731+
assert result.exit_code == 0, result.output
732+
assert helpers.remove_trailing_spaces(result.output) == helpers.dedent(
733+
"""
734+
Metrics validation
735+
736+
Passed: 1
737+
"""
738+
)
739+
740+
741+
def test_passing_invalid_experimental_column(fake_repo, ddev, helpers):
742+
# Testing to ensure that experimental header sample_tags is allowed. But if other tags are added,
743+
# it will be flagged as an error
744+
write_file(
745+
fake_repo.path / "metadata_integration",
746+
'metadata.csv',
747+
"""metric_name,metric_type,interval,unit_name,per_unit_name,description,orientation,integration,short_name,curated_metric,sample_tags,foo
748+
metadata_integration.metric_a,gauge,,,,My metric A,0,metadata_integration,,,,
749+
metadata_integration.metric_b,gauge,,,,My metric B,0,metadata_integration,,,,
750+
""",
751+
)
752+
outfile = os.path.join('metadata_integration', 'metadata.csv')
753+
result = ddev('validate', 'metadata', 'metadata_integration')
754+
755+
assert result.exit_code == 1, result.output
756+
assert helpers.remove_trailing_spaces(result.output) == helpers.dedent(
757+
f"""
758+
Metrics validation
759+
└── metadata_integration
760+
└── {outfile}
761+
762+
metadata_integration:2 Invalid column {{'foo'}}.
763+
metadata_integration:3 Invalid column {{'foo'}}.
764+
765+
Errors: 1
766+
"""
767+
)
768+
769+
718770
def test_metrics_not_ordered(fake_repo, ddev, helpers):
719771
outfile = os.path.join('metadata_integration', 'metadata.csv')
720772
result = ddev('validate', 'metadata', 'metadata_integration')

0 commit comments

Comments
 (0)