Skip to content

Commit 0a3f693

Browse files
authored
Merge pull request #329 from USEPA/314-tcplLoadChem-bug
314 update tcplLoadChem/tests for bug fix involving missing spid col
2 parents 759b6b9 + 874501e commit 0a3f693

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Diff for: R/tcplLoadChem.R

+3-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ tcplLoadChem <- function(field = NULL, val = NULL, exact = TRUE,
5050
include.spid = TRUE) {
5151

5252
if (getOption("TCPL_DRVR") == "API") {
53-
if (tolower(field) != "spid") stop("When drvr option is set to 'API', only 'spid' is a valid 'field' value.")
53+
if (is.null(field) || tolower(field) != "spid") stop("When drvr option is set to 'API', only 'spid' is a valid 'field' value.")
5454
if (!exact) exact <- TRUE
5555
dat <- tcplQueryAPI(resource = "data", fld = "spid", val = val, return_flds = c("spid", "chid", "casn", "chnm", "dsstox_substance_id"))
5656
if (!length(colnames(dat))) {
@@ -65,9 +65,9 @@ tcplLoadChem <- function(field = NULL, val = NULL, exact = TRUE,
6565
if (!is.null(field)) {
6666
vfield <- c("chid", "spid", "chnm", "casn", "code", "chem.only","dsstox_substance_id")
6767
if (!field %in% vfield) stop("Invalid 'field' value.")
68+
if (field == "chem.only") include.spid <- FALSE
6869
}
6970

70-
7171
qstring <- .ChemQ(field = field, val = val, exact = exact)
7272

7373
dat <- tcplQuery(query = qstring, tbl=tbl)
@@ -86,9 +86,7 @@ tcplLoadChem <- function(field = NULL, val = NULL, exact = TRUE,
8686

8787
if (include.spid) return(unique(dat, by = c("spid", "chid")))
8888

89-
dat <- unique(dat[ , list(chid, chnm, casn, code, dsstox_substance_id)])
90-
91-
unique(dat, by = c("spid", "chid"))[]
89+
unique(dat[ , list(chid, chnm, casn, code, dsstox_substance_id)])
9290

9391
}
9492

Diff for: tests/testthat/test-tcplLoadChem.R

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ httptest::with_mock_dir("ctx", {
1818
expect_true(all(c("spid", "chid", "casn", "chnm", "dsstox_substance_id", "code") %in% colnames(dat)))
1919
expect_equal(dat$spid, test_api$spid)
2020
})
21+
test_that("include.spid = FALSE works", {
22+
expect_no_error(dat <- tcplLoadChem(field = "spid", val = test_api$spid, include.spid = FALSE))
23+
expect_true(is.data.table(dat))
24+
expect_equal(nrow(dat), 1)
25+
expect_true(all(c("chid", "casn", "chnm", "dsstox_substance_id", "code") %in% colnames(dat)))
26+
})
2127
test_that("exact is silently changed to TRUE if driver is API", {
2228
expect_no_error(dat <- tcplLoadChem(field = "spid", val = test_api$spid, exact = FALSE))
2329
expect_true(is.data.table(dat))
@@ -27,6 +33,7 @@ httptest::with_mock_dir("ctx", {
2733
})
2834
#error cases
2935
test_that("field can only be spid if driver is API", {
36+
expect_error(tcplLoadChem(), "When drvr option is set to 'API', only 'spid' is a valid 'field' value.")
3037
expect_error(tcplLoadChem(field = "chnm", val = "fakechnm"), "When drvr option is set to 'API', only 'spid' is a valid 'field' value.")
3138
})
3239
test_that("data not found results in null data.table", {

0 commit comments

Comments
 (0)