Skip to content

Commit 1460d5f

Browse files
committed
Centralize some of the common test definitions.
- Added some tests for correct deregistration. - Added explicit tests for non-recursive listFiles.
1 parent e223334 commit 1460d5f

File tree

6 files changed

+72
-52
lines changed

6 files changed

+72
-52
lines changed

tests/testthat/setup.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
basic_config <- function() {
2+
# Starting up an example SewerRat service:
3+
info <- SewerRat::startSewerRat()
4+
5+
# Mocking up a directory of stuff to query.
6+
mydir <- tempfile()
7+
dir.create(mydir)
8+
write(file=file.path(mydir, "metadata.json"), '{ "first": "Aaron", "last": "Lun" }')
9+
10+
dir.create(file.path(mydir, "diet"))
11+
write(file=file.path(mydir, "diet", "metadata.json"), '{ "meal": "lunch", "ingredients": "water" }')
12+
13+
# Registering it:
14+
SewerRat::register(mydir, "metadata.json", url=info$url)
15+
16+
list(info=info, mydir=mydir)
17+
}

tests/testthat/test-listFiles.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# library(testthat); library(SewerRat); source("setup.R"); source("test-listFiles.R")
2+
3+
config <- basic_config()
4+
info <- config$info
5+
mydir <- config$mydir
6+
7+
test_that("listFiles works as expected", {
8+
all <- listFiles(mydir, info$url)
9+
expect_identical(sort(all), c("diet/metadata.json", "metadata.json"))
10+
11+
all <- listFiles(mydir, info$url, recursive=FALSE)
12+
expect_identical(sort(all), c("diet/", "metadata.json"))
13+
14+
all <- listFiles(paste0(mydir, "/diet"), info$url)
15+
expect_identical(sort(all), "metadata.json")
16+
17+
all <- listFiles(mydir, info$url, forceRemote=TRUE)
18+
expect_identical(sort(all), c("diet/metadata.json", "metadata.json"))
19+
})
20+
21+
deregister(mydir, url=info$url)

tests/testthat/test-list.R renamed to tests/testthat/test-listRegisteredDirectories.R

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
1-
# Test the listing function.
2-
# library(testthat); library(SewerRat); source("test-list.R")
1+
# library(testthat); library(SewerRat); source("setup.R"); source("test-listRegisteredDirectories.R")
32

4-
# Starting up an example SewerRat service:
5-
info <- startSewerRat()
6-
7-
# Mocking up a directory of stuff to query.
8-
mydir <- tempfile()
9-
dir.create(mydir)
10-
write(file=file.path(mydir, "metadata.json"), '{ "first": "Aaron", "last": "Lun" }')
11-
dir.create(file.path(mydir, "diet"))
12-
write(file=file.path(mydir, "diet", "metadata.json"),
13-
'{ "meal": "lunch", "ingredients": "water" }')
14-
15-
# Registering it:
16-
register(mydir, "metadata.json", url=info$url)
17-
18-
test_that("listing works as expected", {
19-
expect_identical(sort(listFiles(mydir, url=info$url)), sort(c("diet/metadata.json", "metadata.json")))
20-
expect_identical(sort(listFiles(paste0(mydir, "/diet"), url=info$url)), "metadata.json")
21-
expect_identical(sort(listFiles(mydir, url=info$url, recursive=FALSE)), sort(c("diet/", "metadata.json")))
22-
23-
# Forcing remote access.
24-
expect_identical(sort(listFiles(mydir, url=info$url, forceRemote=TRUE)), sort(c("diet/metadata.json", "metadata.json")))
25-
expect_identical(sort(listFiles(mydir, url=info$url, forceRemote=TRUE, recursive=FALSE)), sort(c("diet/", "metadata.json")))
26-
})
3+
config <- basic_config()
4+
info <- config$info
5+
mydir <- config$mydir
276

287
test_that("listRegisteredDirectories works as expected", {
298
all <- listRegisteredDirectories(info$url)

tests/testthat/test-query.R

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
1-
# library(testthat); library(SewerRat); source("test-query.R")
1+
# library(testthat); library(SewerRat); source("setup.R"); source("test-query.R")
22

3-
# Starting up an example SewerRat service:
4-
info <- startSewerRat()
3+
config <- basic_config()
4+
info <- config$info
5+
mydir <- config$mydir
56

6-
# Mocking up a directory of stuff to query.
7-
mydir <- tempfile()
8-
dir.create(mydir)
9-
write(file=file.path(mydir, "metadata.json"), '{ "first": "Aaron", "last": "Lun" }')
10-
dir.create(file.path(mydir, "diet"))
11-
write(file=file.path(mydir, "diet", "metadata.json"),
12-
'{ "meal": "lunch", "ingredients": "water" }')
7+
test_that("basic queries work", {
8+
q <- query("aaron", url=info$url)
9+
expect_identical(length(q), 1L)
1310

14-
# Registering it:
15-
register(mydir, "metadata.json", url=info$url)
16-
17-
test_that("query works as expected", {
1811
q <- query("lun*", url=info$url)
19-
expect_gte(length(q), 2L)
12+
expect_identical(length(q), 2L)
2013
})
2114

22-
test_that("query works with truncation", {
15+
test_that("truncated queries work", {
2316
expect_message(q <- query("lun*", url=info$url, number=0), "truncated")
2417
expect_identical(length(q), 0L)
2518

@@ -30,7 +23,7 @@ test_that("query works with truncation", {
3023
expect_identical(length(q), 0L)
3124
})
3225

33-
test_that("formatQueryResults works properly", {
26+
test_that("formatting of query results works properly", {
3427
q <- query("lun*", url=info$url)
3528
res <- formatQueryResults(q)
3629

@@ -39,3 +32,5 @@ test_that("formatQueryResults works properly", {
3932
expect_equal(as.double(res$time), vapply(q, function(y) y$time, 0))
4033
expect_identical(res$metadata[[1]], q[[1]]$metadata)
4134
})
35+
36+
deregister(mydir, url=info$url)

tests/testthat/test-register.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# library(testthat); library(SewerRat); source("setup.R"); source("test-register.R")
2+
3+
config <- basic_config()
4+
info <- config$info
5+
mydir <- config$mydir
6+
7+
test_that("(de)registration works as expected", {
8+
res <- query("aaron", url=info$url)
9+
expect_identical(length(res), 1L)
10+
11+
deregister(mydir, url=info$url)
12+
res <- query("aaron", url=info$url)
13+
expect_identical(length(res), 0L)
14+
})

tests/testthat/test-retrieve.R

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
# Test the retrieval functions.
2-
# library(testthat); library(SewerRat); source("test-retrieve.R")
1+
# library(testthat); library(SewerRat); source("setup.R"); source("test-retrieve.R")
32

4-
info <- startSewerRat()
5-
6-
mydir <- tempfile()
7-
dir.create(mydir)
8-
write(file=file.path(mydir, "metadata.json"), '{ "first": "Aaron", "last": "Lun" }')
9-
dir.create(file.path(mydir, "diet"))
10-
write(file=file.path(mydir, "diet", "metadata.json"), '{ "meal": "lunch", "ingredients": "water" }')
11-
register(mydir, "metadata.json", url=info$url)
3+
config <- basic_config()
4+
info <- config$info
5+
mydir <- config$mydir
126

137
test_that("retrieveFile works as expected", {
148
p <- retrieveFile(paste0(mydir, "/metadata.json"), url=info$url)

0 commit comments

Comments
 (0)