Skip to content

Commit

Permalink
Fixes for test conflicts (#178)
Browse files Browse the repository at this point in the history
* Fixes for test conflicts

* Ensure path is reset

* Issue #180

* a fix to AlyxPanel_test/setupPanel, but still failing

* Reverted some changes and added fix for activeFlag test

* Bug fixes and updates for update to test db

* Changes to alyx-matlab

* Fix to recordWeight test

* Fix for db bug cause submodule test fail
  • Loading branch information
k1o0 authored and Jai Bhagat committed Jul 19, 2019
1 parent eba128c commit 2b6b45c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 102 deletions.
6 changes: 3 additions & 3 deletions +eui/AlyxPanel.m
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,12 @@ function recordWeight(obj, weight, subject)
ai = obj.AlyxInstance;
% determine whether there is a session for this subj and date
thisDate = ai.datestr(now);
sessions = ai.getData(['sessions?type=Base&subject=' obj.Subject]);
sessions = ai.getSessions('subject', obj.Subject, 'date', now);
stat = -1; url = [];

% If the date of this latest base session is not the same date
% as today, then create a new one for today
if isempty(sessions) || ~strcmp(sessions(end).start_time(1:10), thisDate(1:10))
if isempty(sessions)
% Ask user whether he/she wants to create new session
% Construct a questdlg with three options
choice = questdlg('Would you like to create a new base session?', ...
Expand Down Expand Up @@ -478,7 +478,7 @@ function recordWeight(obj, weight, subject)
% See also LAUNCHSESSIONURL
ai = obj.AlyxInstance;
s = ai.getData(ai.makeEndpoint(['subjects/' obj.Subject]));
url = fullfile(ai.BaseURL, 'admin', 'subjects', 'subject', s.id, 'change'); % this is wrong - need uuid
url = sprintf('%s/admin/subjects/subject/%s/change', ai.BaseURL, s.id);
stat = web(url, '-browser');
end

Expand Down
2 changes: 1 addition & 1 deletion alyx-matlab
6 changes: 0 additions & 6 deletions cortexlab/+git/changes.m

This file was deleted.

86 changes: 44 additions & 42 deletions tests/AlyxPanel_test.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
GraphData
end

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

methods (TestClassSetup)
function killFigures(testCase)
testCase.FigureVisibleDefault = get(0,'DefaultFigureVisible');
Expand All @@ -46,7 +52,7 @@ function loadData(testCase)
testCase.GraphData = graphData;
end

function setupPanel(testCase)
function setupPanel(testCase, BaseURL)
% Check paths file
assert(endsWith(which('dat.paths'), fullfile('fixtures','+dat','paths.m')));
% Check temp mainRepo folder is empty. An extra safe measure as we
Expand All @@ -59,6 +65,13 @@ function setupPanel(testCase)
localRepo = dat.reposPath('main','local');
if exist(localRepo, 'dir') == 0; mkdir(localRepo); end

% Create config directory
assert(mkdir(getOr(dat.paths,'rigConfig')), 'Failed to create config directory')

% Set the database url
paths.databaseURL = BaseURL;
save(fullfile(getOr(dat.paths,'rigConfig'), 'paths'), 'paths')
clearCBToolsCache
% Create figure for panel
testCase.hPanel = figure('Name', 'alyx GUI',...
'MenuBar', 'none',...
Expand Down Expand Up @@ -87,11 +100,13 @@ function setupPanel(testCase)
% MControl using this as a panel.
testCase.SubjectUI.addlistener('SelectionChanged', ...
@(src, evt)testCase.Panel.dispWaterReq(src, evt));

% Set Alyx Instance and log in
testCase.Panel.login('test_user', 'TapetesBloc18');
testCase.fatalAssertTrue(testCase.Panel.AlyxInstance.IsLoggedIn,...
'Failed to log into Alyx');
testCase.fatalAssertEqual(testCase.Panel.AlyxInstance.BaseURL, BaseURL,...
'Failed to correctly set database url');

% Verify subject folders created
present = ismember([{'default'}; testCase.Subjects(1:end-1)], dat.listSubjects);
Expand All @@ -117,12 +132,11 @@ function restoreFigures(testCase)
idx = cellfun(@(n)any(strcmp(n, testCase.Subjects)),{figHandles.Name});
close(figHandles(idx))
end
% Remove subject directories
% Remove directories
repos = [{getOr(dat.paths,'localAlyxQueue', ['fixtures' filesep 'alyxQ'])};...
dat.reposPath('main'); {getOr(dat.paths, 'globalConfig')}];
rm = @(repo)assert(rmdir(repo, 's'), 'Failed to remove test repo %s', repo);
cellfun(@(repo)iff(exist(repo,'dir') == 7, @()rm(repo), @()nop), dat.reposPath('main'));
% Remove Alyx queue
alyxQ = getOr(dat.paths,'localAlyxQueue', ['fixtures' filesep 'alyxQ']);
assert(rmdir(alyxQ, 's'), 'Failed to remove test Alyx queue')
cellfun(@(repo)iff(exist(repo,'dir') == 7, @()rm(repo), @()nop), repos);
end
end

Expand Down Expand Up @@ -214,8 +228,10 @@ function test_dispWaterReq(testCase)
testCase.verifyTrue(~strcmp(prev, new), 'Failed to retrieve new data')
end

function test_launchSessionURL(testCase)
function test_launchSessionURL(testCase, BaseURL)
% Test the launch of the session page in the admin Web interface
% TODO Use DELETE to test both creating new session and viewing
% existing
p = testCase.Panel;
testCase.Mock.InTest = true;
testCase.Mock.UseDefaults = false;
Expand All @@ -226,32 +242,32 @@ function test_launchSessionURL(testCase)

% Add mock user response
key = 'Would you like to create a new base session?';
testCase.Mock.Dialogs(key) = iff(isempty(todaySession), 'Yes', 'No');
noSub = isempty(todaySession)||~isempty(todaySession.number);
testCase.Mock.Dialogs(key) = iff(noSub, 'Yes', 'No');

[failed, url] = testCase.assertWarningFree(@()p.launchSessionURL);
testCase.verifyTrue(~failed, 'Failed to launch subject page in browser')
[status, url] = testCase.assertWarningFree(@()p.launchSessionURL);
testCase.verifyTrue(status > -1, 'Failed to launch subject page in browser')
if isempty(todaySession)
expected = url;
else
uuid = todaySession.url(find(todaySession.url=='/', 1, 'last')+1:end);
expected = ['https://test.alyx.internationalbrainlab.org/admin/', ...
'actions/session/', uuid, '/change'];
expected = [BaseURL '/admin/actions/session/', uuid, '/change'];
end

testCase.verifyEqual(url, expected, 'Unexpected url')

% todo: close tab after opening? (for `test_launchSubjectURL` as well)
end

function test_launchSubjectURL(testCase)
function test_launchSubjectURL(testCase, BaseURL)
% Test the launch of the subject page in the admin Web interface
p = testCase.Panel;
% Set new subject
testCase.SubjectUI.Selected = testCase.SubjectUI.Option{2};
[failed, url] = p.launchSubjectURL;
testCase.verifyTrue(~failed, 'Failed to launch subject page in browser')
expected = ['https:\\test.alyx.internationalbrainlab.org\admin\'...
'subjects\subject\bcefd268-68c2-4ea8-9b60-588ee4e99ebb\change'];
expected = [BaseURL '/admin/subjects/subject/'...
'bcefd268-68c2-4ea8-9b60-588ee4e99ebb/change'];
testCase.verifyEqual(url, expected, 'unexpected subject page url')
end

Expand All @@ -274,7 +290,7 @@ function test_recordWeight(testCase)
'Failed to update weight label color')

% Post weight < 80
weight = 28 + rand;
weight = 16 + rand;
testCase.Panel.recordWeight(weight)
expected = sprintf('Weight today: %.2f (< 80%%)', weight);
testCase.verifyTrue(startsWith(strip(weight_text.String(2,:)), expected),...
Expand Down Expand Up @@ -481,41 +497,27 @@ function test_activeFlag(testCase)
button = findobj(testCase.Parent, 'String', str);
testCase.assertEqual(button.Enable, 'on', 'AlyxPanel not enabled')

% Comment out the databaseURL field in the paths file
fid = fopen(which('dat.paths'));
data = cellflat(textscan(fid, '%s', 'Delimiter', '\n', 'CollectOutput', true));
fclose(fid);

data{startsWith(data,'p.databaseURL')} = ['%' data{startsWith(data,'p.databaseURL')}];

fid = fopen(which('dat.paths'), 'w');
cellfun(@(ln)fprintf(fid, '%s\n', ln), data);
fclose(fid);
% Set invalid database URL
baseURL = testCase.Panel.AlyxInstance.BaseURL;
paths.databaseURL = '';
save(fullfile(getOr(dat.paths,'rigConfig'), 'paths'), 'paths')
testCase.assertEmpty(getOr(dat.paths,'databaseURL'),...
'Failed to create custom paths file')

testCase.Figure = figure('Name', testCase.Subjects{end});
eui.AlyxPanel(testCase.Figure);

testCase.assertEmpty(getOr(dat.paths, 'databaseURL'), ...
% Reset URL
paths.databaseURL = baseURL;
save(fullfile(getOr(dat.paths,'rigConfig'), 'paths'), 'paths')
clearCBToolsCache % Ensure paths are reloaded
testCase.fatalAssertEqual(getOr(dat.paths, 'databaseURL'), baseURL, ...
'Failed to remove databaseURL field in paths')
button = findobj(testCase.Figure, 'String', 'Login');
testCase.verifyEqual(button.Enable, 'off', ...
'AlyxPanel enabled while databaseURL undefined')

close(testCase.Figure)
% Restore paths
fid = fopen(which('dat.paths'));
data = cellflat(textscan(fid, '%s', 'Delimiter', '\n', 'CollectOutput', true));
fclose(fid);

idx = startsWith(data,'%p.databaseURL');
if any(idx)
data{idx}(1) = [];
fid = fopen(which('dat.paths'), 'w');
cellfun(@(ln)fprintf(fid, '%s\n', ln), data);
fclose(fid);
testCase.fatalAssertTrue(~isempty(getOr(dat.paths, 'databaseURL')), ...
'Failed to restore databaseURL field in paths')
end
end

function test_round(testCase)
Expand Down
2 changes: 2 additions & 0 deletions tests/dat_test.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function test_paths(testCase)
paths.main2Repository = [p.mainRepository '2'];
paths.altRepository = [p.mainRepository '3'];
save(fullfile(p.rigConfig, 'paths'), 'paths')
clearCBToolsCache

p = dat.paths('testRig');
testCase.verifyTrue(ismember('novelRepo', fieldnames(p)), ...
Expand Down Expand Up @@ -312,6 +313,7 @@ function altMain2Paths(testCase)
'Secondary main repo already in path, expected otherwise')
paths.main2Repository = [dat.reposPath('main','m') '2'];
save(fullfile(getOr(dat.paths,'rigConfig'), 'paths'), 'paths')
clearCBToolsCache % Ensure paths are reloaded
testCase.assertEqual(paths.main2Repository, getOr(dat.paths,'main2Repository'),...
'Failed to create custom paths file')
end
Expand Down
Binary file modified tests/fixtures/data/viewSubjectData.mat
Binary file not shown.
1 change: 0 additions & 1 deletion tests/runall.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
% TODO May become a function
% TODO May add flags for levels of testing
% TODO Method setup in dat_test may become global fixture
% TODO Delete sinusoidLayer_test from this folder
% TODO Deal with directory changes
main_tests = testsuite;

Expand Down
49 changes: 0 additions & 49 deletions tests/sinusoidLayer_test.m

This file was deleted.

0 comments on commit 2b6b45c

Please sign in to comment.