Skip to content
Merged
15 changes: 15 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
The released versions correspond to PyPi releases.
`dicom-validator` versions follow [Semantic Versioning](https://semver.org/).

## Unreleased

### Changes
* tag error and failed-validation message formatting is now shared via a new
`ValidationResultFormatter` class, used by both `LoggingResultHandler` and
`HtmlErrorHandler` (see [#291](https://github.com/pydicom/dicom-validator/issues/291))
* added `NullValidationResultHandler`, a no-op handler as already seen in the API
documentation, provided as a small convenience.

### Fixes
* `HtmlErrorHandler` now reports why validation cannot be started, matching `LoggingResultHandler`
* `HtmlErrorHandler` tag error messages now include the same detail as
`LoggingResultHandler` for enum values and invalid sequences, and tag names
are now ordered consistently with `LoggingResultHandler`'s output

## [Version 0.8.3](https://pypi.python.org/pypi/dicom-validator/0.8.3) (2026-08-28)
Changed invalid sequence handling.

Expand Down
151 changes: 150 additions & 1 deletion dicom_validator/tests/validator/test_error_handler.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import logging
from pathlib import Path

import pytest
from pydicom import uid
from pydicom import DataElement, uid

from dicom_validator.tests.utils import has_tag_error
from dicom_validator.validator.dicom_file_validator import DicomFileValidator
from dicom_validator.validator.error_handler import (
NullValidationResultHandler,
ValidationResultFormatter,
ValidationResultHandler,
)
from dicom_validator.validator.html_error_handler import HtmlErrorHandler
from dicom_validator.validator.validation_result import (
DicomTag,
ErrorCode,
Status,
ValidationResult,
)

Expand Down Expand Up @@ -60,3 +67,145 @@ def test_html_error_handler(dicom_info, dicom_fixture_path) -> None:
'<h3><a href="https://dicom.nema.org/medical/dicom/current/output/chtml/part03'
'/sect_C.8.8.html">RT Series</a></h3>' in handler.html
)


def test_html_error_handler_failed_validation(dicom_info, validator) -> None:
"""Check that a failed validation is reported."""
handler = HtmlErrorHandler(dicom_info)
validator.handler = handler
validator.validate()
assert "<p>Missing SOP Class UID</p>" in handler.html


@pytest.mark.tag_set(
{
"SOPClassUID": uid.CTImageStorage,
"DerivationCodeSequence": DataElement(
"DerivationCodeSequence", "OB", b"\x00" * 10
),
}
)
def test_html_error_handler_invalid_sequence(dicom_info, validator) -> None:
"""Check that an invalid sequence is reported."""
handler = HtmlErrorHandler(dicom_info)
validator.handler = handler
validator.validate()
assert (
"<li>(0008,9215) (Derivation Code Sequence)"
" is not a valid sequence, ignoring it</li>" in handler.html
)


def test_null_validation_result_handler(validator, caplog) -> None:
"""Check that validation happens with a null handler, but nothing is logged."""
validator.handler = NullValidationResultHandler()
with caplog.at_level(logging.DEBUG):
result = validator.validate()

assert result.status == Status.MissingSOPClassUID
assert result.errors == 1
assert caplog.records == []


@pytest.mark.tag_set(
{
"SOPClassUID": uid.CTImageStorage,
"PatientName": "XXX",
"PatientID": "ZZZ",
}
)
def test_null_validation_result_handler_with_module_errors(validator, caplog) -> None:
"""Check module errors are found with the null handler."""
validator.handler = NullValidationResultHandler()
with caplog.at_level(logging.DEBUG):
result = validator.validate()

assert has_tag_error(result, "Patient", 0x0010_0040, ErrorCode.TagMissing)
assert caplog.records == []


@pytest.mark.tag_set(
{
"SOPClassUID": uid.CTImageStorage,
"PatientName": "XXX",
"PatientID": "ZZZ",
"UltrasoundColorDataPresent": 1, # triggers TagUnexpected
}
)
def test_formatter_error_message_tag_unexpected(validator) -> None:
"""Check unexpected tags are correctly converted to human-readable text."""
validator.handler = NullValidationResultHandler()
result = validator.validate()
tag_error = result.module_errors["General"].get(DicomTag(0x0028_0014))
assert tag_error is not None

formatter = ValidationResultFormatter()
assert formatter.error_message(tag_error) == " is unexpected"


@pytest.mark.tag_set(
{
"SOPClassUID": uid.CTImageStorage,
"PatientName": "XXX",
"PatientID": "ZZZ",
"MultienergyCTAcquisition": "YES",
"CTAdditionalXRaySourceSequence": [],
}
)
def test_formatter_error_message_tag_not_allowed(dicom_info, validator) -> None:
"""Check formatting of text for tags that are not allowed by a condition.

Based on test_iod_validator.py::test_not_allowed_tag
"""
validator.handler = NullValidationResultHandler()
result = validator.validate()
tag_error = result.module_errors["CT Image"].get(DicomTag(0x0018_9360))
assert tag_error is not None

# without a dictionary, condition text is left out
formatter = ValidationResultFormatter()
assert formatter.error_message(tag_error) == " is not allowed"

# with a dictionary, the condition is rendered too
formatter = ValidationResultFormatter(dicom_info.dictionary)
assert formatter.error_message(tag_error) == (
" is not allowed by condition:\n"
' Multi-energy CT Acquisition is equal to "YES"'
)


@pytest.mark.parametrize(
"result,expected",
[
(
ValidationResult(status=Status.MissingSOPClassUID),
"Missing SOP Class UID",
),
(
ValidationResult(status=Status.UnknownSOPClassUID, sop_class_uid="1.2.3"),
"Unknown or retired SOP Class UID: 1.2.3",
),
(
ValidationResult(status=Status.MissingFile, file_path="foo.dcm"),
"Missing DICOM File: foo.dcm",
),
(
ValidationResult(status=Status.InvalidFile, file_path="foo.dcm"),
"Not a DICOM File: foo.dcm - ignoring",
),
# Passed is never actually passed to this method, but the default branch should still
# behave sensibly if it's ever reached
(ValidationResult(status=Status.Passed), "Unknown error"),
],
ids=[
"missing-sop-class-uid",
"unknown-sop-class-uid",
"missing-file",
"invalid-file",
"unreachable-status-fallback",
],
)
def test_formatter_failed_validation_message(result, expected) -> None:
"""Check that every failed-validation status maps to the correct message."""
formatter = ValidationResultFormatter()
assert formatter.failed_validation_message(result) == expected
Loading