protomapr/tests/testthat/test-addProtomaps.R
2026-03-06 15:46:39 +11:00

74 lines
2.2 KiB
R

test_that("protomaps_url requires API key", {
# Clear any existing env var
old_key <- Sys.getenv("PROTOMAPS_API_KEY")
Sys.unsetenv("PROTOMAPS_API_KEY")
on.exit(Sys.setenv(PROTOMAPS_API_KEY = old_key))
expect_error(protomaps_url(), "API key required")
})
test_that("protomaps_url accepts direct API key", {
url <- protomaps_url(api_key = "test-key")
expect_true(grepl("api.protomaps.com", url))
expect_true(grepl("\\{z\\}/\\{x\\}/\\{y\\}", url))
expect_true(grepl("key=test-key", url))
})
test_that("protomaps_url uses environment variable", {
old_key <- Sys.getenv("PROTOMAPS_API_KEY")
Sys.setenv(PROTOMAPS_API_KEY = "env-test-key")
on.exit(Sys.setenv(PROTOMAPS_API_KEY = old_key))
url <- protomaps_url()
expect_true(grepl("key=env-test-key", url))
})
test_that("set_protomaps_key sets environment variable", {
old_key <- Sys.getenv("PROTOMAPS_API_KEY")
on.exit(Sys.setenv(PROTOMAPS_API_KEY = old_key))
expect_message(set_protomaps_key("my-key"), "API key set")
expect_equal(Sys.getenv("PROTOMAPS_API_KEY"), "my-key")
})
test_that("protomaps_demo_url is deprecated", {
expect_warning(protomaps_demo_url(api_key = "test"), "deprecated")
})
test_that("protomapsOptions creates correct structure", {
opts <- protomapsOptions(maxDataZoom = 14, tileSize = 512)
expect_equal(opts$maxDataZoom, 14)
expect_equal(opts$tileSize, 512)
})
test_that("protomapsOptions handles debug mode", {
opts <- protomapsOptions(debug = TRUE)
expect_true(opts$debug)
})
test_that("protomapsDependency returns htmlDependency", {
dep <- protomapsDependency()
expect_s3_class(dep, "html_dependency")
expect_equal(dep$name, "protomaps-leaflet")
})
test_that("addProtomaps returns modified leaflet map", {
skip_if_not_installed("leaflet")
map <- leaflet::leaflet()
# Use mock URL to avoid API key requirement
result <- addProtomaps(map, url = "https://example.com/tiles.pmtiles")
expect_s3_class(result, "leaflet")
})
test_that("addProtomaps validates flavor argument", {
skip_if_not_installed("leaflet")
map <- leaflet::leaflet()
expect_error(
addProtomaps(map, url = "https://example.com/tiles.pmtiles", flavor = "invalid"),
"should be one of"
)
})