diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R new file mode 100644 index 0000000..f291aaf --- /dev/null +++ b/tests/testthat/setup.R @@ -0,0 +1,17 @@ +basic_config <- function() { + # Starting up an example SewerRat service: + info <- SewerRat::startSewerRat() + + # Mocking up a directory of stuff to query. + mydir <- tempfile() + dir.create(mydir) + write(file=file.path(mydir, "metadata.json"), '{ "first": "Aaron", "last": "Lun" }') + + dir.create(file.path(mydir, "diet")) + write(file=file.path(mydir, "diet", "metadata.json"), '{ "meal": "lunch", "ingredients": "water" }') + + # Registering it: + SewerRat::register(mydir, "metadata.json", url=info$url) + + list(info=info, mydir=mydir) +} diff --git a/tests/testthat/test-listFiles.R b/tests/testthat/test-listFiles.R new file mode 100644 index 0000000..6f43929 --- /dev/null +++ b/tests/testthat/test-listFiles.R @@ -0,0 +1,21 @@ +# library(testthat); library(SewerRat); source("setup.R"); source("test-listFiles.R") + +config <- basic_config() +info <- config$info +mydir <- config$mydir + +test_that("listFiles works as expected", { + all <- listFiles(mydir, info$url) + expect_identical(sort(all), c("diet/metadata.json", "metadata.json")) + + all <- listFiles(mydir, info$url, recursive=FALSE) + expect_identical(sort(all), c("diet/", "metadata.json")) + + all <- listFiles(paste0(mydir, "/diet"), info$url) + expect_identical(sort(all), "metadata.json") + + all <- listFiles(mydir, info$url, forceRemote=TRUE) + expect_identical(sort(all), c("diet/metadata.json", "metadata.json")) +}) + +deregister(mydir, url=info$url) diff --git a/tests/testthat/test-list.R b/tests/testthat/test-listRegisteredDirectories.R similarity index 54% rename from tests/testthat/test-list.R rename to tests/testthat/test-listRegisteredDirectories.R index 7bf52db..4d82c9e 100644 --- a/tests/testthat/test-list.R +++ b/tests/testthat/test-listRegisteredDirectories.R @@ -1,29 +1,8 @@ -# Test the listing function. -# library(testthat); library(SewerRat); source("test-list.R") +# library(testthat); library(SewerRat); source("setup.R"); source("test-listRegisteredDirectories.R") -# Starting up an example SewerRat service: -info <- startSewerRat() - -# Mocking up a directory of stuff to query. -mydir <- tempfile() -dir.create(mydir) -write(file=file.path(mydir, "metadata.json"), '{ "first": "Aaron", "last": "Lun" }') -dir.create(file.path(mydir, "diet")) -write(file=file.path(mydir, "diet", "metadata.json"), - '{ "meal": "lunch", "ingredients": "water" }') - -# Registering it: -register(mydir, "metadata.json", url=info$url) - -test_that("listing works as expected", { - expect_identical(sort(listFiles(mydir, url=info$url)), sort(c("diet/metadata.json", "metadata.json"))) - expect_identical(sort(listFiles(paste0(mydir, "/diet"), url=info$url)), "metadata.json") - expect_identical(sort(listFiles(mydir, url=info$url, recursive=FALSE)), sort(c("diet/", "metadata.json"))) - - # Forcing remote access. - expect_identical(sort(listFiles(mydir, url=info$url, forceRemote=TRUE)), sort(c("diet/metadata.json", "metadata.json"))) - expect_identical(sort(listFiles(mydir, url=info$url, forceRemote=TRUE, recursive=FALSE)), sort(c("diet/", "metadata.json"))) -}) +config <- basic_config() +info <- config$info +mydir <- config$mydir test_that("listRegisteredDirectories works as expected", { all <- listRegisteredDirectories(info$url) diff --git a/tests/testthat/test-query.R b/tests/testthat/test-query.R index ee9d92f..53feb48 100644 --- a/tests/testthat/test-query.R +++ b/tests/testthat/test-query.R @@ -1,25 +1,18 @@ -# library(testthat); library(SewerRat); source("test-query.R") +# library(testthat); library(SewerRat); source("setup.R"); source("test-query.R") -# Starting up an example SewerRat service: -info <- startSewerRat() +config <- basic_config() +info <- config$info +mydir <- config$mydir -# Mocking up a directory of stuff to query. -mydir <- tempfile() -dir.create(mydir) -write(file=file.path(mydir, "metadata.json"), '{ "first": "Aaron", "last": "Lun" }') -dir.create(file.path(mydir, "diet")) -write(file=file.path(mydir, "diet", "metadata.json"), - '{ "meal": "lunch", "ingredients": "water" }') +test_that("basic queries work", { + q <- query("aaron", url=info$url) + expect_identical(length(q), 1L) -# Registering it: -register(mydir, "metadata.json", url=info$url) - -test_that("query works as expected", { q <- query("lun*", url=info$url) - expect_gte(length(q), 2L) + expect_identical(length(q), 2L) }) -test_that("query works with truncation", { +test_that("truncated queries work", { expect_message(q <- query("lun*", url=info$url, number=0), "truncated") expect_identical(length(q), 0L) @@ -30,7 +23,7 @@ test_that("query works with truncation", { expect_identical(length(q), 0L) }) -test_that("formatQueryResults works properly", { +test_that("formatting of query results works properly", { q <- query("lun*", url=info$url) res <- formatQueryResults(q) @@ -39,3 +32,5 @@ test_that("formatQueryResults works properly", { expect_equal(as.double(res$time), vapply(q, function(y) y$time, 0)) expect_identical(res$metadata[[1]], q[[1]]$metadata) }) + +deregister(mydir, url=info$url) diff --git a/tests/testthat/test-register.R b/tests/testthat/test-register.R new file mode 100644 index 0000000..a36cfbb --- /dev/null +++ b/tests/testthat/test-register.R @@ -0,0 +1,14 @@ +# library(testthat); library(SewerRat); source("setup.R"); source("test-register.R") + +config <- basic_config() +info <- config$info +mydir <- config$mydir + +test_that("(de)registration works as expected", { + res <- query("aaron", url=info$url) + expect_identical(length(res), 1L) + + deregister(mydir, url=info$url) + res <- query("aaron", url=info$url) + expect_identical(length(res), 0L) +}) diff --git a/tests/testthat/test-retrieve.R b/tests/testthat/test-retrieve.R index 73e6a18..20f1c56 100644 --- a/tests/testthat/test-retrieve.R +++ b/tests/testthat/test-retrieve.R @@ -1,14 +1,8 @@ -# Test the retrieval functions. -# library(testthat); library(SewerRat); source("test-retrieve.R") +# library(testthat); library(SewerRat); source("setup.R"); source("test-retrieve.R") -info <- startSewerRat() - -mydir <- tempfile() -dir.create(mydir) -write(file=file.path(mydir, "metadata.json"), '{ "first": "Aaron", "last": "Lun" }') -dir.create(file.path(mydir, "diet")) -write(file=file.path(mydir, "diet", "metadata.json"), '{ "meal": "lunch", "ingredients": "water" }') -register(mydir, "metadata.json", url=info$url) +config <- basic_config() +info <- config$info +mydir <- config$mydir test_that("retrieveFile works as expected", { p <- retrieveFile(paste0(mydir, "/metadata.json"), url=info$url)