From bb753149233e9fd8afa32b2deb534a5b3d97e2c6 Mon Sep 17 00:00:00 2001 From: Cameron Paul Date: Tue, 19 May 2015 21:52:40 -0500 Subject: [PATCH 1/5] 100% test coverage --- Makefile | 4 +-- rawkit/raw.py | 4 +-- requirements-test.txt | 1 + tests/unit/__init__.py | 0 tests/unit/raw_test.py | 78 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 tests/unit/__init__.py create mode 100644 tests/unit/raw_test.py diff --git a/Makefile b/Makefile index 6b8d942..c580417 100644 --- a/Makefile +++ b/Makefile @@ -44,11 +44,11 @@ upload: build test .PHONY: clean clean: + $(ACTIVATE); $(MAKE) -C docs $@ find . -iname '*.pyc' | xargs rm -f rm -rf .tox - rm -rf $(VENV) rm -rf dist - $(MAKE) -C docs $@ + rm -rf $(VENV) .PHONY: gendoc docs: docs/*.rst diff --git a/rawkit/raw.py b/rawkit/raw.py index aaa668c..1c9e11f 100644 --- a/rawkit/raw.py +++ b/rawkit/raw.py @@ -30,7 +30,7 @@ class Raw(object): def __init__(self, filename=None): """Initializes a new Raw object.""" self.data = libraw.libraw_init(0) - libraw.libraw_open_file(self.data, bytes(filename, 'utf-8')) + libraw.libraw_open_file(self.data, filename.encode('ascii')) def __enter__(self): """Return a Raw object for use in context managers.""" @@ -67,7 +67,7 @@ def save(self, filename=None, filetype='ppm'): self.data.contents.params.output_tiff = 0 if filetype is 'ppm' else 1 libraw.libraw_dcraw_ppm_tiff_writer( - self.data, bytes(filename, 'utf-8')) + self.data, filename.encode('ascii')) def to_buffer(self): """Return the image data as an RGB buffer.""" diff --git a/requirements-test.txt b/requirements-test.txt index 6957e14..70b1728 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,5 +1,6 @@ coverage flake8 +mock pytest tox sphinx diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/raw_test.py b/tests/unit/raw_test.py new file mode 100644 index 0000000..058f015 --- /dev/null +++ b/tests/unit/raw_test.py @@ -0,0 +1,78 @@ +import mock +import pytest +from rawkit.raw import Raw + + +@pytest.yield_fixture +def mock_libraw(): + with mock.patch('rawkit.raw.libraw') as libraw: + yield libraw + + +@pytest.fixture +def input_file(): + return 'potato_salad.CR2' + + +@pytest.fixture +def output_file(): + return 'potato_salad.out' + + +def test_create(mock_libraw, input_file): + with Raw(filename=input_file) as raw: + mock_libraw.libraw_init.assert_called_once() + mock_libraw.libraw_open_file.assert_called_once_with( + raw.data, + input_file.encode('ascii'), + ) + + mock_libraw.libraw_close.assert_called_once_with(raw.data) + + +def test_process(mock_libraw, input_file): + with Raw(filename=input_file) as raw: + raw.process() + + mock_libraw.libraw_unpack.assert_called_once_with(raw.data) + mock_libraw.libraw_dcraw_process.assert_called_once_with(raw.data) + + +def _test_save(mock_libraw, input_file, output_file, filetype): + with Raw(filename=input_file) as raw: + raw.process() + raw.save(filename=output_file, filetype=filetype) + + mock_libraw.libraw_dcraw_ppm_tiff_writer.assert_called_once_with( + raw.data, + output_file.encode('ascii'), + ) + + +def test_save_ppm(mock_libraw, input_file, output_file): + _test_save(mock_libraw, input_file, output_file, 'ppm') + + +def test_save_tiff(mock_libraw, input_file, output_file): + _test_save(mock_libraw, input_file, output_file, 'tiff') + + +def test_save_invalid(mock_libraw, input_file, output_file): + with pytest.raises(AssertionError): + _test_save(mock_libraw, input_file, output_file, 'jpg') + + +def test_to_buffer(mock_libraw, input_file): + with Raw(filename=input_file) as raw: + raw.process() + # Quick hack because to_buffer does some ctypes acrobatics + with mock.patch('rawkit.raw.ctypes'): + raw.to_buffer() + + mock_libraw.libraw_dcraw_make_mem_image.assert_called_once_with( + raw.data, + ) + + mock_libraw.libraw_dcraw_clear_mem.assert_called_once_with( + mock_libraw.libraw_dcraw_make_mem_image(raw.data), + ) From 9037c28bead4c750eb3b7335bafb53c61f397d33 Mon Sep 17 00:00:00 2001 From: Cameron Paul Date: Tue, 19 May 2015 22:05:14 -0500 Subject: [PATCH 2/5] Updated .travis.yml with libraw-bin --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c207ae3..92a6545 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ env: - TOXENV=pypy3 before_install: - sudo apt-get update -qq - - sudo apt-get install -qq libraw-dev + - sudo apt-get install -qq libraw-bin install: - pip install -r requirements-test.txt From aa08496456ae99544222c5db0c23181201e742bc Mon Sep 17 00:00:00 2001 From: Cameron Paul Date: Tue, 19 May 2015 23:02:28 -0500 Subject: [PATCH 3/5] Don't hard code libraw file name --- rawkit/libraw.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rawkit/libraw.py b/rawkit/libraw.py index 1204bad..0cf3c47 100644 --- a/rawkit/libraw.py +++ b/rawkit/libraw.py @@ -3,9 +3,11 @@ """ from ctypes import * # noqa +from ctypes import util -libraw = cdll.LoadLibrary('libraw.so.10') +# TODO: This will do bad things if the API version isn't 10 +libraw = cdll.LoadLibrary(util.find_library('raw')) class ph1_t(Structure): From 8f8f6787cf18435b993900927c3880fb3e49b3ce Mon Sep 17 00:00:00 2001 From: Cameron Paul Date: Tue, 19 May 2015 23:23:26 -0500 Subject: [PATCH 4/5] Add travis hack --- rawkit/libraw.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/rawkit/libraw.py b/rawkit/libraw.py index 0cf3c47..191acc0 100644 --- a/rawkit/libraw.py +++ b/rawkit/libraw.py @@ -6,10 +6,6 @@ from ctypes import util -# TODO: This will do bad things if the API version isn't 10 -libraw = cdll.LoadLibrary(util.find_library('raw')) - - class ph1_t(Structure): """Contains color data read by Phase One cameras.""" @@ -244,5 +240,13 @@ class libraw_processed_image_t(Structure): ] -libraw.libraw_init.restype = POINTER(libraw_data_t) -libraw.libraw_dcraw_make_mem_image.restype = POINTER(libraw_processed_image_t) +# TODO: This is necessary because Travis CI is still using Ubuntu 12.04 +try: + # TODO: This will do bad things if the API version isn't 10 + libraw = cdll.LoadLibrary(util.find_library('raw')) + libraw.libraw_init.restype = POINTER(libraw_data_t) + libraw.libraw_dcraw_make_mem_image.restype = POINTER( + libraw_processed_image_t + ) +except: # pragma: no cover + libraw = cdll.LoadLibrary('') # pragma: no cover From 731f02ddc4f943e48376860cbe733d350ec00a9b Mon Sep 17 00:00:00 2001 From: Cameron Paul Date: Tue, 19 May 2015 23:43:06 -0500 Subject: [PATCH 5/5] Added more `pragma: no cover` for travis hack --- rawkit/libraw.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rawkit/libraw.py b/rawkit/libraw.py index 191acc0..cd2c86e 100644 --- a/rawkit/libraw.py +++ b/rawkit/libraw.py @@ -243,10 +243,10 @@ class libraw_processed_image_t(Structure): # TODO: This is necessary because Travis CI is still using Ubuntu 12.04 try: # TODO: This will do bad things if the API version isn't 10 - libraw = cdll.LoadLibrary(util.find_library('raw')) - libraw.libraw_init.restype = POINTER(libraw_data_t) - libraw.libraw_dcraw_make_mem_image.restype = POINTER( - libraw_processed_image_t - ) -except: # pragma: no cover - libraw = cdll.LoadLibrary('') # pragma: no cover + libraw = cdll.LoadLibrary(util.find_library('raw')) # pragma: no cover + libraw.libraw_init.restype = POINTER(libraw_data_t) # pragma: no cover + libraw.libraw_dcraw_make_mem_image.restype = POINTER( # pragme: no cover + libraw_processed_image_t # pragma: no cover + ) # pragma: no cover +except: # pragma: no cover + libraw = cdll.LoadLibrary('') # pragma: no cover