Skip to content

Commit

Permalink
better entry handling for non-numeric vpus
Browse files Browse the repository at this point in the history
  • Loading branch information
jsta committed Aug 8, 2019
1 parent d409742 commit b6077f6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
7 changes: 4 additions & 3 deletions R/get.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ nhd_plus_get <- function(vpu = NA, component = "NHDSnapshot", force_dl = FALSE,
"' was not found. Is it misspelled?"))
}

if(!vpu %in% c("National", 1:22, "10L", "10U", "03N", "03W", "03S",
paste0("0", 1:9), as.character(nhdR::vpu_shp$UnitID))){
stop(paste0(vpu, " is not a valid vpu"))
if(!vpu %in% c("National", 1:2, 4:9, 11:22, "10L", "10U", "03N", "03W", "03S",
paste0("0", 1:2), paste0("0", 4:9),
as.character(nhdR::vpu_shp$UnitID))){
stop(paste0(vpu, " is not a valid vpu. Are you missing a letter designation? See VPU map."))
}

url <- get_plus_remotepath(vpu, component = component)
Expand Down
6 changes: 4 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ get_if_not_exists <- function(url, destfile, force_dl = FALSE){
}

zero_pad <- function(x, digits){
if(nchar(x) < 2){
paste0(paste0(rep(0, digits), collapse = ""), x, collapse = "")
if(nchar(stringr::str_extract(x, "\\d+")) < 2){
paste0(
paste0(
rep(0, digits), collapse = ""), x, collapse = "")
}else{
x
}
Expand Down
26 changes: 23 additions & 3 deletions tests/testthat/test-load.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
context("load")

test_that("nhd_plus_load works with non-numeric VPU characters", {
test_that("nhd_plus_load works for single digit VPU entries", {

x <- nhd_plus_load(vpu = '8', component = "NHDPlusAttributes",
dsn = "PlusFlowLineVAA", approve_all_dl = TRUE)

expect_s3_class(x, "data.frame")
})


test_that("nhd_plus_load handles non-numeric VPU characters", {
skip_on_cran()
skip_on_travis()

y = nhd_plus_load(vpu='03N', component='NHDPlusAttributes',
dsn='PlusFlowlineVAA', approve_all_dl=TRUE)
x = nhd_plus_load(vpu='03S', component='NHDPlusAttributes',
dsn='PlusFlowlineVAA', approve_all_dl=TRUE)
y = nhd_plus_load(vpu='03N', component='NHDPlusAttributes',
dsn='PlusFlowlineVAA', approve_all_dl=TRUE)

expect_false(identical(x, y))

z <- nhd_plus_load(vpu = '3N', component = "NHDPlusAttributes",
dsn = "PlusFlowLineVAA", approve_all_dl = TRUE)

expect_true(identical(y, z))

expect_error(
nhd_plus_load(vpu = '03', component = "NHDPlusAttributes",
dsn = "PlusFlowLineVAA", approve_all_dl = TRUE),
"03 is not a valid vpu. Are you missing a letter designation? See VPU map.",
fixed = TRUE)
})

0 comments on commit b6077f6

Please sign in to comment.