Skip to content

Commit

Permalink
tests made robust to failing APIS, #125
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanbeaudette committed Jan 27, 2020
1 parent 1bfb235 commit 112e191
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: soilDB
Type: Package
Title: Soil Database Interface
Version: 2.5
Date: 2020-01-23
Date: 2020-01-27
Authors@R: c(person(given="Dylan", family="Beaudette", role = c("cre", "aut"), email = "[email protected]"), person(given="Jay", family="Skovlin", role = c("aut")), person(given="Stephen", family="Roecker", role = c("aut")) )
Author: Dylan Beaudette [cre, aut], Jay Skovlin [aut], Stephen Roecker [aut]
Maintainer: Dylan Beaudette <[email protected]>
Expand Down
13 changes: 12 additions & 1 deletion R/openNASISchannel.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,15 @@
stop("no local NASIS database available", call.=FALSE)

return(channel)
}
}

# check for presence of `nasis_local` ODBC data source
# primarily used in test suite
.local_NASIS_defined <- function() {
# check for user-defined
if('nasis_local' %in% names(RODBC::odbcDataSources())) {
return(TRUE)
} else {
return(FALSE)
}
}
1 change: 0 additions & 1 deletion tests/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ library(soilDB)




test_check("soilDB")
12 changes: 12 additions & 0 deletions tests/testthat/test-OSDquery.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

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

Expand All @@ -17,6 +23,12 @@ 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# a message is printed and NULL returned when no results
res <- suppressMessages(OSDquery(geog_assoc_soils = 'XXX'))
expect_null(res)
Expand Down
48 changes: 48 additions & 0 deletions tests/testthat/test-SDA_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

## sample data

# single-table result
Expand Down Expand Up @@ -34,6 +40,12 @@ 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# table dimensions
expect_equal(nrow(x.1), 1)
expect_equal(ncol(x.1), 2)
Expand All @@ -49,6 +61,12 @@ 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# bad SQL should result in a local error
expect_error(SDA_query("SELECT this from that"))

Expand All @@ -63,6 +81,12 @@ 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

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

# testing known values
Expand All @@ -77,6 +101,12 @@ 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

res <- SDA_spatialQuery(p, what = 'geom')

# testing known values
Expand All @@ -89,6 +119,12 @@ 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# x.3 is from the component table
expect_equal(
names(x.3),
Expand All @@ -103,6 +139,12 @@ 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# x.3 is from the component table
expect_true(inherits(x.3$mukey, 'integer'))
expect_true(inherits(x.3$cokey, 'integer'))
Expand All @@ -122,6 +164,12 @@ 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# https://github.com/ncss-tech/soilDB/issues/28
expect_true(inherits(x.4, 'data.frame'))
expect_true(nrow(x.4) == 6)
Expand Down
36 changes: 36 additions & 0 deletions tests/testthat/test-fetchKSSL.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ test_that("fetchKSSL() 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

## sample data
x <<- fetchKSSL(series='sierra')
x.morph <<- fetchKSSL(series='sierra', returnMorphologicData = TRUE)
Expand All @@ -20,6 +26,12 @@ test_that("fetchKSSL() returns an SPC or list", {

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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# SPC + morphologic data
expect_true(inherits(x.morph, 'list'))
expect_true(inherits(x.morph$SPC, 'SoilProfileCollection'))
Expand All @@ -35,6 +47,12 @@ test_that("fetchKSSL() returns reasonable data", {

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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# standard request
expect_equal(nrow(site(x)) > 0, TRUE)
expect_equal(nrow(horizons(x)) > 0, TRUE)
Expand All @@ -47,6 +65,12 @@ test_that("fetchKSSL() returns data associated with named series (sierra)", {

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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# all of the results should contain the search term
f <- grepl('sierra', x$taxonname, ignore.case = TRUE)
expect_equal(all(f), TRUE)
Expand All @@ -58,6 +82,12 @@ test_that("fetchKSSL() 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# a message is printed and NULL returned when no results
res <- suppressMessages(fetchKSSL(series='XXX'))
expect_null(res)
Expand All @@ -69,6 +99,12 @@ test_that("fetchKSSL() fails gracefully when morphology data are missing", {

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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# pedon_key 37457 is missing:
# * most lab data
# * all morphologic data
Expand Down
29 changes: 17 additions & 12 deletions tests/testthat/test-fetchNASIS.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ context("fetchNASIS() -- requires local NASIS and ODBC connection")
# * pedons / component missing from local database


check_local_NASIS <- function() {
# check for connection
if(! 'nasis_local' %in% names(RODBC::odbcDataSources())) {
skip("local NASIS database not available")
}
}


check_local_NASIS_pedons_available <- function() {

# attempt to load pedons
Expand Down Expand Up @@ -55,7 +47,11 @@ check_local_NASIS_components_available <- function() {
test_that("fetchNASIS(from='pedons') returns reasonable data", {

# test for conditions permitting this test to run
check_local_NASIS()
if(! soilDB:::.local_NASIS_defined()) {
skip("local NASIS database not available")
}

# pedons must be present for tests
check_local_NASIS_pedons_available()

# get data
Expand All @@ -79,7 +75,11 @@ test_that("fetchNASIS(from='pedons') returns reasonable data", {
test_that("fetchNASIS(from='pedons') nullFragsAreZero works as expected", {

# test for conditions permitting this test to run
check_local_NASIS()
if(! soilDB:::.local_NASIS_defined()) {
skip("local NASIS database not available")
}

# components must be present for tests
check_local_NASIS_pedons_available()

# get data
Expand All @@ -94,8 +94,13 @@ test_that("fetchNASIS(from='pedons') nullFragsAreZero works as expected", {

test_that("fetchNASIS(from='components') returns reasonable data", {

# test for conditions permitting this test to run
check_local_NASIS()
# hack for in-house testing only
# WWW services aren't always available and will cause CRAN to drop our package if tests fail
if(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# must have components to complete test
check_local_NASIS_components_available()

# get data
Expand Down
15 changes: 6 additions & 9 deletions tests/testthat/test-fetchNASISLabData.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ context("fetchNASISLabData() -- requires local NASIS and ODBC connection")
# * pedons / component missing from local database


check_local_NASIS <- function() {
# check for connection
if(! 'nasis_local' %in% names(RODBC::odbcDataSources())) {
skip("local NASIS database not available")
}
}


check_local_NASIS_labdata_available <- function() {

Expand All @@ -30,7 +23,11 @@ check_local_NASIS_labdata_available <- function() {
test_that("fetchNASISLabData returns reasonable data", {

# test for conditions permitting this test to run
check_local_NASIS()
if(! soilDB:::.local_NASIS_defined()) {
skip("local NASIS database not available")
}

# test for lab data to check
check_local_NASIS_labdata_available()

# get data
Expand All @@ -44,4 +41,4 @@ test_that("fetchNASISLabData returns reasonable data", {
expect_equal(idname(x), 'labpeiid')
expect_equal(hzidname(x), 'labphiid')
expect_equal(horizonDepths(x), c("hzdept", "hzdepb"))
})
})
30 changes: 30 additions & 0 deletions tests/testthat/test-fetchNASISWebReport.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ test_that("fetchNASISWebReport() 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

## sample data
pn <- "MLRA 18 - Sierra sandy loam, 2 to 9 percent slopes"

Expand All @@ -19,6 +25,12 @@ test_that("fetchNASISWebReport() returns an SPC of component/horizon data and da

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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# SPC + morphologic data
expect_true(inherits(x$spc, 'SoilProfileCollection'))
expect_true(inherits(x$mapunit, 'data.frame'))
Expand All @@ -30,6 +42,12 @@ test_that("fetchNASISWebReport() returns reasonable data", {

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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# standard request
expect_equal(nrow(site(x$spc)) > 0, TRUE)
expect_equal(nrow(horizons(x$spc)) > 0, TRUE)
Expand All @@ -43,6 +61,12 @@ test_that("fetchNASISWebReport() returns data for component name (Sierra)", {

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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# all major components are Sierra
f <- grepl('Sierra', x$spc$compname[x$spc$majcompflag == 1], ignore.case = TRUE)
expect_equal(all(f), TRUE)
Expand All @@ -54,6 +78,12 @@ test_that("fetchNASISWebReport() 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(! soilDB:::.local_NASIS_defined()) {
skip("in-house testing only")
}

# a message is printed and NULL returned when no results
res <- suppressMessages(fetchNASISWebReport(projectname='XXX'))
expect_null(res)
Expand Down
Loading

0 comments on commit 112e191

Please sign in to comment.