Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp tests #255

Merged
merged 7 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Suggests:
testthat (>= 3.0.2),
knitr,
rmarkdown,
covr
covr,
withr
VignetteBuilder: knitr
Language: en_GB
Config/testthat/edition: 3
47 changes: 47 additions & 0 deletions R/test-helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This (unexported) function is used to implement the so-called test-fixtures:
# https://testthat.r-lib.org/articles/test-fixtures.html#test-fixtures.

# The idea is to automatically run some specific tasks (see below) before and
# after each test and "leave the world exactly as we found it". More precisely,
# the function performs the follwing steps:

# 1. Setup the cleaning of all pbf/gpkg files in the folder where "path" is
# defined using oe_clean.
# 2. Copy the "its-example.osm.pbf" file to "path". Default value is a file
# named "test_its-example.osm.pbf" inside tempdir(). The naming is important
# since it's the same pattern created by oe_get("ITS Leeds") and we can mimic
# the behaviour more precisely.
# 3. Return that "path" (invisibly). This is important since the returned path
# is used to define the "its_pbf" variable inside each test using the following
# pattern:
#
# its_pbf = setup_pbf()
#
# NB: The tests are typically run setting the tempdir() as the
# download_directory. This is achieved setting the OSMEXT_DOWNLOAD_DIR
# enviromental variable equal to tempdir() through a temporal change of the
# enviromental variable. See withr::local_envvar calls in tests/testthat.

setup_pbf <- function(
path = file.path(tempdir(), "test_its-example.osm.pbf"),
env = parent.frame()
) {
withr::defer({
oe_clean(dirname(path), force = TRUE)
}, envir = env)

# Just to be sure that there is no pbf/gpkg file in the chosen di
if (!identical(
list.files(dirname(path), "\\.(osm\\.pbf|gpkg)$", full.names = TRUE),
character(0)
)) {
stop("There is one or leftover pbf/gpkg file. Check tests.", call. = FALSE)
}

file.copy(
from = system.file("its-example.osm.pbf", package = "osmextract"),
to = path
)

invisible(path)
}
7 changes: 5 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ stop_custom <- function(.subclass, message, call = NULL, ...) {
#'
#' @param download_directory The directory where the `.osm.pbf` and `.gpkg`
#' files are saved. Default value is `oe_download_directory()`.
#' @param force Internal option. It can be used to skip the checks run at the
#' beginning of the function and force the removal of all `pbf`/`gpkg` files.
#'
#' @return The same as `unlink()`.
#' @export
Expand All @@ -160,12 +162,13 @@ stop_custom <- function(.subclass, message, call = NULL, ...) {
#' # Warning: the following removes all files in oe_download_directory()
#' \dontrun{
#' oe_clean()}
oe_clean <- function(download_directory = oe_download_directory()) {
oe_clean <- function(download_directory = oe_download_directory(), force = FALSE) {
continue = 1L
if ( # nocov start
interactive() &&
!identical(Sys.getenv("TESTTHAT"), "true") &&
!isTRUE(getOption("knitr.in.progress"))
!isTRUE(getOption("knitr.in.progress")) &&
!force
) {
message(
"You are going to delete all pbf and gpkg files in ",
Expand Down
5 changes: 4 additions & 1 deletion man/oe_clean.Rd

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

25 changes: 18 additions & 7 deletions tests/testthat/test-download.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
################################################################################
# NB: ALWAYS REMEMBER TO SET #
# withr::local_envvar( #
# .new = list("OSMEXT_DOWNLOAD_DIRECTORY" = tempdir()) #
# ) #
# IF YOU NEED TO MODIFY THE OSMEXT_DOWNLOAD_DIRECTORY envvar INSIDE THE TESTS. #
# #
# I could also set the same option at the beginning of the script but that #
# makes the debugging more difficult since I have to manually reset the #
# options at the end of the debugging process. #
# #
# See R/test-helpers.R for more details #
# #
################################################################################

test_that("oe_download: simplest examples work", {
skip_on_cran()
skip_if_offline("github.com")
on.exit(
oe_clean(tempdir()),
add = TRUE,
after = TRUE
withr::defer(oe_clean(tempdir()))
withr::local_envvar(
.new = list("OSMEXT_DOWNLOAD_DIRECTORY" = tempdir())
)

# Run tests
its_match = oe_match("ITS Leeds", quiet = TRUE)
expect_error(
oe_download(
file_url = its_match$url,
provider = "test",
download_directory = tempdir(),
quiet = TRUE
),
NA
Expand All @@ -23,7 +35,6 @@ test_that("oe_download: simplest examples work", {
oe_download(
file_url = its_match$url,
provider = "test",
download_directory = tempdir(),
quiet = FALSE
),
"Skip downloading."
Expand Down
58 changes: 34 additions & 24 deletions tests/testthat/test-find.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,55 @@
# Fill the tempdir without downloading a new file
file.copy(
system.file("its-example.osm.pbf", package = "osmextract"),
to = file.path(tempdir(), "its-example.osm.pbf")
)
################################################################################
# NB: ALWAYS REMEMBER TO SET #
# withr::local_envvar( #
# .new = list("OSMEXT_DOWNLOAD_DIRECTORY" = tempdir()) #
# ) #
# IF YOU NEED TO MODIFY THE OSMEXT_DOWNLOAD_DIRECTORY envvar INSIDE THE TESTS. #
# #
# I could also set the same option at the beginning of the script but that #
# makes the debugging more difficult since I have to manually reset the #
# options at the end of the debugging process. #
# #
# See R/test-helpers.R for more details #
# #
################################################################################

test_that("oe_find: simplest example works", {
its_pbf = setup_pbf()
withr::local_envvar(
.new = list("OSMEXT_DOWNLOAD_DIRECTORY" = tempdir())
)

oe_vectortranslate(
file_path = file.path(tempdir(), "its-example.osm.pbf"),
file_path = its_pbf,
quiet = TRUE
)

# Run the code and tests
its_leeds_find = oe_find(
"ITS Leeds",
download_directory = tempdir(),
quiet = TRUE
)
expect_type(its_leeds_find, "character")
expect_length(its_leeds_find, 2)
})

test_that("oe_find: return_gpkg and return_pbf arguments work", {
its_pbf = setup_pbf()
withr::local_envvar(
.new = list("OSMEXT_DOWNLOAD_DIRECTORY" = tempdir())
)

oe_vectortranslate(
file_path = its_pbf,
quiet = TRUE
)

pbf_find = oe_find(
"ITS Leeds",
download_directory = tempdir(),
quiet = TRUE,
return_gpkg = FALSE
)
gpkg_find = oe_find(
"ITS Leeds",
download_directory = tempdir(),
quiet = TRUE,
return_pbf = FALSE
)
Expand All @@ -44,34 +64,24 @@ test_that("oe_find: return_gpkg and return_pbf arguments work", {
expect_match(gpkg_find, "gpkg")
})

# Clean tempdir
oe_clean(tempdir())

test_that("download_if_missing in oe_find works", {
skip_on_cran()
skip_if_offline("github.com")

# Clean tempdir
on.exit(
oe_clean(tempdir()),
add = TRUE,
after = TRUE
withr::defer(oe_clean(tempdir()))
withr::local_envvar(
.new = list("OSMEXT_DOWNLOAD_DIRECTORY" = tempdir())
)

# Test that tempdir is really empty
expect_true(!file.exists(file.path(tempdir(), "its-example.osm.pbf")))
expect_true(!file.exists(file.path(tempdir(), "test_its-example.osm.pbf")))

# Test download_if_missing
its_leeds_find = oe_find(
"ITS Leeds",
provider = "test",
download_directory = tempdir(),
download_if_missing = TRUE,
quiet = TRUE
)
expect_type(its_leeds_find, "character")
expect_length(its_leeds_find, 2)
})

# Clean tempdir
oe_clean(tempdir())
Loading