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

65 lines
1.7 KiB
R

test_that("protomaps_sample_tiles validates region", {
expect_error(
protomaps_sample_tiles(region = "invalid"),
"sf-bay"
)
})
test_that("protomaps_sample_tiles uses cached file", {
temp_dir <- tempdir()
temp_file <- file.path(temp_dir, "protomapr-sample-sf-bay.pmtiles")
# Create fake cached file
writeLines("test", temp_file)
expect_message(
result <- protomaps_sample_tiles(cache_dir = temp_dir),
"Using cached"
)
expect_equal(result, temp_file)
# Cleanup
unlink(temp_file)
})
test_that("protomaps_clear_cache handles missing directory", {
expect_message(
protomaps_clear_cache(cache_dir = "/nonexistent/path/12345"),
"does not exist"
)
})
test_that("protomaps_clear_cache handles empty cache", {
temp_dir <- tempdir()
cache_subdir <- file.path(temp_dir, "protomapr_test_cache")
dir.create(cache_subdir, showWarnings = FALSE)
expect_message(
protomaps_clear_cache(cache_dir = cache_subdir),
"No cached tiles"
)
unlink(cache_subdir, recursive = TRUE)
})
test_that("protomaps_clear_cache removes pmtiles files", {
temp_dir <- tempdir()
cache_subdir <- file.path(temp_dir, "protomapr_test_cache2")
dir.create(cache_subdir, showWarnings = FALSE)
# Create fake cached files
writeLines("test1", file.path(cache_subdir, "test1.pmtiles"))
writeLines("test2", file.path(cache_subdir, "test2.pmtiles"))
writeLines("keep", file.path(cache_subdir, "other.txt"))
result <- expect_message(
protomaps_clear_cache(cache_dir = cache_subdir),
"Removed 2"
)
# Check pmtiles removed but other file kept
expect_false(file.exists(file.path(cache_subdir, "test1.pmtiles")))
expect_true(file.exists(file.path(cache_subdir, "other.txt")))
unlink(cache_subdir, recursive = TRUE)
})