Skip to content

Commit ad10d36

Browse files
committed
clean tests
1 parent 3aa7927 commit ad10d36

12 files changed

Lines changed: 149 additions & 116 deletions

R/inspire_grid.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ inspire_grid <- function(
7878
include_llc = TRUE,
7979
point_type = "centroid",
8080
parallel = "auto",
81-
quiet = FALSE,
81+
quiet = getOption("gridmaker.quiet", FALSE),
8282
dsn = NULL,
8383
layer = NULL,
8484
max_memory_gb = NULL,
@@ -116,7 +116,7 @@ inspire_grid.sf <- function(
116116
include_llc = TRUE,
117117
point_type = "centroid",
118118
parallel = "auto",
119-
quiet = FALSE,
119+
quiet = getOption("gridmaker.quiet", FALSE),
120120
dsn = NULL,
121121
layer = NULL,
122122
max_memory_gb = NULL,
@@ -160,7 +160,7 @@ inspire_grid.sfc <- function(
160160
include_llc = TRUE,
161161
point_type = "centroid",
162162
parallel = "auto",
163-
quiet = FALSE,
163+
quiet = getOption("gridmaker.quiet", FALSE),
164164
dsn = NULL,
165165
layer = NULL,
166166
max_memory_gb = NULL,
@@ -204,7 +204,7 @@ inspire_grid.bbox <- function(
204204
include_llc = TRUE,
205205
point_type = "centroid",
206206
parallel = "auto",
207-
quiet = FALSE,
207+
quiet = getOption("gridmaker.quiet", FALSE),
208208
dsn = NULL,
209209
layer = NULL,
210210
max_memory_gb = NULL,
@@ -248,7 +248,7 @@ inspire_grid.numeric <- function(
248248
include_llc = TRUE,
249249
point_type = "centroid",
250250
parallel = "auto",
251-
quiet = FALSE,
251+
quiet = getOption("gridmaker.quiet", FALSE),
252252
dsn = NULL,
253253
layer = NULL,
254254
max_memory_gb = NULL,
@@ -292,7 +292,7 @@ inspire_grid.matrix <- function(
292292
include_llc = TRUE,
293293
point_type = "centroid",
294294
parallel = "auto",
295-
quiet = FALSE,
295+
quiet = getOption("gridmaker.quiet", FALSE),
296296
dsn = NULL,
297297
layer = NULL,
298298
max_memory_gb = NULL,
@@ -337,7 +337,7 @@ inspire_grid.character <- function(
337337
include_llc = TRUE, # Used
338338
point_type = "llc", # Used
339339
parallel = "auto", # Ignored (Sink)
340-
quiet = FALSE, # Used
340+
quiet = getOption("gridmaker.quiet", FALSE), # Used
341341
dsn = NULL, # Used
342342
layer = NULL, # Used
343343
max_memory_gb = NULL, # Ignored (Sink)
@@ -429,7 +429,7 @@ inspire_grid_from_extent <- function(
429429
include_llc = TRUE,
430430
point_type = "centroid",
431431
parallel = "auto",
432-
quiet = FALSE,
432+
quiet = getOption("gridmaker.quiet", FALSE),
433433
dsn = NULL,
434434
layer = NULL,
435435
max_memory_gb = NULL,

R/inspire_grid_from_ids.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ inspire_grid_from_ids <- function(
4646
include_llc = TRUE,
4747
id_format = c("both", "long", "short"),
4848
axis_order = c("NE", "EN"),
49-
quiet = FALSE,
49+
quiet = getOption("gridmaker.quiet", FALSE),
5050
dsn = NULL,
5151
layer = NULL,
5252
...

R/stream_grid_raster.R

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@ stream_grid_raster_terra <- function(
1818
max_memory_gb = NULL
1919
) {
2020
# --- 1. SETUP ---
21-
# Configure terra options for this session
22-
old_opts <- terra::terraOptions()
23-
on.exit(suppressWarnings(do.call(terra::terraOptions, old_opts)), add = TRUE)
21+
# Save and restore terra options on exit
22+
old_opts <- terra::terraOptions(print = FALSE)
23+
on.exit({
24+
# Only restore options that are safe to restore
25+
restore_opts <- old_opts[names(old_opts) %in%
26+
c("memfrac", "memmax", "memmin", "todisk", "tempdir")]
27+
do.call(terra::terraOptions, restore_opts)
28+
}, add = TRUE)
29+
30+
# Configure for streaming (always write to disk)
31+
terra::terraOptions(todisk = TRUE)
2432

2533
if (!is.null(max_memory_gb)) {
26-
terra::terraOptions(memmax = max_memory_gb, todisk = TRUE)
27-
} else {
28-
terra::terraOptions(todisk = TRUE)
29-
if (is.null(old_opts$memfrac) || old_opts$memfrac > 0.6) {
30-
terra::terraOptions(memfrac = 0.5)
31-
}
34+
terra::terraOptions(memmax = max_memory_gb)
35+
} else if (old_opts$memfrac > 0.6) {
36+
terra::terraOptions(memfrac = 0.5)
3237
}
3338

3439
# --- RAT SUPPORT DISABLED ---

R/stream_grid_raster_parallel.R

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,22 @@ stream_raster_parallel_mirai <- function(
8888
}
8989

9090
# --- 1. SETUP ---
91-
old_opts <- terra::terraOptions()
92-
on.exit(suppressWarnings(do.call(terra::terraOptions, old_opts)), add = TRUE)
91+
# Save and restore terra options on exit
92+
old_opts <- terra::terraOptions(print = FALSE)
93+
on.exit({
94+
# Only restore options that are safe to restore
95+
restore_opts <- old_opts[names(old_opts) %in%
96+
c("memfrac", "memmax", "memmin", "todisk", "tempdir")]
97+
do.call(terra::terraOptions, restore_opts)
98+
}, add = TRUE)
99+
100+
# Configure for streaming (always write to disk)
101+
terra::terraOptions(todisk = TRUE)
93102

94103
if (!is.null(max_memory_gb)) {
95-
terra::terraOptions(memmax = max_memory_gb, todisk = TRUE)
96-
} else {
97-
terra::terraOptions(todisk = TRUE)
98-
if (is.null(old_opts$memfrac) || old_opts$memfrac > 0.6) {
99-
terra::terraOptions(memfrac = 0.5)
100-
}
104+
terra::terraOptions(memmax = max_memory_gb)
105+
} else if (old_opts$memfrac > 0.6) {
106+
terra::terraOptions(memfrac = 0.5)
101107
}
102108

103109
# RAT deprecation warning

R/utils.R

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,31 +122,40 @@ regex_match <- function(text, pattern, i = NULL, ...) {
122122
c(xmin = 0, ymin = 0, xmax = n1 * cellsize_m, ymax = cellsize_m),
123123
crs = grid_crs
124124
)
125-
sample_grid_1 <- inspire_grid_from_extent_internal(
125+
126+
# Construct args to avoid passing ignored point_type (which warns)
127+
args1 <- list(
126128
grid_extent = sample_extent_1,
127129
cellsize_m = cellsize_m,
128130
output_type = output_type,
129131
id_format = id_format,
130132
axis_order = "NE",
131-
include_llc = include_llc,
132-
point_type = point_type
133+
include_llc = include_llc
133134
)
135+
if (output_type == "sf_points") {
136+
args1$point_type <- point_type
137+
}
138+
sample_grid_1 <- do.call(inspire_grid_from_extent_internal, args1)
134139
size1 <- as.numeric(utils::object.size(sample_grid_1))
135140

136141
# Create the second, larger sample grid.
137142
sample_extent_2 <- sf::st_bbox(
138143
c(xmin = 0, ymin = 0, xmax = n2 * cellsize_m, ymax = cellsize_m),
139144
crs = grid_crs
140145
)
141-
sample_grid_2 <- inspire_grid_from_extent_internal(
146+
147+
args2 <- list(
142148
grid_extent = sample_extent_2,
143149
cellsize_m = cellsize_m,
144150
output_type = output_type,
145151
id_format = id_format,
146152
axis_order = "NE",
147-
include_llc = include_llc,
148-
point_type = point_type
153+
include_llc = include_llc
149154
)
155+
if (output_type == "sf_points") {
156+
args2$point_type <- point_type
157+
}
158+
sample_grid_2 <- do.call(inspire_grid_from_extent_internal, args2)
150159
size2 <- as.numeric(utils::object.size(sample_grid_2))
151160

152161
# Calculate the memory cost per additional cell (the slope).
@@ -261,36 +270,42 @@ regex_match <- function(text, pattern, i = NULL, ...) {
261270
n1 <- 10
262271
n2 <- 20
263272

273+
# Create sample 1
264274
# Create sample 1
265275
sample_extent_1 <- sf::st_bbox(
266276
c(xmin = 0, ymin = 0, xmax = n1 * cellsize_m, ymax = cellsize_m),
267277
crs = grid_crs
268278
)
269-
sample_grid_1 <- inspire_grid_from_extent_internal(
279+
280+
# Prepare safe arguments
281+
out_type <- dot_args$output_type %||% "sf_polygons"
282+
pt_type <- dot_args$point_type %||% "centroid"
283+
284+
args1 <- list(
270285
grid_extent = sample_extent_1,
271286
cellsize_m = cellsize_m,
272-
output_type = dot_args$output_type %||% "sf_polygons",
287+
output_type = out_type,
273288
id_format = dot_args$id_format %||% "both",
274289
axis_order = dot_args$axis_order %||% "NE",
275-
include_llc = dot_args$include_llc %||% TRUE,
276-
point_type = dot_args$point_type %||% "centroid"
290+
include_llc = dot_args$include_llc %||% TRUE
277291
)
292+
if (out_type == "sf_points") {
293+
args1$point_type <- pt_type
294+
}
295+
296+
sample_grid_1 <- do.call(inspire_grid_from_extent_internal, args1)
278297
size1 <- as.numeric(utils::object.size(sample_grid_1))
279298

280299
# Create sample 2
281300
sample_extent_2 <- sf::st_bbox(
282301
c(xmin = 0, ymin = 0, xmax = n2 * cellsize_m, ymax = cellsize_m),
283302
crs = grid_crs
284303
)
285-
sample_grid_2 <- inspire_grid_from_extent_internal(
286-
grid_extent = sample_extent_2,
287-
cellsize_m = cellsize_m,
288-
output_type = dot_args$output_type %||% "sf_polygons",
289-
id_format = dot_args$id_format %||% "both",
290-
axis_order = dot_args$axis_order %||% "NE",
291-
include_llc = dot_args$include_llc %||% TRUE,
292-
point_type = dot_args$point_type %||% "centroid"
293-
)
304+
305+
args2 <- args1
306+
args2$grid_extent <- sample_extent_2
307+
308+
sample_grid_2 <- do.call(inspire_grid_from_extent_internal, args2)
294309
size2 <- as.numeric(utils::object.size(sample_grid_2))
295310

296311
# Calculate marginal bytes per cell

man/inspire_grid.Rd

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/setup.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Gridmaker Options for Testing
2+
options(gridmaker.quiet = TRUE)
3+
14
library(sf)
25

36
# --- Test Data Preparation ---

tests/testthat/test-inspire_grid-raster-streaming.R

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ test_that("inspire_grid raster streaming via terra works", {
1212
# 1. Test basic streaming to disk (NO RAT by default)
1313
tf <- tempfile(fileext = ".tif")
1414

15-
expect_message(
16-
res_dsn <- inspire_grid(
17-
nc_sub,
18-
cellsize_m = cellsize,
19-
output_type = "spatraster",
20-
dsn = tf,
21-
quiet = FALSE
22-
),
23-
"Streaming raster to disk"
15+
res_dsn <- inspire_grid(
16+
nc_sub,
17+
cellsize_m = cellsize,
18+
output_type = "spatraster",
19+
dsn = tf,
20+
quiet = TRUE
2421
)
2522

2623
expect_type(res_dsn, "character")
@@ -55,16 +52,13 @@ test_that("inspire_grid raster streaming via terra works", {
5552
# 3. Test Clipping in Streaming
5653
tf_clipped <- tempfile(fileext = ".tif")
5754

58-
expect_message(
59-
inspire_grid(
60-
nc_sub,
61-
cellsize_m = cellsize,
62-
output_type = "spatraster",
63-
dsn = tf_clipped,
64-
clip_to_input = TRUE,
65-
quiet = FALSE
66-
),
67-
"Applying mask"
55+
inspire_grid(
56+
nc_sub,
57+
cellsize_m = cellsize,
58+
output_type = "spatraster",
59+
dsn = tf_clipped,
60+
clip_to_input = TRUE,
61+
quiet = TRUE
6862
)
6963

7064
r_cl <- terra::rast(tf_clipped)

tests/testthat/test-inspire_grid.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ test_that("`quiet` parameter correctly suppresses messages", {
301301
inspire_grid_from_extent(
302302
grid_extent = nc,
303303
cellsize_m = CELLSIZE,
304-
parallel = "auto" # Allow auto-detection to fall back to sequential
304+
parallel = "auto", # Allow auto-detection to fall back to sequential
305+
quiet = FALSE
305306
),
306307
regexp = "No parallel backend detected"
307308
)
@@ -631,7 +632,7 @@ test_that("Warning for ignored point_type", {
631632
crs = 3857,
632633
output_type = "sf_polygons",
633634
point_type = "llc",
634-
quiet = FALSE
635+
quiet = TRUE
635636
),
636637
"Argument 'point_type'.*is ignored"
637638
)

0 commit comments

Comments
 (0)