Skip to content

Commit

Permalink
fixing/adding tests for writeTo
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmseddy committed Jul 8, 2024
1 parent 0b454e9 commit 67e094e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
20 changes: 12 additions & 8 deletions tests/testthat/test-postProcess.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ test_that("prepInputs doesn't work (part 3)", {
expect_equal(s1[], b1[], ignore_attr = TRUE)

b <- writeRaster(b, filename = tmpfile[1], overwrite = TRUE)
b1 <- postProcess(b, studyArea = ncSmall, useCache = FALSE, filename2 = tmpfile[2], overwrite = TRUE)
b1 <- postProcess(b, studyArea = ncSmall, useCache = FALSE, writeTo = tmpfile[2], overwrite = TRUE)
expect_true(inherits(b1, "SpatRaster"))

s1 <- postProcess(s, studyArea = ncSmall, useCache = FALSE, filename2 = tmpfile[2], overwrite = TRUE)
s1 <- postProcess(s, studyArea = ncSmall, useCache = FALSE, writeTo = tmpfile[2], overwrite = TRUE)
expect_true(inherits(s1, "SpatRaster"))

# Test datatype setting
dt1 <- "INT2U"
s <- writeRaster(s, filename = tmpfile[2], overwrite = TRUE)
s1 <- postProcess(s,
studyArea = ncSmall, useCache = FALSE, filename2 = tmpfile[1], overwrite = TRUE,
studyArea = ncSmall, useCache = FALSE, writeTo = tmpfile[1], overwrite = TRUE,
datatype = dt1
)
expect_identical(terra::datatype(s1), rep(dt1, terra::nlyr(s)))
Expand All @@ -99,7 +99,7 @@ test_that("prepInputs doesn't work (part 3)", {
s <- writeRaster(s, filename = tmpfile[1], overwrite = TRUE)
warns <- capture_error({
s1 <- postProcess(s,
studyArea = ncSmall, useCache = FALSE, filename2 = tmpfile[2], overwrite = TRUE,
studyArea = ncSmall, useCache = FALSE, writeTo = tmpfile[2], overwrite = TRUE,
datatype = dt1
)
})
Expand All @@ -108,7 +108,7 @@ test_that("prepInputs doesn't work (part 3)", {
dt1 <- "INT4U"
b <- writeRaster(b, filename = tmpfile[2], overwrite = TRUE)
b1 <- postProcess(b,
studyArea = ncSmall, useCache = FALSE, filename2 = tmpfile[1], overwrite = TRUE,
studyArea = ncSmall, useCache = FALSE, writeTo = tmpfile[1], overwrite = TRUE,
datatype = dt1
)
expect_identical(terra::datatype(b1), rep(dt1, terra::nlyr(b1)))
Expand Down Expand Up @@ -203,16 +203,20 @@ test_that("prepInputs doesn't work (part 3)", {
expect_false(terra::is.valid(p6))
# projectInputs pass through
expect_error(projectInputs(x = 1), "argument .+ is missing")

#using deprecated filename2 arg
expect_error(cropInputs(ncSmall, studyArea = ncSmallShifted, filename2 = "use_WriteTo_Instead.shp"))

})

test_that("writeOutputs with non-matching filename2", {
test_that("writeOutputs with non-matching writeTo", {
testInit(c("terra"), tmpFileExt = c(".grd", ".tif"))

r <- terra::rast(terra::ext(0, 10, 0, 10), vals = rnorm(100))
r <- terra::writeRaster(r, filename = tmpfile[1], overwrite = TRUE)
r[] <- r[]
warn <- capture_warnings({
r1 <- writeOutputs(r, filename2 = tmpfile[2])
r1 <- writeOutputs(r, writeTo = tmpfile[2])
})
r2 <- terra::rast(Filenames(r1))
vals1 <- r2[]
Expand Down Expand Up @@ -304,7 +308,7 @@ test_that("maskInputs errors when x is Lat-Long", {
useCache = FALSE,
fun = "sf::st_read",
destinationPath = tmpdir,
filename2 = "miniRoad.shp"
writeTo = "miniRoad.shp"
)
)
)
Expand Down
9 changes: 8 additions & 1 deletion tests/testthat/test-prepInputs.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ test_that("prepInputs doesn't work (part 1)", {
alsoExtract = reproducible::asPath(ecozoneFiles),
studyArea = StudyArea,
destinationPath = dPath,
filename2 = "EcozoneFile.shp",
writeTo = "EcozoneFile.shp",
useCache = TRUE # with useTerra = TRUE, this is only for loading, not postProcess
),
quick = "destinationPath"
Expand Down Expand Up @@ -168,6 +168,13 @@ test_that("prepInputs doesn't work (part 1)", {
)
)
expect_true(is(shpEcozone, vectorType()))

#stops if deprecated arguments used
expect_error(prepInputs(destinationPath = dPath,
url = "http://sis.agr.gc.ca/cansis/nsdb/ecostrat/zone/ecozone_shp.zip",
archive = file.path(dPath, "ecozone_shp.zip"),
filename2 = "use_writeTo_instead.shp"))

})

test_that("interactive prepInputs", {
Expand Down

0 comments on commit 67e094e

Please sign in to comment.