Skip to content

Commit

Permalink
Patch tests to pass with CKAN 2.10
Browse files Browse the repository at this point in the history
* See ropensci#215: support multiple CKAN versions
* Disable tests for deprecated API endpoints as of CKAN 2.10
* Introduce a default for get_test_url and get_test_behaviour
* Format ckanr_settings
  • Loading branch information
florianm committed May 29, 2024
1 parent e1aac3f commit bbe14b0
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 131 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/R-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ jobs:

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
CKANR_TEST_URL: ${{secrets.CKANR_TEST_URL}}
CKANR_TEST_BEHAVIOUR: ${{secrets.CKANR_TEST_BEHAVIOUR}}
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
R_KEEP_PKG_SOURCE: yes
CKAN_VERSION: ${{ matrix.config.ckan-version }} # for docker compose up
Expand Down
102 changes: 64 additions & 38 deletions R/ckanr_settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ assign("ckanr_proxy", NULL, envir = ckanr_settings_env)
#' @examples
#' ckanr_settings()
ckanr_settings <- function() {
ops <- list(url = Sys.getenv("CKANR_DEFAULT_URL", ""),
key = Sys.getenv("CKANR_DEFAULT_KEY", ""),
test_url = Sys.getenv("CKANR_TEST_URL", ""),
test_key = Sys.getenv("CKANR_TEST_KEY", ""),
test_did = Sys.getenv("CKANR_TEST_DID", ""),
test_rid = Sys.getenv("CKANR_TEST_RID", ""),
test_gid = Sys.getenv("CKANR_TEST_GID", ""),
test_oid = Sys.getenv("CKANR_TEST_OID", ""),
test_behaviour = Sys.getenv("CKANR_TEST_BEHAVIOUR", ""),
proxy = get("ckanr_proxy", ckanr_settings_env)
ops <- list(
url = Sys.getenv("CKANR_DEFAULT_URL", ""),
key = Sys.getenv("CKANR_DEFAULT_KEY", ""),
test_url = Sys.getenv("CKANR_TEST_URL", ""),
test_key = Sys.getenv("CKANR_TEST_KEY", ""),
test_did = Sys.getenv("CKANR_TEST_DID", ""),
test_rid = Sys.getenv("CKANR_TEST_RID", ""),
test_gid = Sys.getenv("CKANR_TEST_GID", ""),
test_oid = Sys.getenv("CKANR_TEST_OID", ""),
test_behaviour = Sys.getenv("CKANR_TEST_BEHAVIOUR", ""),
proxy = get("ckanr_proxy", ckanr_settings_env)
)
structure(ops, class = "ckanr_settings")
}
Expand Down Expand Up @@ -64,7 +65,12 @@ print.ckanr_settings <- function(x, ...) {
#' @export
#' @param url A CKAN URL (optional), default: https://data.ontario.ca
#' @param key A CKAN API key (optional, character)
#' @param test_url (optional, character) A valid CKAN URL for testing purposes
#' @param test_url (optional, character) A valid CKAN URL for testing purposes.
#' Default: "http://localhost:5000".
#' The default works with a local installation of CKAN via docker compose
#' running on port 5000, as well as for CI on GitHub.
#' This enables contributors to run the CI test suite on GitHub when submitting
#' a pull request.
#' @param test_key (optional, character) A valid CKAN API key privileged to
#' create datasets at `test_url`
#' @param test_did (optional, character) A valid CKAN dataset ID, existing at
Expand All @@ -76,7 +82,8 @@ print.ckanr_settings <- function(x, ...) {
#' `test_url`
#' @param test_behaviour (optional, character) Whether to fail ("FAIL") or skip
#' ("SKIP") writing tests in case of problems with the configured test CKAN.
#' @param proxy an object of class `request` from a call to
#' Default: "FAIL".
#' @param proxy an object of class `request` from a call to
#' [crul::proxy()]
#' @details
#' [ckanr_setup()] sets CKAN connection details. ckanr's functions
Expand Down Expand Up @@ -106,34 +113,35 @@ print.ckanr_settings <- function(x, ...) {
#' ckanr_setup(url = "https://data.ontario.ca/", key = "some-CKAN-API-key")
#'
#' # ckanR developers/testers can run:
#' ckanr_setup(url = "https://data.ontario.ca/", key = "some-CKAN-API-key",
#' test_url = "http://test-ckan.gov/",test_key = "test-ckan-API-key",
#' test_did = "test-ckan-dataset-id",test_rid = "test-ckan-resource-id",
#' test_gid = "test-group-name", test_oid = "test-organzation-name",
#' test_behaviour = "FAIL")
#' ckanr_setup(
#' url = "https://data.ontario.ca/", key = "some-CKAN-API-key",
#' test_url = "http://test-ckan.gov/", test_key = "test-ckan-API-key",
#' test_did = "test-ckan-dataset-id", test_rid = "test-ckan-resource-id",
#' test_gid = "test-group-name", test_oid = "test-organization-name",
#' test_behaviour = "FAIL"
#' )
#'
#' # Not specifying the default CKAN URL will reset the CKAN URL to its default
#' # "https://data.ontario.ca/":
#' ckanr_setup()
#'
#'
#' # set a proxy
#' ckanr_setup(proxy = crul::proxy("64.251.21.73:8080"))
#' ckanr_settings()
#' ## run without setting proxy to reset to no proxy
#' ckanr_setup()
#' ckanr_settings()
ckanr_setup <- function(
url = "https://data.ontario.ca/",
key = NULL,
test_url = NULL,
test_key = NULL,
test_did = NULL,
test_rid = NULL,
test_gid = NULL,
test_oid = NULL,
test_behaviour = NULL,
proxy = NULL) {

url = "https://data.ontario.ca/",
key = NULL,
test_url = NULL,
test_key = NULL,
test_did = NULL,
test_rid = NULL,
test_gid = NULL,
test_oid = NULL,
test_behaviour = NULL,
proxy = NULL) {
Sys.setenv("CKANR_DEFAULT_URL" = url)
if (!is.null(key)) Sys.setenv("CKANR_DEFAULT_KEY" = key)
if (!is.null(test_url)) Sys.setenv("CKANR_TEST_URL" = test_url)
Expand All @@ -151,36 +159,54 @@ ckanr_setup <- function(
#
#' @export
#' @rdname ckanr_settings
get_default_url <- function(){ Sys.getenv("CKANR_DEFAULT_URL") }
get_default_url <- function() {
Sys.getenv("CKANR_DEFAULT_URL")
}

#' @export
#' @rdname ckanr_settings
get_default_key <- function(){ Sys.getenv("CKANR_DEFAULT_KEY") }
get_default_key <- function() {
Sys.getenv("CKANR_DEFAULT_KEY")
}

#' @export
#' @rdname ckanr_settings
get_test_url <- function(){ Sys.getenv("CKANR_TEST_URL") }
get_test_url <- function() {
Sys.getenv("CKANR_TEST_URL", unset = "http://localhost:5000")
}

#' @export
#' @rdname ckanr_settings
get_test_key <- function(){ Sys.getenv("CKANR_TEST_KEY") }
get_test_key <- function() {
Sys.getenv("CKANR_TEST_KEY")
}

#' @export
#' @rdname ckanr_settings
get_test_did <- function(){ Sys.getenv("CKANR_TEST_DID") }
get_test_did <- function() {
Sys.getenv("CKANR_TEST_DID")
}

#' @export
#' @rdname ckanr_settings
get_test_rid <- function(){ Sys.getenv("CKANR_TEST_RID") }
get_test_rid <- function() {
Sys.getenv("CKANR_TEST_RID")
}

#' @export
#' @rdname ckanr_settings
get_test_gid <- function(){ Sys.getenv("CKANR_TEST_GID") }
get_test_gid <- function() {
Sys.getenv("CKANR_TEST_GID")
}

#' @export
#' @rdname ckanr_settings
get_test_oid <- function(){ Sys.getenv("CKANR_TEST_OID") }
get_test_oid <- function() {
Sys.getenv("CKANR_TEST_OID")
}

#' @export
#' @rdname ckanr_settings
get_test_behaviour <- function(){ Sys.getenv("CKANR_TEST_BEHAVIOUR") }
get_test_behaviour <- function() {
Sys.getenv("CKANR_TEST_BEHAVIOUR", unset = "FAIL")
}
22 changes: 15 additions & 7 deletions man/ckanr_setup.Rd

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

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

skip_on_cran()

u <- get_test_url()
check_ckan(u)

test_that("changes gives back expected class types", {
cat(u, sep = "\n")
a <- changes(url = u)

expect_is(a, "list")
expect_is(a[[1]], "list")
expect_is(a[[1]]$user_id, "character")
expect_is(a[[1]]$data, "list")
})

test_that("changes works giving back json output", {
b <- changes(url = u, as = 'json')
b_df <- jsonlite::fromJSON(b)

expect_is(b, "character")
expect_is(b_df, "list")
expect_is(b_df$result, "data.frame")
})

test_that("changes fails correctly", {

expect_error(changes("adf"), "offset Invalid integer")
expect_error(changes(limit = "Adf"), "limit Invalid integer")
expect_error(changes("adf", url = "http://www.google.com"),
regexp = "404")
})
# context("changes")
#
# skip_on_cran()
#
# u <- get_test_url()
# check_ckan(u)
#
# test_that("changes gives back expected class types", {
# cat(u, sep = "\n")
# a <- changes(url = u)
#
# expect_is(a, "list")
# expect_is(a[[1]], "list")
# expect_is(a[[1]]$user_id, "character")
# expect_is(a[[1]]$data, "list")
# })
#
# test_that("changes works giving back json output", {
# b <- changes(url = u, as = 'json')
# b_df <- jsonlite::fromJSON(b)
#
# expect_is(b, "character")
# expect_is(b_df, "list")
# expect_is(b_df$result, "data.frame")
# })
#
# test_that("changes fails correctly", {
#
# expect_error(changes("adf"), "offset Invalid integer")
# expect_error(changes(limit = "Adf"), "limit Invalid integer")
# expect_error(changes("adf", url = "http://www.google.com"),
# regexp = "404")
# })
10 changes: 8 additions & 2 deletions tests/testthat/test-ckan_fetch.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ context("ckan_fetch")

skip_on_cran()

url <- get_test_url()
rid <- get_test_rid()

test_that("ckan_fetch returns error when file format can't be determined from URL", {
Expand All @@ -17,7 +18,11 @@ check_ckan(u)
test_that("ckan_fetch doesn't write any files to working directory when session = TRUE", {
expect_identical(list.files(test_path()), {
res <- resource_show(id = rid, as = "table")
df <- ckan_fetch(res$url)
# When running CKAN via docker compose, it returns its hostname as
# http://ckan:5000, but we can only reach it via localhost unless we patch
# /etc/hosts
url <- gsub("ckan", "localhost", res$url)
df <- ckan_fetch(url)
list.files(test_path())
})
})
Expand All @@ -26,7 +31,8 @@ test_that("ckan_fetch doesn't retain any files in temporary directory when sessi
dir <- tempdir()
expect_identical(list.files(dir), {
res <- resource_show(id = rid, as = "table")
df <- ckan_fetch(res$url)
url <- gsub("ckan", "localhost", res$url)
df <- ckan_fetch(url)
list.files(dir)
})
})
8 changes: 4 additions & 4 deletions tests/testthat/test-organization_show.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ test_that("organization_show gives back expected class types", {
a <- organization_show(o, url=u)

expect_is(a, "ckan_organization")
expect_is(a[[1]], "list")
expect_is(a[[1]][[1]]$name, "character")
expect_equal(as.integer(length(a)), 19L)
expect_is(a[[1]], "character")
expect_is(a$users[[1]]$name, "character")
expect_equal(as.integer(length(a)), 18L)

a <- organization_show(o, url=u, include_datasets = TRUE)
expect_equal(as.integer(a$package_count), dataset_num)
Expand All @@ -45,5 +45,5 @@ test_that("organization_show works giving back json output", {
expect_is(b, "character")
expect_is(b_df, "list")
expect_is(b_df$result, "list")
expect_equal(as.integer(length(b_df$result)), 19L)
expect_equal(as.integer(length(b_df$result)), 18L)
})
Loading

0 comments on commit bbe14b0

Please sign in to comment.