Skip to content

Commit

Permalink
test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dblodgett-usgs committed Nov 7, 2019
1 parent 8a7c324 commit c5680da
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 72 deletions.
5 changes: 1 addition & 4 deletions R/subset_nhdplus.R
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ intersection_write <- function(layer_name, data_path, envelope,
if (nrow(out) > 0) {
if (status) message(paste("Writing", layer_name))
if(is.null(output_file)) {
return(layer)
return(out)
} else {
sf::write_sf(out, output_file, layer_name)
return(layer_name)
Expand Down Expand Up @@ -433,9 +433,6 @@ get_catchment_subset <- function(nhdplus_data, comids, output_file,
if (grepl("*.rds$", catchment_path)) {
catchment <- readRDS(catchment_path)
} else {
if(!layer_name %in% st_layers(catchment_path)$name) {
layer_name <- "NHDPlusCatchment"
}
catchment <- sf::read_sf(catchment_path, layer_name)
catchment <- rename_nhdplus(catchment)
}
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test_plot_nhdplus.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
context("plot tests")

test_that("basics work", {
skip_on_cran()
site <- "USGS-05428500"
d <- nhdplusTools:::get_plot_data(site)
expect_equal(names(d), c("plot_bbox", "sites", "flowline", "basin"))

p_ready <- nhdplusTools:::gt(d$flowline)
expect_equal(st_crs(p_ready), st_crs(3857))
expect_s3_class(p_ready, "sfc_LINESTRING")

pdf(NULL)
tempd <- tempdir()
dir.create(tempd, recursive = TRUE)
tempf <- file.path(tempd, "temp.png")
png(file.path(tempd, "temp.png"))
plot_nhdplus("USGS-05428500")
dev.off()

expect_true(file.exists(tempf))
})
175 changes: 107 additions & 68 deletions tests/testthat/test_subset.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
context("subset")


test_that("subset runs as expected", {
test_that("subset errors", {

temp_dir <- tempdir()

Expand All @@ -11,23 +10,42 @@ test_that("subset runs as expected", {
output_file = tempfile(fileext = ".gpkg")),
"couldn't find nhdplus data")

expect_error(subset_nhdplus(comids = "dud",
output_file = "test",
"output_file must end in '.gpkg'"))

expect_error(subset_nhdplus(output_file = "test",
"must provide comids or bounding box"))

nhdplus_path("../NHDPlusV21_National_Seamless.gdb")
})

test_that("subset runs as expected", {

temp_dir <- tempdir()
dir.create(temp_dir, recursive = TRUE, showWarnings = FALSE)

sample_data <- system.file("extdata/sample_natseamless.gpkg",
package = "nhdplusTools")

expect_equal(nhdplusTools:::get_catchment_layer_name(TRUE, sample_data), "CatchmentSP")

nc <- read_sf(system.file("shape/nc.shp", package="sf"))
tempf <- file.path(temp_dir, "temp.geojson")
write_sf(nc, tempf, "Catchment")
expect_equal(nhdplusTools:::get_catchment_layer_name(FALSE, tempf), "Catchment")

nhdplus_path(sample_data)

if (!dir.exists(temp_dir)) dir.create(temp_dir)

staged_nhdplus <- stage_national_data(output_path = temp_dir)

start_comid <- 13293392

sample_flines <- readRDS(staged_nhdplus$flowline)

comids <- get_UM(sample_flines, start_comid)
comids <- get_UM(readRDS(staged_nhdplus$flowline), 13293392)

out_file <- tempfile(fileext = ".gpkg")

# No output file
fi <- subset_nhdplus(comids = comids,
output_file = NULL,
nhdplus_data = sample_data,
Expand All @@ -36,14 +54,18 @@ test_that("subset runs as expected", {
expect_equal(names(fi), c("NHDFlowline_Network", "CatchmentSP", "NHDArea",
"NHDWaterbody", "Gage", "Sink",
"NHDFlowline_NonNetwork"))
expect_equal(nrow(fi$CatchmentSP), 4)
expect_equal(nrow(fi$NHDWaterbody), 1)

# write to output file
fi <- subset_nhdplus(comids = comids,
output_file = out_file,
nhdplus_data = sample_data,
status = FALSE)

expect_equal(fi, out_file)

# don't overwrite
expect_error(
subset_nhdplus(comids = comids,
output_file = out_file,
Expand All @@ -52,6 +74,7 @@ test_that("subset runs as expected", {
status = FALSE),
"output_file exists and overwrite is false.")

# check that status works
messages <- capture_messages(
fi <- subset_nhdplus(comids = comids,
output_file = out_file,
Expand All @@ -67,12 +90,9 @@ test_that("subset runs as expected", {

check_layers()

expect_error(subset_nhdplus(comids = "dud",
output_file = "test",
"output_file must end in '.gpkg'"))

unlink(file.path(temp_dir, "*"))

# Make sure passing data directly works
fi <- subset_nhdplus(comids = comids,
output_file = out_file,
nhdplus_data = sample_data,
Expand All @@ -86,6 +106,8 @@ test_that("subset runs as expected", {
nhdplus_path("../NHDPlusV21_National_Seamless.gdb")

skip_on_cran()

# download option
fi <- subset_nhdplus(comids = comids,
output_file = out_file,
nhdplus_data = "download",
Expand All @@ -96,70 +118,18 @@ test_that("subset runs as expected", {

})

test_that("prep_nhdplus runs as expected", {

temp_dir <- tempdir()

if (!dir.exists(temp_dir)) dir.create(temp_dir)

expect_error(suppressWarnings(stage_national_data()),
paste("Didn't find NHDPlus national data in default",
"location: ../NHDPlusV21_National_Seamless.gdb"))

sample_gpkg <- file.path(temp_dir, "sample_natseamless.gpkg")

file.copy(system.file("extdata/sample_natseamless.gpkg",
package = "nhdplusTools"), sample_gpkg)

nhdplus_path(sample_gpkg)

expect_warning(temp_data <- stage_national_data(),
regexp = paste0("No output path provided, using:.*"))

temp_data <- lapply(temp_data, unlink)

temp_data <- stage_national_data(output_path = temp_dir)

expect_true(suppressWarnings(all(lapply(temp_data, file.exists))))

temp_data <- lapply(temp_data, unlink)

nhdplus_path("bogus")

expect_error(suppressWarnings(stage_national_data()),
paste("Didn't find NHDPlus national data in",
"user specified location: bogus"))

nhdplus_path(sample_gpkg)

expect_error(stage_national_data(include = c("bogus"),
output_path = "data/temp"),
paste("Got invalid include entries. Expect one",
"or more of: attribute, flowline, catchment."))

temp_data <- stage_national_data(output_path = temp_dir)

expect_equal(
capture_warnings(
temp_data <- stage_national_data(output_path = temp_dir)),
c("attributes file exists", "flowline file exists",
"catchment already exists."))

temp_data <- lapply(temp_data, unlink)

unlink(sample_gpkg)

})

test_that("subset works with HR", {
work_dir <- tempdir()

skip_on_cran()
get_test_file(work_dir)

hr <- get_nhdplushr(work_dir, out_gpkg = file.path(work_dir, "temp.gpkg"),
layers = c("NHDFlowline", "NHDPlusCatchment", "NHDWaterbody",
"NHDArea", "NHDPlusSink"))
layers = c("NHDFlowline", "NHDPlusCatchment", "NHDWaterbody",
"NHDArea", "NHDPlusSink"))

expect_equal(nhdplusTools:::get_catchment_layer_name(TRUE, hr), "NHDPlusCatchment")
expect_equal(nhdplusTools:::get_catchment_layer_name(FALSE, hr), "NHDPlusCatchment")

flowlines <- sf::read_sf(hr, "NHDFlowline")

Expand Down Expand Up @@ -207,4 +177,73 @@ test_that("subset by bounding box", {
status = FALSE)

check_layers()

expect_warning(fi <- subset_nhdplus(comids = c(1, 2, 3),
bbox = as.numeric(bbox),
nhdplus_data = sample_data,
overwrite = TRUE,
simplified = TRUE,
status = FALSE),
"using bounding box rather than submitted comids")

bbox[1] <- -190
expect_error(fi <- subset_nhdplus(bbox = as.numeric(bbox),
nhdplus_data = sample_data,
status = FALSE),
"invalid bbox entry")
})

test_that("prep_nhdplus runs as expected", {

temp_dir <- tempdir()

if (!dir.exists(temp_dir)) dir.create(temp_dir)

expect_error(suppressWarnings(stage_national_data()),
paste("Didn't find NHDPlus national data in default",
"location: ../NHDPlusV21_National_Seamless.gdb"))

sample_gpkg <- file.path(temp_dir, "sample_natseamless.gpkg")

file.copy(system.file("extdata/sample_natseamless.gpkg",
package = "nhdplusTools"), sample_gpkg)

nhdplus_path(sample_gpkg)

expect_warning(temp_data <- stage_national_data(),
regexp = paste0("No output path provided, using:.*"))

temp_data <- lapply(temp_data, unlink)

temp_data <- stage_national_data(output_path = temp_dir)

expect_true(suppressWarnings(all(lapply(temp_data, file.exists))))

temp_data <- lapply(temp_data, unlink)

nhdplus_path("bogus")

expect_error(suppressWarnings(stage_national_data()),
paste("Didn't find NHDPlus national data in",
"user specified location: bogus"))

nhdplus_path(sample_gpkg)

expect_error(stage_national_data(include = c("bogus"),
output_path = "data/temp"),
paste("Got invalid include entries. Expect one",
"or more of: attribute, flowline, catchment."))

temp_data <- stage_national_data(output_path = temp_dir)

expect_equal(
capture_warnings(
temp_data <- stage_national_data(output_path = temp_dir)),
c("attributes file exists", "flowline file exists",
"catchment already exists."))

temp_data <- lapply(temp_data, unlink)

unlink(sample_gpkg)

})

0 comments on commit c5680da

Please sign in to comment.