tests/testthat/test-region-tree.R

# region.tree(): the network-dependent path (GBIF) is exercised only when
# online and not on CRAN. The argument handling, the Canadian GADM lookup and
# the WKT circle geometry are deterministic and tested offline.

test_that("region.tree validates arguments locally", {
  expect_error(region.tree(), "required")                       # no taxon
  expect_error(region.tree("Aves"), "Provide a location")       # no area
  expect_error(
    region.tree("Aves", lat = 45, lon = -73, radius_km = 10, province = "QC"),
    "not both"                                                   # both modes
  )
  expect_error(region.tree("Aves", lat = 45, lon = -73), "all of lat")
})

test_that("Canadian province lookup maps names and codes to GADM level-1 GIDs", {
  expect_identical(.aptg_gadm_gid(province = "Alberta"), "CAN.1_1")
  expect_identical(.aptg_gadm_gid(province = "BC"), "CAN.2_1")
  expect_identical(.aptg_gadm_gid(province = "Quebec"), "CAN.11_1")
  expect_identical(.aptg_gadm_gid(province = "Québec"), "CAN.11_1") # accent
  expect_identical(.aptg_gadm_gid(province = "Nunavut"), "CAN.8_1")
  expect_identical(.aptg_gadm_gid(province = "NWT"), "CAN.6_1")
  expect_identical(.aptg_gadm_gid(province = "yukon"), "CAN.13_1")

  # All 13 provinces/territories resolve, to 13 distinct GIDs.
  provs <- c("AB", "BC", "MB", "NB", "NL", "NT", "NS", "NU", "ON", "PE",
             "QC", "SK", "YT")
  gids <- vapply(provs, function(p) .aptg_gadm_gid(province = p), character(1))
  expect_length(unique(gids), 13L)

  # A raw GID passes through untouched; unknown names error.
  expect_identical(.aptg_gadm_gid(gadm = "USA.5_1"), "USA.5_1")
  expect_error(.aptg_gadm_gid(province = "Atlantis"), "Unknown")
})

test_that("WKT circle is a closed, well-formed polygon", {
  skip_if_not_installed("geosphere")
  wkt <- .aptg_wkt_circle(lat = 45.5, lon = -73.57, radius_km = 50, n = 32L)
  expect_match(wkt, "^POLYGON\\(\\(")
  coords <- strsplit(sub("^POLYGON\\(\\((.*)\\)\\)$", "\\1", wkt), ", ")[[1]]
  # n + 1 vertices, and the ring is closed (first == last).
  expect_length(coords, 33L)
  expect_identical(coords[[1]], coords[[length(coords)]])
})

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.