diff --git a/test/Run_Test.m b/test/Run_Test.m index eb4096f50fffbfafd78f74278f940c05373cdab1..05dd0afd0d44f80d3ca84e60c2ed6c9a853cff11 100644 --- a/test/Run_Test.m +++ b/test/Run_Test.m @@ -19,8 +19,9 @@ pkg load caosdb; all_tests = true; -all_tests &= moxunit_runtests("-verbose", "test_query_retrieve.m"); -all_tests &= moxunit_runtests("-verbose", "test_transaction.m"); +% all_tests &= moxunit_runtests("-verbose", "test_query_retrieve.m"); +% all_tests &= moxunit_runtests("-verbose", "test_transaction.m"); +all_tests &= moxunit_runtests("-verbose", "test_files.m"); if not(all_tests) exit(1); diff --git a/test/cleanup.m b/test/cleanup.m index 877aa25931367dab0ad0b66c148d77794b7f2a37..1f73825db1145b6d68bc2a6cbe4d420e4743975f 100644 --- a/test/cleanup.m +++ b/test/cleanup.m @@ -18,8 +18,17 @@ %% Clean up CaosDB (remove everything with ID > 99) function cleanup() + % if nargin < 1 + % filenames = {} + % end c = Caosdb(); ids = caosdb_get_ids(c.query('FIND ENTITY WITH ID > 99')); result = c.delete_by_id(ids); - % disp(result) + +% +% If FILENAMES is given, it must be a cell string array with filenames to delete. + % for filename = filenames + % filename = filename{1}; + % fsdelete(filename) % TODO + % end end diff --git a/test/test_files.m b/test/test_files.m new file mode 100644 index 0000000000000000000000000000000000000000..c61c3d5037ec1a399257ea700d54dd9ae073d93a --- /dev/null +++ b/test/test_files.m @@ -0,0 +1,103 @@ +% 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_files() + try % assignment of 'localfunctions' is necessary in Matlab >= 2016 + test_functions = localfunctions(); + catch % no problem; early Matlab versions can use initTestSuite fine + end + initTestSuite; +end + +% Create files with nearly random content. +function filenames = create_testfiles(n) + + seed = 20210831; + + rand("state", seed); + filenames = {}; + for i = [1:n] + [fid, filename, msg] = mkstemp ("testfile_XXXXXX", true); + if fid == -1 + error(["Error while creating temporary file:\n ", msg]); + end + data = rand([10, 10]); + save(filename, "data"); + end +end + +function test_insert_file() + cleanup(); + filenames = create_testfiles(1); + c = Caosdb(); + + % Create file + F1 = Entity(); + F1.role = "FILE"; + F1.name = "file_1"; + F1.filepath = "testfile1.xyz"; + F1.localpath = filenames{1}; + + % upload file + insert = c.insert({F1}); + assertFalse(insert.has_errors(), print_messages(insert.get_errors(), "error")); + assertFalse(insert.has_warnings(), print_messages(insert.get_warnings(), "warning")); + assertFalse(insert.has_infos(), print_messages(insert.get_infos(), "info")); + assertEqual(numel(insert), 1); + + F1_id = insert{1}.id; + + % query for file + q_result = c.query(["COUNT FILE WHICH IS STORED AT '", F1.filepath, "'"]); + assertEqual(q_result, 1); + + % download file and compare + [fid, outfilename_1, msg] = mkstemp ("testfile_out_XXXXXX", true); + if fid == -1 + error(["Error while creating temporary file:\n ", msg]); + end + c.retrieve_file_by_id(F1_id, outfilename_1); + + fid_orig = fopen(filenames{1}, "r"); + fid_retr = fopen(outfilename_1, "r"); + [data_orig, count_orig] = fread(fid_orig, Inf, "*uint8"); + count_retr = count_orig + 1; % try to read up to one byte more + [data_retr, count_retr] = fread(fid_retr, count_retr, "*uint8"); + fclose(fid_orig); + fclose(fid_retr); + assertEqual(count_retr, count_orig); + assertEqual(data_orig, data_retr); + + cleanup(); +end + + +% Utility functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%% print the messages into a cell +function result = print_messages(message_cell, log_name) + result = ""; + if nargin > 1 && ~isempty(log_name) + result = ["[ ", log_name, " ] "]; + end + for msg = message_cell + msg = msg{1}; + result = [result, "\n ", msg.str()]; + end +end