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..ef74c56 --- /dev/null +++ b/tests/testthat/test-listFiles.R @@ -0,0 +1,24 @@ +# library(testthat); library(SewerRat); source("setup.R"); source("test-listFiles.R") + +(function() { + +config <- basic_config() +info <- config$info +mydir <- config$mydir +on.exit(deregister(mydir, url=info$url), add=TRUE, after=FALSE) + +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")) +}) + +})() diff --git a/tests/testthat/test-list.R b/tests/testthat/test-listRegisteredDirectories.R similarity index 53% rename from tests/testthat/test-list.R rename to tests/testthat/test-listRegisteredDirectories.R index 7bf52db..f461de2 100644 --- a/tests/testthat/test-list.R +++ b/tests/testthat/test-listRegisteredDirectories.R @@ -1,29 +1,11 @@ -# 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() +(function(){ -# 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 +on.exit(deregister(mydir, url=info$url), add=TRUE, after=FALSE) test_that("listRegisteredDirectories works as expected", { all <- listRegisteredDirectories(info$url) @@ -64,4 +46,4 @@ test_that("listRegisteredDirectories works as expected", { expect_identical(all, filtered) }) -deregister(mydir, url=info$url) +})() diff --git a/tests/testthat/test-query.R b/tests/testthat/test-query.R index ee9d92f..d4af9a8 100644 --- a/tests/testthat/test-query.R +++ b/tests/testthat/test-query.R @@ -1,25 +1,21 @@ -# 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() +(function(){ -# 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" }') +config <- basic_config() +info <- config$info +mydir <- config$mydir +on.exit(deregister(mydir, url=info$url), add=TRUE, after=FALSE) -# Registering it: -register(mydir, "metadata.json", url=info$url) +test_that("basic queries work", { + q <- query("aaron", url=info$url) + expect_identical(length(q), 1L) -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 +26,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 +35,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) }) + +})() diff --git a/tests/testthat/test-register.R b/tests/testthat/test-register.R new file mode 100644 index 0000000..ceb7aed --- /dev/null +++ b/tests/testthat/test-register.R @@ -0,0 +1,19 @@ +# library(testthat); library(SewerRat); source("setup.R"); source("test-register.R") + +(function(){ + +config <- basic_config() +info <- config$info +mydir <- config$mydir +on.exit(deregister(mydir, url=info$url), add=TRUE, after=FALSE) + +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..ee5b98f 100644 --- a/tests/testthat/test-retrieve.R +++ b/tests/testthat/test-retrieve.R @@ -1,14 +1,11 @@ -# 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() +(function(){ -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 +on.exit(deregister(mydir, url=info$url), add=TRUE, after=FALSE) test_that("retrieveFile works as expected", { p <- retrieveFile(paste0(mydir, "/metadata.json"), url=info$url) @@ -109,4 +106,4 @@ test_that("retrieveDirectory works with remote updates", { expect_false(file.exists(file.path(dir, "3", "metadata.json"))) }) -deregister(mydir, url=info$url) +})()