Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kostrykin committed Sep 24, 2024
1 parent 1ffae85 commit 782f2cb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]

omit = tests/*
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ __pycache__
/build
*.swp
tests/*-out-*
/.venv
/.venv
/.coverage
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
Galaxy Image Analysis: https://github.com/BMCV/galaxy-image-analysis

Use ``python -m unittest`` in the root directory of the repository to run the test suite.

Use ``coverage run -m unittest && coverage html`` to generate a coverage report.
2 changes: 1 addition & 1 deletion giatools/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def imread(*args, **kwargs):
tool has failed: https://docs.galaxyproject.org/en/latest/dev/schema.html#error-detection To prevent this, this
wrapper around ``skimage.io.imread`` will mute all non-fatal errors.
Image loading is first attempted using ``tifffile`` (if available, more reliable for loading TIFF files), and if
Image loading is first attempted using `tifffile` (if available, more reliable for loading TIFF files), and if
that fails (e.g., because the file is not a TIFF file), falls back to ``skimage.io.imread``.
"""

Expand Down
Binary file added tests/data/input4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 24 additions & 1 deletion tests/test_io.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import unittest
import unittest.mock

import giatools.io


# This tests require that the `tifffile` package is installed.
assert giatools.io.tifffile is not None


class imread(unittest.TestCase):

def test_input1(self):
Expand All @@ -16,10 +21,28 @@ def test_input2(self):
def test_input3(self):
"""
This is a multi-page TIFF, that sometimes fails to load properly with ``skimage.io.imread``, but works with
``tifffile``.
`tifffile`.
For details see: https://github.com/BMCV/galaxy-image-analysis/pull/132#issuecomment-2371561435
"""
img = giatools.io.imread('tests/data/input3.tif')
self.assertEqual(img.shape, (5, 198, 356))
self.assertEqual(img.mean(), 1259.6755334241288)

def test_input4(self):
"""
This is an RGB PNG file, that cannot be loaded with `tifffile`, but works with ``skimage.io.imread``.
"""
img = giatools.io.imread('tests/data/input4.png')
self.assertEqual(img.shape, (10, 10, 3))
self.assertEqual(img.mean(), 130.04)

@unittest.mock.patch('skimage.io.imread')
@unittest.mock.patch('giatools.io.tifffile', None)
def test_without_tifffile(self, mock_skimage_io_imread):
"""
Test that loading an image without `tifffile` installed falls back to ``skimage.io.imread``.
"""
img = giatools.io.imread('tests/data/input1.tif')
mock_skimage_io_imread.assert_called_once_with('tests/data/input1.tif')
self.assertIs(img, mock_skimage_io_imread.return_value)

0 comments on commit 782f2cb

Please sign in to comment.