Skip to content

Commit

Permalink
Update syntax to search_taxa (#112)
Browse files Browse the repository at this point in the history
* Also update test file name
  • Loading branch information
daxkellie committed Dec 15, 2021
1 parent 38debdd commit 933844e
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 136 deletions.
6 changes: 3 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

S3method(atlas_counts,data_request)
S3method(atlas_counts,default)
S3method(select_taxa,data_request)
S3method(select_taxa,default)
S3method(search_taxa,data_request)
S3method(search_taxa,default)
export(ala_citation)
export(ala_config)
export(ala_counts)
Expand Down Expand Up @@ -32,10 +32,10 @@ export(galah_geolocate)
export(galah_group_by)
export(galah_select)
export(search_fields)
export(search_taxa)
export(select_columns)
export(select_filters)
export(select_locations)
export(select_taxa)
import(assertthat)
import(httr)
import(sf)
Expand Down
28 changes: 14 additions & 14 deletions R/select_taxa.R → R/search_taxa.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' In the ALA, all records are associated with an identifier that uniquely
#' identifies the taxon to which that record belongs. However, taxonomic names
#' are often ambiguous due to homonymy; i.e. re-use of names (common or
#' scientific) in different clades. Hence, \code{select_taxa} provides a means
#' scientific) in different clades. Hence, \code{search_taxa} provides a means
#' to search for taxonomic names and check the results are 'correct' before
#' proceeded to download data via \code{\link{ala_occurrences}()},
#' \code{\link{ala_species}()} or \code{\link{ala_counts}()}. The resulting
Expand All @@ -27,42 +27,42 @@
#' @examples
#' \dontrun{
#' # Search using a single term
#' select_taxa("Reptilia")
#' search_taxa("Reptilia")
#' # or equivalently:
#' select_taxa("reptilia") # not case sensitive
#' search_taxa("reptilia") # not case sensitive
#'
#' # Search using an unique taxon identifier
#' select_taxa(query = "https://id.biodiversity.org.au/node/apni/2914510")
#' search_taxa(query = "https://id.biodiversity.org.au/node/apni/2914510")
#'
#' # Search multiple taxa
#' select_taxa(c("reptilia", "mammalia")) # returns one row per taxon
#' search_taxa(c("reptilia", "mammalia")) # returns one row per taxon
#' }
#' @export
select_taxa <- function(...) {
UseMethod("select_taxa")
search_taxa <- function(...) {
UseMethod("search_taxa")
}

#' @export
#' @rdname select_taxa
select_taxa.data_request <- function(request, ...) {
request$taxa <- do.call(select_taxa, merge_args(request, list(...)))
#' @rdname search_taxa
search_taxa.data_request <- function(request, ...) {
request$taxa <- do.call(search_taxa, merge_args(request, list(...)))
return(request)
}

#' @export
#' @rdname select_taxa
select_taxa.default <- function(query, is_id = FALSE) {
#' @rdname search_taxa
search_taxa.default <- function(query, is_id = FALSE) {
assert_that(is.logical(is_id))
if(missing(is_id)){is_id <- FALSE}
verbose <- getOption("galah_config")$verbose

if (getOption("galah_config")$atlas != "Australia") {
stop("`select_taxa` only provides information on Australian taxonomy. To search taxonomy for ",
stop("`search_taxa` only provides information on Australian taxonomy. To search taxonomy for ",
getOption("galah_config")$atlas, " use `taxize`. See vignette('international_atlases') for more information")
}

if (missing(query)) {
stop("`select_taxa` requires a query to search for")
stop("`search_taxa` requires a query to search for")
}

if (is_id) {
Expand Down
6 changes: 3 additions & 3 deletions man/atlas_counts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions man/select_taxa.Rd → man/search_taxa.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions tests/testthat/test-search_taxa.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
context("Taxa search")

test_that("search_taxa checks inputs", {
expect_error(search_taxa())
expect_warning( #FIXME: There are no warnings, just an error. Did this change since adding ala_taxonomy()?
expect_error(search_taxa("Varanus varius", children = "false")))
})

test_that("search_taxa check atlas", {
galah_config(atlas = "Austria")
expect_error(search_taxa("Vulpes vulpes"))
galah_config(atlas = "Australia")
})


test_that("search_taxa works for simple queries", {
vcr::use_cassette("search_taxa_simple", {
taxa <- search_taxa("Microseris lanceolata")
})
expect_equal(nrow(taxa), 1)
})


test_that("search_taxa works for multiple queries", {
vcr::use_cassette("search_taxa_multiple", {
taxa <- search_taxa(c("Eucalyptus", "Banksia", "Acacia"))
})
expect_equal(nrow(taxa), 3)
})


test_that("search_taxa handles data.frame input", {
vcr::use_cassette("search_taxa_df", {
taxa <- search_taxa(
data.frame(genus = c("Banksia", "Microseris"), kingdom = "Plantae"))
})
expect_equal(nrow(taxa), 2)
})

vcr::use_cassette("search_taxa_valid_invalid", {
test_that("search_taxa handles a mix of valid and invalid queries", {
expect_message(taxa <- search_taxa(c("Eucalyptus", "Banksia", "Wattle")))
expect_equal(nrow(taxa), 3)
})
})

vcr::use_cassette("search_taxa_invalid", {
test_that("search_taxa gives a message for invalid ids", {
# unrecognised name
expect_message(search_taxa("bad_term"))
# unrecognised id
expect_message(search_taxa("1234", is_id = TRUE))
})
})

vcr::use_cassette("search_taxa_extended", { # FIXME: search_taxa() fails - search_taxa no longer accepts these arguments
test_that("search_taxa uses additional arguments", {
expect_warning(
taxa <- search_taxa("Anas", all_ranks = TRUE, children = TRUE,
counts = TRUE)
)
expect_true("subfamily" %in% names(taxa))
expect_gt(nrow(taxa), 1)
expect_true("count" %in% names(taxa))
})
})


test_that("child_concepts behaves correctly", {
vcr::use_cassette("child_concepts", {
id <- "urn:lsid:biodiversity.org.au:afd.taxon:e4c87583-08ed-4183-8653-c8f487a93735"
children <- child_concepts(id)
}, preserve_exact_body_bytes = TRUE)

expect_s3_class(children, "data.frame")
expect_equal(nrow(children), 1)
})


test_that("search_taxa searches using multiple ranks", {
vcr::use_cassette("search_taxa_rank_search", {
taxa <- search_taxa(list(genus = "Acacia", kingdom = "Plantae"))
})
expect_s3_class(taxa, "data.frame")
expect_equal(taxa$rank, "genus")
expect_equal(nrow(taxa), 1)
})


test_that("search_taxa searches using identifier", {
vcr::use_cassette("search_taxa_id_search", {
# check different types of id
query <- c("urn:lsid:biodiversity.org.au:afd.taxon:08b9a1f0-62ae-45ca-9208-e773b00021ed",
"NZOR-6-1742", "https://id.biodiversity.org.au/node/apni/2910467")
taxa <- search_taxa(query)
})
expect_s3_class(taxa, "data.frame")
expect_equal(nrow(taxa), 3)
})

test_that("search_taxa handles name issues", {
expect_warning(search_taxa("Microseris"))
})
103 changes: 0 additions & 103 deletions tests/testthat/test-select_taxa.R

This file was deleted.

0 comments on commit 933844e

Please sign in to comment.