Skip to content

Commit

Permalink
added oai fxn tests, made id param in getrecord mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Feb 11, 2016
1 parent 035786f commit d2ac600
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 22 deletions.
15 changes: 5 additions & 10 deletions R/dc_oai_getrecord.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@
#' dc_oai_getrecord(temp$identifier[1:2])
#' }

dc_oai_getrecord <- function(id = NULL) {
if (!is.null(id)) {
tt <- sapply(id, function(z) grepl('oai:oai.datacite.org', z))
if (!all(tt)) {
id <- unname(sapply(id, strextract, pattern = "[0-9]+"))
id <- paste("oai:oai.datacite.org:", id, sep = "")
}
} else {
id <- NULL
dc_oai_getrecord <- function(id) {
tt <- sapply(id, function(z) grepl('oai:oai.datacite.org', z))
if (!all(tt)) {
id <- unname(sapply(id, strextract, pattern = "[0-9]+"))
id <- paste("oai:oai.datacite.org:", id, sep = "")
}

oai::get_records(ids = id, prefix = "oai_dc", url = dc_oai_base())
}
12 changes: 4 additions & 8 deletions R/dc_oai_listmetadataformats.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#' List available metadata formats that the DataCite repository can disseminate
#'
#' @export
#' @param id DataCite identifier, e.g., "56225"
#' @param id DataCite identifier, e.g., "oai:oai.datacite.org:6718729". If left
#' blank, get all metadataformats
#' @param ... Curl options passed on to \code{\link[httr]{GET}}
#' @examples \dontrun{
#' dc_oai_listmetadataformats()
#' dc_oai_listmetadataformats("56225")
#' dc_oai_listmetadataformats("oai:oai.datacite.org:6718729")
#' }
dc_oai_listmetadataformats <- function(id = NULL, ...) {
if (!is.null(id)) {
oaiid <- paste("oai:oai.datacite.org:", id, sep = "")
} else {
oaiid <- NULL
}
oai::list_metadataformats(dc_oai_base(), oaiid, ...)
oai::list_metadataformats(dc_oai_base(), id, ...)
}
5 changes: 3 additions & 2 deletions man/dc_oai_getrecord.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/dc_oai_listmetadataformats.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions tests/testthat/test-dc_oai_getrecord.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
context("dc_oai_getrecord")

test_that("dc_oai_getrecord - basic functionality works", {
skip_on_cran()

aa <- dc_oai_getrecord("oai:oai.datacite.org:32255")

expect_is(aa, "data.frame")
expect_is(aa, "oai_df")
expect_is(aa$identifier, "character")
expect_is(aa$title, "character")
})

test_that("dc_oai_getrecord - many record Ids input works", {
skip_on_cran()

recs <- c("oai:oai.datacite.org:32255", "oai:oai.datacite.org:32325")
aa <- dc_oai_getrecord(recs)

expect_is(aa, "data.frame")
expect_is(aa, "oai_df")
expect_is(aa$identifier, "character")
expect_is(aa$title, "character")
expect_equal(NROW(aa), 2)
expect_equal(aa$identifier, recs)
})

test_that("dc_oai_getrecord fails well", {
skip_on_cran()

expect_error(dc_oai_getrecord(),
"argument \"id\" is missing, with no default")
expect_error(dc_oai_getrecord('5000000000000asfaffs'),
"is unknown or illegal in this repository")
})
18 changes: 18 additions & 0 deletions tests/testthat/test-dc_oai_identify.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
context("dc_oai_identify")

test_that("dc_oai_identify - default uses datacite", {
skip_on_cran()

aa <- dc_oai_identify()

expect_is(aa, "data.frame")
expect_match(aa$repositoryName, "DataCite")
expect_match(aa$baseURL, "oai.datacite.org")
})

test_that("dc_oai_identify - httr options work", {
skip_on_cran()

library("httr")
expect_error(dc_oai_identify(config = timeout(0.01)))
})
51 changes: 51 additions & 0 deletions tests/testthat/test-dc_oai_listidentifiers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
context("dc_oai_listidentifiers")

test_that("dc_oai_listidentifiers - from", {
skip_on_cran()

aa <- dc_oai_listidentifiers(from = "2015-09-03T00:00:00Z", until="2015-09-03T00:30:00Z")

expect_is(aa, "data.frame")
expect_is(aa, "oai_df")
expect_is(aa$identifier, "character")
expect_is(aa$datestamp, "character")
expect_equal(as.character(as.Date(aa$datestamp[1])), "2015-09-03")
})

test_that("dc_oai_listidentifiers - from & until", {
skip_on_cran()

aa <- dc_oai_listidentifiers(from = '2015-09-03T00:00:00Z', until = '2015-09-03T00:30:00Z')
bb <- dc_oai_listidentifiers(from = '2015-09-03T00:30:00Z', until = '2015-09-03T01:15:00Z')

expect_is(aa, "oai_df")
expect_is(bb, "oai_df")

expect_less_than(NROW(aa), NROW(bb))
})

test_that("dc_oai_listidentifiers - set", {
skip_on_cran()

aa <- dc_oai_listidentifiers(from = '2011-06-01T', until = '2011-11-01T', set = "ANDS")
bb <- dc_oai_listidentifiers(from = '2011-06-01T', until = '2012-11-01T', set = "CDL.OSU")

expect_is(aa, "oai_df")
expect_is(bb, "oai_df")

expect_equal(aa$setSpec[1], "ANDS")
expect_equal(bb$setSpec[1], "CDL")
})

test_that("dc_oai_listidentifiers fails well", {
skip_on_cran()

no_msg <- "The combination of the values of the from, until, set, and metadataPrefix arguments results in an empty list"

expect_error(dc_oai_listidentifiers(from = '2011-06-01T', until = 'adffdsadsf'),
"The request includes illegal arguments")
expect_error(dc_oai_listidentifiers(from = '2011-06-01T', until = 5),
"The request includes illegal arguments")
expect_error(dc_oai_listidentifiers(url = 5), "One or more of your URLs")
expect_error(dc_oai_listidentifiers(from = '2011-06-01T', until = '2011-11-01T', set = "STUFF"), no_msg)
})
38 changes: 38 additions & 0 deletions tests/testthat/test-dc_oai_listmetadataformats.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
context("dc_oai_listmetadataformats")

test_that("dc_oai_listmetadataformats - basic functionality works", {
skip_on_cran()

aa <- dc_oai_listmetadataformats()

expect_is(aa, "data.frame")
expect_is(aa$metadataPrefix, "character")
expect_is(aa$schema, "character")
expect_is(aa$metadataNamespace, "character")

expect_named(aa, c('metadataPrefix', 'schema', 'metadataNamespace'))
})

test_that("dc_oai_listmetadataformats - no formats avail. vs. avail", {
skip_on_cran()

aa <- dc_oai_listmetadataformats(id = "oai:oai.datacite.org:22")
bb <- dc_oai_listmetadataformats(id = "oai:oai.datacite.org:6718729")

expect_null(aa[[1]])
expect_is(bb[[1]], "data.frame")
})

test_that("dc_oai_listmetadataformats - curl options", {
skip_on_cran()

library("httr")

expect_error(dc_oai_listmetadataformats(config = timeout(0.001)), "Timeout was reached")
})

test_that("dc_oai_listmetadataformats fails well", {
skip_on_cran()

expect_null(dc_oai_listmetadataformats(id = "adfadfsdf")[[1]])
})
21 changes: 21 additions & 0 deletions tests/testthat/test-dc_oai_listrecords.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
context("dc_oai_listrecords")

test_that("dc_oai_listrecords works", {
skip_on_cran()

a <- dc_oai_listrecords(from = '2011-06-01T', until = '2011-07-01T')

expect_is(a, "data.frame")
expect_is(a, "oai_df")
expect_is(a$identifier, "character")
})

test_that("dc_oai_listrecords fails well", {
skip_on_cran()

expect_error(dc_oai_listrecords(from = '2011-06-01T', until = 'adffdsadsf'),
"The request includes illegal arguments")
expect_error(dc_oai_listrecords(from = '2011-06-01T', until = 5),
"The request includes illegal arguments")
expect_error(dc_oai_listrecords(url = 5), "One or more of your URLs")
})
29 changes: 29 additions & 0 deletions tests/testthat/test-dc_oai_listsets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
context("dc_oai_listsets")

test_that("dc_oai_listsets", {
skip_on_cran()

aa <- dc_oai_listsets()

expect_is(aa, "data.frame")
expect_is(aa, "oai_df")
expect_is(aa$setSpec, "character")
expect_is(aa$setName, "character")
})

test_that("dc_oai_listsets - curl options", {
skip_on_cran()

library("httr")

expect_error(dc_oai_listsets(config = timeout(0.001)), "Timeout was reached")
})

test_that("dc_oai_listsets fails well", {
skip_on_cran()

expect_error(dc_oai_listsets(token = 454),
"The value of the resumptionToken argument is invalid or expired")
expect_error(dc_oai_listsets("stuff"),
"The value of the resumptionToken argument is invalid or expired")
})

0 comments on commit d2ac600

Please sign in to comment.