Browse Source

Add ArtificialND2 class to help in testing nd2 file reading and parsing

feature/load_slices
Ruben Verweij 8 years ago
parent
commit
eb12a4cfd4
2 changed files with 63 additions and 4 deletions
  1. +47
    -0
      nd2reader/artificial.py
  2. +16
    -4
      tests/test_common.py

+ 47
- 0
nd2reader/artificial.py View File

@ -0,0 +1,47 @@
"""
Functions to create artificial nd2 data for testing purposes
"""
import six
class ArtificialND2(object):
"""
Artificial ND2 class (for testing purposes)
"""
def __init__(self, file):
self._fh = open(file, 'wb')
self.write_version()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
@property
def file_handle(self):
"""
The file handle to the binary file
Returns:
file: the file handle
"""
return self._fh
def close(self):
"""
Correctly close the file handle
"""
if self._fh is not None:
self._fh.close()
def write_version(self):
"""
Write file header
"""
# write 16 empty bytes
self._fh.write(bytearray(16))
# write version info
version_info = six.b('ND2 FILE SIGNATURE CHUNK NAME01!Ver3.0')
self._fh.write(version_info)

+ 16
- 4
tests/test_common.py View File

@ -1,10 +1,16 @@
import unittest import unittest
from os import path
import six import six
from nd2reader.common import parse_version, parse_date, _add_to_metadata
from nd2reader.artificial import ArtificialND2
from nd2reader.common import get_version, parse_version, parse_date, _add_to_metadata
class TestCommon(unittest.TestCase): class TestCommon(unittest.TestCase):
def setUp(self):
dir_path = path.dirname(path.realpath(__file__))
self.test_file = path.join(dir_path, 'test_data/test.nd2')
def test_parse_version_2(self): def test_parse_version_2(self):
data = 'ND2 FILE SIGNATURE CHUNK NAME01!Ver2.2' data = 'ND2 FILE SIGNATURE CHUNK NAME01!Ver2.2'
actual = parse_version(data) actual = parse_version(data)
@ -17,6 +23,14 @@ class TestCommon(unittest.TestCase):
expected = (3, 0) expected = (3, 0)
self.assertTupleEqual(actual, expected) self.assertTupleEqual(actual, expected)
def test_get_version_from_file(self):
with ArtificialND2(self.test_file) as artificial:
artificial.close()
with open(self.test_file, 'rb') as fh:
version_tuple = get_version(fh)
self.assertTupleEqual(version_tuple, (3, 0))
def test_parse_date_24(self): def test_parse_date_24(self):
date_format = "%m/%d/%Y %H:%M:%S" date_format = "%m/%d/%Y %H:%M:%S"
date = '02/13/2016 23:43:37' date = '02/13/2016 23:43:37'
@ -45,5 +59,3 @@ 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']})

Loading…
Cancel
Save