Skip to content

Commit

Permalink
Use testthat::skip_on_cran #155; closes #127
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag committed Jan 16, 2021
1 parent b137276 commit bcb76c3
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 440 deletions.
52 changes: 25 additions & 27 deletions R/AAAA.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@

# setup a new environment to store error messages, etc.
soilDB.env <- new.env(hash=TRUE, parent = parent.frame())

# safely register some options at package load time
.onLoad <- function(libname, pkgname) {

## TODO: normalize these option names
# set to FALSE at CRAN submission time: do not test APIs, this protects from API failures
# set to TRUE for testing non-NASIS components on machines that don't have local ODBC connection setup
options(.soilDB_testNetworkFunctions=TRUE)

# function verbosity
options(soilDB.verbose=FALSE)

# set default local nasis authentication
options(soilDB.NASIS.credentials="DSN=nasis_local;UID=NasisSqlRO;PWD=nasisRe@d0n1y")

# update according to win 7 or 10
si <- Sys.info()
if( grepl('windows', si['sysname'], ignore.case = TRUE) & grepl('8|10', si['release'], ignore.case = TRUE) ) {
options(soilDB.NASIS.credentials="DSN=nasis_local;UID=NasisSqlRO;PWD=nasisRe@d0n1y365")
}
}




# setup a new environment to store error messages, etc.
soilDB.env <- new.env(hash=TRUE, parent = parent.frame())

# safely register some options at package load time
.onLoad <- function(libname, pkgname) {

# deprecated: soilDB 2.5.9 CRAN release
options(.soilDB_testNetworkFunctions=TRUE)

# function verbosity
options(soilDB.verbose=FALSE)

# set default local nasis authentication
options(soilDB.NASIS.credentials="DSN=nasis_local;UID=NasisSqlRO;PWD=nasisRe@d0n1y")

# update according to win 7 or 10
si <- Sys.info()
if( grepl('windows', si['sysname'], ignore.case = TRUE) & grepl('8|10', si['release'], ignore.case = TRUE) ) {
options(soilDB.NASIS.credentials="DSN=nasis_local;UID=NasisSqlRO;PWD=nasisRe@d0n1y365")
}
}



30 changes: 11 additions & 19 deletions tests/testthat/test-OSDquery.R
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
context("OSDquery() -- requires internet connection")

test_that("OSDquery() works", {

skip_if_offline()

# hack for in-house testing only
# WWW services aren't always available and will cause CRAN to drop our package if tests fail
if( ! getOption('.soilDB_testNetworkFunctions') ) {
skip("in-house testing only")
}


skip_on_cran()

# a message is printed and NULL returned when no results
res <- suppressMessages(OSDquery(geog_assoc_soils = 'pardee'))

# standard request
expect_true(inherits(res, 'data.frame'))

})


test_that("OSDquery() returns NULL with bogus query", {

skip_if_offline()

# hack for in-house testing only
# WWW services aren't always available and will cause CRAN to drop our package if tests fail
if( ! getOption('.soilDB_testNetworkFunctions') ) {
skip("in-house testing only")
}


skip_on_cran()

# a message is printed and NULL returned when no results
res <- suppressMessages(OSDquery(geog_assoc_soils = 'XXX'))
expect_null(res)

})


113 changes: 52 additions & 61 deletions tests/testthat/test-ROSETTA.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,110 +3,101 @@ context("ROSETTA() -- requires internet connection")
# example data
x <<- structure(
list(
musym = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L),
cokey = c(20392868L, 20392868L, 20392870L, 20392870L, 20392870L,
20392870L, 20393517L, 20394343L, 20394343L, 20394343L),
compname = c("Aluf",
"Aluf", "Hitilo", "Hitilo", "Hitilo", "Hitilo", "Aransas", "Muskogee",
"Muskogee", "Muskogee"),
comppct_r = c(47L, 47L, 22L, 22L, 22L,
22L, 100L, 20L, 20L, 20L),
hzname = c("H1", "H2", "H1", "H2",
"H3", "H4", "H1", "H1", "H2", "H3"),
hzdept_r = c(0L, 127L, 0L,
117L, 137L, 157L, 0L, 0L, 38L, 64L),
hzdepb_r = c(127L, 203L,
117L, 137L, 157L, 203L, 152L, 38L, 64L, 203L),
sandtotal_r = c(94.4,
91, 94.1, 55.4, 59.3, 62.8, 23.3, 11.7, 6.8, 23.3),
silttotal_r = c(0.6,
1, 1.4, 17.6, 13.7, 19.2, 29.2, 69.8, 63.2, 29.2),
claytotal_r = c(5,
8, 4.5, 27, 27, 18, 47.5, 18.5, 30, 47.5),
dbthirdbar_r = c(1.6,
1.55, 1.55, 1.58, 1.58, 1.5, 1.45, 1.38, 1.35, 1.33),
wthirdbar_decimal = c(0.117,
musym = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L),
cokey = c(20392868L, 20392868L, 20392870L, 20392870L, 20392870L,
20392870L, 20393517L, 20394343L, 20394343L, 20394343L),
compname = c("Aluf",
"Aluf", "Hitilo", "Hitilo", "Hitilo", "Hitilo", "Aransas", "Muskogee",
"Muskogee", "Muskogee"),
comppct_r = c(47L, 47L, 22L, 22L, 22L,
22L, 100L, 20L, 20L, 20L),
hzname = c("H1", "H2", "H1", "H2",
"H3", "H4", "H1", "H1", "H2", "H3"),
hzdept_r = c(0L, 127L, 0L,
117L, 137L, 157L, 0L, 0L, 38L, 64L),
hzdepb_r = c(127L, 203L,
117L, 137L, 157L, 203L, 152L, 38L, 64L, 203L),
sandtotal_r = c(94.4,
91, 94.1, 55.4, 59.3, 62.8, 23.3, 11.7, 6.8, 23.3),
silttotal_r = c(0.6,
1, 1.4, 17.6, 13.7, 19.2, 29.2, 69.8, 63.2, 29.2),
claytotal_r = c(5,
8, 4.5, 27, 27, 18, 47.5, 18.5, 30, 47.5),
dbthirdbar_r = c(1.6,
1.55, 1.55, 1.58, 1.58, 1.5, 1.45, 1.38, 1.35, 1.33),
wthirdbar_decimal = c(0.117,
0.129, 0.106, 0.252, 0.261, 0.211, 0.369, 0.286, 0.308, 0.399
),
wfifteenbar_decimal = c(0.049, 0.062, 0.039, 0.181, 0.156,
),
wfifteenbar_decimal = c(0.049, 0.062, 0.039, 0.181, 0.156,
0.122, 0.285, 0.134, 0.165, 0.255)
),
row.names = c(NA, -10L),
class = "data.frame",
),
row.names = c(NA, -10L),
class = "data.frame",
SDA_id = "Table"
)



test_that("ROSETTA() works", {

skip_if_offline()

# hack for in-house testing only
# WWW services aren't always available and will cause CRAN to drop our package if tests fail
if( ! getOption('.soilDB_testNetworkFunctions') ) {
skip("in-house testing only")
}


skip_on_cran()

# attempting to use all possible soil properties
vars <- c('sandtotal_r', 'silttotal_r', 'claytotal_r', 'dbthirdbar_r', 'wthirdbar_decimal', 'wfifteenbar_decimal')

# submit request
r <- ROSETTA(x, vars = vars)

# correct object
expect_true(inherits(r, 'data.frame'))

# input / output are conformal
expect_true(nrow(r) == nrow(x))

# output contains new columns
expect_true(all(c('theta_r', 'theta_s', 'alpha', 'npar', 'ksat') %in% names(r)))

})

test_that("correct model selection in the presence of NA", {

skip_if_offline()

# hack for in-house testing only
# WWW services aren't always available and will cause CRAN to drop our package if tests fail
if( ! getOption('.soilDB_testNetworkFunctions') ) {
skip("in-house testing only")
}



skip_on_cran()

# sprinkle NA
x$dbthirdbar_r[1] <- NA
x$wthirdbar_decimal[2] <- NA
x$wfifteenbar_decimal[3] <- NA
x$sandtotal_r[9] <- NA
x[10, ] <- NA

# attempting to use all possible soil properties
vars <- c('sandtotal_r', 'silttotal_r', 'claytotal_r', 'dbthirdbar_r', 'wthirdbar_decimal', 'wfifteenbar_decimal')

# submit request
r <- ROSETTA(x, vars = vars)

# missing 1/3 bar Db -> model 2
expect_true(r$.rosetta.model[1] == 2)

# missing 1/3 bar WT -> model 3
expect_true(r$.rosetta.model[2] == 3)

# missing 15 bar WT -> model 4
expect_true(r$.rosetta.model[3] == 4)

# no records missing -> model 5
expect_true(all(r$.rosetta.model[4:8] == 5))

# missing sand -> NA
expect_true(is.na(r$theta_r[9]) & (r$.rosetta.model[9] == -1))

# all NA
expect_true(all(is.na(r[10, c('theta_r', 'theta_s', 'alpha', 'npar', 'ksat')])))


})
48 changes: 8 additions & 40 deletions tests/testthat/test-SDA_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ test_that("SDA_query() works", {

skip_if_offline()

# hack for in-house testing only
# WWW services aren't always available and will cause CRAN to drop our package if tests fail
if( ! getOption('.soilDB_testNetworkFunctions') ) {
skip("in-house testing only")
}
skip_on_cran()

## sample data

Expand Down Expand Up @@ -40,11 +36,7 @@ test_that("SDA_query() returns expected result", {

skip_if_offline()

# hack for in-house testing only
# WWW services aren't always available and will cause CRAN to drop our package if tests fail
if( ! getOption('.soilDB_testNetworkFunctions') ) {
skip("in-house testing only")
}
skip_on_cran()

# table dimensions
expect_equal(nrow(x.1), 1)
Expand All @@ -61,11 +53,7 @@ test_that("SDA_query() SQL error / no results -> NULL", {

skip_if_offline()

# hack for in-house testing only
# WWW services aren't always available and will cause CRAN to drop our package if tests fail
if( ! getOption('.soilDB_testNetworkFunctions') ) {
skip("in-house testing only")
}
skip_on_cran()

# bad SQL should result in a warning and try-error result.
expect_true(inherits(expect_warning(SDA_query("SELECT this from that")), 'try-error'))
Expand All @@ -81,11 +69,7 @@ test_that("SDA_spatialQuery() simple spatial query, tabular results", {

skip_if_offline()

# hack for in-house testing only
# WWW services aren't always available and will cause CRAN to drop our package if tests fail
if( ! getOption('.soilDB_testNetworkFunctions') ) {
skip("in-house testing only")
}
skip_on_cran()

res <- suppressWarnings(SDA_spatialQuery(p, what = 'mukey'))

Expand All @@ -101,11 +85,7 @@ test_that("SDA_spatialQuery() simple spatial query, spatial results", {

skip_if_offline()

# hack for in-house testing only
# WWW services aren't always available and will cause CRAN to drop our package if tests fail
if( ! getOption('.soilDB_testNetworkFunctions') ) {
skip("in-house testing only")
}
skip_on_cran()

# test with default db = "SSURGO"
res <- suppressWarnings(SDA_spatialQuery(p, what = 'geom'))
Expand All @@ -128,11 +108,7 @@ test_that("SDA_query() interprets column names", {

skip_if_offline()

# hack for in-house testing only
# WWW services aren't always available and will cause CRAN to drop our package if tests fail
if( ! getOption('.soilDB_testNetworkFunctions') ) {
skip("in-house testing only")
}
skip_on_cran()

# x.3 is from the component table
expect_equal(
Expand All @@ -148,11 +124,7 @@ test_that("SDA_query() interprets data type correctly", {

skip_if_offline()

# hack for in-house testing only
# WWW services aren't always available and will cause CRAN to drop our package if tests fail
if( ! getOption('.soilDB_testNetworkFunctions') ) {
skip("in-house testing only")
}
skip_on_cran()

# x.3 is from the component table
expect_true(inherits(x.3$mukey, 'integer'))
Expand All @@ -173,11 +145,7 @@ test_that("SDA_query() works with multi-line records", {

skip_if_offline()

# hack for in-house testing only
# WWW services aren't always available and will cause CRAN to drop our package if tests fail
if( ! getOption('.soilDB_testNetworkFunctions') ) {
skip("in-house testing only")
}
skip_on_cran()

# https://github.com/ncss-tech/soilDB/issues/28
expect_true(inherits(x.4, 'data.frame'))
Expand Down
Loading

0 comments on commit bcb76c3

Please sign in to comment.