Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
Merge pull request #18 from photoshell/add_tests
Browse files Browse the repository at this point in the history
100% test coverage
  • Loading branch information
campaul committed May 21, 2015
2 parents dddf898 + 731f02d commit cae43c8
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions rawkit/libraw.py
Expand Up @@ -3,9 +3,7 @@
"""

from ctypes import * # noqa


libraw = cdll.LoadLibrary('libraw.so.10')
from ctypes import util


class ph1_t(Structure):
Expand Down Expand Up @@ -242,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')) # 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
4 changes: 2 additions & 2 deletions rawkit/raw.py
Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down
1 change: 1 addition & 0 deletions requirements-test.txt
@@ -1,5 +1,6 @@
coverage
flake8
mock
pytest
tox
sphinx
Expand Down
Empty file added tests/unit/__init__.py
Empty file.
78 changes: 78 additions & 0 deletions 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),
)

0 comments on commit cae43c8

Please sign in to comment.