Browse Source

Extend unit tests

feature/load_slices
Ruben Verweij 8 years ago
parent
commit
5251464df5
3 changed files with 57 additions and 3 deletions
  1. +2
    -2
      nd2reader/legacy.py
  2. +30
    -1
      tests/test_common.py
  3. +25
    -0
      tests/test_legacy.py

+ 2
- 2
nd2reader/legacy.py View File

@ -91,7 +91,7 @@ class Nd2(object):
int: the pixel height of the image 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 @property
def width(self): def width(self):
@ -101,7 +101,7 @@ class Nd2(object):
int: the pixel width of the image 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 @property
def z_levels(self): def z_levels(self):


+ 30
- 1
tests/test_common.py View File

@ -1,9 +1,13 @@
import io
import unittest import unittest
from os import path from os import path
import six import six
import struct
from nd2reader.artificial import ArtificialND2 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): class TestCommon(unittest.TestCase):
@ -27,6 +31,10 @@ class TestCommon(unittest.TestCase):
expected = (3, 0) expected = (3, 0)
self.assertTupleEqual(actual, expected) 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): def test_get_version_from_file(self):
self.create_test_nd2() self.create_test_nd2()
@ -62,3 +70,24 @@ class TestCommon(unittest.TestCase):
metadata = {'test': ['value1', 'value2']} metadata = {'test': ['value1', 'value2']}
_add_to_metadata(metadata, 'test', 'value3') _add_to_metadata(metadata, 'test', 'value3')
self.assertDictEqual(metadata, {'test': ['value1', 'value2', '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)

+ 25
- 0
tests/test_legacy.py View File

@ -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)

Loading…
Cancel
Save