| 1 |
============================ |
| 2 |
How to Make a Roundtrip Test |
| 3 |
============================ |
| 4 |
|
| 5 |
The HDMF test suite has tools for easily doing round-trip tests of container classes. These |
| 6 |
tools exist in the :py:mod:`hdmf.testing` module. Round-trip tests exist for the container classes in the |
| 7 |
:py:mod:`hdmf.common` module. We recommend you write any additional round-trip tests in |
| 8 |
the ``tests/unit/common`` subdirectory of the Git repository. |
| 9 |
|
| 10 |
For executing your new tests, we recommend using the `test.py` script in the top of the Git |
| 11 |
repository. Roundtrip tests will get executed as part of the full test suite, which can be executed |
| 12 |
with the following command:: |
| 13 |
|
| 14 |
$ python test.py |
| 15 |
|
| 16 |
The roundtrip test will generate a new HDMF file with the name ``test_<CLASS_NAME>.h5`` where ``CLASS_NAME`` is |
| 17 |
the class name of the container class you are roundtripping. The test |
| 18 |
will write an HDMF file with an instance of the container to disk, read this instance back in, and compare it |
| 19 |
to the instance that was used for writing to disk. Once the test is complete, the HDMF file will be deleted. |
| 20 |
You can keep the HDMF file around after the test completes by setting the environment variable ``CLEAN_HDMF`` |
| 21 |
to ``0``, ``false``, ``False``, or ``FALSE``. Setting ``CLEAN_HDMF`` to any value not listed here will |
| 22 |
cause the roundtrip HDMF file to be deleted once the test has completed |
| 23 |
|
| 24 |
Before writing tests, we also suggest you familiarize yourself with the |
| 25 |
:ref:`software architecture <software-architecture>` of HDMF. |
| 26 |
|
| 27 |
------------------------ |
| 28 |
``H5RoundTripMixin`` |
| 29 |
------------------------ |
| 30 |
|
| 31 |
To write a roundtrip test, you will need to subclass the |
| 32 |
:py:class:`~hdmf.testing.testcase.H5RoundTripMixin` class and the |
| 33 |
:py:class:`~hdmf.testing.testcase.TestCase` class, in that order, and override some of the instance methods of the |
| 34 |
:py:class:`~hdmf.testing.testcase.H5RoundTripMixin` class to test the process of going from in-memory Python object |
| 35 |
to data stored on disk and back. |
| 36 |
|
| 37 |
################## |
| 38 |
``setUpContainer`` |
| 39 |
################## |
| 40 |
|
| 41 |
To configure the test for a particular container class, you need to override the |
| 42 |
:py:meth:`~hdmf.testing.testcase.H5RoundTripMixin.setUpContainer` method. This method should take no arguments, and |
| 43 |
return an instance of the container class you are testing. |
| 44 |
|
| 45 |
Here is an example using a :py:class:`~hdmf.common.sparse.CSRMatrix`: |
| 46 |
|
| 47 |
.. code-block:: python |
| 48 |
|
| 49 |
from hdmf.common import CSRMatrix |
| 50 |
from hdmf.testing import TestCase, H5RoundTripMixin |
| 51 |
import numpy as np |
| 52 |
|
| 53 |
class TestCSRMatrixRoundTrip(H5RoundTripMixin, TestCase): |
| 54 |
|
| 55 |
def setUpContainer(self): |
| 56 |
data = np.array([1, 2, 3, 4, 5, 6]) |
| 57 |
indices = np.array([0, 2, 2, 0, 1, 2]) |
| 58 |
indptr = np.array([0, 2, 3, 6]) |
| 59 |
return CSRMatrix(data, indices, indptr, (3, 3)) |