From 7088c1314974f9195c58b32bb30fcfdf5f947d4d Mon Sep 17 00:00:00 2001 From: Luna Sare Date: Mon, 8 Jan 2024 11:41:38 -0800 Subject: [PATCH] Fix couple of issues with summarizing queried taxon presence in datelife result: when cleaned names have spaces instead of underscores and when datelife_result is empty (no matches). --- NEWS.md | 1 + R/datelife_summary.R | 5 ++-- tests/testthat/test_main.R | 47 +++++++++++++++++++++++++------------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/NEWS.md b/NEWS.md index adbfe6c..2add30c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,7 @@ DONE: - faster and more accurate way to get study ids and tree ids from opentree API on `get_opentree_chronograms()` that have branch lengths in Myrs, no relative time. - update to chronogram database, now with 292 chronograms. - `get_taxon_summary()` now manages case when `datelife_result` is empty; throws warning instead of criptic error. +- better `testthat` suite for `datelife_search()` inner functions. # datelife v0.6.8 diff --git a/R/datelife_summary.R b/R/datelife_summary.R index c84012f..e2a7fbb 100644 --- a/R/datelife_summary.R +++ b/R/datelife_summary.R @@ -32,7 +32,7 @@ get_taxon_summary <- function(datelife_result = NULL, message("Taxon summary can not be generated.") return(NA) } - if (length(datelife_result == 0)) { + if (length(datelife_result) == 0) { warning("'datelife_result' is empty (length == 0).") message("Taxon summary can not be generated.") return(NA) @@ -42,7 +42,8 @@ get_taxon_summary <- function(datelife_result = NULL, cleaned_names <- gsub(" ", "_", datelife_query$cleaned_names) } else { input <- attributes(datelife_result)$datelife_query - cleaned_names <- gsub(" ", "_", input$cleaned_names) + input$cleaned_names <- gsub(" ", "_", input$cleaned_names) + cleaned_names <- input$cleaned_names } } else { message("'datelife_query' argument was not provided.") diff --git a/tests/testthat/test_main.R b/tests/testthat/test_main.R index d3525f3..602a142 100644 --- a/tests/testthat/test_main.R +++ b/tests/testthat/test_main.R @@ -1,22 +1,25 @@ test_that("a known datelife_search run works as it should", { - datelife_query <- make_datelife_query(input = c("Delphinus_delphis", - "Gallus gallus", - "elephas Maximus", - "felis_catus", - "homo-sapiens")) + expect_no_error( + datelife_query <- make_datelife_query( + input = c("Delphinus_delphis", + "Gallus gallus", + "elephas Maximus", + "felis_catus", + "homo-sapiens")) + ) - datelifeSearch <- datelife_search(datelife_query, - summary_format = "phylo_median") - - datelife_result <- get_datelife_result_datelifequery( - datelife_query = datelife_query, - partial = TRUE, - cache = "opentree_chronograms") + expect_no_error( + datelife_result <- get_datelife_result_datelifequery( + datelife_query = datelife_query, + partial = TRUE, + cache = "opentree_chronograms") + ) expect_true(length(datelife_result) > 0) - res <- summarize_datelife_result( + expect_no_error( + res <- summarize_datelife_result( datelife_result = datelife_result, datelife_query = datelife_query, summary_format = "phylo_median", @@ -24,12 +27,22 @@ test_that("a known datelife_search run works as it should", { summary_print = c("citations", "taxa"), taxon_summary = c("none", "summary", "matrix"), criterion = "taxa") + ) - taxon_summ <- get_taxon_summary( + expect_no_error( + taxon_summ <- get_taxon_summary( datelife_result = datelife_result, datelife_query = datelife_query) + ) + + expect_no_error( + datelifeSearch <- datelife_search( + datelife_query, + summary_format = "phylo_median") + ) }) +########################################## test_that("datelife_use workflows work", { du <- datelife_use( input = "Rhea americana, Struthio camelus, Gallus gallus", @@ -78,6 +91,7 @@ test_that("datelife_use workflows work", { input_process(input = du0) }) +############################################################################# test_that("input processing a newick string and multiPhylo workflows work", { newick <- "(Gallus_gallus,(Rhea_americana,Struthio_camelus)Palaeognathae)Aves;" phylo <- input_process(newick) @@ -94,16 +108,17 @@ test_that("input processing a newick string and multiPhylo workflows work", { expect_warning(match_all_calibrations(phy = phylo, calibrations = calibs)) }) +################################# test_that("object checks work", { expect_warning(match_all_calibrations(phy = NULL)) expect_error(use_calibrations(phy = NULL)) expect_error(get_calibrations_datelifequery(datelife_query = NULL)) }) - +############################################### test_that("you can load opentree_chronograms",{ data(opentree_chronograms, package = "datelife") - expect_equal(ls(opentree_chronograms), c("authors","curators","dois","studies","trees")) + expect_true(all(c("authors","curators","dois","studies","trees") %in% ls(opentree_chronograms))) })