Skip to content

Commit c746134

Browse files
authored
Refactor NWB IO tests (#706)
* Remove "nwbRead" and "nwbExport" tests from NWBFileIOTest Moving tests for nwbRead and nwbExport to other test classes as the in/out testing with pynwb is not needed for those tests * Update nwbExportTest.m Moved test for testing export of multiple files from tests.system.NWBFileIOTest * Refactor nwb i/o tests Moved tests for nwbRead from tests.system.NWBFileIOTest to nwbReadTest
1 parent 06fe7f9 commit c746134

File tree

3 files changed

+173
-85
lines changed

3 files changed

+173
-85
lines changed

+tests/+system/NWBFileIOTest.m

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -24,89 +24,4 @@ function addContainer(testCase, file) %#ok<INUSL>
2424
c = file;
2525
end
2626
end
27-
28-
methods (Test)
29-
function writeMultipleFiles(testCase)
30-
31-
fileA = testCase.file;
32-
fileB = NwbFile( ...
33-
'session_description', 'a second test NWB File', ...
34-
'identifier', 'TEST456', ...
35-
'session_start_time', '2018-12-02T12:57:27.371444-08:00', ...
36-
'file_create_date', '2017-04-15T12:00:00.000000-08:00',...
37-
'timestamps_reference_time', '2018-12-02T12:57:27.371444-08:00');
38-
39-
fileNameA = ['MatNWB.' testCase.className() '.testWriteMultiA.nwb'];
40-
fileNameB = ['MatNWB.' testCase.className() '.testWriteMultiB.nwb'];
41-
42-
nwbExport([fileA, fileB], {fileNameA, fileNameB});
43-
end
44-
45-
function testLoadAll(testCase)
46-
fileName = ['MatNWB.' testCase.className() '.testLoadAll.nwb'];
47-
nwbExport(testCase.file, fileName)
48-
nwb = nwbRead(fileName, "ignorecache");
49-
nwb.loadAll();
50-
end
51-
52-
function readWithStringArg(testCase)
53-
fileName = ['MatNWB.' testCase.className() '.testReadWithStringArg.nwb'];
54-
fileName = string(fileName);
55-
nwbExport(testCase.file, fileName)
56-
nwbRead(fileName, "ignorecache");
57-
end
58-
59-
function readFileWithoutSpec(testCase)
60-
fileName = ['MatNWB.' testCase.className() '.testReadFileWithoutSpec.nwb'];
61-
nwbExport(testCase.file, fileName)
62-
63-
io.internal.h5.deleteGroup(fileName, 'specifications')
64-
65-
nwbRead(fileName, "ignorecache");
66-
end
67-
68-
function readFileWithoutSpecLoc(testCase)
69-
70-
fileName = ['MatNWB.' testCase.className() '.testReadFileWithoutSpecLoc.nwb'];
71-
nwbExport(testCase.file, fileName)
72-
73-
io.internal.h5.deleteAttribute(fileName, '/', '.specloc')
74-
75-
% When specloc is missing, the specifications are not added to
76-
% the blacklist, so it will get passed as an input to NwbFile.
77-
testCase.verifyError(@(fn) nwbRead(fileName, "ignorecache"), 'MATLAB:TooManyInputs');
78-
end
79-
80-
function readFileWithUnsupportedVersion(testCase)
81-
fileName = ['MatNWB.' testCase.className() '.testReadFileWithUnsupportedVersion.nwb'];
82-
nwbExport(testCase.file, fileName)
83-
84-
io.internal.h5.deleteAttribute(fileName, '/', 'nwb_version')
85-
86-
file_id = H5F.open(fileName, 'H5F_ACC_RDWR', 'H5P_DEFAULT');
87-
io.writeAttribute(file_id, '/nwb_version', '1.0.0')
88-
H5F.close(file_id);
89-
90-
testCase.verifyWarning(@(fn) nwbRead(fileName, "ignorecache"), 'NWB:Read:UnsupportedSchema')
91-
end
92-
93-
function readFileWithUnsupportedVersionAndNoSpecloc(testCase)
94-
import matlab.unittest.fixtures.SuppressedWarningsFixture
95-
testCase.applyFixture(SuppressedWarningsFixture('NWB:Read:UnsupportedSchema'))
96-
97-
fileName = ['MatNWB.' testCase.className() '.testReadFileWithUnsupportedVersionAndNoSpecloc.nwb'];
98-
nwbExport(testCase.file, fileName)
99-
100-
io.internal.h5.deleteAttribute(fileName, '/', '.specloc')
101-
io.internal.h5.deleteAttribute(fileName, '/', 'nwb_version')
102-
103-
file_id = H5F.open(fileName, 'H5F_ACC_RDWR', 'H5P_DEFAULT');
104-
io.writeAttribute(file_id, '/nwb_version', '1.0.0')
105-
H5F.close(file_id);
106-
107-
% When specloc is missing, the specifications are not added to
108-
% the blacklist, so it will get passed as an input to NwbFile.
109-
testCase.verifyError(@(fn) nwbRead(fileName, "ignorecache"), 'MATLAB:TooManyInputs');
110-
end
111-
end
11227
end

+tests/+unit/nwbExportTest.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,5 +304,24 @@ function testWasGeneratedByProperty(testCase)
304304
% Verify that was_generated_by still has one entry (i.e not getting duplicate entries)
305305
testCase.verifyEqual(size(nwbIn2.general_was_generated_by.load()), [2,1])
306306
end
307+
308+
function testExportMultipleFiles(testCase)
309+
fileA = tests.factory.NWBFile();
310+
fileA.identifier = 'File A';
311+
312+
fileB = tests.factory.NWBFile();
313+
fileB.identifier = 'File B';
314+
315+
fileNameA = 'testWriteMultipleA.nwb';
316+
fileNameB = 'testWriteMultipleB.nwb';
317+
318+
nwbExport([fileA, fileB], {fileNameA, fileNameB});
319+
320+
fileAIn = nwbRead(fileNameA, 'ignorecache');
321+
fileBIn = nwbRead(fileNameB, 'ignorecache');
322+
323+
testCase.verifyEqual(fileAIn.identifier, fileA.identifier);
324+
testCase.verifyEqual(fileBIn.identifier, fileB.identifier);
325+
end
307326
end
308327
end

+tests/+unit/nwbReadTest.m

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
classdef nwbReadTest < tests.abstract.NwbTestCase
2+
% nwbReadTest - Unit tests for testing the nwbRead function.
3+
4+
methods (TestClassSetup)
5+
function setupTemporaryWorkingFolder(testCase)
6+
% Use a fixture to create a temporary working directory
7+
testCase.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture);
8+
end
9+
end
10+
11+
methods (TestMethodSetup)
12+
% No method setup
13+
end
14+
15+
methods (Test)
16+
17+
function readFileWithUnsupportedVersion(testCase)
18+
nwbFile = tests.factory.NWBFile();
19+
fileName = 'testReadFileWithUnsupportedVersion.nwb';
20+
nwbExport(nwbFile, fileName)
21+
22+
% Override the version attribute with an unsupported version number
23+
testCase.changeVersionNumberInFile(fileName, '1.0.0')
24+
25+
testCase.verifyWarning(@(fn) nwbRead(fileName, "ignorecache"), 'NWB:Read:UnsupportedSchema')
26+
end
27+
28+
function readFileWithoutEmbeddedSpecs(testCase)
29+
% Use a fixture to create a temporary working directory
30+
testCase.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture);
31+
32+
nwbFile = tests.factory.NWBFile();
33+
34+
fileName = 'testReadFileWithoutSpec.nwb';
35+
nwbExport(nwbFile, fileName)
36+
37+
io.internal.h5.deleteGroup(fileName, 'specifications')
38+
39+
nwbRead(fileName, "savedir", pwd);
40+
41+
% Should generate types even if specifications are not embedded
42+
testCase.verifyTrue( isfolder(fullfile(pwd, "+types")) )
43+
end
44+
45+
function readFileWithUnsupportedVersionAndWithoutEmbeddedSpecs(testCase)
46+
% Use a fixture to create a temporary working directory
47+
testCase.applyFixture(...
48+
matlab.unittest.fixtures.WorkingFolderFixture)
49+
50+
% Use a fixture to ignore warning
51+
testCase.applyFixture(...
52+
matlab.unittest.fixtures.SuppressedWarningsFixture(...
53+
'NWB:Read:UnsupportedSchema'))
54+
55+
nwbFile = tests.factory.NWBFile();
56+
57+
fileName = 'testReadFileWithoutSpecAndWithInvalidVersion.nwb';
58+
nwbExport(nwbFile, fileName)
59+
60+
io.internal.h5.deleteGroup(fileName, 'specifications')
61+
62+
% Override the version attribute with an unsupported version number
63+
testCase.changeVersionNumberInFile(fileName, '1.0.0')
64+
65+
nwbRead(fileName, "savedir", pwd);
66+
67+
% Should not generate types for file without embedded
68+
% specifications and with unsupported version
69+
testCase.verifyFalse( isfolder(fullfile(pwd, "+types")) )
70+
end
71+
72+
function readFileWithoutSpecLoc(testCase)
73+
nwbFile = tests.factory.NWBFile();
74+
fileName = 'testReadFileWithoutSpecLoc.nwb';
75+
nwbExport(nwbFile, fileName)
76+
77+
io.internal.h5.deleteAttribute(fileName, '/', '.specloc')
78+
79+
% When specloc is missing, the specifications are not added to
80+
% the blacklist, so it will get passed as an input to NwbFile.
81+
testCase.verifyError(@(fn) nwbRead(fileName, "ignorecache"), 'MATLAB:TooManyInputs');
82+
end
83+
84+
function readFileWithUnsupportedVersionAndNoSpecloc(testCase)
85+
% Todo: Is this different from readFileWithoutEmbeddedSpecsAndWithUnsupportedVersion
86+
import matlab.unittest.fixtures.SuppressedWarningsFixture
87+
testCase.applyFixture(SuppressedWarningsFixture('NWB:Read:UnsupportedSchema'))
88+
89+
nwbFile = tests.factory.NWBFile();
90+
fileName = 'testReadFileWithUnsupportedVersionAndNoSpecloc.nwb';
91+
nwbExport(nwbFile, fileName)
92+
93+
io.internal.h5.deleteAttribute(fileName, '/', '.specloc')
94+
95+
% Override the version attribute with an unsupported version number
96+
testCase.changeVersionNumberInFile(fileName, '1.0.0')
97+
98+
% When specloc is missing, the specifications are not added to
99+
% the blacklist, so it will get passed as an input to NwbFile.
100+
testCase.verifyError(@(fn) nwbRead(fileName, "ignorecache"), 'MATLAB:TooManyInputs');
101+
end
102+
103+
function testWasGeneratedByProperty(testCase)
104+
nwb = tests.factory.NWBFile();
105+
nwbFilename = testCase.getRandomFilename();
106+
nwbExport(nwb, nwbFilename);
107+
108+
nwbIn = nwbRead(nwbFilename, 'ignorecache');
109+
testCase.verifyTrue(any(contains(nwbIn.general_was_generated_by.load(), 'matnwb')))
110+
111+
% Export again
112+
nwbFilename2 = testCase.getRandomFilename();
113+
nwbExport(nwbIn, nwbFilename2);
114+
115+
nwbIn2 = nwbRead(nwbFilename2, 'ignorecache');
116+
117+
% Verify that was_generated_by still has one entry (i.e not getting duplicate entries)
118+
testCase.verifyEqual(size(nwbIn2.general_was_generated_by.load()), [2,1])
119+
end
120+
121+
function testLoadAll(testCase)
122+
nwbFile = tests.factory.NWBFile();
123+
nwbFile.acquisition.set('ts', tests.factory.TimeSeriesWithTimestamps);
124+
fileName = 'testLoadAll.nwb';
125+
nwbExport(nwbFile, fileName)
126+
127+
nwbIn = nwbRead(fileName, "ignorecache");
128+
testCase.verifyClass(nwbIn.session_start_time, 'types.untyped.DataStub')
129+
130+
nwbIn.loadAll();
131+
testCase.verifyTrue(isa(nwbIn.session_start_time, 'datetime'))
132+
133+
% Todo: Acquisition
134+
end
135+
136+
function readWithStringFilenameArg(testCase)
137+
fileName = "testReadWithStringArg.nwb";
138+
nwbExport(tests.factory.NWBFile(), fileName)
139+
nwb = nwbRead(fileName, "ignorecache");
140+
141+
testCase.verifyTrue(~isempty(nwb));
142+
testCase.verifyClass(nwb, 'NwbFile');
143+
end
144+
end
145+
146+
methods (Access = private, Static)
147+
function changeVersionNumberInFile(fileName, newVersionNumber)
148+
% Override the version attribute with an unsupported version number
149+
[fileId, fileCleanupObj] = io.internal.h5.openFile(fileName, 'w'); %#ok<ASGLU>
150+
io.internal.h5.deleteAttribute(fileId, '/', 'nwb_version')
151+
io.writeAttribute(fileId, '/nwb_version', newVersionNumber)
152+
end
153+
end
154+
end

0 commit comments

Comments
 (0)