Skip to content
Snippets Groups Projects
test_caosdb.m 4.15 KiB
Newer Older
Daniel Hornung's avatar
Daniel Hornung committed
% This file is a part of the CaosDB Project.
%
% Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
% Copyright (C) 2021 Daniel Hornung <d.hornung@indiscale.com>
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU Affero General Public License as
% published by the Free Software Foundation, either version 3 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU Affero General Public License for more details.
%
% You should have received a copy of the GNU Affero General Public License
% along with this program. If not, see <https://www.gnu.org/licenses/>.

%% The main function which intitializes the tests.
function test_suite = test_caosdb()
  try % assignment of 'localfunctions' is necessary in Matlab >= 2016
Daniel Hornung's avatar
Daniel Hornung committed
    test_functions = localfunctions();
  catch % no problem; early Matlab versions can use initTestSuite fine
  end
  initTestSuite;
end
Timm Fitschen's avatar
Timm Fitschen committed

%% Only test the connection
function test_connection()
  % Default connection
  c1 = Caosdb();
  info = c1.info();

  % another working connection
Daniel Hornung's avatar
Daniel Hornung committed
  c2 = Caosdb("local-caosdb-admin");
  c2.info();

  % This connection should fail
Daniel Hornung's avatar
Daniel Hornung committed
  % c3 = Caosdb(connection = "does-not-exist");
  % Error message is:
  % maox_info: The ConnectionManager does not know any connection of this name.
  % No connection named 'does-not-exist' present.
  if moxunit_util_platform_is_octave()
Daniel Hornung's avatar
Daniel Hornung committed
    assertExceptionThrown(@non_existing_connection);
Daniel Hornung's avatar
Daniel Hornung committed
    assertExceptionThrown(@non_existing_connection, "16");
Daniel Hornung's avatar
Daniel Hornung committed
  expected_msg = ...
Daniel Hornung's avatar
Daniel Hornung committed
    strjoin({"maox_info: The ConnectionManager does not know any connection of this name.", ...
             "No connection named 'does-not-exist' present."}, "\n");
  assertEqual(lasterror().message, expected_msg);
Daniel Hornung's avatar
Daniel Hornung committed
end

function info = non_existing_connection()
Daniel Hornung's avatar
Daniel Hornung committed
  c = Caosdb("does-not-exist");
Daniel Hornung's avatar
Daniel Hornung committed
  info = c.info();

%% Test retrieval of a single Entity
function test_retrieve_single()
  % Default connection configuration is sufficient.
  c = Caosdb();

  % Retrieve a single entity
  violin = c.retrieve("120"){1};

  % Check content
  assertEqual(violin.name, "Sherlock Holmes' violin");
  props = violin.get_properties();
  assertEqual(props{1}.name, "price");
  assertEqual(props{1}.value, 814873.0);
  assertEqual(props{2}.name, "Manufacturer");
  assertEqual(props{2}.value, "119");

  assertFalse(violin.has_errors());
  assertFalse(violin.has_warnings());
  assertFalse(violin.has_infos());
end

%% Test retrieval of multiple Entities
function test_retrieve_multiple()
  % Retrieve two entities
  c = Caosdb();
  violin = c.retrieve("120"){1};
  collection = c.retrieve({"120", "119"});
  assertEqual(length(collection), 2);
  assertEqual(numel(collection), 2);
  assertEqual(collection{1}.id, violin.id);
  assertEqual(collection{1}.name, violin.name);
  assertEqual(collection{2}.name, "Antonio Stradivari");
  props = collection{2}.get_properties();
  assertEqual(props{1}.name, "latitude");
  assertEqual(props{1}.value, 45.07353);
  assertEqual(props{2}.name, "longitude");
  assertEqual(props{2}.value, 7.68315);

%% Test retrieval with errors
function test_retrieve_failure()
  % Default connection configuration is sufficient.
  c = Caosdb();

  % Retrieve a non-existing entity.
  does_not_exist = c.retrieve("i-do-not-exist"){1};
  assertTrue(does_not_exist.has_errors());
  error = does_not_exist.get_errors(){1};
  assertEqual(typeinfo(error.code), "int64 scalar");
  assertEqual(error.code, int64(2));
  assertEqual(error.description, "Entity does not exist.");

  % Call retrieve with wrong arguments.
  % (Only m file level, no invalid arguments should make it to the mex function.)
  assertExceptionThrown(@()c.retrieve(120), "maox:InvalidArgument");
  assertExceptionThrown(@()c.retrieve({120}), "maox:InvalidArgument");
  assertExceptionThrown(@()c.retrieve({"120"}, {}), "maox:InvalidArgument");
  assertExceptionThrown(@()c.retrieve({"120"}, {120}), "maox:InvalidArgument");
  assertExceptionThrown(@()c.retrieve({"120"}, {"120", 120}), "maox:InvalidArgument");
end