Skip to content

Commit

Permalink
Use packages in tests conditionally
Browse files Browse the repository at this point in the history
  • Loading branch information
wkmor1 committed Jul 22, 2024
1 parent 31cb929 commit 2821898
Show file tree
Hide file tree
Showing 12 changed files with 651 additions and 555 deletions.
26 changes: 15 additions & 11 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
library("vcr")

branch <- Sys.getenv("BRANCH")

gh_ref <- Sys.getenv("GITHUB_REF_NAME")
Expand All @@ -16,13 +14,19 @@ if (is_dev_branch && has_dev_token) {

}

invisible(vcr::vcr_configure(
dir = vcr::vcr_test_path("fixtures"),
filter_sensitive_data = list(
"<finbif_token>" = Sys.getenv("FINBIF_ACCESS_TOKEN"),
"<finbif_dev_token>" = Sys.getenv("FINBIF_DEV_ACCESS_TOKEN"),
"<finbif_dl_token>" = Sys.getenv("FINBIF_RESTRICTED_FILE_ACCESS_TOKEN")
)
))
if (requireNamespace("vcr", quietly = TRUE)) {

library("vcr")

invisible(vcr::vcr_configure(
dir = vcr::vcr_test_path("fixtures"),
filter_sensitive_data = list(
"<finbif_token>" = Sys.getenv("FINBIF_ACCESS_TOKEN"),
"<finbif_dev_token>" = Sys.getenv("FINBIF_DEV_ACCESS_TOKEN"),
"<finbif_dl_token>" = Sys.getenv("FINBIF_RESTRICTED_FILE_ACCESS_TOKEN")
)
))

vcr::check_cassette_names()
vcr::check_cassette_names()

}
61 changes: 42 additions & 19 deletions tests/testthat/test-caching.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,78 @@ test_that("cache invalidation works", {

dir.create(cache)

db <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")

options(finbif_rate_limit = Inf)

finbif_clear_cache()

vcr::use_cassette("finbif_cache_timeout", {
if (requireNamespace("vcr", quietly = TRUE)) {

vcr::use_cassette("finbif_cache_timeout", {

finbif_occurrence(cache = 1e-09)

finbif_occurrence(cache = 1e-09)
cached <- finbif_occurrence(cache = TRUE)

cached <- finbif_occurrence(cache = TRUE)
})

})
expect_snapshot(cached)

expect_snapshot(cached)
}

finbif_clear_cache()

options(finbif_cache_path = cache)

vcr::use_cassette("finbif_cache_file_timeout", {
if (requireNamespace("vcr", quietly = TRUE)) {

finbif_occurrence(cache = 1e-09)
vcr::use_cassette("finbif_cache_file_timeout", {

cached <- finbif_occurrence(cache = TRUE)
finbif_occurrence(cache = 1e-09)

})
cached <- finbif_occurrence(cache = TRUE)

expect_snapshot(cached)
})

expect_snapshot(cached)

}

finbif_clear_cache()

options(finbif_cache_path = db)
if (
requireNamespace("DBI", quietly = TRUE) &&
requireNamespace("RSQLite", quietly = TRUE)
) {

db <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")

vcr::use_cassette("finbif_cache_db_timeout", {
options(finbif_cache_path = db)

finbif_occurrence(cache = 1e-09)
}

cached <- finbif_occurrence(cache = TRUE)
if (requireNamespace("vcr", quietly = TRUE)) {

})
vcr::use_cassette("finbif_cache_db_timeout", {

expect_snapshot(cached)
finbif_occurrence(cache = 1e-09)

cached <- finbif_occurrence(cache = TRUE)

})

expect_snapshot(cached)

}

finbif_clear_cache()

options(finbif_cache_path = NULL)

DBI::dbDisconnect(db)
if (requireNamespace("DBI", quietly = TRUE)) {

DBI::dbDisconnect(db)

}

options(op)

Expand Down
32 changes: 18 additions & 14 deletions tests/testthat/test-finbif_check_taxa.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,30 @@ test_that("checking taxa works", {

finbif_clear_cache()

if (requireNamespace("vcr", quietly = TRUE)) {

vcr::use_cassette("finbif_check_taxa", {

capture.output(
cygnus_cygnus <- print(
finbif_check_taxa(list(species = "Cygnus cygnus", "not_a_taxa"))
capture.output(
cygnus_cygnus <- print(
finbif_check_taxa(list(species = "Cygnus cygnus", "not_a_taxa"))
)
)
)

})
})

expect_equal(
cygnus_cygnus,
structure(
list(
species = c("Cygnus cygnus" = "MX.26280"),
c(not_a_taxa = NA_character_)
),
class = c("list", "finbif_taxa_list")
expect_equal(
cygnus_cygnus,
structure(
list(
species = c("Cygnus cygnus" = "MX.26280"),
c(not_a_taxa = NA_character_)
),
class = c("list", "finbif_taxa_list")
)
)
)

}

options(finbif_cache_path = NULL)

Expand Down
19 changes: 13 additions & 6 deletions tests/testthat/test-finbif_clear_cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@ test_that("clearing cache works", {

expect_null(finbif_clear_cache())

db <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
if (
requireNamespace("DBI", quietly = TRUE) &&
requireNamespace("RSQLite", quietly = TRUE)
) {

DBI::dbCreateTable(db, "finbif_cache", c(hash = "TEXT"))
db <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")

options(finbif_cache_path = db)
DBI::dbCreateTable(db, "finbif_cache", c(hash = "TEXT"))

expect_null(finbif_clear_cache())
options(finbif_cache_path = db)

expect_null(finbif_clear_cache())

options(finbif_cache_path = NULL)

options(finbif_cache_path = NULL)
DBI::dbDisconnect(db)

DBI::dbDisconnect(db)
}

options(op)

Expand Down
30 changes: 17 additions & 13 deletions tests/testthat/test-finbif_collections.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,29 @@ test_that("fetching collections works", {

options(finbif_cache_path = cache, finbif_rate_limit = Inf)

vcr::use_cassette("finbif_collections", {
if (requireNamespace("vcr", quietly = TRUE)) {

collections <- finbif_collections(
has_children, NA, subcollections = FALSE, supercollections = TRUE
)
vcr::use_cassette("finbif_collections", {

collections <- finbif_collections(
has_children, NA, subcollections = FALSE, supercollections = TRUE
)

collections_error <- try(finbif_collections(1), silent = TRUE)
collections_error <- try(finbif_collections(1), silent = TRUE)

})
})

expect_snapshot(collections)
expect_snapshot(collections)

expect_equal(
collections_error[[1L]],
paste(
"Error in finbif_collections(1) :",
"\n Collections filter must be a logical vector\n"
expect_equal(
collections_error[[1L]],
paste(
"Error in finbif_collections(1) :",
"\n Collections filter must be a logical vector\n"
)
)
)

}

options(finbif_cache_path = NULL)

Expand Down
41 changes: 23 additions & 18 deletions tests/testthat/test-finbif_informal_groups.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,35 @@ test_that("fetching informal groups works", {

options(finbif_cache_path = cache, finbif_rate_limit = Inf)

vcr::use_cassette("finbif_informal_groups", {
if (requireNamespace("vcr", quietly = TRUE)) {

limit <- capture.output(finbif_informal_groups(limit = 1, locale = "ru"))
vcr::use_cassette("finbif_informal_groups", {

bryophytes <- capture.output(finbif_informal_groups("Bryophytes"))
limit <- capture.output(finbif_informal_groups(limit = 1, locale = "ru"))

})
bryophytes <- capture.output(finbif_informal_groups("Bryophytes"))

expect_equal(
limit,
c(
"Algae",
" --Macro algae",
" --Brown algae and yellow green algae",
" --Green algae",
" --Red algae",
" --Stoneworts",
"...148 more groups"
})

expect_equal(
limit,
c(
"Algae",
" --Macro algae",
" --Brown algae and yellow green algae",
" --Green algae",
" --Red algae",
" --Stoneworts",
"...148 more groups"
)
)

expect_equal(
bryophytes,
c("Bryophytes", " --Hornworts", " --Liverworts", " --Mosses")
)
)

expect_equal(
bryophytes, c("Bryophytes", " --Hornworts", " --Liverworts", " --Mosses")
)
}

options(finbif_cache_path = NULL)

Expand Down
44 changes: 24 additions & 20 deletions tests/testthat/test-finbif_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,49 @@ test_that("fetching metadata works", {
finbif_use_cache = 1, finbif_cache_path = cache, finbif_rate_limit = Inf
)

vcr::use_cassette("finbif_metadata", {
if (requireNamespace("vcr", quietly = TRUE)) {

sources <- finbif_metadata("source", "ru")
vcr::use_cassette("finbif_metadata", {

regulatory_status <- finbif_metadata("regulatory_status")
sources <- finbif_metadata("source", "ru")

taxon_rank <- finbif_metadata("taxon_rank")
regulatory_status <- finbif_metadata("regulatory_status")

country <- finbif_metadata("country")
taxon_rank <- finbif_metadata("taxon_rank")

region <- finbif_metadata("region")
country <- finbif_metadata("country")

bio_province <- finbif_metadata("bio_province")
region <- finbif_metadata("region")

bird_assoc_area <- finbif_metadata("bird_assoc_area")
bio_province <- finbif_metadata("bio_province")

finnish_occurrence_status <- finbif_metadata("finnish_occurrence_status")
bird_assoc_area <- finbif_metadata("bird_assoc_area")

restriction_level <- finbif_metadata("restriction_level")
finnish_occurrence_status <- finbif_metadata("finnish_occurrence_status")

})
restriction_level <- finbif_metadata("restriction_level")

expect_snapshot(sources)
})

expect_snapshot(regulatory_status)
expect_snapshot(sources)

expect_snapshot(taxon_rank)
expect_snapshot(regulatory_status)

expect_snapshot(country)
expect_snapshot(taxon_rank)

expect_snapshot(region)
expect_snapshot(country)

expect_snapshot(bio_province)
expect_snapshot(region)

expect_snapshot(bird_assoc_area)
expect_snapshot(bio_province)

expect_snapshot(finnish_occurrence_status)
expect_snapshot(bird_assoc_area)

expect_snapshot(restriction_level)
expect_snapshot(finnish_occurrence_status)

expect_snapshot(restriction_level)

}

expect_error(
finbif_metadata("not_metadata"),
Expand Down
Loading

0 comments on commit 2821898

Please sign in to comment.