def test_add_plugin_module_import_failure(self): """Test a module import failure when adding a plugin.""" enc = Encoder(RLELossless) msg = r"No module named 'badpath'" with pytest.raises(ModuleNotFoundError, match=msg): enc.add_plugin("foo", ('badpath', '_encode_frame')) assert {} == enc._available assert {} == enc._unavailable
def test_no_plugins(self): """Test with no available plugins""" enc = Encoder(RLELossless) enc.add_plugin('foo', ('pydicom.encoders.pylibjpeg', 'encode_pixel_data')) msg = (r"Unable to encode because the encoding plugins are missing " r"dependencies:\n foo - requires numpy, pylibjpeg and " r"pylibjpeg-rle") with pytest.raises(RuntimeError, match=msg): enc._process(b'')
def test_remove_plugin_unavailable(self): """Test removing a plugin.""" enc = Encoder(RLELossless) enc.add_plugin("foo", ('pydicom.encoders.pylibjpeg', 'encode_pixel_data')) assert 'foo' in enc._unavailable assert {} == enc._available enc.remove_plugin("foo") assert {} == enc._unavailable
def test_add_plugin_unavailable(self): """Test adding an unavailable plugin.""" enc = Encoder(RLELossless) assert not enc.is_available enc.add_plugin("foo", ('pydicom.encoders.pylibjpeg', 'encode_pixel_data')) assert enc._available == {} assert "foo" in enc._unavailable assert enc._unavailable["foo"] == ("numpy", "pylibjpeg", "pylibjpeg-rle") assert not enc.is_available
def test_add_plugin_function_missing(self): """Test encoding function missing when adding a plugin.""" enc = Encoder(RLELossless) msg = (r"module 'pydicom.encoders.native' has no " r"attribute 'bad_function_name'") with pytest.raises(AttributeError, match=msg): enc.add_plugin( "foo", ('pydicom.encoders.native', 'bad_function_name'), ) assert {} == enc._available assert {} == enc._unavailable
def test_add_plugin_unavailable(self): """Test adding an unavailable plugin.""" enc = Encoder(RLELossless) assert not enc.is_available enc.add_plugin( "foo", ('pydicom.pixel_data_handlers.rle_handler', '_wrap_rle_encode_frame'), ) assert {} == enc._available assert "foo" in enc._unavailable assert ("numpy", ) == enc._unavailable["foo"] assert not enc.is_available
def test_remove_plugin_unavailable(self): """Test removing a plugin.""" enc = Encoder(RLELossless) enc.add_plugin( "foo", ('pydicom.pixel_data_handlers.rle_handler', '_wrap_rle_encode_frame'), ) assert 'foo' in enc._unavailable assert {} == enc._available enc.remove_plugin("foo") assert {} == enc._unavailable
class TestEncoder: """Non-encoding tests for encoders.Encoder""" def setup(self): self.enc = Encoder(UID('1.2.3')) def test_init(self): """Test creating a new Encoder""" uid = UID('1.2.3') enc = Encoder(uid) assert {} == enc._available assert {} == enc._unavailable assert '<' == enc._defaults['byteorder'] assert uid == enc._defaults['transfer_syntax_uid'] def test_properties(self): """Test Encoder properties""" enc = Encoder(RLELossless) assert 'RLELosslessEncoder' == enc.name assert RLELossless == enc.UID assert not enc.is_available @pytest.mark.skipif(not HAVE_NP, reason="Numpy not available") def test_add_plugin_available(self): """Test adding an available plugin.""" assert not self.enc.is_available self.enc.add_plugin("foo", ('pydicom.encoders.native', '_encode_frame')) assert "foo" in self.enc._available assert {} == self.enc._unavailable assert self.enc.is_available @pytest.mark.skipif(HAVE_NP, reason="Numpy is available") def test_add_plugin_unavailable(self): """Test adding an unavailable plugin.""" enc = Encoder(RLELossless) assert not enc.is_available enc.add_plugin("foo", ('pydicom.encoders.pylibjpeg', 'encode_pixel_data')) assert enc._available == {} assert "foo" in enc._unavailable assert enc._unavailable["foo"] == ("numpy", "pylibjpeg", "pylibjpeg-rle") assert not enc.is_available def test_add_plugin_module_import_failure(self): """Test a module import failure when adding a plugin.""" enc = Encoder(RLELossless) msg = r"No module named 'badpath'" with pytest.raises(ModuleNotFoundError, match=msg): enc.add_plugin("foo", ('badpath', '_encode_frame')) assert {} == enc._available assert {} == enc._unavailable @pytest.mark.skipif(not HAVE_NP, reason="Numpy is available") def test_add_plugin_function_missing(self): """Test encoding function missing when adding a plugin.""" enc = Encoder(RLELossless) msg = (r"module 'pydicom.encoders.native' has no " r"attribute 'bad_function_name'") with pytest.raises(AttributeError, match=msg): enc.add_plugin( "foo", ('pydicom.encoders.native', 'bad_function_name'), ) assert {} == enc._available assert {} == enc._unavailable @pytest.mark.skipif(not HAVE_NP, reason="Numpy is unavailable") def test_add_plugin_twice(self): """Test adding a plugin that already exists.""" self.enc.add_plugin("foo", ('pydicom.encoders.native', '_encode_frame')) assert 'foo' in self.enc._available assert {} == self.enc._unavailable msg = r"'Encoder' already has a plugin named 'foo'" with pytest.raises(ValueError, match=msg): self.enc.add_plugin("foo", ('pydicom.encoders.native', '_encode_frame')) assert 'foo' in self.enc._available assert {} == self.enc._unavailable @pytest.mark.skipif(not HAVE_NP, reason="Numpy is unavailable") def test_remove_plugin(self): """Test removing a plugin.""" self.enc.add_plugin("foo", ('pydicom.encoders.native', '_encode_frame')) self.enc.add_plugin("bar", ('pydicom.encoders.native', '_encode_frame')) assert 'foo' in self.enc._available assert 'bar' in self.enc._available assert {} == self.enc._unavailable assert self.enc.is_available self.enc.remove_plugin("foo") assert 'bar' in self.enc._available assert self.enc.is_available self.enc.remove_plugin("bar") assert {} == self.enc._available assert not self.enc.is_available @pytest.mark.skipif(HAVE_NP, reason="Numpy is available") def test_remove_plugin_unavailable(self): """Test removing a plugin.""" enc = Encoder(RLELossless) enc.add_plugin("foo", ('pydicom.encoders.pylibjpeg', 'encode_pixel_data')) assert 'foo' in enc._unavailable assert {} == enc._available enc.remove_plugin("foo") assert {} == enc._unavailable def test_remove_plugin_raises(self): """Test removing a plugin that doesn't exist raises exception""" msg = r"Unable to remove 'foo', no such plugin" with pytest.raises(ValueError, match=msg): self.enc.remove_plugin('foo') def test_check_kwargs_missing(self): """Test _check_kwargs""" enc = Encoder(RLELossless) kwargs = { 'rows': 0, 'columns': 0, 'samples_per_pixel': 0, 'bits_allocated': 0, 'bits_stored': 0, 'pixel_representation': 0, 'number_of_frames': 0, 'photometric_interpretation': 'RGB' } assert enc._check_kwargs(kwargs) is None del kwargs['columns'] del kwargs['bits_allocated'] msg = r"Missing expected arguments: 'columns', 'bits_allocated'" with pytest.raises(TypeError, match=msg): enc._check_kwargs(kwargs) def test_kwargs_from_ds(self): """Test Encoder.kwargs_from_ds()""" # Note no NumberOfFrames element ds = Dataset() ds.Rows = 10 ds.Columns = 12 ds.SamplesPerPixel = 1 ds.BitsAllocated = 8 ds.BitsStored = 8 ds.PixelRepresentation = 0 ds.PhotometricInterpretation = 'RGB' enc = Encoder(RLELossless) kwargs = enc.kwargs_from_ds(ds) assert 1 == kwargs['number_of_frames'] assert enc._check_kwargs(kwargs) is None # Test conversion of empty *Number of Frames* ds.NumberOfFrames = None kwargs = enc.kwargs_from_ds(ds) assert 1 == kwargs['number_of_frames'] # Test already present *Number of Frames* is unaffected ds.NumberOfFrames = 10 kwargs = enc.kwargs_from_ds(ds) assert 10 == kwargs['number_of_frames'] # Test missing elements del ds.Columns del ds.BitsAllocated msg = ( r"The following required elements are missing from the dataset: " r"'Columns', 'BitsAllocated'") with pytest.raises(AttributeError, match=msg): enc.kwargs_from_ds(ds) # Test VM 0 ds.Columns = None ds.BitsAllocated = None msg = (r"The following required dataset elements have a VM of 0: " r"'Columns', 'BitsAllocated'") with pytest.raises(AttributeError, match=msg): enc.kwargs_from_ds(ds) @pytest.mark.skipif(HAVE_NP, reason="Numpy available") def test_missing_dependencies(self): """Test the required encoder being unavailable.""" enc = RLELosslessEncoder s = enc.missing_dependencies assert s[0] == "gdcm - requires gdcm" assert ( s[1] == "pylibjpeg - requires numpy, pylibjpeg and pylibjpeg-rle") def test_invalid_profile_raises(self): """Test an invalid encoding profile raises exception.""" ds = get_testdata_file("rtdose_1frame.dcm", read=True) assert ds.BitsAllocated == 32 # Invalid for RLE Lossless msg = ( r"Unable to encode as one or more of 'photometric " r"interpretation', 'samples per pixel', 'bits allocated', 'bits " r"stored' or 'pixel representation' is not valid for 'RLE " r"Lossless'") with pytest.raises(ValueError, match=msg): ds.compress(RLELossless) def test_missing_no_dependencies(self): """Test an encoder with no dependencies being unavailable.""" enc = self.enc enc._unavailable['foo'] = () s = enc.missing_dependencies assert 'foo - plugin indicating it is unavailable' == s[0] def test_missing_one_dependency(self): """Test an encoder with one dependency being unavailable.""" enc = self.enc enc._unavailable['foo'] = ('bar', ) s = enc.missing_dependencies assert 'foo - requires bar' == s[0]