Skip to content

Commit 3aa7927

Browse files
committed
bump test cov, fix bug in utils
1 parent 4b4ffef commit 3aa7927

4 files changed

Lines changed: 364 additions & 23 deletions

File tree

R/utils.R

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -445,29 +445,7 @@ validate_disk_compatibility <- function(output_type, dsn) {
445445
# Formats explicitly confirmed to NOT support append
446446
no_append_formats <- c("kml", "gml")
447447

448-
# --- 1. Handle text outputs (csv, tsv, txt) ---
449-
if (is_text) {
450-
if (!requireNamespace("readr", quietly = TRUE)) {
451-
stop(
452-
"Package 'readr' is required to write to .csv/.tsv/.txt files. Please install it.",
453-
call. = FALSE
454-
)
455-
}
456-
return(TRUE)
457-
}
458-
459-
# --- 2. Handle dataframe outputs ---
460-
if (is_dataframe) {
461-
stop(
462-
sprintf(
463-
"Output type 'dataframe' cannot be written to file extension '.%s'.\n Please use '.csv', '.tsv', or '.txt' for dataframes, or change output_type to 'sf_polygons'/'sf_points'.",
464-
ext
465-
),
466-
call. = FALSE
467-
)
468-
}
469-
470-
# --- 3. Handle raster outputs (spatraster) ---
448+
# --- 1. Handle raster outputs (spatraster) ---
471449
if (is_raster) {
472450
if (!nzchar(ext)) {
473451
stop(
@@ -494,6 +472,28 @@ validate_disk_compatibility <- function(output_type, dsn) {
494472
return(TRUE)
495473
}
496474

475+
# --- 2. Handle text outputs (csv, tsv, txt) ---
476+
if (is_text) {
477+
if (!requireNamespace("readr", quietly = TRUE)) {
478+
stop(
479+
"Package 'readr' is required to write to .csv/.tsv/.txt files. Please install it.",
480+
call. = FALSE
481+
)
482+
}
483+
return(TRUE)
484+
}
485+
486+
# --- 3. Handle dataframe outputs ---
487+
if (is_dataframe) {
488+
stop(
489+
sprintf(
490+
"Output type 'dataframe' cannot be written to file extension '.%s'.\n Please use '.csv', '.tsv', or '.txt' for dataframes, or change output_type to 'sf_polygons'/'sf_points'.",
491+
ext
492+
),
493+
call. = FALSE
494+
)
495+
}
496+
497497
# --- 4. Handle vector outputs (sf_polygons, sf_points) ---
498498
if (is_spatial_vector) {
499499
driver_name <- .ext_to_driver(ext, "vector")
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
test_that("stream_raster_parallel_mirai options work", {
3+
skip_on_cran()
4+
5+
# Ensure cleanup
6+
on.exit({
7+
mirai::daemons(0)
8+
}, add = TRUE)
9+
10+
# Set up minimal daemons
11+
mirai::daemons(2, dispatcher = FALSE)
12+
13+
# Create a small grid extent
14+
grid_extent <- sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 50, ymax = 50), crs = 3857)
15+
16+
# Temp file
17+
tmp_tif <- tempfile(fileext = ".tif")
18+
19+
# Test with explicit workers
20+
expect_no_error(
21+
stream_raster_parallel_mirai(
22+
grid_extent = grid_extent,
23+
cellsize_m = 10,
24+
crs = 3857,
25+
dsn = tmp_tif,
26+
layer = "test",
27+
dot_args = list(),
28+
quiet = TRUE,
29+
n_workers = 2
30+
)
31+
)
32+
33+
expect_true(file.exists(tmp_tif))
34+
35+
# Verify output
36+
r <- terra::rast(tmp_tif)
37+
expect_equal(terra::nrow(r), 5)
38+
expect_equal(terra::ncol(r), 5)
39+
expect_equal(terra::ncell(r), 25)
40+
41+
# Test cleanup
42+
unlink(tmp_tif)
43+
})
44+
45+
test_that(".compute_raster_chunk generates correct indices", {
46+
# Scenario: 10x10 grid (100 cells)
47+
# Chunk: rows 3-4 (2 rows)
48+
# Global cell IDs:
49+
# Row 1: 1-10
50+
# Row 2: 11-20
51+
# Row 3: 21-30
52+
# Row 4: 31-40
53+
54+
start_row <- 3
55+
nrows <- 2
56+
ncols <- 10
57+
58+
res <- .compute_raster_chunk(start_row, nrows, ncols)
59+
60+
expect_equal(res$start, 3)
61+
expect_equal(res$nrows, 2)
62+
expect_equal(length(res$values), 20)
63+
expect_equal(res$values[1], 21)
64+
expect_equal(res$values[20], 40)
65+
expect_equal(res$values, 21:40)
66+
})
67+
68+
test_that("stream_raster_parallel_mirai validates dependencies", {
69+
skip_on_cran()
70+
# It's hard to robustly mock missing packages in a way that affects requireNamespace
71+
# inside the function without affecting the test file itself.
72+
# Skipping mocking test for now as we are in a package context where dependencies are assumed met
73+
# if installed.
74+
})
75+
76+
test_that("stream_raster_parallel_mirai handles auto-workers", {
77+
skip_on_cran()
78+
on.exit({
79+
mirai::daemons(0)
80+
}, add = TRUE)
81+
mirai::daemons(2, dispatcher = FALSE)
82+
83+
tmp_tif <- tempfile(fileext = ".tif")
84+
grid_extent <- sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 100, ymax = 100), crs = 3857)
85+
86+
# Should run without error and infer workers from mirai::status()
87+
expect_no_error(
88+
stream_raster_parallel_mirai(
89+
grid_extent = grid_extent,
90+
cellsize_m = 10,
91+
crs = 3857,
92+
dsn = tmp_tif,
93+
layer = "test",
94+
dot_args = list(),
95+
quiet = TRUE,
96+
n_workers = NULL
97+
)
98+
)
99+
expect_true(file.exists(tmp_tif))
100+
unlink(tmp_tif)
101+
})

tests/testthat/test-utils.R

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
2+
test_that("regex_match works correctly", {
3+
text <- c("a1", "b2", "c3")
4+
pattern <- "([a-z])([0-9])"
5+
6+
# Basic match
7+
res <- regex_match(text, pattern)
8+
expect_length(res, 3)
9+
expect_equal(res[[1]][1], "a1")
10+
expect_equal(res[[1]][2], "a")
11+
expect_equal(res[[1]][3], "1")
12+
13+
# With index extraction
14+
res_idx <- regex_match(text, pattern, i = 2)
15+
expect_equal(res_idx, c("a", "b", "c"))
16+
17+
res_idx_3 <- regex_match(text, pattern, i = 3)
18+
expect_equal(res_idx_3, c("1", "2", "3"))
19+
20+
# Out of bounds index
21+
res_oob <- regex_match(text, pattern, i = 4)
22+
expect_true(all(is.na(res_oob)))
23+
})
24+
25+
test_that(".get_ram_gb handles options and system info", {
26+
# Test mock
27+
withr::with_options(list(gridmaker.fake_ram = 100), {
28+
expect_equal(.get_ram_gb("avail"), 100)
29+
expect_equal(.get_ram_gb()$available, 100)
30+
})
31+
32+
# Test actual fallback (smoke test)
33+
skip_on_cran() # PS package behavior might vary
34+
res <- .get_ram_gb()
35+
expect_type(res, "list")
36+
# Expect reasonable keys usually present
37+
# But won't assert too strictly on system dependent stuff
38+
})
39+
40+
test_that(".estimate_grid_memory_gb calculates reasonable estimates", {
41+
# Mock environment where we know sizing roughly?
42+
# Or just smoke test that it returns a number.
43+
44+
grid_extent <- sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 1000, ymax = 1000), crs = 3857)
45+
46+
est <- .estimate_grid_memory_gb(
47+
grid_extent = grid_extent,
48+
cellsize_m = 10,
49+
crs = 3857,
50+
output_type = "sf_polygons",
51+
id_format = "both",
52+
include_llc = TRUE,
53+
point_type = "centroid"
54+
)
55+
56+
expect_type(est, "double")
57+
expect_gte(est, 0)
58+
59+
# Trivial grid
60+
est_zero <- .estimate_grid_memory_gb(
61+
grid_extent = sf::st_bbox(c(xmin=0,ymin=0,xmax=1,ymax=1), crs=3857),
62+
cellsize_m = 100,
63+
crs = 3857,
64+
output_type = "sf_polygons",
65+
id_format = "both",
66+
include_llc = TRUE,
67+
point_type = "centroid"
68+
)
69+
expect_equal(est_zero, 0)
70+
})
71+
72+
test_that("validate_disk_compatibility rejects invalid combinations", {
73+
# DataFrame to KEA
74+
expect_error(
75+
validate_disk_compatibility("dataframe", "test.kea"),
76+
"cannot be written to"
77+
)
78+
79+
# Raster to CSV
80+
# Raster requires specific extensions
81+
expect_error(
82+
validate_disk_compatibility("spatraster", "test.txt"),
83+
"Unsupported raster format"
84+
)
85+
86+
# Raster to unknown
87+
expect_error(
88+
validate_disk_compatibility("spatraster", "test.xyz"),
89+
"Unsupported raster format"
90+
)
91+
92+
# Vector to No-Append format (KML)
93+
expect_error(
94+
validate_disk_compatibility("sf_polygons", "test.kml"),
95+
"does not support appending"
96+
)
97+
})
98+
99+
test_that(".ext_to_driver maps correctly", {
100+
expect_equal(.ext_to_driver("tif", "raster"), "GTiff")
101+
expect_equal(.ext_to_driver("gpkg", "vector"), "GPKG")
102+
expect_null(.ext_to_driver("xyz", "vector"))
103+
})
104+
105+
test_that(".tz_count counts trailing zeros", {
106+
expect_equal(.tz_count(100), 2)
107+
expect_equal(.tz_count(10), 1)
108+
expect_equal(.tz_count(1), 0)
109+
expect_equal(.tz_count(0), 0)
110+
expect_equal(.tz_count(101), 0)
111+
})

0 commit comments

Comments
 (0)