Skip to content

Commit

Permalink
tests: codecov fix
Browse files Browse the repository at this point in the history
  • Loading branch information
atsyplenkov committed Jan 8, 2025
1 parent ca4114f commit fc172c3
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 30 deletions.
6 changes: 5 additions & 1 deletion R/crs.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
#'
#' @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()
}
)

S7::method(wbw_ext, WhiteboxRaster) <-
function(x) {
# Checks
check_env(wbe)
conf <- x@source$configs
WhiteboxExtent(
west = conf$west,
Expand Down
5 changes: 5 additions & 0 deletions man/wbw_ext.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/wbw_high_pass_median_filter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/test-setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library(wbw)

raster_path <-
system.file("extdata/dem.tif", package = "wbw")
x <- wbw_read_raster(raster_path)
2 changes: 2 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ have_numpy <- reticulate::py_module_available("numpy")
if (!have_wbw & !have_numpy) {
wbw_install(system = TRUE)
}

test_check("wbw")
11 changes: 11 additions & 0 deletions tests/testthat/_snaps/crs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# WhiteboxExtent recognized correctly

Code
ext
Output
<wbw::WhiteboxExtent>
@ west : num 1925449
@ east : num 1929446
@ south: num 5582091
@ north: num 5585717

19 changes: 19 additions & 0 deletions tests/testthat/test-checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))

})
4 changes: 1 addition & 3 deletions tests/testthat/test-conversions.R
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-crs.R
Original file line number Diff line number Diff line change
@@ -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")
})
4 changes: 1 addition & 3 deletions tests/testthat/test-dims.R
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-filter.R
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-geomorphometry.R
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-io.R
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-math.R
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
51 changes: 48 additions & 3 deletions tests/testthat/test-primitives.R
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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)
})
4 changes: 1 addition & 3 deletions tests/testthat/test-system.R
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 1 addition & 4 deletions tests/testthat/test-terra.R
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -111,4 +109,3 @@ test_that(
)
}
)

1 comment on commit fc172c3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.