|
| 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