You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.4 KiB

  1. import unittest
  2. from os import path
  3. import array
  4. import six
  5. import struct
  6. from nd2reader.artificial import ArtificialND2
  7. from nd2reader.common import get_version, parse_version, parse_date, _add_to_metadata, _parse_unsigned_char, \
  8. _parse_unsigned_int, _parse_unsigned_long, _parse_double, check_or_make_dir, _parse_string, _parse_char_array, \
  9. get_from_dict_if_exists
  10. from nd2reader.exceptions import InvalidVersionError
  11. class TestCommon(unittest.TestCase):
  12. def setUp(self):
  13. dir_path = path.dirname(path.realpath(__file__))
  14. check_or_make_dir(path.join(dir_path, 'test_data/'))
  15. self.test_file = path.join(dir_path, 'test_data/test.nd2')
  16. def create_test_nd2(self):
  17. with ArtificialND2(self.test_file) as artificial:
  18. artificial.close()
  19. def test_parse_version_2(self):
  20. data = 'ND2 FILE SIGNATURE CHUNK NAME01!Ver2.2'
  21. actual = parse_version(data)
  22. expected = (2, 2)
  23. self.assertTupleEqual(actual, expected)
  24. def test_parse_version_3(self):
  25. data = 'ND2 FILE SIGNATURE CHUNK NAME01!Ver3.0'
  26. actual = parse_version(data)
  27. expected = (3, 0)
  28. self.assertTupleEqual(actual, expected)
  29. def test_parse_version_invalid(self):
  30. data = 'ND2 FILE SIGNATURE CHUNK NAME!Version2.2.3'
  31. self.assertRaises(InvalidVersionError, parse_version, data)
  32. def test_get_version_from_file(self):
  33. self.create_test_nd2()
  34. with open(self.test_file, 'rb') as fh:
  35. version_tuple = get_version(fh)
  36. self.assertTupleEqual(version_tuple, (3, 0))
  37. def test_parse_date_24(self):
  38. date_format = "%m/%d/%Y %H:%M:%S"
  39. date = '02/13/2016 23:43:37'
  40. textinfo = {six.b('TextInfoItem9'): six.b(date)}
  41. result = parse_date(textinfo)
  42. self.assertEqual(result.strftime(date_format), date)
  43. def test_parse_date_12(self):
  44. date_format = "%m/%d/%Y %I:%M:%S %p"
  45. date = '02/13/2016 11:43:37 PM'
  46. textinfo = {six.b('TextInfoItem9'): six.b(date)}
  47. result = parse_date(textinfo)
  48. self.assertEqual(result.strftime(date_format), date)
  49. def test_parse_date_exception(self):
  50. date = 'i am no date'
  51. textinfo = {six.b('TextInfoItem9'): six.b(date)}
  52. result = parse_date(textinfo)
  53. self.assertIsNone(result)
  54. def test_add_to_meta_simple(self):
  55. metadata = {}
  56. _add_to_metadata(metadata, 'test', 'value')
  57. self.assertDictEqual(metadata, {'test': 'value'})
  58. def test_add_to_meta_new_list(self):
  59. metadata = {'test': 'value1'}
  60. _add_to_metadata(metadata, 'test', 'value2')
  61. self.assertDictEqual(metadata, {'test': ['value1', 'value2']})
  62. def test_add_to_meta_existing_list(self):
  63. metadata = {'test': ['value1', 'value2']}
  64. _add_to_metadata(metadata, 'test', 'value3')
  65. self.assertDictEqual(metadata, {'test': ['value1', 'value2', 'value3']})
  66. @staticmethod
  67. def _prepare_bin_stream(binary_format, *value):
  68. file = six.BytesIO()
  69. data = struct.pack(binary_format, *value)
  70. file.write(data)
  71. file.seek(0)
  72. return file
  73. def test_parse_functions(self):
  74. file = self._prepare_bin_stream("B", 9)
  75. self.assertEqual(_parse_unsigned_char(file), 9)
  76. file = self._prepare_bin_stream("I", 333)
  77. self.assertEqual(_parse_unsigned_int(file), 333)
  78. file = self._prepare_bin_stream("Q", 7564332)
  79. self.assertEqual(_parse_unsigned_long(file), 7564332)
  80. file = self._prepare_bin_stream("d", 47.9)
  81. self.assertEqual(_parse_double(file), 47.9)
  82. test_string = 'colloid'
  83. file = self._prepare_bin_stream("%ds" % len(test_string), six.b(test_string))
  84. parsed = _parse_string(file)
  85. self.assertEqual(parsed, test_string)
  86. test_data = [1, 2, 3, 4, 5]
  87. file = self._prepare_bin_stream("Q" + ''.join(['B'] * len(test_data)), len(test_data), *test_data)
  88. parsed = _parse_char_array(file)
  89. self.assertEqual(parsed, array.array('B', test_data))
  90. def test_get_from_dict_if_exists(self):
  91. test_dict = {
  92. six.b('existing'): 'test',
  93. 'string': 'test2'
  94. }
  95. self.assertIsNone(get_from_dict_if_exists('nowhere', test_dict))
  96. self.assertEqual(get_from_dict_if_exists('existing', test_dict), 'test')
  97. self.assertEqual(get_from_dict_if_exists('string', test_dict, convert_key_to_binary=False), 'test2')