diff --git a/NAMESPACE b/NAMESPACE index 4ab6a394..7c689dcc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -73,9 +73,7 @@ importFrom(stringr,str_remove_all) importFrom(stringr,str_replace_all) importFrom(stringr,str_squish) importFrom(styler,style_text) -importFrom(testthat,compare) importFrom(testthat,context) -importFrom(testthat,expect) importFrom(testthat,expect_silent) importFrom(testthat,local_edition) importFrom(testthat,test_that) diff --git a/R/test_functions.R b/R/test_functions.R new file mode 100644 index 00000000..3c13f2ec --- /dev/null +++ b/R/test_functions.R @@ -0,0 +1,377 @@ + +test_expect_no_error <- function(object, info) { + + error <- tryCatch({ + object + NULL + }, error = function(e) { + e + }) + testthat::expect( + is.null(error), + sprintf("%s threw an error:\n\n" %+% red("%s"), info, paste(error, collapse = ","))) + invisible(object) +} + + +test_expect_no_warning <- function(object, info) { + warning <- tryCatch({ + object + NULL + }, warning = function(w) { + w + }) + testthat::expect(is.null(warning), info) +} + + +test_expect_is_in <- function(object, expected, info, label, na.rm = TRUE) { + + if (na.rm) + object <- object[!is.na(object)] + i <- object %in% expected + + comp <- testthat::compare(all(i), TRUE) + testthat::expect( + comp$equal, + sprintf( + "%s - %s should not contain: '%s'", + info, label, + paste(object[!i], collapse = "', '") + )) + + invisible(object) + } + + +test_expect_contains <- function(object, expected, info) { + + i <- expected %in% object + + comp <- testthat::compare(all(i), TRUE) + testthat::expect( + comp$equal, + sprintf("%s - does not contain: '%s'", info, paste(expected[!i], collapse = "', '")) + ) + + invisible(object) +} + + +test_expect_allowed <- function(object, allowed, info, label) { + + i <- object %in% allowed + + comp <- testthat::compare(all(i), TRUE) + testthat::expect( + comp$equal, + sprintf( + "%s - %s include(s) invalid terms: '%s'", + info, label, + paste(object[!i], collapse = "', '") + )) + + invisible(object) +} + + +test_expect_equal <- function(object, expected, info) { + i <- object == expected + comp <- testthat::compare(all(i), TRUE) + testthat::expect(comp$equal, info) +} + + +test_expect_true <- function(object, info) { + i <- object == TRUE + comp <- testthat::compare(all(i), TRUE) + testthat::expect(comp$equal, info) +} + + +test_expect_false <- function(object, info) { + i <- object == FALSE + comp <- testthat::compare(all(i), TRUE) + testthat::expect(comp$equal, info) +} + + +test_expect_named <- function(object, expected_names, info, label) { + + if (missing(expected_names)) { + testthat::expect( + !identical(names(object), NULL), + sprintf("%s - %s do not exist", info, label)) + } else { + testthat::expect( + identical(names(object), expected_names), + sprintf( + "%s\tnames of %s (%s) don't match %s", + info, + label, paste0("'", names(object), "'", collapse = ", "), + paste0("'", expected_names, "'", collapse = ", ") + ) + ) + } +} + + +test_expect_type <- function(object, type, info, label) { + stopifnot(is.character(type), length(type) == 1) + testthat::expect( + identical(typeof(object), type), + sprintf("%s - %s has type %s, not %s", info, label, typeof(object), type) + ) +} + + +test_expect_not_NA <- function(object, info, label) { + i <- !is.na(object) + comp <- testthat::compare(all(i), TRUE) + testthat::expect(comp$equal, sprintf("%s - %s contain(s) NAs", info, label)) + invisible(object) +} + + +test_expect_length_zero <- function(object, info, label) { + comp <- testthat::compare(length(object), 0) + testthat::expect(comp$equal, sprintf("%s: %s", info, label)) + invisible(object) +} + + +test_expect_unique <- function(object, info, label) { + x <- table(unlist(object)) + i <- x == 1 + comp <- testthat::compare(all(i), TRUE) + testthat::expect(comp$equal, sprintf("%s - %s not unique: '%s'", info, label, paste(names(x)[!i], collapse = "', '"))) + invisible(object) +} + + +test_expect_allowed_text <- function(object, is_data = FALSE, is_col_names = FALSE, info, label) { + + if (length(object) > 0) { + + if (is_data) { + disallowed <- + object %>% lapply(check_disallowed_chars, exceptions = c("")) %>% simplify2array() + } else { + disallowed <- + object %>% lapply(check_disallowed_chars) %>% simplify2array() + } + + check <- disallowed %>% lapply(any) %>% unlist() + + txt <- "\n" + for (i in which(check)) { + if (is_col_names) { + txt <- sprintf( + "%s\t- col %s: %s\n", + txt, i, colour_characters(object[[i]], which(disallowed[[i]]))) + } else { + txt <- sprintf( + "%s\t- ln %s: %s\n", + txt, i, colour_characters(object[[i]], which(disallowed[[i]]))) + } + + } + + if (is_data) { + testthat::expect( + identical(as.vector(all(!check)), TRUE), + sprintf( + "%s\tdisallowed characters in data detected: %s\n\tPlease replace using `custom_R_code`", + info, txt + ) + ) + } else { + testthat::expect( + identical(as.vector(all(!check)), TRUE), + sprintf("%s - disallowed characters in %s detected: \n%s", info, label, txt) + ) + } + + } + + invisible(object) + +} + + +colour_characters <- function(x, i = NULL) { + + chars <- x %>% charToRaw() %>% lapply(rawToChar) %>% unlist() + + # Wrapper around characters to print as colour + # obtained from crayon::red(x) + if (!is.null(i)) + chars[i] <- sprintf("\033[31m%s\033[39m", chars[i]) + + paste0(chars, collapse = "") +} + + +check_disallowed_chars <- function(x, exceptions = c("ÁÅÀÂÄÆÃĀâíåæäãàáíÇčóöøéèłńl°êÜüùúû±µµ“”‘’-–—≈˜×≥≤")) { + + i <- charToRaw(x) + # Allow all ascii text + is_ascii <- i < 0x7F + + # Allow some utf8 characters, those with accents over letters for foreign names + # List of codes is here: http://www.utf8-chartable.de/ + # Note c3 is needed because this is prefix for allowed UTF8 chars + # Warning: Portable packages must use only ASCII characters in their R code + # Sophie - could replace these with unicode like Lizzy did before? + exceptions <- exceptions + + is_allowed <- i %in% charToRaw(exceptions) + !(is_ascii | is_allowed) +} + + +test_expect_list_elements_contains_names <- function(object, expected, info) { + for (i in seq_along(object)) + test_expect_contains(names(object[[i]]), expected, info = paste(info, i)) + invisible(object) +} + + +test_expect_list_elements_allowed_names <- function(object, allowed, info, label) { + for (i in seq_along(object)) + test_expect_allowed(names(object[[i]]), allowed, info = paste(info, i), label = "field names") + invisible(object) +} + + +test_expect_list_elements_exact_names <- function(object, expected, info) { + for (i in seq_along(object)) { + test_expect_contains(names(object[[i]]), expected, info = paste(info, i)) + test_expect_allowed(names(object[[i]]), expected, info = paste(info, i), label = "field names") + } + invisible(object) +} + + +test_expect_dataframe_valid <- function(data, info, label) { + test_expect_not_NA(colnames(data), info, label) + test_expect_allowed_text(colnames(data), is_col_names = TRUE, info = info, label = label) + test_expect_unique(colnames(data), info, label) + test_expect_true(is.data.frame(data), info = sprintf("%s - is not a dataframe", info)) +} + + +test_expect_dataframe_named <- function(data, expected_colnames, info, label) { + test_expect_dataframe_valid(data, info, label) + test_expect_named(data, expected_colnames, info = info, label = label) +} + + +test_expect_dataframe_names_contain <- function(data, expected_colnames, info, label) { + test_expect_dataframe_valid(data, info, label) + test_expect_contains(names(data), expected_colnames, info = info) +} + + + +test_expect_list <- function(data, info) { + test_expect_true("list" %in% class(data), info = sprintf("%s - is not a list", info)) +} + + +test_expect_list_names_valid <- function(data, info, label) { + test_expect_list(data, info) + test_expect_not_NA(names(data), info = info, label = paste0("names of ", label)) + test_expect_unique(names(data), info = info, label = paste0("names of ", label)) +} + + +test_expect_list_names_exact <- function(data, expected_names, info, label) { + test_expect_list_names_valid(data, info, label = label) + test_expect_named(data, expected_names, info = info, label = label) +} + + +test_expect_list_names_allowed <- function(data, allowed_names, info, label) { + test_expect_list_names_valid(data, info, label = label) + test_expect_named(data, info = info, label = label) + test_expect_allowed(names(data), allowed_names, info = info, label = label) +} + + +test_expect_list_names_contain <- function(data, expected_names, info, label) { + test_expect_list_names_valid(data, info, label = label) + test_expect_named(data, info = info, label = label) + test_expect_contains(names(data), expected_names, info = info) +} + + +test_build_dataset <- function( + path_metadata, path_data, info, definitions, unit_conversions, schema, resource_metadata, taxon_list) { + + # Test it builds with no errors + test_expect_no_error( + build_config <- dataset_configure(path_metadata, definitions), + info = "`dataset_configure`" + ) + + test_expect_no_error( + build_dataset_raw <- dataset_process(path_data, build_config, schema, resource_metadata, unit_conversions), + info = "`dataset_process`" + ) + + test_expect_no_error( + build_dataset <- dataset_update_taxonomy(build_dataset_raw, taxon_list), + info = "`dataset_update_taxonomy`" + ) + + test_expect_structure(build_dataset, info, schema, definitions, single_dataset = TRUE) + + build_dataset +} + + +test_expect_structure <- function(data, info, schema, definitions, single_dataset = TRUE) { + + vars_austraits <- schema$austraits$elements %>% names() + + vars_tables <- + vars_austraits %>% + subset(., !(. %in% c( + "definitions", "schema", "sources", "metadata", + "build_info", "taxonomic_updates", "taxa"))) + + # Test lists have the right objects + comparison <- vars_austraits + + test_expect_list_names_exact(data, comparison, info, label = "output tables") + + # Test structure of tables + for (v in vars_tables) { + comparison <- schema$austraits$elements[[v]]$elements %>% names() + test_expect_dataframe_named(data[[v]], comparison, info = info, label = paste0(v, " table column names")) + } + + # Test that minimum expected columns are in `taxa` and `taxonomic_updates` tables + test_expect_contains(names(data[["taxa"]]), c("taxon_name", "taxon_rank"), info = paste0(info, "\tnames of `taxa` table")) + test_expect_contains( + names(data[["taxonomic_updates"]]), + c("dataset_id", "original_name", "aligned_name", "taxon_name", "taxonomic_resolution"), + info = paste0(info, "\tnames of `taxonomic_updates` table") + ) + +} + + +## A helper function to determine if this is being run as part of a test +is_testing_env <- function() { + # Calling scope + tb <- .traceback(x = 0) + + # Check if called in `testthat` or interactive + if (any(unlist(lapply(tb, function(x) any(grepl("test_env", x)))))) { + return(TRUE) + } else { + return(FALSE) + } +} diff --git a/R/testdata.R b/R/testdata.R index fcd90058..65de742e 100644 --- a/R/testdata.R +++ b/R/testdata.R @@ -41,7 +41,7 @@ dataset_test <- #' @inheritParams dataset_test #' @param schema Data schema #' @param definitions Trait defininitons -#' @importFrom testthat local_edition compare expect test_that context expect_silent +#' @importFrom testthat local_edition test_that context expect_silent #' @importFrom rlang .data #' @importFrom stats na.omit dataset_test_worker <- @@ -55,310 +55,10 @@ dataset_test_worker <- # We're using 2nd edition of test that, which has "context" field # https://cran.r-project.org/web/packages/testthat/vignettes/third-edition.html - # Is there a reason to be using the 2nd edition, while context has been superseded in later versions? - local_edition(2) - - expect_is_in <- function(object, expected, ..., info, label, na.rm = TRUE) { - - if (na.rm) - object <- object[!is.na(object)] - i <- object %in% expected - - comp <- compare(all(i), TRUE, ...) - expect( - comp$equal, - sprintf( - "%s - %s should not contain: '%s'", - info, label, - paste(object[!i], collapse = "', '") - )) - - invisible(object) - } - - expect_contains <- function(object, expected, ..., info) { - - i <- expected %in% object - - comp <- compare(all(i), TRUE, ...) - expect( - comp$equal, - sprintf("%s - does not contain: '%s'", info, paste(expected[!i], collapse = "', '")) - ) - - invisible(object) - } - - expect_allowed <- function(object, allowed, ..., info, label) { - - i <- object %in% allowed - - comp <- compare(all(i), TRUE, ...) - expect( - comp$equal, - sprintf( - "%s - %s include(s) invalid terms: '%s'", - info, label, - paste(object[!i], collapse = "', '") - )) - - invisible(object) - } - - expect_equal <- function(object, expected, info) { - i <- object == expected - comp <- compare(all(i), TRUE) - expect(comp$equal, info) - } - - expect_true <- function(object, info) { - i <- object == TRUE - comp <- compare(all(i), TRUE) - expect(comp$equal, info) - } - - expect_false <- function(object, info) { - i <- object == FALSE - comp <- compare(all(i), TRUE) - expect(comp$equal, info) - } - - expect_named <- function(object, expected_names, info, label) { - - if (missing(expected_names)) { - expect( - !identical(names(object), NULL), - sprintf("%s - %s do not exist", info, label)) - } else { - expect( - identical(names(object), expected_names), - sprintf( - "%s\tnames of %s (%s) don't match %s", - info, - label, paste0("'", names(object), "'", collapse = ", "), - paste0("'", expected_names, "'", collapse = ", ") - ) - ) - } - } - - expect_type <- function(object, type, info, label) { - stopifnot(is.character(type), length(type) == 1) - expect( - identical(typeof(object), type), - sprintf("%s - %s has type %s, not %s", info, label, typeof(object), type) - ) - } - - expect_not_NA <- function(object, info, label) { - i <- !is.na(object) - comp <- compare(all(i), TRUE) - expect(comp$equal, sprintf("%s - %s contain(s) NAs", info, label)) - invisible(object) - } - - expect_length_zero <- function(object, info, label) { - comp <- compare(length(object), 0) - expect(comp$equal, sprintf("%s: %s", info, label)) - invisible(object) - } - - expect_unique <- function(object, info, label) { - x <- table(unlist(object)) - i <- x == 1 - comp <- compare(all(i), TRUE) - expect(comp$equal, sprintf("%s - %s not unique: '%s'", info, label, paste(names(x)[!i], collapse = "', '"))) - invisible(object) - } - - expect_allowed_text <- function(object, is_data = FALSE, is_col_names = FALSE, info, label) { - - if (length(object) > 0) { - - if (is_data) { - disallowed <- - object %>% lapply(check_disallowed_chars, exceptions = c("")) %>% simplify2array() - } else { - disallowed <- - object %>% lapply(check_disallowed_chars) %>% simplify2array() - } - - check <- disallowed %>% lapply(any) %>% unlist() - - txt <- "\n" - for (i in which(check)) { - if (is_col_names) { - txt <- sprintf( - "%s\t- col %s: %s\n", - txt, i, colour_characters(object[[i]], which(disallowed[[i]]))) - } else { - txt <- sprintf( - "%s\t- ln %s: %s\n", - txt, i, colour_characters(object[[i]], which(disallowed[[i]]))) - } - - } - - if (is_data) { - expect( - identical(as.vector(all(!check)), TRUE), - sprintf( - "%s\tdisallowed characters in data detected: %s\n\tPlease replace using `custom_R_code`", - info, txt - ) - ) - } else { - expect( - identical(as.vector(all(!check)), TRUE), - sprintf("%s - disallowed characters in %s detected: \n%s", info, label, txt) - ) - } - } - - invisible(object) - - } - - colour_characters <- function(x, i = NULL) { - - chars <- x %>% charToRaw() %>% lapply(rawToChar) %>% unlist() - - # Wrapper around characters to print as colour - # obtained from crayon::red(x) - if (!is.null(i)) - chars[i] <- sprintf("\033[31m%s\033[39m", chars[i]) - - paste0(chars, collapse = "") - } - - check_disallowed_chars <- function(x, exceptions = c("ÁÅÀÂÄÆÃĀâíåæäãàáíÇčóöøéèłńl°êÜüùúû±µµ“”‘’-–—≈˜×≥≤")) { - - i <- charToRaw(x) - # Allow all ascii text - is_ascii <- i < 0x7F - - # Allow some utf8 characters, those with accents over letters for foreign names - # List of codes is here: http://www.utf8-chartable.de/ - # Note c3 is needed because this is prefix for allowed UTF8 chars - # Warning: Portable packages must use only ASCII characters in their R code - # Sophie - could replace these with unicode like Lizzy did before? - exceptions <- exceptions - - is_allowed <- i %in% charToRaw(exceptions) - !(is_ascii | is_allowed) - } - - # Better than expect_silent as contains `info` and allows for complete failures - expect_no_error <- function(object, ..., info) { - error <- tryCatch({ - object - NULL - }, error = function(e) { - e - }) - expect( - is.null(error), - sprintf("%s threw an error:\n\n" %+% red("%s"), info, paste(error, collapse = ","))) - invisible(object) - } - - expect_no_warning <- function(object, ..., info) { - warning <- tryCatch({ - object - NULL - }, warning = function(w) { - w - }) - expect(is.null(warning), info) - } - - expect_list_elements_contains_names <- function(object, expected, info) { - for (i in seq_along(object)) - expect_contains(names(object[[i]]), expected, info = paste(info, i)) - invisible(object) - } - - expect_list_elements_allowed_names <- function(object, allowed, info, label) { - for (i in seq_along(object)) - expect_allowed(names(object[[i]]), allowed, info = paste(info, i), label = "field names") - invisible(object) - } - - expect_list_elements_exact_names <- function(object, expected, info) { - for (i in seq_along(object)) { - expect_contains(names(object[[i]]), expected, info = paste(info, i)) - expect_allowed(names(object[[i]]), expected, info = paste(info, i), label = "field names") - } - invisible(object) - } - - expect_dataframe_valid <- function(data, info, label) { - expect_not_NA(colnames(data), info, label) - expect_allowed_text(colnames(data), is_col_names = TRUE, info = info, label = label) - expect_unique(colnames(data), info, label) - expect_true(is.data.frame(data), info = sprintf("%s - is not a dataframe", info)) - } - - # Function is assigned but not used - expect_dataframe_named <- function(data, expected_colnames, info, label) { - expect_dataframe_valid(data, info, label) - expect_named(data, expected_colnames, info = info, label = label) - } - - expect_dataframe_names_contain <- function(data, expected_colnames, info, label) { - expect_dataframe_valid(data, info, label) - expect_contains(names(data), expected_colnames, info = info) - } - - expect_list <- function(data, info) { - expect_true("list" %in% class(data), info = sprintf("%s - is not a list", info)) - } - - expect_list_names_valid <- function(data, info, label) { - expect_list(data, info) - expect_not_NA(names(data), info = info, label = label) - expect_unique(names(data), info = info, label = label) - } - - expect_list_names_exact <- function(data, expected_names, info, label) { - expect_list_names_valid(data, info, label = label) - expect_named(data, expected_names, info = info, label = label) - } - - expect_list_names_allowed <- function(data, allowed_names, info, label) { - expect_list_names_valid(data, info, label = label) - expect_named(data, info = info, label = label) - expect_allowed(names(data), allowed_names, info = info, label = label) - } - - # Function is assigned but not used - expect_list_names_contain <- function(data, expected_names, info, label) { - expect_list_names_valid(data, info, label = label) - expect_named(data, info = info, label = label) - expect_contains(names(data), expected_names, info = info) - } - - test_build_dataset <- function( - path_metadata, path_data, info, definitions, unit_conversions, schema, resource_metadata, taxon_list) { - - # Test it builds with no errors - expect_no_error({ - build_config <- dataset_configure(path_metadata, definitions) - }, info = "`dataset_configure`") - - expect_no_error({ - build_dataset_raw <- dataset_process(path_data, build_config, schema, resource_metadata, unit_conversions) - }, info = "`dataset_process`") - - expect_no_error({ - build_dataset <- dataset_update_taxonomy(build_dataset_raw, taxon_list) - }, info = "`dataset_update_taxonomy`") - - build_dataset - } + local_edition(2) - # Now run tests for each dataset + # Run tests for each dataset for (dataset_id in test_dataset_ids) { @@ -371,12 +71,12 @@ dataset_test_worker <- ## Files exist files <- file.path(s, c("data.csv", "metadata.yml")) for (f in files) { - expect_true(file.exists(f), info = sprintf("%s" %+% "\tfile does not exist", red(f))) + test_expect_true(file.exists(f), info = sprintf("%s\tfile does not exist", red(f))) } ## Check for other files vals <- c("data.csv", "metadata.yml", "raw") - expect_is_in( + test_expect_is_in( dir(s), vals, info = paste0(red(file.path(path_data, dataset_id)), "\tdisallowed files"), label = "folder" @@ -384,23 +84,23 @@ dataset_test_worker <- ## `data.csv` f <- files[1] - expect_silent( + testthat::expect_silent( data <- read_csv(f, col_types = cols(), guess_max = 1e5, progress = FALSE) # Time columns get reformatted ) ## Check no issues flagged when parsing file - expect_no_error( + test_expect_no_error( readr::stop_for_problems(data), info = sprintf(red("`read_csv(%s)`"), f) ) - expect_dataframe_valid(data, info = paste0(red(f), "\tdata"), label = "column names") + test_expect_dataframe_valid(data, info = paste0(red(f), "\tdata"), label = "column names") ## Metadata f <- files[2] - expect_allowed_text(readLines(f, encoding = "UTF-8"), info = paste0(red(f), "\tmetadata"), label = "metadata") - expect_silent(metadata <- yaml::read_yaml(f)) - expect_list_names_exact( + test_expect_allowed_text(readLines(f, encoding = "UTF-8"), info = paste0(red(f), "\tmetadata"), label = "metadata") + testthat::expect_silent(metadata <- yaml::read_yaml(f)) + test_expect_list_names_exact( metadata, schema$metadata$elements %>% names(), info = red(f), label = "metadata sections" ) @@ -408,28 +108,28 @@ dataset_test_worker <- ## Custom R code txt <- metadata[["dataset"]][["custom_R_code"]] # Check that `custom_R_code` is immediately followed by `collection_date` - expect_equal( + test_expect_equal( metadata[["dataset"]][which(names(metadata[["dataset"]]) == "custom_R_code") + 1] %>% names(), "collection_date", info = sprintf("%s\tdataset - the `custom_R_code` field must be followed by `collection_date`", red(f)) ) # Apply custom manipulations - expect_no_error(data <- process_custom_code(txt)(data), info = paste0(red(f), "\t`custom_R_code`")) + test_expect_no_error(data <- process_custom_code(txt)(data), info = paste0(red(f), "\t`custom_R_code`")) ## Source - expect_list_names_valid(metadata[["source"]], info = sprintf("%s\tsource", red(f)), label = "field names") + test_expect_list_names_valid(metadata[["source"]], info = sprintf("%s\tsource", red(f)), label = "field names") v <- names(metadata[["source"]]) i <- grepl("primary", v) | grepl("secondary", v) | grepl("original", v) - expect_contains(v, "primary", info = paste0(red(f), "\tsource")) + test_expect_contains(v, "primary", info = paste0(red(f), "\tsource")) - expect_true( + test_expect_true( sum(grepl("primary", v)) <= 1, info = paste0(red(f), "\tsources can have max 1 type labelled 'primary': ", paste(v, collapse = ", ")) ) - expect_true( + test_expect_true( all(i), info = paste0(red(f), "\tsources must be primary, secondary or original: ", paste(v[!i], collapse = ", ")) ) @@ -437,7 +137,7 @@ dataset_test_worker <- vals <- c("key", "bibtype", "author", "title", "year") for (bib in names(metadata[["source"]])) { - expect_contains( + test_expect_contains( names(metadata[["source"]][[bib]]), vals, info = sprintf("%s\tsource '%s'", red(f), bib) ) @@ -445,16 +145,16 @@ dataset_test_worker <- keys <- unlist(lapply(metadata[["source"]], "[[", "key")) - expect_unique( + test_expect_unique( keys, info = paste0(red(f), "\tsources"), label = "keys" ) ## People - expect_list(metadata[["contributors"]], info = paste0(red(f), "\tcontributors")) + test_expect_list(metadata[["contributors"]], info = paste0(red(f), "\tcontributors")) - expect_list_names_allowed( + test_expect_list_names_allowed( metadata[["contributors"]], schema$metadata$elements$contributors$elements %>% names(), info = paste0(red(f), "\tcontributors"), label = "contributor type fields" @@ -463,17 +163,17 @@ dataset_test_worker <- ## Data collectors if (!is.na(metadata[["contributors"]][["data_collectors"]][1])) { - expect_list(metadata[["contributors"]][["data_collectors"]], info = paste0(red(f), "\tdata_collectors")) + test_expect_list(metadata[["contributors"]][["data_collectors"]], info = paste0(red(f), "\tdata_collectors")) vars <- schema$metadata$elements$contributors$elements$data_collectors$elements %>% names() for (i in seq_along(metadata[["contributors"]][["data_collectors"]])) { - expect_list_names_allowed( + test_expect_list_names_allowed( metadata[["contributors"]][["data_collectors"]][[i]], vars, info = paste0(red(f), "\tdata_collector ", i), label = "`data_collector` field names" ) - expect_contains( + test_expect_contains( metadata[["contributors"]][["data_collectors"]][[i]] %>% names(), vars[1:4], info = sprintf("%s\tdata_collector %s", red(f), i) ) @@ -481,42 +181,42 @@ dataset_test_worker <- } ## Dataset curators - expect_true( + test_expect_true( !is.null(metadata[["contributors"]][["dataset_curators"]]), info = sprintf("%s\tcontributors - `dataset_curators` is missing", red(f)) ) - expect_type( + test_expect_type( metadata[["contributors"]][["dataset_curators"]], "character", info = paste0(red(f), "\tcontributors"), label = "`dataset_curators`" ) ## Assistants if (!is.null(metadata[["contributors"]][["assistants"]][1])) - expect_type( + test_expect_type( metadata[["contributors"]][["assistants"]], "character", info = paste0(red(f), "\tcontributors"), label = "`assistants`" ) ## Dataset - expect_list_names_allowed( + test_expect_list_names_allowed( metadata[["dataset"]], schema$metadata$elements$dataset$values %>% names(), info = paste0(red(f), "\tdataset"), label = "`dataset` field names" ) - expect_type( + test_expect_type( metadata[["dataset"]][["data_is_long_format"]], "logical", info = paste0(red(f), "\tdataset"), label = "`data_is_long_format`" ) - expect_type( + test_expect_type( metadata[["dataset"]], "list", info = paste0(red(f), "\tdataset"), label = "metadata" ) ## Locations - expect_silent( + testthat::expect_silent( locations <- metadata$locations %>% process_format_locations(dataset_id, schema) %>% @@ -525,9 +225,9 @@ dataset_test_worker <- if (length(unlist(metadata[["locations"]])) > 1) { - expect_list(metadata[["locations"]], info = paste0(red(f), "\tlocations")) + test_expect_list(metadata[["locations"]], info = paste0(red(f), "\tlocations")) - expect_dataframe_names_contain( + test_expect_dataframe_names_contain( locations, c("dataset_id", "location_name", "location_property", "value"), info = paste0(red(f), "\tlocations"), label = "field names" @@ -535,13 +235,13 @@ dataset_test_worker <- for (v in names(metadata$locations)) { - expect_list(metadata[["locations"]][[v]], info = paste0(red(f), "\tlocation ", v)) + test_expect_list(metadata[["locations"]][[v]], info = paste0(red(f), "\tlocation ", v)) # If fields do not contain both 'latitude range (deg)' and 'longitude range (deg)' if (!(all(c("latitude range (deg)", "longitude range (deg)") %in% names(metadata[["locations"]][[v]])))) { # Check that it contains 'latitude (deg)' and 'longitude (deg)' - expect_contains( + test_expect_contains( names(metadata[["locations"]][[v]]), c("latitude (deg)", "longitude (deg)"), info = paste0(red(f), "\tlocation '", v, "'") @@ -550,12 +250,12 @@ dataset_test_worker <- } # Check `location_name`'s from metadata are in dataset and vice versa - expect_true( + test_expect_true( !is.null(metadata[["dataset"]][["location_name"]]), info = paste0(red(files[2]), "\tdataset - `location_name` is missing") ) - expect_contains( + test_expect_contains( names(data), metadata[["dataset"]][["location_name"]], info = paste0( @@ -565,7 +265,7 @@ dataset_test_worker <- v <- data[[metadata[["dataset"]][["location_name"]]]] %>% unique %>% na.omit i <- v %in% names(metadata$locations) - expect_true( + test_expect_true( all(i), info = paste0( red(f), @@ -574,7 +274,7 @@ dataset_test_worker <- ) i <- names(metadata$locations) %in% v - expect_true( + test_expect_true( all(i), info = paste0( red(f), @@ -584,7 +284,7 @@ dataset_test_worker <- } ## Contexts - expect_silent( + testthat::expect_silent( contexts <- metadata$contexts %>% process_format_contexts(dataset_id, data) @@ -594,7 +294,7 @@ dataset_test_worker <- context_properties <- sapply(metadata[["contexts"]], "[[", "context_property") context_vars_in <- sapply(metadata[["contexts"]], "[[", "var_in") - expect_equal( + test_expect_equal( context_properties |> duplicated() |> sum(), 0, info = sprintf( @@ -602,7 +302,7 @@ dataset_test_worker <- red(f), paste(context_properties[duplicated(context_properties)], collapse = "', '")) ) - expect_equal( + test_expect_equal( context_vars_in |> duplicated() |> sum(), 0, info = sprintf( @@ -614,7 +314,7 @@ dataset_test_worker <- # Check context details load if (nrow(contexts) > 0) { - expect_dataframe_names_contain( + test_expect_dataframe_names_contain( contexts, c("context_property", "category", "var_in"), info = paste0(red(f), "\tcontexts"), label = "field names" @@ -622,7 +322,7 @@ dataset_test_worker <- # Check that unique context `value`'s only have one unique description - expect_equal( + test_expect_equal( contexts %>% dplyr::group_by(.data$context_property, .data$value) %>% dplyr::summarise(n = dplyr::n_distinct(.data$description)) %>% dplyr::filter(.data$n > 1) %>% nrow(), 0, info = sprintf( @@ -636,7 +336,7 @@ dataset_test_worker <- ) # Check that there are no duplicate `find` fields - expect_equal( + test_expect_equal( contexts %>% dplyr::group_by(.data$context_property, .data$find) %>% dplyr::summarise(n = dplyr::n()) %>% dplyr::filter(.data$n > 1) %>% nrow(), 0, info = sprintf( @@ -659,7 +359,7 @@ dataset_test_worker <- # Check that no `find` values are NA if (!is.null(vals[[s]][["find"]])) { - expect_false( + test_expect_false( is.na(vals[[s]][["find"]]), info = paste0(red(f), sprintf( @@ -669,7 +369,7 @@ dataset_test_worker <- } # Check that there are no `find` fields without accompanying `value` fields - expect_false( + test_expect_false( !is.null(vals[[s]][["find"]]) && is.null(vals[[s]][["value"]]), info = paste0(red(f), sprintf( @@ -679,7 +379,7 @@ dataset_test_worker <- # Check that there are no `description` fields with NA `value` fields if (!is.null(vals[[s]][["value"]]) && !is.null(vals[[s]][["description"]])) { - expect_false( + test_expect_false( !is.na(vals[[s]][["description"]]) && is.na(vals[[s]][["value"]]), info = paste0(red(f), sprintf( @@ -689,7 +389,7 @@ dataset_test_worker <- } # Check that there are no `description` fields without accompanying `find` and `value` fields - expect_false( + test_expect_false( !is.null(vals[[s]][["description"]]) && is.null(vals[[s]][["find"]]) && is.null(vals[[s]][["value"]]), info = paste0(red(f), sprintf( @@ -705,26 +405,26 @@ dataset_test_worker <- ## Traits - expect_list_elements_contains_names( + test_expect_list_elements_contains_names( metadata[["traits"]], c("var_in", "unit_in", "trait_name", "value_type", "basis_of_value"), info = paste0(red(f), "\ttrait") ) - expect_list_elements_allowed_names( + test_expect_list_elements_allowed_names( metadata[["traits"]], c(schema$metadata$elements$traits$elements %>% names(), unique(contexts$var_in)), info = paste0(red(f), "\ttrait") ) - expect_silent(traits <- traits.build::util_list_to_df2(metadata[["traits"]])) + testthat::expect_silent(traits <- traits.build::util_list_to_df2(metadata[["traits"]])) - expect_true( + test_expect_true( is.data.frame(traits), info = paste0(red(f), "\ttraits - metadata cannot be converted to a dataframe") ) - expect_is_in( + test_expect_is_in( traits$trait_name, definitions$elements %>% names(), info = paste0(red(f), "\ttraits"), label = "`trait_name`'s" @@ -732,7 +432,7 @@ dataset_test_worker <- # Check units are found in `unit_conversions.csv` units <- read_csv("config/unit_conversions.csv") - expect_is_in( + test_expect_is_in( traits$unit_in, units$unit_from, info = paste0(red(f), "\ttraits"), label = "`unit_in`'s" @@ -740,7 +440,7 @@ dataset_test_worker <- # Check no duplicate `var_in`'s - expect_equal( + test_expect_equal( traits %>% dplyr::group_by(.data$var_in) %>% dplyr::summarise(n = dplyr::n()) %>% dplyr::filter(.data$n > 1) %>% nrow(), 0, @@ -759,7 +459,7 @@ dataset_test_worker <- if (nrow(contexts > 0)) { # Check they are in context dataset - expect_contains( + test_expect_contains( c(names(data), names(traits)), unique(contexts$var_in), info = paste0(red(files[1]), red(", "), red(files[2]), "\tdata and/or traits metadata") @@ -783,7 +483,7 @@ dataset_test_worker <- # Look for context values in `find` column i <- v %in% contextsub[["find"]] - expect_true( + test_expect_true( all(i), info = ifelse( "hms" %in% class(v), @@ -797,7 +497,7 @@ dataset_test_worker <- i <- contextsub[["find"]] %in% v - expect_true( + test_expect_true( all(i), info = ifelse( !is.null(data[[j]]), @@ -825,7 +525,7 @@ dataset_test_worker <- cols <- traits[[field]][i] %>% unique() # Check fixed values in metadata are allowed - expect_is_in( + test_expect_is_in( fixed, c("unknown", schema[[field]][["values"]] %>% names), info = paste0(red(f), "\ttraits"), label = sprintf("`%s`", field) ) @@ -833,7 +533,7 @@ dataset_test_worker <- # Check column values are allowed if (length(cols) > 0) { for (c in cols) { - expect_is_in( + test_expect_is_in( stringr::str_split(data[[c]], " ") %>% unlist() %>% unique(), c("unknown", schema[[field]][["values"]] %>% names), info = sprintf("%s\t'%s'", red(files[1]), c), label = sprintf("`%s` column", field) @@ -850,7 +550,7 @@ dataset_test_worker <- # If the metadata field is a column in the data (and not an accepted value of the field) if (metadata[["dataset"]][[field]] %in% names(data) & !(metadata[["dataset"]][[field]] %in% not_allowed)) { - expect_is_in( + test_expect_is_in( stringr::str_split(data[[metadata[["dataset"]][[field]]]], " ") %>% unlist() %>% unique(), c("unknown", schema[[field]][["values"]] %>% names), info = sprintf("%s\t'%s'", red(files[1]), metadata[["dataset"]][[field]]), label = sprintf("`%s` column", field) @@ -860,7 +560,7 @@ dataset_test_worker <- } else { fields_by_word <- stringr::str_split(metadata[["dataset"]][[field]], " ") %>% unlist() - expect_is_in( + test_expect_is_in( fields_by_word, c("unknown", schema[[field]][["values"]] %>% names), info = paste0(red(f), "\tdataset"), label = sprintf("`%s`", field) ) @@ -873,22 +573,22 @@ dataset_test_worker <- if (!is.na(metadata[["substitutions"]][1])) { - expect_list_elements_exact_names( + test_expect_list_elements_exact_names( metadata[["substitutions"]], schema$metadata$elements$substitutions$values %>% names(), info = paste0(red(f), "\tsubstitution") ) trait_names <- sapply(metadata[["substitutions"]], "[[", "trait_name") - expect_is_in( + test_expect_is_in( unique(trait_names), definitions$elements %>% names(), info = paste0(red(f), "\tsubstitutions"), label = "`trait_name`'s" ) - expect_is_in( + test_expect_is_in( unique(trait_names), unique(traits$trait_name), info = paste0(red(f), "\tsubstitutions"), label = "`trait_name`'s" ) - expect_no_error( + test_expect_no_error( x <- metadata[["substitutions"]] %>% util_list_to_df2() %>% split(.$trait_name), info = paste0(red(f), "\tconverting substitutions to a dataframe and splitting by `trait_name`") ) @@ -897,7 +597,7 @@ dataset_test_worker <- for (trait in names(x)) { # First check no duplicate combinations of `find` - expect_equal( + test_expect_equal( x[[trait]] %>% dplyr::group_by(.data$find) %>% dplyr::summarise(n = dplyr::n()) %>% dplyr::filter(.data$n > 1) %>% nrow(), 0, info = sprintf( @@ -923,7 +623,7 @@ dataset_test_worker <- to_check %in% allowable | to_check %>% sapply(util_check_all_values_in, allowable) )] - expect_length_zero( + test_expect_length_zero( failing, info = sprintf( "%s\tsubstitutions - `%s` has invalid replacement values", @@ -938,13 +638,13 @@ dataset_test_worker <- if (!is.na(metadata[["taxonomic_updates"]][1])) { - expect_no_error( + test_expect_no_error( x <- metadata[["taxonomic_updates"]] %>% util_list_to_df2(), info = paste0(red(f), "\tconverting `taxonomic_updates` to a dataframe") ) # Check no duplicate `find` values - expect_equal( + test_expect_equal( x %>% dplyr::group_by(.data$find) %>% dplyr::summarise(n = dplyr::n()) %>% dplyr::filter(.data$n > 1) %>% nrow(), 0, info = sprintf( "%s\ttaxonomic_updates - duplicate `find` values detected: '%s'", @@ -956,14 +656,14 @@ dataset_test_worker <- ) ) - expect_list_elements_exact_names( + test_expect_list_elements_exact_names( metadata[["taxonomic_updates"]], schema$metadata$elements$taxonomic_updates$values %>% names(), info = paste0(red(f), "\ttaxonomic_update") ) taxon_names <- sapply(metadata[["taxonomic_updates"]], "[[", "find") - expect_is_in( + test_expect_is_in( unique(taxon_names), data[[metadata[["dataset"]][["taxon_name"]]]] %>% unique(), info = paste0(red(f), "\ttaxonomic_updates"), label = "`taxon_name`'s" ) @@ -971,12 +671,12 @@ dataset_test_worker <- } ## Check that special characters do not make it into the data - expect_no_error( + test_expect_no_error( parsed_data <- data %>% process_parse_data(dataset_id, metadata, contexts, schema), info = sprintf("%s\t`process_parse_data`", red(dataset_id))) - expect_allowed_text( + test_expect_allowed_text( parsed_data$traits$value, is_data = TRUE, info = sprintf("%s", red(files[1])) ) @@ -1035,13 +735,13 @@ dataset_test_worker <- if (!is.na(metadata[["exclude_observations"]][1])) { - expect_list_elements_exact_names( + test_expect_list_elements_exact_names( metadata[["exclude_observations"]], schema$metadata$elements$exclude_observations$values %>% names(), info = paste0(red(f), "\texclude_observations") ) - expect_no_error( + test_expect_no_error( x <- metadata[["exclude_observations"]] %>% util_list_to_df2() %>% tidyr::separate_longer_delim("find", delim = ", ") %>% @@ -1050,7 +750,7 @@ dataset_test_worker <- ) # Check no duplicate `find` values - expect_equal( + test_expect_equal( x %>% dplyr::group_by(.data$variable, .data$find) %>% dplyr::summarise(n = dplyr::n()) %>% dplyr::filter(.data$n > 1) %>% nrow(), 0, info = sprintf( @@ -1062,7 +762,7 @@ dataset_test_worker <- collapse = "', '") ) ) - expect_no_error( + test_expect_no_error( x <- x %>% split(.$variable), info = paste0(red(f), "\tsplitting `exclude_observations` by variable") ) @@ -1074,7 +774,7 @@ dataset_test_worker <- # If the variable to be excluded is a trait: if (variable %in% traits$trait_name) { - expect_is_in( + test_expect_is_in( find_values, # Extract values from the data for that variable parsed_data %>% dplyr::filter(.data$trait_name == variable) %>% dplyr::pull(.data$value) %>% unique(), @@ -1084,7 +784,7 @@ dataset_test_worker <- } else { # If the variable to be excluded is `taxon_name`, `location_name` or other metadata fields - expect_is_in( + test_expect_is_in( find_values, parsed_data %>% dplyr::pull(variable) %>% unique(), info = paste0(red(f), "\texclude_observations"), label = sprintf("variable '%s'", variable) ) @@ -1100,7 +800,7 @@ dataset_test_worker <- var_in <- unlist(metadata[["dataset"]]) i <- match("trait_name", var_out) values <- unique(data[[var_in[i]]]) - expect_contains( + test_expect_contains( traits[["var_in"]], values, info = paste0(red(files[2]), "\ttraits") ) @@ -1108,7 +808,7 @@ dataset_test_worker <- } else { # For wide datasets, expect variables in traits are headers in the data - expect_is_in( + test_expect_is_in( traits[["var_in"]], names(data), info = paste0(red(files[2]), "\ttraits"), label = "`var_in`" ) @@ -1128,14 +828,14 @@ dataset_test_worker <- ## Check that not all trait names are NAs - expect_false( + test_expect_false( nrow(traits %>% dplyr::filter(!is.na(.data$trait_name))) == 0, info = paste0(red(f), "\ttraits - only contain NA `trait_name`'s")) if (nrow(traits %>% dplyr::filter(!is.na(.data$trait_name))) > 0) { # Test build dataset - expect_no_error( + test_expect_no_error( dataset <- test_build_dataset( file.path(path_data, dataset_id, "metadata.yml"), file.path(path_data, dataset_id, "data.csv"), @@ -1149,11 +849,11 @@ dataset_test_worker <- info = sprintf("%s\tbuilding dataset", red(dataset_id))) ## Check that traits table is not empty - expect_false(nrow(dataset$traits) == 0, info = sprintf("%s\t`traits` table is empty", red(dataset_id))) + test_expect_false(nrow(dataset$traits) == 0, info = sprintf("%s\t`traits` table is empty", red(dataset_id))) ## Check that dataset can pivot wider if (nrow(dataset$traits) > 0) { - expect_true( + test_expect_true( dataset %>% check_pivot_wider(), info = sprintf("%s\tduplicate rows detected; `traits` table cannot pivot wider", red(dataset_id)) ) @@ -1163,14 +863,14 @@ dataset_test_worker <- # Testing per study, not on all studies combined (is this ideal?) # I'm not testing whether the functions work as intended, just that they throw no error - expect_no_warning( + test_expect_no_warning( dataset_wider <- db_traits_pivot_wider(dataset$traits), info = paste0(red(dataset_id), "\t`db_traits_pivot_wider` threw a warning; duplicate rows detected") ) if (exists("dataset_wider")) { - expect_no_warning( - expect_no_error( + test_expect_no_warning( + test_expect_no_error( dataset_longer <- db_traits_pivot_longer(dataset_wider), info = paste0(red(dataset_id), "\t`db_traits_pivot_longer`")), info = paste0(red(dataset_id), "\t`db_traits_pivot_longer` threw a warning") diff --git a/tests/testthat/functions.R b/tests/testthat/functions.R deleted file mode 100644 index 3b821b4c..00000000 --- a/tests/testthat/functions.R +++ /dev/null @@ -1,160 +0,0 @@ - -# Are these functions supposed to be the same as those in testdata.R? - -expect_no_error <- function(object, regexp = NULL, ..., info = NULL, label = NULL) { - error <- tryCatch({ - object - NULL - }, error = function(e) { - e - }) - if (is.null(label)) - expect(is.null(error), sprintf("%s", paste(error$message, collapse = ", ")), info = info) - else - expect(is.null(error), sprintf("%s threw an error: %s", label, paste(error$message, collapse = ", ")), info = info) - invisible(NULL) -} - - -expect_unique <- function(object, info = NULL, label = NULL) { - x <- table(unlist(object)) - i <- x == 1 - comp <- testthat::compare(all(i), TRUE) - expect(comp$equal, - sprintf("%s - not unique: %s", info, paste(names(x)[!i], collapse = ", "))) - invisible(object) -} - - -expect_is_in <- function(object, expected, ..., info = NULL, label = NULL, - expected.label = NULL, na.rm = TRUE) { - - if (na.rm) - object <- object[!is.na(object)] - i <- object %in% expected - - comp <- compare(all(i), TRUE, ...) - expect( - comp$equal, - sprintf("%s - should not contain: %s", info, paste(object[!i], collapse = ", ")) - ) - - invisible(object) -} - - -expect_not_NA <- function(object, info = NULL, label = NULL) { - - i <- !is.na(object) - comp <- compare(all(i), TRUE) - expect(comp$equal, - sprintf("%s - object contains NAs", info)) - invisible(object) -} - - -expect_list <- function(data, info) { - expect_true("list" %in% class(data), info = info) -} - - -expect_list_names_valid <- function(data, info) { - expect_list(data, info) - expect_not_NA(names(data), info = info) -# expect_allowed_text(names(data), info = info) - expect_unique(names(data), info = info) -} - - -expect_named_list <- function(data, expected_names, info) { - expect_list_names_valid(data, info) - expect_named(data, expected_names, info = info) -} - - -expect_list_names_contain <- function(data, expected_names, info) { - expect_list_names_valid(data, info) - expect_is_in(names(data), expected_names, info = info) -} - - -expect_dataframe_valid <- function(data, info) { - expect_not_NA(colnames(data), info = info) -# expect_allowed_text(colnames(data), info = info) - expect_unique(colnames(data), info = info) - expect_true(is.data.frame(data), info = info) -} - - -expect_dataframe_named <- function(data, expected_colnames, info) { - # I think the ordering of naming currently matters, maybe we don't want that? - # Affected by what order fields are entered into the metadata - expect_dataframe_valid(data, info) - expect_named(data, expected_colnames, info = info) -} - - -test_build_dataset <- function( - path_metadata, path_data, info, definitions, unit_conversions, schema, resource_metadata, taxon_list) { - - # Test it builds with no errors - expect_no_error({ - build_config <- dataset_configure(path_metadata, definitions) - }, info = paste(info, "`dataset_configure`")) - - expect_no_error({ - build_dataset_raw <- dataset_process(path_data, build_config, schema, resource_metadata, unit_conversions) - }, info = paste(info, "`dataset_process`")) - - expect_no_error({ - build_dataset <- dataset_update_taxonomy(build_dataset_raw, taxon_list) - }, info = paste(info, "`dataset_update_taxonomy`")) - - test_structure(build_dataset, info, schema, definitions, single_dataset = TRUE) - - build_dataset -} - - -test_structure <- function( - data, info, schema, definitions, single_dataset = TRUE) { - - vars_austraits <- - schema$austraits$elements %>% names() - - vars_tables <- - vars_austraits %>% - subset(., !(. %in% c("dataset_id", "definitions", "schema", "sources", "metadata", "build_info", "taxonomic_updates", "taxa"))) - - # Test lists have the right objects - comparison <- vars_austraits - - expect_named_list(data, comparison, info = c(info, " - main elements")) - - # Test structure of tables - for (v in vars_tables) { - comparison <- schema$austraits$elements[[v]]$elements %>% names() - expect_dataframe_named(data[[v]], comparison, info = paste(info, "- structure of", v)) - } - - # Test that minimum expected columns are in taxa, taxonomic_updates tables - expect_contains(names(data[["taxa"]]), c("taxon_name", "taxon_rank")) - expect_contains(names(data[["taxonomic_updates"]]), c("dataset_id", "original_name", "aligned_name", "taxon_name", "taxonomic_resolution")) - - # Contains allowed traits - expect_is_in(data$traits$trait_name %>% unique(), definitions$elements %>% names(), info = paste("traits ", v)) -} - - -## A helper function to determine if this is being run as part of a test -is_testing_env <- function() { - # Calling scope - tb <- .traceback(x = 0) - - # Check if called in `testthat` or interactive - if (any(unlist(lapply(tb, function(x) any(grepl("test_env", x)))))) { - return(TRUE) - } else { - return(FALSE) - } -} diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index dfa392d1..e3468dac 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -1,2 +1,2 @@ library(traits.build) - source("functions.R") + library(crayon) diff --git a/tests/testthat/test-setup.R b/tests/testthat/test-setup.R index 2a8e5114..4412159d 100644 --- a/tests/testthat/test-setup.R +++ b/tests/testthat/test-setup.R @@ -74,8 +74,7 @@ test_that("`metadata_create_template` is working with simulated user input", { expect_no_error( test_metadata <- metadata_create_template( "Test_2022", - user_responses = user_responses), - label = "`metadata_create_template`" + user_responses = user_responses) ) # Test metadata exists with correct names @@ -238,8 +237,7 @@ test_that("`metadata_add_contexts` is working", { user_responses = list( var_in = var_in, categories = categories, - replace_needed = c("y", "n"))), - label = "`metadata_add_contexts`" + replace_needed = c("y", "n"))) ) expect_equal(lapply(x$contexts, "[[", "context_property") %>% unlist() %>% unique, "unknown") @@ -720,7 +718,7 @@ test_that("`build_setup_pipeline` is working", { testthat::test_that("`dataset_find_taxon` is working", { expect_silent(suppressMessages(austraits <- remake::make("test_name"))) taxon <- c("Acacia celsa", "Acronychia acidula", "Aleurites rockinghamensis", "Syzygium sayeri") - expect_no_error(x <- dataset_find_taxon(taxon, austraits), label = "`dataset_find_taxon`") + expect_no_error(x <- dataset_find_taxon(taxon, austraits)) expect_equal(unname(x[[4]]), "Test_2022") expect_equal(names(x[[4]]), "Syzygium sayeri") }) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index a7167612..0bdb15c7 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -69,7 +69,6 @@ test_that("`util_separate_and_sort` returns alphabetically sorted characters", { }) - test_that("testing env is working", { expect_true(is_testing_env()) }) diff --git a/tests/testthat/test-xamples.R b/tests/testthat/test-xamples.R index 786fb85f..c5885cfd 100644 --- a/tests/testthat/test-xamples.R +++ b/tests/testthat/test-xamples.R @@ -20,19 +20,17 @@ testthat::test_that("Test Dataset 1 builds correctly", { file.path(examples_dir, "Test_2023_1/metadata.yml"), file.path(examples_dir, "Test_2023_1/data.csv"), "Test Dataset 1", definitions, unit_conversions, schema, resource_metadata, taxon_list - ), - info = "Building Test Dataset 1") + )) # Expected output tables <- c("traits", "locations", "contexts", "methods", "excluded_data", "taxonomic_updates", "taxa", "contributors") - expect_no_error( + expect_no_error( expected_output <- purrr::map( - tables, ~read_csv(sprintf("examples/Test_2023_1/output/%s.csv", .x), col_types = cols(.default = "c"))), - info = "Reading in expected output tables" + tables, ~read_csv(sprintf("examples/Test_2023_1/output/%s.csv", .x), col_types = cols(.default = "c"))) ) # Todo: also load and test non-csv outputs names(expected_output) <- tables @@ -56,8 +54,7 @@ testthat::test_that("Test Dataset 2 builds correctly", { file.path(examples_dir, "Test_2023_2/metadata.yml"), file.path(examples_dir, "Test_2023_2/data.csv"), "Test Dataset 2", definitions, unit_conversions, schema, resource_metadata, taxon_list - ), - info = "Building Test Dataset 2") + )) # Expected output tables <- c("traits", "locations", "contexts", "methods", "excluded_data", @@ -65,8 +62,7 @@ testthat::test_that("Test Dataset 2 builds correctly", { expect_no_error( expected_output <- purrr::map( - tables, ~read_csv(sprintf("examples/Test_2023_2/output/%s.csv", .x), col_types = cols(.default = "c"))), - info = "Reading in expected output tables" + tables, ~read_csv(sprintf("examples/Test_2023_2/output/%s.csv", .x), col_types = cols(.default = "c"))) ) # Todo: also load and test non-csv outputs names(expected_output) <- tables @@ -90,8 +86,7 @@ testthat::test_that("Test Dataset 3 builds correctly", { file.path(examples_dir, "Test_2023_3/metadata.yml"), file.path(examples_dir, "Test_2023_3/data.csv"), "Test Dataset 3", definitions, unit_conversions, schema, resource_metadata, taxon_list - ), - info = "Building Test Dataset 3") + )) # Expected output tables <- c("traits", "locations", "contexts", "methods", "excluded_data", @@ -99,8 +94,7 @@ testthat::test_that("Test Dataset 3 builds correctly", { expect_no_error( expected_output <- purrr::map( - tables, ~read_csv(sprintf("examples/Test_2023_3/output/%s.csv", .x), col_types = cols(.default = "c"))), - info = "Reading in expected output tables" + tables, ~read_csv(sprintf("examples/Test_2023_3/output/%s.csv", .x), col_types = cols(.default = "c"))) ) # Todo: also load and test non-csv outputs names(expected_output) <- tables @@ -124,8 +118,7 @@ testthat::test_that("Test Dataset 4 builds correctly", { file.path(examples_dir, "Test_2023_4/metadata.yml"), file.path(examples_dir, "Test_2023_4/data.csv"), "Test Dataset 4", definitions, unit_conversions, schema, resource_metadata, taxon_list - ), - info = "Building Test Dataset 4") + )) # Expected output tables <- c("traits", "locations", "contexts", "methods", "excluded_data", @@ -133,8 +126,7 @@ testthat::test_that("Test Dataset 4 builds correctly", { expect_no_error( expected_output <- purrr::map( - tables, ~read_csv(sprintf("examples/Test_2023_4/output/%s.csv", .x), col_types = cols(.default = "c"))), - info = "Reading in expected output tables" + tables, ~read_csv(sprintf("examples/Test_2023_4/output/%s.csv", .x), col_types = cols(.default = "c"))) ) # Todo: also load and test non-csv outputs names(expected_output) <- tables @@ -187,8 +179,7 @@ testthat::test_that("Test Dataset 5 builds correctly", { file.path(examples_dir, "Test_2023_5/metadata.yml"), file.path(examples_dir, "Test_2023_5/data.csv"), "Test Dataset 5", definitions, unit_conversions, schema, resource_metadata, taxon_list - ), - info = "Building Test Dataset 5") + )) # Expected output tables <- c("traits", "locations", "contexts", "methods", "excluded_data", @@ -196,8 +187,7 @@ testthat::test_that("Test Dataset 5 builds correctly", { expect_no_error( expected_output <- purrr::map( - tables, ~read_csv(sprintf("examples/Test_2023_5/output/%s.csv", .x), col_types = cols(.default = "c"))), - info = "Reading in expected output tables" + tables, ~read_csv(sprintf("examples/Test_2023_5/output/%s.csv", .x), col_types = cols(.default = "c"))) ) # Todo: also load and test non-csv outputs names(expected_output) <- tables @@ -221,8 +211,7 @@ testthat::test_that("Test Dataset 6 builds correctly", { file.path(examples_dir, "Test_2023_6/metadata.yml"), file.path(examples_dir, "Test_2023_6/data.csv"), "Test Dataset 6", definitions, unit_conversions, schema, resource_metadata, taxon_list - ), - info = "Building Test Dataset 6") + )) # Expected output tables <- c("traits", "locations", "contexts", "methods", "excluded_data", @@ -230,8 +219,7 @@ testthat::test_that("Test Dataset 6 builds correctly", { expect_no_error( expected_output <- purrr::map( - tables, ~read_csv(sprintf("examples/Test_2023_6/output/%s.csv", .x), col_types = cols(.default = "c"))), - info = "Reading in expected output tables" + tables, ~read_csv(sprintf("examples/Test_2023_6/output/%s.csv", .x), col_types = cols(.default = "c"))) ) # Todo: also load and test non-csv outputs names(expected_output) <- tables @@ -255,8 +243,7 @@ testthat::test_that("Test Dataset 7 builds correctly", { file.path(examples_dir, "Test_2023_7/metadata.yml"), file.path(examples_dir, "Test_2023_7/data.csv"), "Test Dataset 7", definitions, unit_conversions, schema, resource_metadata, taxon_list - ), - info = "Building Test Dataset 7") + )) # Expected output tables <- c("traits", "locations", "contexts", "methods", "excluded_data", @@ -264,8 +251,7 @@ testthat::test_that("Test Dataset 7 builds correctly", { expect_no_error( expected_output <- purrr::map( - tables, ~read_csv(sprintf("examples/Test_2023_7/output/%s.csv", .x), col_types = cols(.default = "c"))), - info = "Reading in expected output tables" + tables, ~read_csv(sprintf("examples/Test_2023_7/output/%s.csv", .x), col_types = cols(.default = "c"))) ) # Todo: also load and test non-csv outputs names(expected_output) <- tables @@ -289,8 +275,7 @@ testthat::test_that("Test Dataset 8 builds correctly", { file.path(examples_dir, "Test_2023_8/metadata.yml"), file.path(examples_dir, "Test_2023_8/data.csv"), "Test Dataset 8", definitions, unit_conversions, schema, resource_metadata, taxon_list - ), - info = "Building Test Dataset 8") + )) # Expected output tables <- c("traits", "locations", "contexts", "methods", "excluded_data", @@ -298,8 +283,7 @@ testthat::test_that("Test Dataset 8 builds correctly", { expect_no_error( expected_output <- purrr::map( - tables, ~read_csv(sprintf("examples/Test_2023_8/output/%s.csv", .x), col_types = cols(.default = "c"))), - info = "Reading in expected output tables" + tables, ~read_csv(sprintf("examples/Test_2023_8/output/%s.csv", .x), col_types = cols(.default = "c"))) ) # Todo: also load and test non-csv outputs names(expected_output) <- tables