Skip to content

Commit

Permalink
additional tests (#132)
Browse files Browse the repository at this point in the history
Add tests for parts of functions not being tested
  • Loading branch information
ehwenk authored Nov 18, 2024
1 parent f379fff commit 32b5177
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion R/join_.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-as_wide_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
8 changes: 8 additions & 0 deletions tests/testthat/test-extract_.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}

Expand Down Expand Up @@ -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)
Expand Down
24 changes: 23 additions & 1 deletion tests/testthat/test-flatten_database.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 32b5177

Please sign in to comment.