Skip to content

Commit

Permalink
Added tests for mapping iRODS paths to local paths and parsing replicate
Browse files Browse the repository at this point in the history
numbers.
  • Loading branch information
kjsanger committed Dec 3, 2014
1 parent 8f4f390 commit c247794
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 29 deletions.
55 changes: 26 additions & 29 deletions src/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,41 +524,38 @@ char *json_to_local_path(json_t *object, baton_error_t *error) {
if (error->code != 0) goto error;
const char *filename = get_file_value(object, error);
if (error->code != 0) goto error;
//const char *collection = get_collection_value(object, error);
//if (error->code != 0) goto error;
const char *data_object = get_data_object_value(object, error);
if (error->code != 0) goto error;

if (represents_directory(object) && !represents_data_object(object)) {
// A collection to local directory mapping
if (directory && filename) {
path = make_file_path(directory, filename, error);
}
else if (directory && data_object) {
path = make_file_path(directory, data_object, error);
}
/* else if (collection && filename) { */
/* path = make_file_path(collection, filename, error); */
/* } */
/* else if (collection && data_object) { */
/* path = make_file_path(collection, data_object, error); */
/* } */
else if (filename) {
path = make_file_path(".", filename, error);
}
else if (data_object) {
path = make_file_path(".", data_object, error);
}
else if (directory) {
path = make_dir_path(directory, error);
if (error->code != 0) goto error;
}
else {
// All these are data object to local file mappings
if (represents_directory(object) && represents_data_object(object)) {
// No local filename; use the data object name as
// surrogate filename
const char *surrogate = get_data_object_value(object, error);
if (error->code != 0) goto error;
path = make_file_path(directory, surrogate, error);
if (error->code != 0) goto error;
}
else if (represents_file(object)) {
// Both local directory and filename specified
path = make_file_path(directory, filename, error);
if (error->code != 0) goto error;
}
else if (!directory && filename) {
// No local directory, use CWD as surrogate
path = make_file_path(".", filename, error);
if (error->code != 0) goto error;
}
else if (!filename) {
// No local filename, use data object name as surrogate
const char *surrogate = get_data_object_value(object, error);
if (error->code != 0) goto error;
path = make_file_path(".", surrogate, error);
if (error->code != 0) goto error;
}
path = make_dir_path(".", error);
}

if (error->code != 0) goto error;

return path;

error:
Expand Down
56 changes: 56 additions & 0 deletions tests/check_baton.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include <assert.h>
#include <limits.h>
#include <unistd.h>

#include <jansson.h>
Expand Down Expand Up @@ -170,6 +171,16 @@ START_TEST(test_parse_timestamp) {
}
END_TEST

// Can we parse file size strings?
START_TEST(test_parse_size) {
ck_assert_int_eq(0, parse_size("0"));

char max[1024];
snprintf(max, sizeof max, "%lu", ULONG_MAX);
ck_assert_int_eq(ULONG_MAX, parse_size(max));
}
END_TEST

// Can we coerce ISO-8859-1 to UTF-8?
START_TEST(test_to_utf8) {
char in[2] = { 0, 0 };
Expand Down Expand Up @@ -1422,6 +1433,49 @@ START_TEST(test_json_to_path) {
}
END_TEST

// Can we convert JSON representation to a useful local path string?
START_TEST(test_json_to_local_path) {
const char *file_name = "file1.txt";
const char *file_path = "/file1/path";

const char *obj_name = "obj1.txt";
const char *coll_path = "/obj/path";

json_t *path1 = json_pack("{s:s s:s s:s s:s}",
JSON_DIRECTORY_KEY, file_path,
JSON_FILE_KEY, file_name,
JSON_COLLECTION_KEY, coll_path,
JSON_DATA_OBJECT_KEY, obj_name);

baton_error_t error_path1;
ck_assert_str_eq(json_to_local_path(path1, &error_path1),
"/file1/path/file1.txt");
ck_assert_int_eq(error_path1.code, 0);
json_decref(path1);

json_t *path2 = json_pack("{s:s s:s s:s}",
JSON_FILE_KEY, file_name,
JSON_COLLECTION_KEY, coll_path,
JSON_DATA_OBJECT_KEY, obj_name);

baton_error_t error_path2;
ck_assert_str_eq(json_to_local_path(path1, &error_path2),
"./file1.txt");
ck_assert_int_eq(error_path2.code, 0);
json_decref(path2);

json_t *path3 = json_pack("{s:s s:s}",
JSON_COLLECTION_KEY, coll_path,
JSON_DATA_OBJECT_KEY, obj_name);

baton_error_t error_path3;
ck_assert_str_eq(json_to_local_path(path1, &error_path3),
"./obj1.txt");
ck_assert_int_eq(error_path3.code, 0);
json_decref(path3);
}
END_TEST

// Can we test JSON for the presence of an AVU?
START_TEST(test_contains_avu) {
json_t *avu1 = json_pack("{s:s, s:s}",
Expand Down Expand Up @@ -1706,6 +1760,7 @@ Suite *baton_suite(void) {
tcase_add_test(utilities_tests, test_maybe_stdin);
tcase_add_test(utilities_tests, test_format_timestamp);
tcase_add_test(utilities_tests, test_parse_timestamp);
tcase_add_test(utilities_tests, test_parse_size);
tcase_add_test(utilities_tests, test_to_utf8);

TCase *basic_tests = tcase_create("basic");
Expand Down Expand Up @@ -1753,6 +1808,7 @@ Suite *baton_suite(void) {
tcase_add_test(basic_tests, test_represents_file);

tcase_add_test(basic_tests, test_json_to_path);
tcase_add_test(basic_tests, test_json_to_local_path);
tcase_add_test(basic_tests, test_contains_avu);

tcase_add_test(basic_tests, test_get_user);
Expand Down

0 comments on commit c247794

Please sign in to comment.