From 32b51771858ad37701d9caaf3dd2a65d833923cf Mon Sep 17 00:00:00 2001 From: Elizabeth Wenk Date: Mon, 18 Nov 2024 17:19:45 +1100 Subject: [PATCH] additional tests (#132) Add tests for parts of functions not being tested --- R/join_.R | 2 +- tests/testthat/test-as_wide_table.R | 16 ++++++++++++++++ tests/testthat/test-extract_.R | 8 ++++++++ tests/testthat/test-flatten_database.R | 24 +++++++++++++++++++++++- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/R/join_.R b/R/join_.R index 729a628..bcab06d 100644 --- a/R/join_.R +++ b/R/join_.R @@ -280,7 +280,7 @@ join_contributors <- function(database, compacted_contributors_column <- contributors_tmp %>% - tidyr::nest(-dplyr::all_of("dataset_id")) %>% + tidyr::nest(data = -dplyr::all_of(c("dataset_id"))) %>% dplyr::mutate(data_contributors = purrr::map_chr(data, jsonlite::toJSON)) %>% dplyr::select(-dplyr::any_of("data")) %>% dplyr::ungroup() diff --git a/tests/testthat/test-as_wide_table.R b/tests/testthat/test-as_wide_table.R index 6743310..b6d3449 100644 --- a/tests/testthat/test-as_wide_table.R +++ b/tests/testthat/test-as_wide_table.R @@ -10,3 +10,19 @@ test_that("Function is working", { }) expect_error(as_wide_table()) + + +test_that("old versions will complain", { + expect_error(as_wide_table(austraits_3.0.2_lite)) + expect_error(as_wide_table(austraits_4.2.0_lite)) + expect_error(join_contexts_old(austraits_3.0.2_lite)) + expect_error(join_contexts_old(austraits_4.2.0_lite)) + }) + + +test_that("function `collapse_cols` works", { + data <- (austraits_5.0.0_lite %>% extract_dataset("Bloomfield_2018"))$locations + data_collapsed <- (data %>% collapse_cols())[[1]] + expect_equal(stringr::str_count(data_collapsed, "="), 5) + expect_equal(stringr::str_count(data_collapsed, "; "), 4) +}) diff --git a/tests/testthat/test-extract_.R b/tests/testthat/test-extract_.R index ac04062..c184c2c 100644 --- a/tests/testthat/test-extract_.R +++ b/tests/testthat/test-extract_.R @@ -17,6 +17,8 @@ test_extract_error <- function(austraits){ expect_error(austraits %>% extract_taxa()) expect_error(austraits %>% extract_dataset()) expect_error(austraits %>% extract_trait()) + expect_error(austraits_3.0.2_lite %>% extract_data()) + expect_error(austraits_4.2.0_lite %>% extract_data()) }) } @@ -188,6 +190,12 @@ test_that("Extraction of dataset was successful", { expect_equal(1, dplyr::n_distinct(trait_subset$traits$trait_name)) }) +test_that("Expect error if taxon_name column missing", { + traits_without_taxon_name <- extract_dataset(austraits_5.0.0_lite, dataset_id = dataset_id) + traits_without_taxon_name$traits <- traits_without_taxon_name$traits %>% dplyr::select(-taxon_name) + expect_error((traits_without_taxon_name %>% extract_trait("leaf_area"))$traits) +}) + test_that("Extraction of dataset was successful using `extract_data`", { subset <- extract_data(database = austraits_5.0.0_lite, table = "traits", col = "dataset_id", col_value = dataset_id) trait_subset <- extract_data(database = austraits_5.0.0_lite, table = "traits", col = "trait_name", col_value = trait_name) diff --git a/tests/testthat/test-flatten_database.R b/tests/testthat/test-flatten_database.R index 56792f1..b944883 100644 --- a/tests/testthat/test-flatten_database.R +++ b/tests/testthat/test-flatten_database.R @@ -8,7 +8,6 @@ combined_table <- flatten_database(database) # Crous_2013 has many context properties, from 4 of 5 possible categories dataset_id_2 <- "Crous_2013" database_2 <- extract_dataset(austraits_5.0.0_lite, dataset_id_2) -combined_table_2 <- flatten_database(database_2) #expected_output <- readr::read_csv("tests/testthat/Falster_2003_combined_format.csv", show_col_types = FALSE) expected_output <- readr::read_csv("Falster_2003_combined_format.csv", show_col_types = FALSE) @@ -21,6 +20,27 @@ test_that("`flatten_database` is working with format = single_column_pretty", { expect_true(stringr::str_detect(combined_table$data_contributors[1], "<")) }) +database_3 <- extract_dataset(austraits_5.0.0_lite, "Bloomfield_2018") +combined_table_3a <- flatten_database(database_3, format = "single_column_pretty") +combined_table_3b <- flatten_database(database_3, format = "many_columns") + +test_that("`flatten_database` defaults to `single_column_pretty` for contributors", { + expect_equal(combined_table_3a$data_contributors, combined_table_3b$data_contributors) +}) + +# test that join_location_coordinates works as intended + +database_no_coord <- austraits_5.0.0_lite %>% extract_dataset("Kooyman_2011") + +test_that("`join_location_coordinate` works as intended", { + +expect_contains(names(combined_table), c("latitude (deg)", "longitude (deg)")) # proper columns present in full combined table +expect_contains(names(join_location_coordinates(database)$traits), c("latitude (deg)", "longitude (deg)")) # proper columns present when just `join_location_coordinate` used +expect_contains(names(join_location_coordinates(database_no_coord)$traits), c("latitude (deg)", "longitude (deg)")) # proper columns present if dataset without location coordinates is used + +}) + + # test different packing formats for locations location_vars <- (database$locations %>% dplyr::distinct(location_property))$location_property @@ -51,12 +71,14 @@ test_that("`join_locations` is working with different formats, vars", { contributors_no_ORCID <- (database %>% join_contributors(vars = c("affiliation", "additional_role")))$traits contributors_with_ORCID <- (database %>% join_contributors(vars = "all"))$traits contributors_default <- (database %>% join_contributors())$traits +contributors_json <- (database %>% join_contributors(format = "single_column_json"))$traits test_that("`join_contributors` is working with vars options", { expect_equal(length(contributors_no_ORCID), length(contributors_default)) expect_equal(contributors_with_ORCID, contributors_default) expect_true(stringr::str_detect(contributors_with_ORCID$data_contributors[3], "ORCID")) expect_false(stringr::str_detect(contributors_no_ORCID$data_contributors[3], "ORCID")) + expect_equal(stringr::str_extract(contributors_json$data_contributors[[1]], "^[:punct:]+"), "[{\"") }) # test different packing formats & `include_description` for contexts