tests/testthat/test-galah_identify.R

# Warning generated by `galah_identify()` about empty query
test_that("galah_identify returns an empty tibble when no args provided", {
  expect_warning({result <- galah_identify()})
  expect_equal(nrow(result), 0)
  expect_warning(galah_identify(), "No query passed")
})

test_that("galah_identify runs a search when given a string", {
  result <- galah_identify("Litoria peronii")
  expect_equal(nrow(result), 1)
})

test_that("galah_identify runs a search on multiple strings", {
  result <- galah_identify("amphibia", "reptilia", "aves", "mammalia")
  expect_equal(nrow(result), 4)
})

test_that("galah_identify runs a search on multiple strings wrapped by c()", {
  search_terms <- c("amphibia", "reptilia", "aves", "mammalia")
  result <- galah_identify(search_terms)
  expect_equal(nrow(result), 4)
})

test_that("galah_identify pipes correctly", {
  result <- galah_call() |>
    galah_identify("Litoria") |>
    galah_filter(year == 2020)
  expect_false(is.null(result$identify))
  expect_false(is.null(result$filter))
})

test_that("galah_identify pipes correctly when taxa are partially returned", {
  galah_config(verbose = FALSE)
  result <- galah_call() |>
    galah_identify("Litoria", "blarghy") |>
    galah_filter(year == 2020)
  expect_false(is.null(result$identify))
  expect_false(is.null(result$filter))
  galah_config(verbose = TRUE)
})

test_that("galah_identify warns when taxa are partially returned", {
  skip_if_offline()
  expect_message(
    galah_call() |>
      galah_identify("Litoria", "blarghy") |>
      galah_filter(year == 2020) |>
      count() |>
      compute(),
    cli::cli_text("Matched {.bold 1 of 2}")
  )
})

test_that("galah_identify warns when identifiers are partially returned", {
  skip_if_offline()
  galah_config(run_checks = TRUE)
  ids <- c("https://biodiversity.org.au/afd/taxa/0df99ece-1982-4605-a2b3-4fcb7660ee2b",
           "https://id.biodiversity.org.au/node/apni/2910467",
           "https://id.biodiversity.org.au/node/apni/291047") # wrong id
  expect_message(
    galah_call() |>
      galah_identify(ids) |>
      galah_filter(year == 2020) |>
      count() |>
      compute(),
    cli::cli_text("Matched {.bold 2 of 3}")
  )
  expect_no_warning(
    galah_identify(ids)
  )
})

test_that("galah_identify truncates unmatched list of taxa at 3 ", {
  skip_if_offline()
  expect_message(
    galah_call() |>
      galah_identify("Litoria", "blarghy", "blorp", "florp", "skorp") |>
      galah_filter(year == 2020) |>
      count() |>
      compute(),
    c(
      cli::cli_text("Matched {.bold 1 of 5} taxonomic search terms in selected atlas (Australia)."),
      "!" = cli::cli_text("{.yellow 4 unmatched search term:}"),
      cli::cli_text(format_error_bullets(c("{.yellow \"blarghy\", \"blorp\", \"florp\" + 1 more}")))
    )
  )
})

test_that("galah_identify errors for deprecated `search = FALSE` argument", {
  skip_if_offline()
  galah_config(run_checks = TRUE)
  ids <- c("https://biodiversity.org.au/afd/taxa/0df99ece-1982-4605-a2b3-4fcb7660ee2b",
           "https://id.biodiversity.org.au/node/apni/2910467",
           "https://id.biodiversity.org.au/node/apni/291047", 
           "fred",
           "bort") # wrong id
  expect_error(
    galah_call() |>
      galah_identify(ids, search = FALSE) |>
      galah_filter(year == 2020) |>
      count() |>
      collapse())
})

test_that("galah_identify warns for deprecated `search = TRUE` argument", {
  skip_if_offline()
  galah_config(run_checks = TRUE)
  ids <- c("Litoria", "Crinia")
  expect_warning(
    galah_call() |>
      galah_identify(ids, search = TRUE) |>
      galah_filter(year == 2020) |>
      count() |>
      collapse())
})

## NOTE: Not certain if this is a necessary test
# cli::test_that_cli("Partial taxonomic match message theming", {
#   testthat::local_edition(3)
#   testthat::expect_snapshot(local({
#     cli::cli_div(theme = list(span.bold = list("font-weight" = "bold"),
#                               span.yellow = list(color = "yellow")))
#     c(
#       cli::cli_text("Matched {.bold 2 of 3} taxonomic search terms in selected atlas (Australia)."),
#       "!" = cli::cli_text("{.yellow 1 unmatched search term:}"),
#       cli::cli_text(format_error_bullets(c("{.yellow \"https://id.biodiversity.org.au/node/apni/291047\"}")))
#     )
#   }))
# })

Try the galah package in your browser

Any scripts or data that you put into this service are public.

galah documentation built on Nov. 20, 2023, 9:07 a.m.