Skip to content

Commit

Permalink
refactor cache tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ErdaradunGaztea committed Apr 14, 2024
1 parent 4572a96 commit b1c78f0
Show file tree
Hide file tree
Showing 24 changed files with 44 additions and 46 deletions.
4 changes: 2 additions & 2 deletions tests/testthat/setup-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ expect_no_error <- function(object) {
expect_error(object, regexp = NA)
}

expect_cache <- function(tested_function, expected, ...) {
expect_cache <- function(expression, expected) {
# If the function reads from cache, it means that it will work offline
httptest2::without_internet({
httptest2::expect_no_request({
result_from_cache <- tested_function(...)
result_from_cache <- force(expression)
})

expect_identical(result_from_cache, expected)
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/setup-test_suites.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
test_cache <- function(tested_function, expected_value, ...) {
# TODO: pass expression as first parameter
test_cache <- function(expression, expected_value) {
test_that("if possible, reads from cache", {
expect_cache(tested_function, expected_value, ...)
expect_cache(expression, expected_value)
})
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-bioc-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("a", {

# TESTS ----
test_dependencies(Biostrings_deps)
test_cache(wood_bioc_dependencies, Biostrings_deps, "Biostrings")
test_cache({ wood_bioc_dependencies("Biostrings") }, Biostrings_deps)
test_param_package(wood_bioc_dependencies)
test_param_bioc_release(wood_bioc_dependencies, package = "Biostrings")

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-bioc-packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("c", {

# TESTS ----
test_packages(bioc_packages)
test_cache(wood_bioc_packages, bioc_packages)
test_cache({ wood_bioc_packages() }, bioc_packages)
test_param_bioc_release(wood_bioc_packages)

test_that("there's a Biostrings package in the list", {
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-bioc-releases.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,4 @@ test_that("bioc releases contain some of the published release codes", {
expect_subset(c("1.0", "1.9", "2.13", "3.2", "3.14"), bioc_releases)
})

test_that("if possible, reads from cache", {
expect_cache(wood_bioc_releases, bioc_releases)
})
test_cache({ wood_bioc_releases() }, bioc_releases)
2 changes: 1 addition & 1 deletion tests/testthat/test-bioc-version.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("f", {

# TESTS ----
test_version(affy_version)
test_cache(wood_bioc_version, affy_version, "affy")
test_cache({ wood_bioc_version("affy") }, affy_version)
test_param_package(wood_bioc_version)
test_param_bioc_release(wood_bioc_version, package = "affy")

Expand Down
9 changes: 5 additions & 4 deletions tests/testthat/test-cran-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ with_mock_dir("h", {

# TESTS ----
test_dependencies(deepdep_deps)
test_cache(wood_cran_dependencies, deepdep_deps, "deepdep", version = "0.2.0")
test_cache({ wood_cran_dependencies("deepdep", version = "0.2.0") }, deepdep_deps)
test_param_package(wood_cran_dependencies, version = "0.2.0")
test_param_version(wood_cran_dependencies, package = "deepdep")

test_that("uses cache from wood_cran_versions() if available", {
wood_clear_cache()
wood_cran_versions("deepdep")
expect_cache(wood_cran_dependencies, deepdep_deps,
package = "deepdep",
version = "0.2.0")
expect_cache(
{ wood_cran_dependencies(package = "deepdep", version = "0.2.0") },
deepdep_deps
)
})

with_mock_dir("i", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-cran-latest.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("j", {

# TESTS ----
test_version(versionsort_latest)
test_cache(wood_cran_latest, versionsort_latest, "versionsort")
test_cache({ wood_cran_latest("versionsort") }, versionsort_latest)
test_param_package(wood_cran_latest)

skip_if_offline()
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-cran-packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("k", {

# TESTS ----
test_packages(cran_packages)
test_cache(wood_cran_packages, cran_packages)
test_cache({ wood_cran_packages() }, cran_packages)

test_that("there's a ggplot2 package in the list", {
expect_subset("ggplot2", cran_packages)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-cran-versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("l", {

# TESTS ----
test_versions(versionsort_versions)
test_cache(wood_cran_versions, versionsort_versions, "versionsort")
test_cache({ wood_cran_versions("versionsort") }, versionsort_versions)
test_param_package(wood_cran_versions)

test_that("versionsort versions contain some of the published version codes", {
Expand All @@ -23,7 +23,7 @@ test_that("use cache even if expired, but latest version hasn't changed yet", {
with_mock_dir("m", {
wood_cran_latest("versionsort")
})
expect_cache(wood_cran_versions, versionsort_versions, "versionsort")
expect_cache({ wood_cran_versions("versionsort") }, versionsort_versions)
})

skip_if_offline()
Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-github-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ with_mock_dir("n", {

# TESTS ----
test_dependencies(gglgbtq_deps)
test_cache(wood_github_dependencies, gglgbtq_deps, "gglgbtq", "turtletopia", tag = "v0.1.0")
test_cache({ wood_github_dependencies("gglgbtq", "turtletopia", tag = "v0.1.0") }, gglgbtq_deps)
test_param_package(wood_github_dependencies, user = "turtletopia")
test_param_gh_user(wood_github_dependencies, package = "gglgbtq")
test_param_tag(wood_github_dependencies, package = "gglgbtq", user = "turtletopia")
Expand All @@ -18,10 +18,10 @@ with_mock_dir("o", {
test_that("uses cache from wood_github_versions() if available if not latest commit", {
wood_clear_cache()
wood_github_versions("gglgbtq", "turtletopia")
expect_cache(wood_github_dependencies, gglgbtq_deps,
package = "gglgbtq",
user = "turtletopia",
tag = "v0.1.0")
expect_cache(
{ wood_github_dependencies(package = "gglgbtq", user = "turtletopia", tag = "v0.1.0") },
gglgbtq_deps
)
})
})

Expand All @@ -32,10 +32,10 @@ with_mock_dir("p", {
)
wood_clear_cache()
wood_github_latest("gglgbtq", "turtletopia")
expect_cache(wood_github_dependencies, gglgbtq_deps_latest,
package = "gglgbtq",
user = "turtletopia",
tag = "latest")
expect_cache(
{ wood_github_dependencies(package = "gglgbtq", user = "turtletopia", tag = "latest") },
gglgbtq_deps_latest
)
})
})

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-github-latest.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ with_mock_dir("q", {

# TESTS ----
test_version(gglgbtq_latest)
test_cache(wood_github_latest, gglgbtq_latest, "gglgbtq", "turtletopia")
test_cache({ wood_github_latest("gglgbtq", "turtletopia") }, gglgbtq_latest)
test_param_package(wood_github_latest, user = "turtletopia")
test_param_gh_user(wood_github_latest, package = "gglgbtq")

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-github-packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ turtletopia_packages <- wood_github_packages("turtletopia")

# TESTS ----
test_packages(turtletopia_packages)
test_cache(wood_github_packages, turtletopia_packages, user = "turtletopia")
test_cache({ wood_github_packages(user = "turtletopia") }, turtletopia_packages)
test_param_gh_user(wood_github_packages)
test_param_include_forks(wood_github_packages, user = "turtletopia")

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-github-tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ with_mock_dir("s", {

# TESTS ----
test_tags(gglgbtq_tags)
test_cache(wood_github_tags, gglgbtq_tags, "gglgbtq", "turtletopia")
test_cache({ wood_github_tags("gglgbtq", "turtletopia") }, gglgbtq_tags)
test_param_package(wood_github_tags, user = "turtletopia")
test_param_gh_user(wood_github_tags, package = "gglgbtq")

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-github-versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ with_mock_dir("t", {

# TESTS ----
test_versions(gglgbtq_versions)
test_cache(wood_github_versions, gglgbtq_versions, "gglgbtq", "turtletopia")
test_cache({ wood_github_versions("gglgbtq", "turtletopia") }, gglgbtq_versions)
test_param_package(wood_github_versions, user = "turtletopia")
test_param_gh_user(wood_github_versions, package = "gglgbtq")

Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-gitlab-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("l0", {

# TESTS ----
test_dependencies(rock_deps)
test_cache(wood_gitlab_dependencies, rock_deps, "rock", "r-packages", tag = "0.6.0")
test_cache({ wood_gitlab_dependencies("rock", "r-packages", tag = "0.6.0") }, rock_deps)
test_param_package(wood_gitlab_dependencies, user = "r-packages")
test_param_gh_user(wood_gitlab_dependencies, package = "rock")
test_param_tag(wood_gitlab_dependencies, package = "rock", user = "r-packages")
Expand All @@ -31,10 +31,10 @@ with_mock_dir("l2", {
)
wood_clear_cache()
wood_gitlab_latest("rock", "r-packages")
expect_cache(wood_gitlab_dependencies, rock_deps_latest,
package = "rock",
user = "r-packages",
tag = "latest")
expect_cache(
{ wood_gitlab_dependencies(package = "rock", user = "r-packages", tag = "latest") },
rock_deps_latest
)
})
})

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-gitlab-latest.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("l3", {

# TESTS ----
test_version(rock_latest)
test_cache(wood_gitlab_latest, rock_latest, "rock", "r-packages")
test_cache({ wood_gitlab_latest("rock", "r-packages") }, rock_latest)
test_param_package(wood_gitlab_latest, user = "r-packages")
test_param_gh_user(wood_gitlab_latest, package = "rock")

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-gitlab-packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("l4", {

# TESTS ----
test_packages(r_packages_packages)
test_cache(wood_gitlab_packages, r_packages_packages, user = "r-packages")
test_cache({ wood_gitlab_packages(user = "r-packages") }, r_packages_packages)
test_param_gh_user(wood_gitlab_packages)
test_param_include_forks(wood_gitlab_packages, user = "r-packages")

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-runiverse-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("u", {

# TESTS ----
test_dependencies(ggplot2_deps)
test_cache(wood_runiverse_dependencies, ggplot2_deps, "ggplot2", "tidyverse")
test_cache({ wood_runiverse_dependencies("ggplot2", "tidyverse") }, ggplot2_deps)
test_param_package(wood_runiverse_dependencies, universe = "tidyverse")
test_param_runiverse(wood_runiverse_dependencies, package = "ggplot2")

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-runiverse-packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("v", {

# TESTS ----
test_packages(tidyverse_packages)
test_cache(wood_runiverse_packages, tidyverse_packages, universe <- "tidyverse")
test_cache({ wood_runiverse_packages(universe = "tidyverse") }, tidyverse_packages)
test_param_runiverse(wood_runiverse_packages)

test_that("tidyverse universe has ggplot2 package", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-runiverse-version.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("w", {

# TESTS ----
test_version(versionsort_version)
test_cache(wood_runiverse_version, versionsort_version, "versionsort", "turtletopia")
test_cache({ wood_runiverse_version("versionsort", "turtletopia") }, versionsort_version)
test_param_package(wood_runiverse_version, universe = "turtletopia")
test_param_runiverse(wood_runiverse_version, package = "versionsort")

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-url-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("x", {

# TESTS ----
test_dependencies(dockerfiler_deps)
test_cache(wood_url_dependencies, dockerfiler_deps, "dockerfiler", "https://colinfay.me")
test_cache({ wood_url_dependencies("dockerfiler", "https://colinfay.me") }, dockerfiler_deps)
test_param_package(wood_url_dependencies, repository = "https://colinfay.me")
test_param_url_repo(wood_url_dependencies, package = "dockerfiler")

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-url-packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("y", {

# TESTS ----
test_packages(cynkra_packages)
test_cache(wood_url_packages, cynkra_packages, "https://cynkra.r-universe.dev")
test_cache({ wood_url_packages("https://cynkra.r-universe.dev") }, cynkra_packages)
test_param_url_repo(wood_url_packages)

test_that("Cynkra repository has cynkrathis package", {
Expand All @@ -22,5 +22,5 @@ with_mock_dir("x", {
})

test_that("trailing slash url and without one share the same cache", {
expect_cache(wood_url_packages, cynkra_packages, "https://cynkra.r-universe.dev/")
expect_cache({ wood_url_packages("https://cynkra.r-universe.dev/") }, cynkra_packages)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-url-version.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with_mock_dir("x", {

# TESTS ----
test_version(dockerfiler_version)
test_cache(wood_url_version, dockerfiler_version, "dockerfiler", "https://colinfay.me")
test_cache({ wood_url_version("dockerfiler", "https://colinfay.me") }, dockerfiler_version)
test_param_package(wood_url_version, repository = "https://colinfay.me")
test_param_url_repo(wood_url_version, package = "dockerfiler")

Expand Down

0 comments on commit b1c78f0

Please sign in to comment.