Skip to content
Snippets Groups Projects
Commit 44c0129b authored by Daniel Hornung's avatar Daniel Hornung
Browse files

WIP: Test for file handling.

parent 63b97043
Branches
No related tags found
1 merge request!3ENH: Full functionality
Pipeline #12889 failed
...@@ -19,8 +19,9 @@ ...@@ -19,8 +19,9 @@
pkg load caosdb; pkg load caosdb;
all_tests = true; all_tests = true;
all_tests &= moxunit_runtests("-verbose", "test_query_retrieve.m"); % all_tests &= moxunit_runtests("-verbose", "test_query_retrieve.m");
all_tests &= moxunit_runtests("-verbose", "test_transaction.m"); % all_tests &= moxunit_runtests("-verbose", "test_transaction.m");
all_tests &= moxunit_runtests("-verbose", "test_files.m");
if not(all_tests) if not(all_tests)
exit(1); exit(1);
......
...@@ -18,8 +18,17 @@ ...@@ -18,8 +18,17 @@
%% Clean up CaosDB (remove everything with ID > 99) %% Clean up CaosDB (remove everything with ID > 99)
function cleanup() function cleanup()
% if nargin < 1
% filenames = {}
% end
c = Caosdb(); c = Caosdb();
ids = caosdb_get_ids(c.query('FIND ENTITY WITH ID > 99')); ids = caosdb_get_ids(c.query('FIND ENTITY WITH ID > 99'));
result = c.delete_by_id(ids); 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 end
% 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment