Skip to content

Commit

Permalink
test: unit tests for versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Feb 13, 2024
1 parent bceed87 commit 3f9eec0
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 14 deletions.
19 changes: 19 additions & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' Setup Tests Infrastructure
#'

## Temporary Directory ----

create_tempdir <- function(path = file.path(tempdir(), "sandbox")) {

old_wd <- getwd()

withr::defer(fs::dir_delete(path), envir = parent.frame())

dir.create(path)

setwd(path)

withr::defer(setwd(old_wd), envir = parent.frame())

invisible(path)
}
14 changes: 0 additions & 14 deletions tests/testthat/test-variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,6 @@ test_that("Test crs_robinson() for success", {



## zen_record_id() ----

test_that("Test zen_record_id() for success", {

x <- zen_record_id()

expect_equal(x, "7390791")

expect_equal(class(x), "character")
expect_equal(length(x), 1L)
})



## species_list() ----

test_that("Test species_list() for success", {
Expand Down
159 changes: 159 additions & 0 deletions tests/testthat/test-version.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
## zenodo_id() ----

test_that("Test zenodo_id() for success", {

x <- zenodo_id()

expect_equal(x, "7390791")

expect_equal(class(x), "character")
expect_equal(length(x), 1L)
})



## check_version() ----

test_that("Test check_version() for error", {

expect_error(check_version(),
"Argument 'version' is required",
fixed = TRUE)

expect_error(check_version(NA),
"Argument 'version' must be character",
fixed = TRUE)

expect_error(check_version(6),
"Argument 'version' must be character",
fixed = TRUE)

expect_error(check_version(character(0)),
"Argument 'version' must be character of length 1",
fixed = TRUE)

expect_error(check_version(c("06", "07")),
"Argument 'version' must be character of length 1",
fixed = TRUE)
})

test_that("Test check_version() for success", {

x <- check_version(NULL)
expect_null(x)

x <- check_version("06")
expect_null(x)

expect_invisible(check_version("06"))
})


## get_metadata() ----

test_that("Test get_metadata() for success", {

x <- get_metadata()

expect_equal(class(x), "list")
expect_true("hits" %in% names(x))
expect_true(x$"hits"$"total" > 0L)
})



## get_available_versions() ----

test_that("Test get_available_versions() for success", {

x <- get_available_versions()

expect_equal(class(x), "data.frame")
expect_true(nrow(x) > 0L)
expect_equal(ncol(x), 3L)

expect_true("publication_date" %in% colnames(x))
expect_true("version" %in% colnames(x))
expect_true("access_right" %in% colnames(x))
})



## get_version_info() ----

test_that("Test get_version_info() for error", {

expect_error(get_version_info(version = "999"),
paste0("The required version is not available. Please run ",
"'get_available_versions()' to list available versions."),
fixed = TRUE)
})

test_that("Test get_version_info() for success", {

x <- get_version_info(version = NULL)

expect_equal(class(x), "list")
expect_true("version" %in% names(x))
expect_true("files" %in% names(x))

x <- get_version_info(version = "08")

expect_equal(class(x), "list")
expect_true("version" %in% names(x))
expect_true("files" %in% names(x))

expect_true(x$"version" == "08")
expect_true(x$"publication_date" == "2024-02-09")
})



## get_latest_version() ----

test_that("Test get_latest_version() for success", {

x <- get_latest_version()

expect_equal(class(x), "character")
})



## save_version() ----

test_that("Test save_version() for success", {

create_tempdir()

expect_invisible(save_version("06"))
expect_true(file.exists(".forcis"))
expect_equal(readLines(".forcis"), "FORCIS_VERSION=06")

x <- save_version("07")
expect_null(x)
expect_true(file.exists(".forcis"))
expect_equal(readLines(".forcis"), "FORCIS_VERSION=07")
})



## get_current_version() ----

test_that("Test get_current_version() for success", {

create_tempdir()

x <- get_current_version()
expect_null(x)

save_version("07")
x <- get_current_version()
expect_equal(class(x), "character")
expect_equal(length(x), 1L)
expect_equal(x, "07")
})



## set_version() ----

0 comments on commit 3f9eec0

Please sign in to comment.