Browse Source

Extend unit tests, remove badge

Ruben Verweij 8 years ago
parent
commit
a0ae292794
5 changed files with 46 additions and 67 deletions
  1. +0
    -1
      README.md
  2. +0
    -59
      badge.svg
  3. +7
    -6
      nd2reader/artificial.py
  4. +34
    -1
      tests/test_common.py
  5. +5
    -0
      tests/test_raw_metadata.py

+ 0
- 1
README.md View File

@ -5,7 +5,6 @@
[![Build Status](https://travis-ci.org/rbnvrw/nd2reader.svg?branch=master)](https://travis-ci.org/rbnvrw/nd2reader) [![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) [![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) [![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 ### About


+ 0
- 59
badge.svg View File

@ -1,59 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
version="1.1"
width="107"
height="16"
id="svg2"
inkscape:version="0.91 r13725"
sodipodi:docname="badge.svg">
<metadata
id="metadata12">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs10" />
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="1920"
inkscape:window-height="1018"
id="namedview8"
showgrid="false"
inkscape:zoom="1"
inkscape:cx="50"
inkscape:cy="25"
inkscape:window-x="-8"
inkscape:window-y="-8"
inkscape:window-maximized="1"
inkscape:current-layer="svg2" />
<text
font-size="12"
x="0.34722221"
y="11.666667"
id="text4"
style="font-size:12px">
<tspan
id="tspan6"
style="fill:#a9a9a9">(score out of 4.0)</tspan>
</text>
</svg>

+ 7
- 6
nd2reader/artificial.py View File

@ -24,7 +24,7 @@ class ArtificialND2(object):
def __init__(self, file, version=(3, 0)): def __init__(self, file, version=(3, 0)):
self.version = version 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)) check_or_make_dir(path.dirname(file))
self._fh = open(file, 'w+b', 0) self._fh = open(file, 'w+b', 0)
self.write_file() self.write_file()
@ -53,16 +53,17 @@ class ArtificialND2(object):
def write_file(self): def write_file(self):
self.write_version() 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): def write_version(self):
"""Write file header """Write file header
""" """
# write 16 empty bytes # write 16 empty bytes
self._fh.write(bytearray(16))
self.raw_text += bytearray(16)
# write version info # write version info
self._fh.write(self._get_version_string())
self.raw_text += self._get_version_string()
def _get_version_string(self): def _get_version_string(self):
return six.b('ND2 FILE SIGNATURE CHUNK NAME01!Ver%s.%s' % self.version) 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): def write_label_map(self):
raw_text, locations, data = self.create_label_map_bytes() 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): def create_label_map_bytes(self):
"""Construct a binary label map """Construct a binary label map


+ 34
- 1
tests/test_common.py View File

@ -8,7 +8,7 @@ import struct
from nd2reader.artificial import ArtificialND2 from nd2reader.artificial import ArtificialND2
from nd2reader.common import get_version, parse_version, parse_date, _add_to_metadata, _parse_unsigned_char, \ 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, \ _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 from nd2reader.exceptions import InvalidVersionError
@ -120,3 +120,36 @@ class TestCommon(unittest.TestCase):
self.assertIsNone(get_from_dict_if_exists('nowhere', test_dict)) 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('existing', test_dict), 'test')
self.assertEqual(get_from_dict_if_exists('string', test_dict, convert_key_to_binary=False), 'test2') 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.")

+ 5
- 0
tests/test_raw_metadata.py View File

@ -35,6 +35,11 @@ class TestRawMetadata(unittest.TestCase):
for required in required_keys: for required in required_keys:
self.assertTrue(required in metadata) 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): def test_pfs_status(self):
self.assertEqual(self.file_data['pfs_status'], self.metadata.pfs_status[0]) self.assertEqual(self.file_data['pfs_status'], self.metadata.pfs_status[0])


Loading…
Cancel
Save