From fc172c3c4c394325be8bacdbb8b90e397379616a Mon Sep 17 00:00:00 2001 From: Anatolii Tsyplenkov Date: Wed, 8 Jan 2025 16:30:57 +1300 Subject: [PATCH] tests: codecov fix --- R/crs.R | 6 +++- man/wbw_ext.Rd | 5 +++ man/wbw_high_pass_median_filter.Rd | 2 +- tests/test-setup.R | 5 +++ tests/testthat.R | 2 ++ tests/testthat/_snaps/crs.md | 11 ++++++ tests/testthat/test-checks.R | 19 +++++++++++ tests/testthat/test-conversions.R | 4 +-- tests/testthat/test-crs.R | 18 ++++++++++ tests/testthat/test-dims.R | 4 +-- tests/testthat/test-filter.R | 4 +-- tests/testthat/test-geomorphometry.R | 4 +-- tests/testthat/test-io.R | 4 +-- tests/testthat/test-math.R | 4 +-- tests/testthat/test-primitives.R | 51 ++++++++++++++++++++++++++-- tests/testthat/test-system.R | 4 +-- tests/testthat/test-terra.R | 5 +-- 17 files changed, 122 insertions(+), 30 deletions(-) create mode 100644 tests/test-setup.R create mode 100644 tests/testthat/_snaps/crs.md create mode 100644 tests/testthat/test-crs.R diff --git a/R/crs.R b/R/crs.R index 4f72f3a..74d2286 100644 --- a/R/crs.R +++ b/R/crs.R @@ -3,12 +3,17 @@ #' #' @eval rd_input_raster("x") #' +#' @examples +#' f <- system.file("extdata/dem.tif", package = "wbw") +#' wbw_read_raster(f) |> +#' wbw_ext() #' @export wbw_ext <- S7::new_generic( name = "wbw_ext", dispatch_args = "x", fun = function(x) { + # Add some input validation S7::S7_dispatch() } ) @@ -16,7 +21,6 @@ wbw_ext <- S7::method(wbw_ext, WhiteboxRaster) <- function(x) { # Checks - check_env(wbe) conf <- x@source$configs WhiteboxExtent( west = conf$west, diff --git a/man/wbw_ext.Rd b/man/wbw_ext.Rd index 699bdac..fd9c3bb 100644 --- a/man/wbw_ext.Rd +++ b/man/wbw_ext.Rd @@ -12,4 +12,9 @@ wbw_ext(x) \description{ Get WhiteboxExtent } +\examples{ +f <- system.file("extdata/dem.tif", package = "wbw") +wbw_read_raster(f) |> + wbw_ext() +} \keyword{crs} diff --git a/man/wbw_high_pass_median_filter.Rd b/man/wbw_high_pass_median_filter.Rd index 86db33d..628e5cd 100644 --- a/man/wbw_high_pass_median_filter.Rd +++ b/man/wbw_high_pass_median_filter.Rd @@ -44,6 +44,6 @@ wbw_read_raster(f) |> For more information, see \url{https://www.whiteboxgeo.com/manual/wbw-user-manual/book/tool_help.html#high_pass_median_filter} } \seealso{ -\code{\link[=wbw_mean_filter]{wbw_mean_filter()}}, \code{\link[=wbw_high_pass_filter]{wbw_high_pass_filter()}} +\code{\link[=wbw_median_filter]{wbw_median_filter()}}, \code{\link[=wbw_high_pass_filter]{wbw_high_pass_filter()}} } \keyword{image_processing} diff --git a/tests/test-setup.R b/tests/test-setup.R new file mode 100644 index 0000000..60498a0 --- /dev/null +++ b/tests/test-setup.R @@ -0,0 +1,5 @@ +library(wbw) + +raster_path <- + system.file("extdata/dem.tif", package = "wbw") +x <- wbw_read_raster(raster_path) \ No newline at end of file diff --git a/tests/testthat.R b/tests/testthat.R index ca64766..8e0c7fc 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -15,3 +15,5 @@ have_numpy <- reticulate::py_module_available("numpy") if (!have_wbw & !have_numpy) { wbw_install(system = TRUE) } + +test_check("wbw") \ No newline at end of file diff --git a/tests/testthat/_snaps/crs.md b/tests/testthat/_snaps/crs.md new file mode 100644 index 0000000..3c36516 --- /dev/null +++ b/tests/testthat/_snaps/crs.md @@ -0,0 +1,11 @@ +# WhiteboxExtent recognized correctly + + Code + ext + Output + + @ west : num 1925449 + @ east : num 1929446 + @ south: num 5582091 + @ north: num 5585717 + diff --git a/tests/testthat/test-checks.R b/tests/testthat/test-checks.R index 42f578c..0e60375 100644 --- a/tests/testthat/test-checks.R +++ b/tests/testthat/test-checks.R @@ -77,3 +77,22 @@ test_that("check_input_file works for raster files", { check_input_file(temp_wrong, "raster") ) }) + +test_that("check_input_file validates type argument", { + temp_file <- tempfile(fileext = ".tif") + file.create(temp_file) + on.exit(unlink(temp_file)) + + # Should error with invalid type + expect_error( + check_input_file(temp_file, "invalid_type") + ) +}) + +test_that("check_env works with wbe environment", { + skip_if_no_wbw() + + # Test with actual wbe environment + expect_no_error(check_env(wbe)) + +}) diff --git a/tests/testthat/test-conversions.R b/tests/testthat/test-conversions.R index ae84026..90f6b6a 100644 --- a/tests/testthat/test-conversions.R +++ b/tests/testthat/test-conversions.R @@ -1,6 +1,4 @@ -raster_path <- - system.file("extdata/dem.tif", package = "wbw") -x <- wbw_read_raster(raster_path) +source('../test-setup.R') test_that( "conversion to radians works", diff --git a/tests/testthat/test-crs.R b/tests/testthat/test-crs.R new file mode 100644 index 0000000..a4b72cc --- /dev/null +++ b/tests/testthat/test-crs.R @@ -0,0 +1,18 @@ +source("../test-setup.R") + +test_that("WhiteboxExtent recognized correctly", { + # Test the extent extraction + ext <- wbw_ext(x) + expect_s7_class(ext, WhiteboxExtent) + expect_snapshot(ext) + + # Test individual components + expect_identical(ext@west, x@source$configs$west) + expect_identical(ext@east, x@source$configs$east) + expect_identical(ext@south, x@source$configs$south) + expect_identical(ext@north, x@source$configs$north) + + # Test error cases + expect_error(wbw_ext("x"), class = "S7_error_method_not_found") + expect_error(wbw_ext(NULL), class = "S7_error_method_not_found") +}) diff --git a/tests/testthat/test-dims.R b/tests/testthat/test-dims.R index 5a1b8af..5da7d49 100644 --- a/tests/testthat/test-dims.R +++ b/tests/testthat/test-dims.R @@ -1,6 +1,4 @@ -raster_path <- - system.file("extdata/dem.tif", package = "wbw") -x <- wbw_read_raster(raster_path) +source('../test-setup.R') test_that( "WhiteboxRaster dimensions detected correctly", diff --git a/tests/testthat/test-filter.R b/tests/testthat/test-filter.R index 61cfbc8..8c3943e 100644 --- a/tests/testthat/test-filter.R +++ b/tests/testthat/test-filter.R @@ -1,6 +1,4 @@ -raster_path <- - system.file("extdata/dem.tif", package = "wbw") -x <- wbw_read_raster(raster_path) +source('../test-setup.R') test_that( "filter fails", diff --git a/tests/testthat/test-geomorphometry.R b/tests/testthat/test-geomorphometry.R index 4ab4d9e..6e2ac4b 100644 --- a/tests/testthat/test-geomorphometry.R +++ b/tests/testthat/test-geomorphometry.R @@ -1,6 +1,4 @@ -raster_path <- - system.file("extdata/dem.tif", package = "wbw") -x <- wbw_read_raster(raster_path) +source('../test-setup.R') test_that( "geomorphometry fails", diff --git a/tests/testthat/test-io.R b/tests/testthat/test-io.R index 25fa19c..1084190 100644 --- a/tests/testthat/test-io.R +++ b/tests/testthat/test-io.R @@ -1,6 +1,4 @@ -raster_path <- - system.file("extdata/dem.tif", package = "wbw") -x <- wbw_read_raster(raster_path) +source('../test-setup.R') test_that( desc = "wbw_read function works", diff --git a/tests/testthat/test-math.R b/tests/testthat/test-math.R index 4e1c168..31ac0b6 100644 --- a/tests/testthat/test-math.R +++ b/tests/testthat/test-math.R @@ -1,6 +1,4 @@ -raster_path <- - system.file("extdata/dem.tif", package = "wbw") -x <- wbw_read_raster(raster_path) +source('../test-setup.R') test_that( "wbw_random_sample works", diff --git a/tests/testthat/test-primitives.R b/tests/testthat/test-primitives.R index e9e183a..c3556a8 100644 --- a/tests/testthat/test-primitives.R +++ b/tests/testthat/test-primitives.R @@ -1,6 +1,4 @@ -raster_path <- - system.file("extdata/dem.tif", package = "wbw") -x <- wbw_read_raster(raster_path) +source('../test-setup.R') test_that( "as_matrix converts WhiteboxRaster to matrix", @@ -61,3 +59,50 @@ test_that( expect_equal(round(variance(x), 1), round(var(as.vector(m)), 1)) } ) + +test_that("summary function works correctly", { + + # Test summary output + expect_output(summary(x), "minimum") + expect_output(summary(x), "maximum") + expect_output(summary(x), "average") + expect_output(summary(x), "standard deviation") +}) + +test_that("summary stats handle edge cases", { + + # Test error cases with invalid raster + expect_error(max(invalid_raster)) + expect_error(min(invalid_raster)) + expect_error(median(invalid_raster)) + expect_error(mean(invalid_raster)) + expect_error(stdev(invalid_raster)) + expect_error(variance(invalid_raster)) + + # Test with raster containing NA values + skip_if_not_installed("terra") + f <- system.file("ex/elev.tif", package="terra") + r <- wbw_read_raster(f) + + # Test that summary stats handle NA values + expect_no_error(max(r)) + expect_no_error(min(r)) + expect_no_error(mean(r)) + expect_no_error(median(r)) + expect_no_error(stdev(r)) + expect_no_error(variance(r)) +}) + +test_that("summary stats are consistent with matrix calculations", { + + m <- as_matrix(x) + v <- as.vector(m) + + # More precise testing of summary statistics + expect_equal(max(x), max(m, na.rm = TRUE)) + expect_equal(min(x), min(m, na.rm = TRUE)) + expect_equal(mean(x), mean(m, na.rm = TRUE)) + expect_equal(median(x), median(v, na.rm = TRUE), tolerance = 1e-5) + expect_equal(stdev(x), sd(v, na.rm = TRUE), tolerance = 1e-5) + expect_equal(variance(x), var(v, na.rm = TRUE), tolerance = 1e-5) +}) diff --git a/tests/testthat/test-system.R b/tests/testthat/test-system.R index 46fbbf7..87cd927 100644 --- a/tests/testthat/test-system.R +++ b/tests/testthat/test-system.R @@ -1,6 +1,4 @@ -raster_path <- - system.file("extdata/dem.tif", package = "wbw") -x <- wbw_read_raster(raster_path) +source('../test-setup.R') test_that( "setting custom max_procs fails", diff --git a/tests/testthat/test-terra.R b/tests/testthat/test-terra.R index 9d6a6b7..eb02ec9 100644 --- a/tests/testthat/test-terra.R +++ b/tests/testthat/test-terra.R @@ -1,6 +1,4 @@ -raster_path <- - system.file("extdata/dem.tif", package = "wbw") -x <- wbw_read_raster(raster_path) +source("../test-setup.R") test_that( "WhiteboxRaster to SpatRaster conversion works", @@ -111,4 +109,3 @@ test_that( ) } ) -