From 2b0600ee09de99c92f50eb3209ba89c0a8c87c59 Mon Sep 17 00:00:00 2001 From: Ruben Verweij Date: Thu, 16 Mar 2017 17:31:38 +0100 Subject: [PATCH] Extend unit tests --- nd2reader/legacy.py | 4 ++-- tests/test_common.py | 31 ++++++++++++++++++++++++++++++- tests/test_legacy.py | 25 +++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 tests/test_legacy.py diff --git a/nd2reader/legacy.py b/nd2reader/legacy.py index 244ee73..333198c 100644 --- a/nd2reader/legacy.py +++ b/nd2reader/legacy.py @@ -91,7 +91,7 @@ class Nd2(object): int: the pixel height of the image """ - return self.reader.metadata["height"] + return self.reader.metadata["height"] if self.reader.metadata["height"] is not None else 0 @property def width(self): @@ -101,7 +101,7 @@ class Nd2(object): int: the pixel width of the image """ - return self.reader.metadata["width"] + return self.reader.metadata["width"] if self.reader.metadata["width"] is not None else 0 @property def z_levels(self): diff --git a/tests/test_common.py b/tests/test_common.py index 8f5ac83..ac71516 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -1,9 +1,13 @@ +import io import unittest from os import path import six +import struct from nd2reader.artificial import ArtificialND2 -from nd2reader.common import get_version, parse_version, parse_date, _add_to_metadata +from nd2reader.common import get_version, parse_version, parse_date, _add_to_metadata, _parse_unsigned_char, \ + _parse_unsigned_int, _parse_unsigned_long, _parse_double, _parse_string +from nd2reader.exceptions import InvalidVersionError class TestCommon(unittest.TestCase): @@ -27,6 +31,10 @@ class TestCommon(unittest.TestCase): expected = (3, 0) self.assertTupleEqual(actual, expected) + def test_parse_version_invalid(self): + data = 'ND2 FILE SIGNATURE CHUNK NAME!Version2.2.3' + self.assertRaises(InvalidVersionError, parse_version, data) + def test_get_version_from_file(self): self.create_test_nd2() @@ -62,3 +70,24 @@ class TestCommon(unittest.TestCase): metadata = {'test': ['value1', 'value2']} _add_to_metadata(metadata, 'test', 'value3') self.assertDictEqual(metadata, {'test': ['value1', 'value2', 'value3']}) + + @staticmethod + def _prepare_bin_stream(format, value): + file = io.BytesIO() + data = struct.pack(format, value) + file.write(data) + file.seek(0) + return file + + def test_parse_functions(self): + file = self._prepare_bin_stream("B", 9) + self.assertEqual(_parse_unsigned_char(file), 9) + + file = self._prepare_bin_stream("I", 333) + self.assertEqual(_parse_unsigned_int(file), 333) + + file = self._prepare_bin_stream("Q", 7564332) + self.assertEqual(_parse_unsigned_long(file), 7564332) + + file = self._prepare_bin_stream("d", 47.9) + self.assertEqual(_parse_double(file), 47.9) diff --git a/tests/test_legacy.py b/tests/test_legacy.py new file mode 100644 index 0000000..c28d5d6 --- /dev/null +++ b/tests/test_legacy.py @@ -0,0 +1,25 @@ +import unittest +from os import path + +from nd2reader.artificial import ArtificialND2 +from nd2reader.legacy import Nd2 + + +class TestLegacy(unittest.TestCase): + def create_test_nd2(self): + with ArtificialND2(self.test_file) as artificial: + artificial.close() + + def setUp(self): + dir_path = path.dirname(path.realpath(__file__)) + self.test_file = path.join(dir_path, 'test_data/test.nd2') + + def test_can_open_test_file(self): + self.create_test_nd2() + with Nd2(self.test_file) as reader: + self.assertEqual(reader.width, 0) + self.assertEqual(reader.height, 0) + self.assertEqual(len(reader.z_levels), 1) + self.assertEqual(len(reader.channels), 0) + self.assertEqual(len(reader.frames), 1) +