tests/testthat/test-taxa-tree.R

# These tests hit the Open Tree of Life, NCBI and Fish Tree of Life web
# services. They are skipped on CRAN and when offline so that
# `R CMD check --as-cran` passes without network access, per CRAN policy.

test_that("input validation rejects too-few names without touching the network", {
  expect_error(taxa.tree(character(0)), "at least two")
  expect_error(taxa.tree("Canis lupus"), "at least two")
  expect_error(taxa.tree(c("", NA)), "at least two")
})

test_that("downto.tree validates the taxon argument locally", {
  expect_error(downto.tree(c("a", "b"), "species"), "single non-empty")
  expect_error(downto.tree("", "species"), "single non-empty")
})

test_that("source and downto.tree source are validated by match.arg", {
  expect_error(taxa.tree(c("a", "b"), source = "nope"), "should be one of")
  expect_error(downto.tree("Cervidae", "species", source = "nope"),
               "should be one of")
})

test_that("otl backend splits a mixed list into one tree per phylum", {
  testthat::skip_on_cran()
  testthat::skip_if_offline()
  skip_if_not_installed("rotl")

  res <- taxa.tree(c("Canis lupus", "Canis latrans", "Vulpes vulpes",
                     "Acer saccharum", "Acer rubrum", "Betula alleghaniensis"),
                   plot = FALSE, verbose = FALSE)

  expect_type(res$trees, "list")
  # Chordata (the canids) and Streptophyta (the plants) must be separate.
  expect_gte(length(res$trees), 2L)

  for (grp in res$trees) {
    expect_s3_class(grp$tree, "phylo")
    expect_true(is.matrix(grp$dist))
    expect_identical(nrow(grp$dist), length(grp$tree$tip.label))
  }
})

test_that("fish backend returns a single dated Actinopterygii tree", {
  testthat::skip_on_cran()
  testthat::skip_if_offline()
  skip_if_not_installed("fishtree")

  res <- taxa.tree(c("Thunnus thynnus", "Gadus morhua", "Danio rerio",
                     "Salmo salar"),
                   source = "fish", plot = FALSE, verbose = FALSE)

  expect_named(res$trees, "Actinopterygii")
  tr <- res$trees$Actinopterygii$tree
  expect_s3_class(tr, "phylo")
  # Chronogram => real branch lengths, so patristic distances are non-trivial.
  expect_false(is.null(tr$edge.length))
})

Try the aptg package in your browser

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

aptg documentation built on July 19, 2026, 9:07 a.m.