Skip to content

Commit

Permalink
Exclude coverage testing of abstract properties, methods (#128)
Browse files Browse the repository at this point in the history
* Exclude coverage testing of abstract properties, methods

* Replace calls to deprecated abc.abstractmethod

* Improve tests
  • Loading branch information
rly committed Aug 7, 2019
1 parent b14e064 commit e13fc0b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .coveragerc
Expand Up @@ -2,3 +2,8 @@
branch = True
source = src/
omit = src/hdmf/_version.py

[report]
exclude_lines =
pragma: no cover
@abstract
15 changes: 10 additions & 5 deletions src/hdmf/container.py
@@ -1,4 +1,4 @@
import abc
from abc import abstractmethod
from uuid import uuid4
from six import with_metaclass
from .utils import docval, getargs, ExtenderMeta
Expand Down Expand Up @@ -131,27 +131,32 @@ def parent(self, parent_container):

class Data(Container):

@abc.abstractproperty
@property
@abstractmethod
def data(self):
'''
The data that is held by this Container
'''
pass

def __nonzero__(self):
def __bool__(self):
if not hasattr(self.data, '__len__'):
raise NotImplementedError('__bool__ must be implemented when data has no __len__')
return len(self.data) != 0


class DataRegion(Data):

@abc.abstractproperty
@property
@abstractmethod
def data(self):
'''
The target data that this region applies to
'''
pass

@abc.abstractproperty
@property
@abstractmethod
def region(self):
'''
The region that indexes into data e.g. slice or list of indices
Expand Down
14 changes: 9 additions & 5 deletions src/hdmf/data_utils.py
@@ -1,4 +1,4 @@
from abc import ABCMeta, abstractmethod, abstractproperty
from abc import ABCMeta, abstractmethod
try:
from collections.abc import Iterable # Python 3
except ImportError:
Expand Down Expand Up @@ -83,7 +83,8 @@ def recommended_data_shape(self):
"""
raise NotImplementedError("recommended_data_shape not implemented for derived class")

@abstractproperty
@property
@abstractmethod
def dtype(self):
"""
Define the data type of the array
Expand All @@ -92,7 +93,8 @@ def dtype(self):
"""
raise NotImplementedError("dtype not implemented for derived class")

@abstractproperty
@property
@abstractmethod
def maxshape(self):
"""
Property describing the maximum shape of the data array that is being iterated over
Expand Down Expand Up @@ -556,11 +558,13 @@ def target(self):
def slice(self):
return self.__slice

@abstractproperty
@property
@abstractmethod
def __getitem__(self, idx):
pass

@abstractproperty
@property
@abstractmethod
def __len__(self):
pass

Expand Down
39 changes: 38 additions & 1 deletion tests/unit/test_container.py
@@ -1,6 +1,6 @@
import unittest2 as unittest

from hdmf.container import Container
from hdmf.container import Container, Data


class Subcontainer(Container):
Expand Down Expand Up @@ -119,5 +119,42 @@ def test_type_hierarchy(self):
self.assertEqual(Subcontainer.type_hierarchy(), (Subcontainer, Container, object))


class SubData(Data):

def __init__(self, name, data):
super(SubData, self).__init__(name=name)
self.__data = data

@property
def data(self):
return self.__data


class TestData(unittest.TestCase):

def test_bool_true(self):
"""Test that __bool__ method works correctly on data with len
"""
data_obj = SubData('my_data', [1, 2, 3, 4, 5])
self.assertTrue(data_obj)

def test_bool_false(self):
"""Test that __bool__ method works correctly on empty data
"""
data_obj = SubData('my_data', '')
self.assertFalse(data_obj)

data_obj = SubData('my_data', [])
self.assertFalse(data_obj)

def test_bool_no_len(self):
"""Test that__bool__ method works correctly on data with no len
"""
data_obj = SubData('my_data', Container(''))
err_msg = '__bool__ must be implemented when data has no __len__'
with self.assertRaisesRegex(NotImplementedError, err_msg):
bool(data_obj)


if __name__ == '__main__':
unittest.main()
35 changes: 35 additions & 0 deletions tests/unit/test_io_hdf5_h5tools.py
Expand Up @@ -523,6 +523,41 @@ def test_roundtrip_empty_group(self):
self.assertListEqual([], read_foofile.buckets[0].foos)


class TestHDF5IO(unittest.TestCase):

def setUp(self):
self.manager = _get_manager()
self.path = get_temp_filepath()

foo1 = Foo('foo1', [1, 2, 3, 4, 5], "I am foo1", 17, 3.14)
foobucket = FooBucket('test_bucket', [foo1])
self.foofile = FooFile([foobucket])

self.file_obj = None

def tearDown(self):
if os.path.exists(self.path):
os.remove(self.path)

if self.file_obj is not None:
fn = self.file_obj.filename
self.file_obj.close()
if os.path.exists(fn):
os.remove(fn)

def test_constructor(self):
with HDF5IO(self.path, manager=self.manager, mode='w') as io:
self.assertEquals(io.manager, self.manager)
self.assertEquals(io.source, self.path)

def test_set_file_mismatch(self):
self.file_obj = File(get_temp_filepath())
err_msg = re.escape("You argued %s as this object's path, but supplied a file with filename: %s"
% (self.path, self.file_obj.filename))
with self.assertRaisesRegex(ValueError, err_msg):
HDF5IO(self.path, manager=self.manager, mode='w', file=self.file_obj)


class TestCacheSpec(unittest.TestCase):

def setUp(self):
Expand Down

0 comments on commit e13fc0b

Please sign in to comment.