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.

93 lines
3.2 KiB

  1. import io
  2. import unittest
  3. from os import path
  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, _parse_string
  9. from nd2reader.exceptions import InvalidVersionError
  10. class TestCommon(unittest.TestCase):
  11. def setUp(self):
  12. dir_path = path.dirname(path.realpath(__file__))
  13. self.test_file = path.join(dir_path, 'test_data/test.nd2')
  14. def create_test_nd2(self):
  15. with ArtificialND2(self.test_file) as artificial:
  16. artificial.close()
  17. def test_parse_version_2(self):
  18. data = 'ND2 FILE SIGNATURE CHUNK NAME01!Ver2.2'
  19. actual = parse_version(data)
  20. expected = (2, 2)
  21. self.assertTupleEqual(actual, expected)
  22. def test_parse_version_3(self):
  23. data = 'ND2 FILE SIGNATURE CHUNK NAME01!Ver3.0'
  24. actual = parse_version(data)
  25. expected = (3, 0)
  26. self.assertTupleEqual(actual, expected)
  27. def test_parse_version_invalid(self):
  28. data = 'ND2 FILE SIGNATURE CHUNK NAME!Version2.2.3'
  29. self.assertRaises(InvalidVersionError, parse_version, data)
  30. def test_get_version_from_file(self):
  31. self.create_test_nd2()
  32. with open(self.test_file, 'rb') as fh:
  33. version_tuple = get_version(fh)
  34. self.assertTupleEqual(version_tuple, (3, 0))
  35. def test_parse_date_24(self):
  36. date_format = "%m/%d/%Y %H:%M:%S"
  37. date = '02/13/2016 23:43:37'
  38. textinfo = {six.b('TextInfoItem9'): six.b(date)}
  39. result = parse_date(textinfo)
  40. self.assertEqual(result.strftime(date_format), date)
  41. def test_parse_date_12(self):
  42. date_format = "%m/%d/%Y %I:%M:%S %p"
  43. date = '02/13/2016 11:43:37 PM'
  44. textinfo = {six.b('TextInfoItem9'): six.b(date)}
  45. result = parse_date(textinfo)
  46. self.assertEqual(result.strftime(date_format), date)
  47. def test_add_to_meta_simple(self):
  48. metadata = {}
  49. _add_to_metadata(metadata, 'test', 'value')
  50. self.assertDictEqual(metadata, {'test': 'value'})
  51. def test_add_to_meta_new_list(self):
  52. metadata = {'test': 'value1'}
  53. _add_to_metadata(metadata, 'test', 'value2')
  54. self.assertDictEqual(metadata, {'test': ['value1', 'value2']})
  55. def test_add_to_meta_existing_list(self):
  56. metadata = {'test': ['value1', 'value2']}
  57. _add_to_metadata(metadata, 'test', 'value3')
  58. self.assertDictEqual(metadata, {'test': ['value1', 'value2', 'value3']})
  59. @staticmethod
  60. def _prepare_bin_stream(format, value):
  61. file = io.BytesIO()
  62. data = struct.pack(format, value)
  63. file.write(data)
  64. file.seek(0)
  65. return file
  66. def test_parse_functions(self):
  67. file = self._prepare_bin_stream("B", 9)
  68. self.assertEqual(_parse_unsigned_char(file), 9)
  69. file = self._prepare_bin_stream("I", 333)
  70. self.assertEqual(_parse_unsigned_int(file), 333)
  71. file = self._prepare_bin_stream("Q", 7564332)
  72. self.assertEqual(_parse_unsigned_long(file), 7564332)
  73. file = self._prepare_bin_stream("d", 47.9)
  74. self.assertEqual(_parse_double(file), 47.9)