From 4d6f6bc9fc928e2d3d7b0cb6403ab99a16aed63d Mon Sep 17 00:00:00 2001 From: k1o0 Date: Tue, 28 Jul 2020 22:04:08 +0300 Subject: [PATCH] Fix for multiple base sessions --- @Alyx/newExp.m | 8 ++++---- tests/Alyx_test.m | 51 +++++++++++++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/@Alyx/newExp.m b/@Alyx/newExp.m index 417c814..0ec3edf 100644 --- a/@Alyx/newExp.m +++ b/@Alyx/newExp.m @@ -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'}; diff --git a/tests/Alyx_test.m b/tests/Alyx_test.m index 3c5c234..133f650 100644 --- a/tests/Alyx_test.m +++ b/tests/Alyx_test.m @@ -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 @@ -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 @@ -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'); @@ -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))) @@ -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)