Skip to content

Commit

Permalink
Update unit tests to reduce network requests to D1
Browse files Browse the repository at this point in the history
  • Loading branch information
gothub committed Jun 20, 2018
1 parent 09fa393 commit 8585f2b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 85 deletions.
1 change: 1 addition & 0 deletions tests/testthat/helper-base.R
@@ -1,6 +1,7 @@
# Reduce redundant calls to the same service - this only needs to be made
# once per test
cnProd <- CNode()
cnStaging2 <- CNode("STAGING2")
d1cKNB <- D1Client("PROD", "urn:node:KNB")
mnKNB <- d1cKNB@mn
d1cProd <- D1Client("PROD")
Expand Down
22 changes: 6 additions & 16 deletions tests/testthat/test.CNode.R
Expand Up @@ -5,20 +5,16 @@ test_that("dataone library loads", {
test_that("CNode constructors", {
library(dataone)
# If not specified, "PROD" environment is used.
#cn <- CNode()
expect_match(cnProd@endpoint, "https://cn.dataone.org/cn")
#cn <- CNode("PROD")
expect_match(cnProd@endpoint, "https://cn.dataone.org/cn")
# Skip unstable test environments.
skip_on_cran()
cn <- CNode("STAGING2")
expect_match(cn@endpoint, "https://cn-stage-2.test.dataone.org/cn")
expect_match(cnStaging2@endpoint, "https://cn-stage-2.test.dataone.org/cn")
#cn <- CNode("DEV")
#expect_match(cn@endpoint, "https://cn-dev.test.dataone.org/cn")
})
test_that("CNode listNodes()", {
library(dataone)
#cn <- CNode("PROD")
nodelist <- listNodes(cnProd)
expect_that(length(nodelist) > 0, is_true())
expect_match(class(nodelist[[1]]), "Node")
Expand All @@ -34,7 +30,6 @@ test_that("CNode listNodes()", {
test_that("CNode getObject()", {
library(dataone)
library(XML)
#cn <- CNode("PROD")
pid <- "aceasdata.3.2"
obj <- getObject(cnProd, pid)
if(is.null(obj) || class(obj) != "raw") {
Expand All @@ -51,15 +46,13 @@ test_that("CNode getObject()", {

test_that("CNode getSystemMetadata()", {
library(dataone)
#cn <- CNode("PROD")
pid <- "aceasdata.3.2"
sysmeta <- getSystemMetadata(cnProd, pid)
expect_match(sysmeta@identifier, pid)
})

test_that("CNode describeObject()", {
library(dataone)
#cn <- CNode("PROD")
pid <- "aceasdata.3.2"
res <- dataone::describeObject(cnProd, pid)
expect_is(res, "list")
Expand All @@ -69,7 +62,6 @@ test_that("CNode describeObject()", {
test_that("CNode getMNode()", {
library(dataone)
skip_on_cran()
#cn <- CNode("PROD")
nodelist <- listNodes(cnProd)
nodeid <- nodelist[[length(nodelist)]]@identifier
newnode <- getMNode(cnProd, nodeid)
Expand All @@ -84,7 +76,6 @@ test_that("CNode getMNode()", {

test_that("CNode resolve()",{
library(dataone)
#cn <- CNode("PROD")
id <- "0d7d8e0e-93f5-40ab-9916-501d7cf93e15"
res <- resolve(cnProd,id)
expect_match(res$id, id)
Expand All @@ -96,27 +87,26 @@ test_that("CNode reserveIdentifier(), hasReservation() works",{
skip_on_cran()
library(dataone)
library(uuid)
cn <- CNode("STAGING2")

# For hasReservation(), we have to use the same subject that is in the authorization token or X.509 certificate.
# Until the dataone package can decrypt auth tokens, we have to manually provide same subject
# used by reserveIdentifier.
am <- AuthenticationManager()
# Suppress openssl, cert missing warnings
suppressMessages(authValid <- dataone:::isAuthValid(am, cn))
suppressMessages(authValid <- dataone:::isAuthValid(am, cnStaging2))
# First check if authentication is available and if not, skip this test
if (authValid) {
# TODO: remove this check when Mac OS X can be used with certificates
if(dataone:::getAuthMethod(am, cn) == "cert" && grepl("apple-darwin", sessionInfo()$platform)) skip("Skip authentication w/cert on Mac OS X")
if(dataone:::getAuthMethod(am, cnStaging2) == "cert" && grepl("apple-darwin", sessionInfo()$platform)) skip("Skip authentication w/cert on Mac OS X")
# Set 'subject' to authentication subject, if available, so this userId can check a reservation that it made
subject <- dataone:::getAuthSubject(am, cn)
subject <- dataone:::getAuthSubject(am, cnStaging2)
myId <- sprintf("urn:uuid:%s", UUIDgenerate())
# researveIdentifier will create the reservation using only the client subject from
# the current authentication method - either auth token or certificate.
newId <- reserveIdentifier(cn, myId)
newId <- reserveIdentifier(cnStaging2, myId)
expect_match(myId, newId)
# Have to specify the subject for hasReservation
hasRes <- hasReservation(cn, newId, subject=subject)
hasRes <- hasReservation(cnProd, newId, subject=subject)
expect_true(hasRes, info=sprintf("Didn't find reserved identifier %s", myId))
} else {
skip("This test requires valid authentication.")
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test.D1Client.R
Expand Up @@ -18,8 +18,7 @@ test_that("D1Client constructors", {
# Skip the remainder of the tests because these test environments are
# often down due to upgrades, reconfiguring, testing new features.
skip_on_cran()
cn <- CNode("STAGING2")
cli <- new("D1Client", cn=cn, mn=getMNode(cn, "urn:node:mnTestKNB"))
cli <- new("D1Client", cn=cnStaging2, mn=getMNode(cnStaging2, "urn:node:mnTestKNB"))
expect_false(is.null(cli))
expect_match(class(cli), "D1Client")
expect_match(cli@cn@baseURL, "https://cn.stage-2.test.dataone.org/cn")
Expand Down
79 changes: 36 additions & 43 deletions tests/testthat/test.D1Node.R
Expand Up @@ -5,22 +5,21 @@ test_that("dataone library loads", {

test_that("CNode ping", {
library(dataone)
cn <- CNode("PROD")
alive <- ping(cn)
# 'cnProd' is defined in 'helper-base.R' for all tests
alive <- ping(cnProd)
expect_true(alive)
})

test_that("CNode object index query works with query list param", {
library(dataone)
# Test query of CN object index using query string
queryParams <- "q=id:doi*&rows=2&wt=xml"
cn <- CNode("PROD")
am <- AuthenticationManager()
suppressMessages(authValid <- dataone:::isAuthValid(am, cn))
suppressMessages(authValid <- dataone:::isAuthValid(am, cnProd))
if (authValid) {
if(getAuthMethod(am, cn) == "cert" && grepl("apple-darwin", sessionInfo()$platform)) skip("Skip authentication w/cert on Mac OS X")
if(getAuthMethod(am, cnProd) == "cert" && grepl("apple-darwin", sessionInfo()$platform)) skip("Skip authentication w/cert on Mac OS X")
}
result <- query(cn, queryParams, as="list")
result <- query(cnProd, queryParams, as="list")
#resultList <- parseSolrResult(result)
expect_true(length(result) == 2)
expect_match(result[[2]]$id, "doi:")
Expand All @@ -30,7 +29,7 @@ test_that("CNode object index query works with query list param", {

# Test query of CN object index using query list
queryParamList <- list(q="id:doi*", rows="5", fq="(abstract:chlorophyll AND dateUploaded:[2000-01-01T00:00:00Z TO NOW])", fl="title,id,abstract,size,dateUploaded,attributeName", wt="xml")
result <- query(cn, queryParamList, as="list")
result <- query(cnProd, queryParamList, as="list")
expect_true(length(result) > 0)
expect_match(result[[1]]$id, "doi:")
size <- result[[1]]$size
Expand All @@ -41,42 +40,41 @@ test_that("CNode object index query works with query list param", {
# Test a query that contains embedded quotes
queryParamList <- list(q="(attribute:lake) and (attribute:\"Percent Nitrogen\")", rows="10",
fl="title,id,abstract,size,dateUploaded,attributeName", wt="xml")
result <- query(cn, queryParamList, as="data.frame")
result <- query(cnProd, queryParamList, as="data.frame")
expect_true(class(result) == "data.frame")
expect_true(nrow(result) > 0)

# Test if query can handle solr syntax error
queryParamList <- list(q="(attribute:lake) and attribute:\"Percent Nitrogen\")", rows="10",
fl="title,id,abstract,size,dateUploaded,attributeName", wt="xml")
result <- query(cn, queryParamList, as="data.frame")
result <- query(cnProd, queryParamList, as="data.frame")
expect_true(is.null(result))

# Test if query can handle solr syntax error (mispelled field name "attr")
queryParamList <- list(q="(attribute:lake) and attr:\"Percent Nitrogen\")", rows="10",
fl="title,id,abstract,size,dateUploaded,attributeName", wt="xml")
result <- query(cn, queryParamList, as="data.frame")
result <- query(cnProd, queryParamList, as="data.frame")
expect_true(is.null(result))
})

test_that("Object listing works for CNode, MNode", {
library(dataone)

# Note: this test assumes that there are at least 5 EML 2.1.0 documents in DataONE
cn <- CNode("PROD")
fromDate <- "2001-01-01T01:01:01.000+00:00"
toDate <- "2015-12-31T01:01:01.000+00:00"
formatId <- "eml://ecoinformatics.org/eml-2.1.0"
start <- 0
count <- 5
objects <- listObjects(cn, fromDate=fromDate, toDate=toDate, formatId=formatId, start=start, count=count)
objects <- listObjects(cnProd, fromDate=fromDate, toDate=toDate, formatId=formatId, start=start, count=count)
# The XML parser used in listObjects creates one more element than returned elements, used to hold attributes?
expect_equal(length(objects) - 1, count)
for (i in 1:(length(objects)-1) ) {
expect_match(objects[i]$objectInfo$formatId, formatId)
}
# Note: this test assumes that there are at least 5 EML 2.1.0 documents in KNB
mn <- getMNode(cn, "urn:node:KNB")
objects <- listObjects(cn, fromDate=fromDate, toDate=toDate, formatId=formatId, start=start, count=count)
#mn <- getMNode(cnProd, "urn:node:KNB")
objects <- listObjects(cnProd, fromDate=fromDate, toDate=toDate, formatId=formatId, start=start, count=count)
# The XML parser used in listObjects creates one more element than returned elements, used to hold attributes?
expect_equal(length(objects) - 1, count)
for (i in 1:(length(objects)-1) ) {
Expand All @@ -86,33 +84,31 @@ test_that("Object listing works for CNode, MNode", {
# Test invalid input
fromDate <- "20-01-01T01:01:01.000+00:00" # Invalid year
toDate <- "2015-12-31T01:01:01.000+00:00" # valid
err <- try(objects <- listObjects(cn, fromDate=fromDate, toDate=toDate, formatId=formatId, start=start, count=count), silent=TRUE)
err <- try(objects <- listObjects(cnProd, fromDate=fromDate, toDate=toDate, formatId=formatId, start=start, count=count), silent=TRUE)
expect_match(class(err), ("try-error"))

fromDate <- "2013-01-01T01:01:01.000+00:00" # valid
toDate <- "01/01/15" # Invalid - not ISO 8601
try(objects <- listObjects(cn, fromDate=fromDate, toDate=toDate, formatId=formatId, start=start, count=count), silent=TRUE)
try(objects <- listObjects(cnProd, fromDate=fromDate, toDate=toDate, formatId=formatId, start=start, count=count), silent=TRUE)
expect_match(class(err), ("try-error"))

})

test_that("listQueryEngines, getQueryEngineDescription works for CNode, MNode", {
library(dataone)

#cn <- CNode("STAGING2")
# Get list of query engines for a CN, and get description for each engine
cn <- CNode("PROD")
engines <- listQueryEngines(cn)
engines <- listQueryEngines(cnProd)
expect_gt(length(engines), 0)
for (i in 1:length(engines)) {
engineDesc <- getQueryEngineDescription(cn, engines[[i]])
engineDesc <- getQueryEngineDescription(cnProd, engines[[i]])
expect_gt(length(engineDesc), 0)
expect_match(engineDesc$name, engines[[i]])
expect_true(class(engineDesc$queryFields) == "data.frame")
}

# Get list of query engines for an MN, and get description for each engine
mn <- getMNode(cn, "urn:node:KNB")
mn <- getMNode(cnProd, "urn:node:KNB")
engines <- listQueryEngines(mn)
expect_gt(length(engines), 0)
for (i in 1:length(engines)) {
Expand All @@ -127,16 +123,15 @@ test_that("listQueryEngines, getQueryEngineDescription works for CNode, MNode",
test_that("CNode object index query works with query string param", {
library(dataone)

cn <- CNode("PROD")
am <- AuthenticationManager()
suppressMessages(authValid <- dataone:::isAuthValid(am, cn))
suppressMessages(authValid <- dataone:::isAuthValid(am, cnProd))
if (authValid) {
if(getAuthMethod(am, cn) == "cert" && grepl("apple-darwin", sessionInfo()$platform)) skip("Skip authentication w/cert on Mac OS X")
if(getAuthMethod(am, cnProd) == "cert" && grepl("apple-darwin", sessionInfo()$platform)) skip("Skip authentication w/cert on Mac OS X")
}
# This test assumes that there are at least two pids on KNB that are DOIs (i.e. begin with "doi:")
# This test requests two results and checks that 2 results are returned.
queryParams <- "q=id:doi*&rows=2&wt=xml"
result <- query(cn, queryParams, as="list")
result <- query(cnProd, queryParams, as="list")
expect_true(length(result) == 2)
expect_match(result[[2]]$id, "doi:")
size <- result[[1]]$size
Expand All @@ -147,14 +142,14 @@ test_that("MNode object index query works", {
library(dataone)
queryParams <- "q=id:doi*&rows=2&wt=xml"
#queryParams <- 'q=attribute:"net primary production" AND (abstract:"above ground" OR title:"above ground")'
mn_uri <- "https://knb.ecoinformatics.org/knb/d1/mn/v2"
mn <- MNode(mn_uri)
#mn_uri <- "https://knb.ecoinformatics.org/knb/d1/mn/v2"
#mn <- MNode(mn_uri)
am <- AuthenticationManager()
suppressMessages(authValid <- dataone:::isAuthValid(am, mn))
suppressMessages(authValid <- dataone:::isAuthValid(am, mnKNB))
if (authValid) {
if(getAuthMethod(am, mn) == "cert" && grepl("apple-darwin", sessionInfo()$platform)) skip("Skip authentication w/cert on Mac OS X")
if(getAuthMethod(am, mnKNB) == "cert" && grepl("apple-darwin", sessionInfo()$platform)) skip("Skip authentication w/cert on Mac OS X")
}
result <- query(mn, queryParams, as="list")
result <- query(mnKNB, queryParams, as="list")
expect_true(length(result) > 0)
pid <- result[[1]]$id
expect_is(pid, "character")
Expand All @@ -171,11 +166,11 @@ test_that("MNode object index query works", {
}

# Request that an XML object is returned
result <- query(mn, queryParams, as="xml", parse=TRUE)
result <- query(mnKNB, queryParams, as="xml", parse=TRUE)
expect_is(result, "XMLInternalDocument")

# Request that a character object is returned
result <- query(mn, queryParams, as="xml", parse=FALSE)
result <- query(mnKNB, queryParams, as="xml", parse=FALSE)
expect_is(result, "character")
expect_match(result, "<?xml")
})
Expand All @@ -189,7 +184,6 @@ test_that("D1Node archive() works",{
write.csv(testdf, csvfile, row.names=FALSE)
#mnId <- "urn:node:mnStageUCSB2"
#d1c <- new("D1Client", env="STAGING", mNodeid=mnId)
d1cTest
am <- AuthenticationManager()
suppressMessages(authValid <- dataone:::isAuthValid(am, d1cTest@mn))
if (authValid) {
Expand Down Expand Up @@ -223,28 +217,27 @@ test_that("D1Node archive() works",{
test_that("D1Node isAuthorized() works",{
skip_on_cran()
library(dataone)
cn <- CNode("PROD")
am <- AuthenticationManager()
suppressMessages(authValid <- dataone:::isAuthValid(am, cn))
suppressMessages(authValid <- dataone:::isAuthValid(am, cnProd))
# Don't use a cert on Mac OS X
if (authValid) {
if(getAuthMethod(am, cn) == "cert" && grepl("apple-darwin", sessionInfo()$platform)) skip("Skip authentication w/cert on Mac OS X")
if(getAuthMethod(am, cnProd) == "cert" && grepl("apple-darwin", sessionInfo()$platform)) skip("Skip authentication w/cert on Mac OS X")
}
# Send an authorization check to the D1 production CN.
canRead <- isAuthorized(cn, "doi:10.6073/pasta/7fcb8fea57843fae65f63094472f502d", "read")
canRead <- isAuthorized(cnProd, "doi:10.6073/pasta/7fcb8fea57843fae65f63094472f502d", "read")
expect_true(canRead)
canWrite <- isAuthorized(cn, "doi:10.6073/pasta/7fcb8fea57843fae65f63094472f502d", "write")
canWrite <- isAuthorized(cnProd, "doi:10.6073/pasta/7fcb8fea57843fae65f63094472f502d", "write")
expect_false(canWrite)
canChange <- isAuthorized(cn, "doi:10.6073/pasta/7fcb8fea57843fae65f63094472f502d", "changePermission")
canChange <- isAuthorized(cnProd, "doi:10.6073/pasta/7fcb8fea57843fae65f63094472f502d", "changePermission")
expect_false(canChange)

# Now send a check to a member node.
mn <- getMNode(cn, "urn:node:KNB")
canRead <- isAuthorized(mn, "doi:10.6085/AA/pisco_recruitment.149.1", "read")
#mn <- getMNode(cnProd, "urn:node:KNB")
canRead <- isAuthorized(mnKNB, "doi:10.6085/AA/pisco_recruitment.149.1", "read")
expect_true(canRead)
canWrite <- isAuthorized(mn, "doi:10.6085/AA/pisco_recruitment.149.1", "write")
canWrite <- isAuthorized(mnKNB, "doi:10.6085/AA/pisco_recruitment.149.1", "write")
expect_false(canWrite)
canChange <- isAuthorized(mn, "doi:10.6085/AA/pisco_recruitment.149.1", "changePermission")
canChange <- isAuthorized(mnKNB, "doi:10.6085/AA/pisco_recruitment.149.1", "changePermission")
expect_false(canChange)
})

0 comments on commit 8585f2b

Please sign in to comment.