Browse Source

Add more unit test for common + fix possibly endless loop

Ruben Verweij 8 years ago
parent
commit
17df7ed8a6
3 changed files with 60 additions and 6 deletions
  1. +12
    -3
      nd2reader/common.py
  2. +22
    -0
      tests/test_artificial.py
  3. +26
    -3
      tests/test_common.py

+ 12
- 3
nd2reader/common.py View File

@ -155,10 +155,19 @@ def _parse_string(data):
"""
value = data.read(2)
# the string ends at the first instance of \x00\x00
while not value.endswith(six.b("\x00\x00")):
# the string ends at the first instance of \x00\x00
value += data.read(2)
return value.decode("utf16")[:-1].encode("utf8")
next_data = data.read(2)
if len(next_data) == 0:
break
value += next_data
try:
decoded = value.decode("utf16")[:-1].encode("utf8")
except UnicodeDecodeError:
decoded = value.decode('utf8')
return decoded
def _parse_char_array(data):


+ 22
- 0
tests/test_artificial.py View File

@ -0,0 +1,22 @@
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, _parse_unsigned_char, \
_parse_unsigned_int, _parse_unsigned_long, _parse_double, check_or_make_dir
from nd2reader.exceptions import InvalidVersionError
class TestArtificial(unittest.TestCase):
def setUp(self):
dir_path = path.dirname(path.realpath(__file__))
check_or_make_dir(path.join(dir_path, 'test_data/'))
self.test_file = path.join(dir_path, 'test_data/test.nd2')
self.create_test_nd2()
def create_test_nd2(self):
with ArtificialND2(self.test_file) as artificial:
self.assertIsNotNone(artificial.file_handle)
artificial.close()

+ 26
- 3
tests/test_common.py View File

@ -1,11 +1,14 @@
import unittest
from os import path
import array
import six
import struct
from nd2reader.artificial import ArtificialND2
from nd2reader.common import get_version, parse_version, parse_date, _add_to_metadata, _parse_unsigned_char, \
_parse_unsigned_int, _parse_unsigned_long, _parse_double, check_or_make_dir
_parse_unsigned_int, _parse_unsigned_long, _parse_double, check_or_make_dir, _parse_string, _parse_char_array, \
get_from_dict_if_exists
from nd2reader.exceptions import InvalidVersionError
@ -78,9 +81,9 @@ class TestCommon(unittest.TestCase):
self.assertDictEqual(metadata, {'test': ['value1', 'value2', 'value3']})
@staticmethod
def _prepare_bin_stream(binary_format, value):
def _prepare_bin_stream(binary_format, *value):
file = six.BytesIO()
data = struct.pack(binary_format, value)
data = struct.pack(binary_format, *value)
file.write(data)
file.seek(0)
return file
@ -97,3 +100,23 @@ class TestCommon(unittest.TestCase):
file = self._prepare_bin_stream("d", 47.9)
self.assertEqual(_parse_double(file), 47.9)
test_string = 'colloid'
file = self._prepare_bin_stream("%ds" % len(test_string), six.b(test_string))
parsed = _parse_string(file)
self.assertEqual(parsed, test_string)
test_data = [1, 2, 3, 4, 5]
file = self._prepare_bin_stream("Q" + ''.join(['B'] * len(test_data)), len(test_data), *test_data)
parsed = _parse_char_array(file)
self.assertEqual(parsed, array.array('B', test_data))
def test_get_from_dict_if_exists(self):
test_dict = {
six.b('existing'): 'test',
'string': 'test2'
}
self.assertIsNone(get_from_dict_if_exists('nowhere', test_dict))
self.assertEqual(get_from_dict_if_exists('existing', test_dict), 'test')
self.assertEqual(get_from_dict_if_exists('string', test_dict, convert_key_to_binary=False), 'test2')

Loading…
Cancel
Save