From f039b9e123316d365242077ea2af87715628e3cb Mon Sep 17 00:00:00 2001 From: Ruben Verweij Date: Wed, 19 Jul 2017 10:54:54 +0200 Subject: [PATCH] Extend unit tests, remove badge --- README.md | 1 - badge.svg | 59 -------------------------------------- nd2reader/artificial.py | 13 +++++---- tests/test_common.py | 35 +++++++++++++++++++++- tests/test_raw_metadata.py | 5 ++++ 5 files changed, 46 insertions(+), 67 deletions(-) delete mode 100644 badge.svg diff --git a/README.md b/README.md index ed5da34..8620ad2 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ [![Build Status](https://travis-ci.org/rbnvrw/nd2reader.svg?branch=master)](https://travis-ci.org/rbnvrw/nd2reader) [![Test Coverage](https://codeclimate.com/github/rbnvrw/nd2reader/badges/coverage.svg)](https://codeclimate.com/github/rbnvrw/nd2reader/coverage) [![Code Climate](https://codeclimate.com/github/rbnvrw/nd2reader/badges/gpa.svg)](https://codeclimate.com/github/rbnvrw/nd2reader) -![(score out of 4.0)](https://cdn.rawgit.com/rbnvrw/nd2reader/6f4536f1/badge.svg) ### About diff --git a/badge.svg b/badge.svg deleted file mode 100644 index c40fb1d..0000000 --- a/badge.svg +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - image/svg+xml - - - - - - - - (score out of 4.0) - - diff --git a/nd2reader/artificial.py b/nd2reader/artificial.py index 1870ff0..3700c55 100644 --- a/nd2reader/artificial.py +++ b/nd2reader/artificial.py @@ -24,7 +24,7 @@ class ArtificialND2(object): def __init__(self, file, version=(3, 0)): self.version = version - self.raw_text, self.locations, self.data = None, None, None + self.raw_text, self.locations, self.data = b'', None, None check_or_make_dir(path.dirname(file)) self._fh = open(file, 'w+b', 0) self.write_file() @@ -53,16 +53,17 @@ class ArtificialND2(object): def write_file(self): self.write_version() - self.raw_text, self.locations, self.data = self.write_label_map() + self.locations, self.data = self.write_label_map() + self._fh.write(self.raw_text) def write_version(self): """Write file header """ # write 16 empty bytes - self._fh.write(bytearray(16)) + self.raw_text += bytearray(16) # write version info - self._fh.write(self._get_version_string()) + self.raw_text += self._get_version_string() def _get_version_string(self): return six.b('ND2 FILE SIGNATURE CHUNK NAME01!Ver%s.%s' % self.version) @@ -72,9 +73,9 @@ class ArtificialND2(object): def write_label_map(self): raw_text, locations, data = self.create_label_map_bytes() - self._fh.write(raw_text) + self.raw_text += raw_text - return raw_text, locations, data + return locations, data def create_label_map_bytes(self): """Construct a binary label map diff --git a/tests/test_common.py b/tests/test_common.py index f08ce7f..380af7e 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -8,7 +8,7 @@ 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_string, _parse_char_array, \ - get_from_dict_if_exists + get_from_dict_if_exists, read_chunk from nd2reader.exceptions import InvalidVersionError @@ -120,3 +120,36 @@ class TestCommon(unittest.TestCase): 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') + + def test_read_chunk(self): + with ArtificialND2(self.test_file) as artificial: + fh = artificial.file_handle + chunk_location = artificial.locations['image_attributes'][0] + + chunk_read = read_chunk(fh, chunk_location) + real_data = six.BytesIO(artificial.raw_text) + + real_data.seek(chunk_location) + + # The chunk metadata is always 16 bytes long + chunk_metadata = real_data.read(16) + header, relative_offset, data_length = struct.unpack("IIQ", chunk_metadata) + self.assertEquals(header, 0xabeceda) + + # We start at the location of the chunk metadata, skip over the metadata, and then proceed to the + # start of the actual data field, which is at some arbitrary place after the metadata. + real_data.seek(chunk_location + 16 + relative_offset) + + real_chunk = real_data.read(data_length) + + self.assertEqual(real_chunk, chunk_read) + + def test_read_chunk_fail_bad_header(self): + with ArtificialND2(self.test_file) as artificial: + fh = artificial.file_handle + chunk_location = artificial.locations['image_attributes'][0] + + with self.assertRaises(ValueError) as context: + read_chunk(fh, chunk_location + 1) + + self.assertEquals(str(context.exception), "The ND2 file seems to be corrupted.") diff --git a/tests/test_raw_metadata.py b/tests/test_raw_metadata.py index 75efc75..3ca0463 100644 --- a/tests/test_raw_metadata.py +++ b/tests/test_raw_metadata.py @@ -35,6 +35,11 @@ class TestRawMetadata(unittest.TestCase): for required in required_keys: self.assertTrue(required in metadata) + def test_cached_metadata(self): + metadata_one = self.metadata.get_parsed_metadata() + metadata_two = self.metadata.get_parsed_metadata() + self.assertEqual(metadata_one, metadata_two) + def test_pfs_status(self): self.assertEqual(self.file_data['pfs_status'], self.metadata.pfs_status[0])