| Marc Kupietz | a3a8cd9 | 2026-09-08 07:59:04 +0200 | [diff] [blame] | 1 | test_that("cacheAsFileName appends .rds where it is missing", { |
| 2 | expect_equal(RKorAPClient:::cacheAsFileName("analysis"), "analysis.rds") |
| 3 | expect_equal(RKorAPClient:::cacheAsFileName("analysis.rds"), "analysis.rds") |
| 4 | expect_equal(RKorAPClient:::cacheAsFileName("analysis.RDS"), "analysis.RDS") |
| 5 | }) |
| 6 | |
| Marc Kupietz | afac514 | 2026-09-08 10:37:42 +0200 | [diff] [blame^] | 7 | test_that("cacheAsInfo says what produced a file", { |
| 8 | skip_if_offline() |
| 9 | kco <- KorAPConnection(accessToken = NULL, verbose = FALSE) |
| 10 | file <- tempfile(fileext = ".rds") |
| 11 | on.exit(unlink(file), add = TRUE) |
| 12 | |
| 13 | frequencyQuery(kco, "Ameisenplage", cacheAs = file) |
| 14 | info <- cacheAsInfo(file) |
| 15 | |
| 16 | expect_equal(info$packageVersion, as.character(utils::packageVersion("RKorAPClient"))) |
| 17 | expect_equal(info$apiUrl, kco@apiUrl) |
| 18 | expect_equal(info$indexRevision, kco@indexRevision) |
| 19 | expect_equal(info$parameters$query, "Ameisenplage") |
| 20 | # given without its extension, the .rds file is found |
| 21 | expect_equal(cacheAsInfo(sub("\\.rds$", "", file)), info) |
| 22 | }) |
| 23 | |
| 24 | test_that("cacheAsInfo has nothing to report for a file from before 1.4.0", { |
| 25 | file <- tempfile(fileext = ".rds") |
| 26 | on.exit(unlink(file), add = TRUE) |
| 27 | saveRDS(tibble::tibble(x = 1), file) |
| 28 | |
| 29 | expect_null(cacheAsInfo(file)) |
| 30 | }) |
| 31 | |
| 32 | test_that("cacheAsInfo refuses a file that is not there", { |
| 33 | expect_error( |
| 34 | cacheAsInfo(file.path(tempdir(), "no-such-cache.rds")), |
| 35 | "does not exist" |
| 36 | ) |
| 37 | }) |
| 38 | |
| Marc Kupietz | a3a8cd9 | 2026-09-08 07:59:04 +0200 | [diff] [blame] | 39 | test_that("a cache file is written and read back, without contacting the server", { |
| 40 | skip_if_offline() |
| 41 | kco <- KorAPConnection(accessToken = NULL, verbose = FALSE) |
| 42 | dir <- file.path(tempdir(), "cacheAsTest") |
| 43 | dir.create(dir, showWarnings = FALSE) |
| 44 | on.exit(unlink(dir, recursive = TRUE), add = TRUE) |
| 45 | |
| 46 | # every query function that takes cacheAs, so that one added later without a |
| 47 | # cache file of its own does not go unnoticed |
| 48 | queries <- list( |
| 49 | "frequencyQuery" = function(f) frequencyQuery(kco, "Ameisenplage", cacheAs = f), |
| 50 | "corpusStats" = function(f) corpusStats(kco, vc = "pubDate since 2020", as.df = TRUE, cacheAs = f), |
| 51 | "collocationScoreQuery" = function(f) collocationScoreQuery(kco, "Grund", "triftiger", cacheAs = f), |
| 52 | "textMetadata" = function(f) textMetadata(kco, "WPD17/L79/98721", cacheAs = f) |
| 53 | ) |
| 54 | |
| 55 | for (name in names(queries)) { |
| 56 | query <- queries[[name]] |
| 57 | file <- file.path(dir, name) |
| 58 | |
| 59 | fresh <- query(file) |
| 60 | expect_true(file.exists(paste0(file, ".rds")), info = name) |
| 61 | |
| 62 | fromCache <- local({ |
| 63 | testthat::local_mocked_bindings( |
| 64 | apiCall = function(...) stop("server must not be contacted"), |
| 65 | .package = "RKorAPClient" |
| 66 | ) |
| 67 | query(file) |
| 68 | }) |
| 69 | expect_equal(fromCache, fresh, info = name) |
| 70 | |
| 71 | # what produced the result lives in the file only, not in the value |
| 72 | expect_null(attr(fromCache, RKorAPClient:::cacheAsAttribute), info = name) |
| 73 | expect_equal( |
| 74 | attr(readRDS(paste0(file, ".rds")), RKorAPClient:::cacheAsAttribute)$scoreVersion, |
| 75 | RKorAPClient:::cacheAsScoreVersion, |
| 76 | info = name |
| 77 | ) |
| 78 | } |
| 79 | }) |
| 80 | |
| 81 | test_that("a cache file written before the scores were corrected is refused", { |
| 82 | skip_if_offline() |
| 83 | kco <- KorAPConnection(accessToken = NULL, verbose = FALSE) |
| 84 | file <- tempfile(fileext = ".rds") |
| 85 | on.exit(unlink(file), add = TRUE) |
| 86 | |
| 87 | frequencyQuery(kco, "Ameisenplage", cacheAs = file) |
| 88 | aged <- readRDS(file) |
| 89 | record <- attr(aged, RKorAPClient:::cacheAsAttribute) |
| 90 | # as a file from before the corrections has it: an older score generation, and |
| 91 | # an older version to go with it |
| 92 | record$scoreVersion <- "1.3.0" |
| 93 | record$packageVersion <- "1.3.0" |
| 94 | attr(aged, RKorAPClient:::cacheAsAttribute) <- record |
| 95 | saveRDS(aged, file) |
| 96 | |
| 97 | expect_warning(frequencyQuery(kco, "Ameisenplage", cacheAs = file), "1\\.3\\.0") |
| 98 | # and the refused file is replaced by a current one, so the warning comes once |
| 99 | expect_silent(frequencyQuery(kco, "Ameisenplage", cacheAs = file)) |
| 100 | }) |
| 101 | |
| 102 | test_that("a query that differs is recomputed rather than read back", { |
| 103 | skip_if_offline() |
| 104 | kco <- KorAPConnection(accessToken = NULL, verbose = FALSE) |
| 105 | file <- tempfile(fileext = ".rds") |
| 106 | on.exit(unlink(file), add = TRUE) |
| 107 | |
| 108 | frequencyQuery(kco, "Ameisenplage", cacheAs = file) |
| 109 | expect_warning(frequencyQuery(kco, "Heuschreckenplage", cacheAs = file), "query") |
| 110 | }) |
| 111 | |
| 112 | test_that("verbosity is not part of what a cache file records", { |
| 113 | skip_if_offline() |
| 114 | kco <- KorAPConnection(accessToken = NULL, verbose = FALSE) |
| 115 | file <- tempfile(fileext = ".rds") |
| 116 | on.exit(unlink(file), add = TRUE) |
| 117 | |
| 118 | corpusStats(kco, vc = "pubDate since 2020", as.df = TRUE, cacheAs = file) |
| 119 | # how loud a query is does not change what it returns, so it must not make |
| 120 | # the file be thrown away |
| 121 | expect_silent( |
| 122 | corpusStats(kco, vc = "pubDate since 2020", as.df = TRUE, verbose = TRUE, cacheAs = file) |
| 123 | ) |
| 124 | }) |