Skip to content

Commit

Permalink
Fix for multiple base sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
k1o0 committed Jul 28, 2020
1 parent 7214bfa commit 4d6f6bc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
8 changes: 4 additions & 4 deletions @Alyx/newExp.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@
expDate = obj.datestr(expDate); % date in Alyx format
% Ensure user is logged in
if ~obj.IsLoggedIn; obj = obj.login; end
% Get list of base sessions
% Get list of base sessions. Sessions returned in descending date order
[sessions, statusCode] = obj.getData(['sessions?type=Base&subject=' subject]);

%If the date of this latest base session is not the same date as
%today, then create a new base session for today
if statusCode ~= 000 && (isempty(sessions) || ~strcmp(sessions(end).start_time(1:10), expDate(1:10)))
% If the date of this latest base session is not the same date as
% today, then create a new base session for today.
if statusCode ~= 000 && (isempty(sessions) || ~strcmp(sessions(1).start_time(1:10), expDate(1:10)))
d = struct;
d.subject = subject;
d.procedures = {'Behavior training/tasks'};
Expand Down
51 changes: 38 additions & 13 deletions tests/Alyx_test.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
% Test adapted from Oliver Winter's AlyxClient test

properties (ClassSetupParameter)
% Alyx base URL. test is for the main branch, testDev is for the dev
% code
base_url = cellsprintf('https://%s.alyx.internationalbrainlab.org', {'test', 'testDev'});
% Alyx base URL. 'test' is for the main branch, 'testDev' is for the
% dev code
base_url = {'https://test.alyx.internationalbrainlab.org'}
end

properties % Test objects
Expand Down Expand Up @@ -199,8 +199,9 @@ function test_getSessions(testCase)
testCase.verifyEqual(actual(sess), expected, 'Failed to filter by lab')

% Test user search
sess = ai.getSessions('user', 'olivier');
expected = {'89b82507028a', 'c34dc9004cf8'};
sess = ai.getSessions('user', 'ines');
expected = {'2ea989cd5143', '8aec34753ad0', ...
'4d2628b52ec0', 'ab26823ec5a4', '910203f65589'};
testCase.verifyEqual(actual(sess), expected, 'Failed to filter by users')

% Test dataset search
Expand Down Expand Up @@ -419,23 +420,39 @@ function test_postWeight(testCase)
end

function test_newExp(testCase)
% Tests creating new experiment sessions on Alyx, registering and
% saving parameters.
ai = testCase.alyx;
subject = testCase.subjects{end};
newExp_fn = @()newExp(ai, subject);

% A 'Base' session type is first created, if it doesn't already
% exist, then an 'Experiment' sub-session.
% First query the current number of base sessions
nowstr = datestr(now, 'yyyy-mm-dd');
nToday = nBaseSessions(nowstr);
expected = iff(nToday > 0, nToday, 1);

wrnID = 'Alyx:registerFile:InvalidRepoPath';
[ref1, seq, url] = testCase.verifyWarning(newExp_fn, wrnID);
ref2 = strjoin({datestr(now, 'yyyy-mm-dd'),'1',subject},'_');
testCase.verifyEqual(ref1, ref2, 'Experiment reference mismatch');
testCase.verifyEqual(seq, 1, 'Experiment sequence mismatch');
testCase.verifyMatches(url, [ai.BaseURL '/sessions'], 'Incorrect URL');
testCase.verifyEqual(ref1, ref2, 'Experiment reference mismatch')
testCase.verifyEqual(seq, 1, 'Experiment sequence mismatch')
testCase.verifyMatches(url, [ai.BaseURL '/sessions'], 'Incorrect URL')
paramsSaved = exist(dat.expFilePath(ref1, 'parameters', 'master'), 'file');
testCase.verifyTrue(paramsSaved == 2)
% Check that a base session was created, if one didn't exist already
actual = nBaseSessions(nowstr);
testCase.verifyEqual(actual, expected, 'Failed to create base session')

[ref1, seq, url] = testCase.verifyWarning(newExp_fn, wrnID);
ref2 = strjoin({datestr(now, 'yyyy-mm-dd'),'2',subject},'_');
testCase.verifyEqual(ref1, ref2, 'Experiment reference mismatch');
testCase.verifyEqual(seq, 2, 'Experiment sequence mismatch');
testCase.verifyMatches(url, [ai.BaseURL '/sessions'], 'Incorrect URL');
testCase.verifyEqual(ref1, ref2, 'Experiment reference mismatch')
testCase.verifyEqual(seq, 2, 'Experiment sequence mismatch')
testCase.verifyMatches(url, [ai.BaseURL '/sessions'], 'Incorrect URL')
% Check that the number of base sessions has not changed
actual = nBaseSessions(nowstr);
testCase.verifyEqual(actual, expected, 'Unexpected base session')

% Test creating a new experiment with multiple repos
p = dat.expPath(testCase.subjects{1}, now, 1, 'main2', 'master');
Expand All @@ -445,8 +462,8 @@ function test_newExp(testCase)
[ref1, seq, url] = testCase.verifyWarning(newExp_fn, wrnID);
ref2 = strjoin({datestr(now, 'yyyy-mm-dd'),'2',testCase.subjects{1}},'_');
testCase.verifyEqual(seq, 2, 'Failed to iterate sequence')
testCase.verifyEqual(ref1, ref2, 'Experiment reference mismatch');
testCase.verifyMatches(url, [ai.BaseURL '/sessions'], 'Incorrect URL');
testCase.verifyEqual(ref1, ref2, 'Experiment reference mismatch')
testCase.verifyMatches(url, [ai.BaseURL '/sessions'], 'Incorrect URL')
% Check parameters were saved
expected = dat.expParams(ref1);
testCase.verifyTrue(isstruct(expected) && isequal(fieldnames(expected), fieldnames(params)))
Expand All @@ -471,6 +488,14 @@ function test_newExp(testCase)
testCase.verifyFalse(any(expFolderCreated))

% TODO test newExp when headless

function n = nBaseSessions(dateStr)
% Queries the number of base sessions for the test subject. We
% expect either 0 or 1 base sessions for a given subject per day.
[base, status] = ai.getData(['sessions?type=Base&subject=' subject]);
testCase.assertEqual(status, 200, 'Failed to query Base sessions')
n = sum(arrayfun(@(s)strcmp(s.start_time(1:10), dateStr), base));
end
end

function test_postData(testCase)
Expand Down

0 comments on commit 4d6f6bc

Please sign in to comment.