Skip to content

Commit

Permalink
Test the mismatch warning messages again
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Oct 13, 2024
1 parent 523e8ed commit bb54e2b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
11 changes: 7 additions & 4 deletions pynetdicom/association.py
Original file line number Diff line number Diff line change
Expand Up @@ -1892,10 +1892,13 @@ def send_c_store(
tsyntax.is_little_endian,
)
# `dataset` might also be created from scratch
ds_encoding = getattr(
dataset,
"original_encoding",
(dataset.is_implicit_VR, dataset.is_little_endian),
ds_encoding: tuple[bool | None, bool | None] = (
dataset.is_implicit_VR
if dataset.original_encoding[0] is None
else dataset.original_encoding[0],
dataset.is_little_endian
if dataset.original_encoding[1] is None
else dataset.original_encoding[1],
)
if None not in ds_encoding and ts_encoding != ds_encoding:
s = ("explicit VR", "implicit VR")[cast(bool, ds_encoding[0])]
Expand Down
67 changes: 33 additions & 34 deletions pynetdicom/tests/test_assoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pydicom import dcmread
from pydicom.dataset import Dataset, FileMetaDataset
from pydicom.uid import (
generate_uid,
ImplicitVRLittleEndian,
ExplicitVRLittleEndian,
JPEGBaseline8Bit,
Expand Down Expand Up @@ -1876,51 +1877,49 @@ def handle_store(event):
ae.add_requested_context(CTImageStorage, ImplicitVRLittleEndian)
ae.add_requested_context(CTImageStorage, ExplicitVRBigEndian)
assoc = ae.associate("localhost", 11112)

assert assoc.is_established
ds = dcmread(DATASET_PATH)
assert ds.is_little_endian
assert not ds.is_implicit_VR
assert ds.file_meta.TransferSyntaxUID == ExplicitVRLittleEndian

is_implicit_vr_warning = (
"'FileDataset.is_implicit_VR' will be removed in v4.0, set the "
"Transfer Syntax UID or use the 'implicit_vr' argument with "
r"FileDataset.save_as\(\) or dcmwrite\(\) instead"
)

is_little_endian_warning = (
"'FileDataset.is_little_endian' will be removed in v4.0, set the "
"Transfer Syntax UID or use the 'little_endian' argument with "
r"FileDataset.save_as\(\) or dcmwrite\(\) instead"
)

with pytest.warns(DeprecationWarning, match=is_implicit_vr_warning):
ds.is_implicit_VR = True

status = assoc.send_c_store(ds)
assert status.Status == 0x0000

with pytest.warns(DeprecationWarning, match=is_implicit_vr_warning):
ds.is_implicit_VR = False
with pytest.warns(DeprecationWarning, match=is_little_endian_warning):
ds.is_little_endian = False
ds = Dataset()
ds.SOPClassUID = CTImageStorage
ds.SOPInstanceUID = generate_uid()
file_meta = FileMetaDataset()
file_meta.TransferSyntaxUID = ExplicitVRLittleEndian
ds.file_meta = file_meta

with caplog.at_level(logging.WARNING, logger="pynetdicom"):
with pytest.warns(DeprecationWarning):
ds.is_implicit_VR = True
ds.is_little_endian = True
status = assoc.send_c_store(ds)
assert status.Status == 0x0000

status = assoc.send_c_store(ds)
assert status.Status == 0x0000
with pytest.warns(DeprecationWarning):
ds.is_implicit_VR = False
ds.is_little_endian = False
status = assoc.send_c_store(ds)
assert status.Status == 0x0000

with pytest.warns(DeprecationWarning, match=is_implicit_vr_warning):
assert (
"'dataset' is encoded as implicit VR little endian but the file "
"meta has a (0002,0010) Transfer Syntax UID of 'Explicit VR "
"Little Endian' - using 'Implicit VR Little Endian' instead"
) in caplog.text
assert (
"'dataset' is encoded as explicit VR big endian but the file "
"meta has a (0002,0010) Transfer Syntax UID of 'Explicit VR "
"Little Endian' - using 'Explicit VR Big Endian' instead"
) in caplog.text

with pytest.warns(DeprecationWarning):
ds.is_implicit_VR = False
with pytest.warns(DeprecationWarning, match=is_little_endian_warning):
ds.is_little_endian = True
ds.file_meta.TransferSyntaxUID = ImplicitVRLittleEndian

encoding_mismatch_msg = (
msg = (
"'dataset' is encoded as explicit VR little endian but the file "
r"meta has a \(0002,0010\) Transfer Syntax UID of 'Implicit VR "
"Little Endian' - please set an appropriate Transfer Syntax"
)
with pytest.raises(AttributeError, match=encoding_mismatch_msg):
with pytest.raises(AttributeError, match=msg):
status = assoc.send_c_store(ds)

assoc.release()
Expand Down

0 comments on commit bb54e2b

Please sign in to comment.