Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage #589

Merged
merged 26 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8126f23
Improve coverage for nwbRead/nwbExport
ehennestad Aug 26, 2024
59ed0a2
Remove tutorialTest (function-based)
ehennestad Aug 27, 2024
e386d6c
Create TutorialTest (class-based unit test)
ehennestad Aug 27, 2024
135b59e
Add test classes (issue #583 )
ehennestad Aug 29, 2024
7196f50
Update description for PynwbTutorialTest
ehennestad Aug 29, 2024
827cbad
Add specific tests for io.timestamp2datetime
ehennestad Aug 31, 2024
d862b25
Merge branch '583-test-tutorial-files-across-language' into improve-t…
ehennestad Aug 31, 2024
faaef8d
Merge branch 'master' into improve-test-coverage
ehennestad Aug 31, 2024
954d3af
Update unit tests
ehennestad Aug 31, 2024
878d1c7
Create getTutorialNwbFilePath.m
ehennestad Sep 1, 2024
9cc1ec4
Fix: Add starting_time when creating TimeSeries objects (#584)
ehennestad Sep 1, 2024
6ed7104
Update several tutorials: Specify timezone or NWBFile's session_start…
ehennestad Sep 1, 2024
75e9cde
Merge branch '583-test-tutorial-files-across-language' into improve-t…
ehennestad Sep 1, 2024
133c102
Fix bug in io.timestamp2datetime (Issue #585)
ehennestad Sep 3, 2024
10eae1e
Merge branch '585-fix-timestamp2datetime' into improve-test-coverage
ehennestad Sep 3, 2024
1c61536
Update timestamp2datetime.m
ehennestad Sep 3, 2024
c3b8ebe
Merge branch '585-fix-timestamp2datetime' into improve-test-coverage
ehennestad Sep 3, 2024
f86b3f0
Update GenerationTest.m
ehennestad Sep 3, 2024
2d0f0e9
Update getMatnwbDir.m
ehennestad Sep 3, 2024
cd2abec
Merge branch 'master' into improve-test-coverage
ehennestad Sep 10, 2024
3d1368c
Delete file which was introduced with issue #585
ehennestad Sep 10, 2024
fc5ed96
Change nwbtest: Add selector options as input; Add cleanup routine
ehennestad Sep 12, 2024
10398b1
Add test for constructing Anon with name, value input
ehennestad Sep 12, 2024
cfe2746
Add specific testing of types.untyped.Set
ehennestad Sep 12, 2024
6fa74a5
Remove function call used for debugging
ehennestad Sep 12, 2024
2d25e9f
Merge branch 'master' into improve-test-coverage
bendichter Sep 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions +misc/getMatnwbDir.m
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
function [matnwbDir] = getMatnwbDir(varargin)

try
% Get the actual location of the matnwb directory. This assumes "getMatnwbDir" is
% within the +misc matnwb package folder.
fnLoc = dbstack('-completenames');
fnLoc = fnLoc(1).file;
[fnDir,~,~] = fileparts(fnLoc);
[matnwbDir,~,~] = fileparts(fnDir);

if 7 ~= exist(matnwbDir, 'dir')
warning('NWB:GetMatnwbDir:NotFound',...
'Did not find "matnwb" root directory at %s. Using defaults.',...
matnwbDir);
end
catch err
atLine = repmat('@', 1, 7);
fprintf('%s\n%s\n%s\n',...
atLine,...
getReport(err, 'extended', 'hyperlinks', 'on'),...
atLine);
end
function matnwbDir = getMatnwbDir(varargin)
% Get the absolute path for the matnwb directory.

% This assumes "getMatnwbDir" is within the +misc matnwb package folder.
miscFolder = fileparts(mfilename('fullpath'));
matnwbDir = fileparts(miscFolder);
end
6 changes: 5 additions & 1 deletion +tests/+sanity/GenerationTest.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
classdef GenerationTest < matlab.unittest.TestCase
properties (MethodSetupParameter)
schemaVersion = setdiff({dir('nwb-schema').name}, {'.', '..'});
schemaVersion = listSchemaVersions()
end

methods (TestClassSetup)
Expand Down Expand Up @@ -62,3 +62,7 @@ function dynamicTableMethodsTest(testCase)
end
end

function schemaVersions = listSchemaVersions()
nwbSchemaDir = fullfile(misc.getMatnwbDir, 'nwb-schema');
schemaVersions = setdiff({dir(nwbSchemaDir).name}, {'.', '..'});
end
89 changes: 89 additions & 0 deletions +tests/+system/NWBFileIOTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,94 @@ function addContainer(testCase, file) %#ok<INUSL>
c = file;
end
end

methods (Test)
function writeMultipleFiles(testCase)

fileA = testCase.file;
fileB = NwbFile( ...
'session_description', 'a second test NWB File', ...
'identifier', 'TEST456', ...
'session_start_time', '2018-12-02T12:57:27.371444-08:00', ...
'file_create_date', '2017-04-15T12:00:00.000000-08:00',...
'timestamps_reference_time', '2018-12-02T12:57:27.371444-08:00');

fileNameA = ['MatNWB.' testCase.className() '.testWriteMultiA.nwb'];
fileNameB = ['MatNWB.' testCase.className() '.testWriteMultiB.nwb'];

nwbExport([fileA, fileB], {fileNameA, fileNameB});
end

function readWithStringArg(testCase)
fileName = ['MatNWB.' testCase.className() '.testReadWithStringArg.nwb'];
fileName = string(fileName);
nwbExport(testCase.file, fileName)
nwbRead(fileName, "ignorecache");
end

function readFileWithoutSpec(testCase)
fileName = ['MatNWB.' testCase.className() '.testReadFileWithoutSpec.nwb'];
nwbExport(testCase.file, fileName)

testCase.deleteGroupFromFile(fileName, 'specifications')
nwbRead(fileName);
end

function readFileWithoutSpecLoc(testCase)
fileName = ['MatNWB.' testCase.className() '.testReadFileWithoutSpecLoc.nwb'];
nwbExport(testCase.file, fileName)

testCase.deleteAttributeFromFile(fileName, '/', '.specloc')

nwbRead(fileName);
end

function readFileWithUnsupportedVersion(testCase)
fileName = ['MatNWB.' testCase.className() '.testReadFileWithUnsupportedVersion.nwb'];
nwbExport(testCase.file, fileName)

testCase.deleteAttributeFromFile(fileName, '/', 'nwb_version')

file_id = H5F.open(fileName, 'H5F_ACC_RDWR', 'H5P_DEFAULT');
io.writeAttribute(file_id, '/nwb_version', '1.0.0')
H5F.close(file_id);

nwbRead(fileName);
end
end

methods (Static, Access = private)
function deleteGroupFromFile(fileName, groupName)
if ~startsWith(groupName, '/')
groupName = ['/', groupName];
end

% Open the HDF5 file in read-write mode
file_id = H5F.open(fileName, 'H5F_ACC_RDWR', 'H5P_DEFAULT');

% Delete the group
H5L.delete(file_id, groupName, 'H5P_DEFAULT');

% Close the HDF5 file
H5F.close(file_id);
end

function deleteAttributeFromFile(fileName, objectName, attributeName)
% Open the HDF5 file in read-write mode
file_id = H5F.open(fileName, 'H5F_ACC_RDWR', 'H5P_DEFAULT');

% Open the object (dataset or group)
object_id = H5O.open(file_id, objectName, 'H5P_DEFAULT');

% Delete the attribute
H5A.delete(object_id, attributeName);

% Close the object
H5O.close(object_id);

% Close the HDF5 file
H5F.close(file_id);
end
end
end

6 changes: 6 additions & 0 deletions +tests/+unit/anonTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ function testAnonDataset(testCase)

tests.util.verifyContainerEqual(testCase, nwbRead('testanon.nwb', 'ignorecache'), nwbExpected);
end

function testAnonTypeWithNameValueInput(testCase)
anon = types.untyped.Anon('a', 1);
testCase.verifyEqual(anon.name, 'a')
testCase.verifyEqual(anon.value, 1)
end
5 changes: 5 additions & 0 deletions +tests/+unit/dataPipeTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ function testBoundPipe(testCase)
warning('on', debugId);
end

function testConfigurationFromData(testCase)
conf = types.untyped.datapipe.Configuration.fromData(zeros(10,10), 1);
testCase.verifyClass(conf, 'types.untyped.datapipe.Configuration')
end

function data = createData(dataType, size)
data = randi(intmax(dataType), size, dataType);
end
68 changes: 68 additions & 0 deletions +tests/+unit/untypedSetTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
function tests = untypedSetTest()
tests = functiontests(localfunctions);
end

function setupOnce(testCase)
rootPath = fullfile(fileparts(mfilename('fullpath')), '..', '..');
testCase.applyFixture(matlab.unittest.fixtures.PathFixture(rootPath));
end

function setup(testCase)
testCase.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture);
end

function testCreateSetWithFunctionInput(testCase)
set = types.untyped.Set(@(key, value) true);
testCase.verifyNotEmpty(set.ValidationFcn)
end

function testCreateSetFromStruct(testCase)
untypedSet = types.untyped.Set( struct('a',1, 'b', 2) );
testCase.verifyEqual(untypedSet.get('a'), 1)
end

function testCreateSetFromNvPairs(testCase)
untypedSet = types.untyped.Set( 'a',1, 'b', 2 );
testCase.verifyEqual(untypedSet.get('a'), 1)
end

function testCreateSetFromNvPairsPlusFunctionHandle(testCase)
untypedSet = types.untyped.Set( 'a',1, 'b', 2, @(key, value) disp('Hello World'));
testCase.verifyEqual(untypedSet.get('a'), 1)
end

function testDisplayEmptyObject(testCase)
emptyUntypedSet = types.untyped.Set();
disp(emptyUntypedSet)
end

function testDisplayScalarObject(testCase)
scalarSet = types.untyped.Set('a',1)
disp(scalarSet)
end

function testGetSetSize(testCase)
untypedSet = types.untyped.Set( 'a',1, 'b', 2 );

[nRowsA, nColsA] = size(untypedSet);

nRowsB = size(untypedSet, 1);
nColsB = size(untypedSet, 2);

testCase.verifyEqual(nRowsA, nRowsB);
testCase.verifyEqual(nColsA, nColsB);
end

function testHorizontalConcatenation(testCase)
untypedSetA = types.untyped.Set( struct('a',1, 'b', 2) );
untypedSetB = types.untyped.Set( struct('c',3, 'd', 3) );

testCase.verifyError(@() [untypedSetA, untypedSetB], 'NWB:Set:Unsupported')
end

function testVerticalConcatenation(testCase)
untypedSetA = types.untyped.Set( struct('a',1, 'b', 2) );
untypedSetB = types.untyped.Set( struct('c',3, 'd', 3) );

testCase.verifyError(@() [untypedSetA; untypedSetB], 'NWB:Set:Unsupported')
end
2 changes: 1 addition & 1 deletion +types/+untyped/Set.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
if nargout == 1
varargout{1} = [obj.Count, 1];
else
[varargout{:}] = ones(nargout,1);
varargout = num2cell( ones(1, nargout) );
varargout{1} = obj.Count;
end
end
Expand Down
8 changes: 7 additions & 1 deletion nwbtest.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@
parser = inputParser;
parser.KeepUnmatched = true;
parser.addParameter('Verbosity', 1);
parser.addParameter('Selector', [])
parser.parse(varargin{:});

ws = pwd;

nwbClearGenerated(); % clear default files if any.
nwbClearGenerated(); % Clear default files if any.
cleaner = onCleanup(@generateCore); % Regenerate core when finished

pvcell = struct2pvcell(parser.Unmatched);
suite = TestSuite.fromPackage('tests', 'IncludingSubpackages', true, pvcell{:});
if ~isempty(parser.Results.Selector)
suite = suite.selectIf(parser.Results.Selector);
end

runner = TestRunner.withTextOutput('Verbosity', parser.Results.Verbosity);

Expand Down
Loading