blob: 2a15b75b024d12d3f324c1c052b9a7e6bff4d285 [file] [log] [blame]
Marc Kupietza3a8cd92026-09-08 07:59:04 +02001test_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
7test_that("a cache file is written and read back, without contacting the server", {
8 skip_if_offline()
9 kco <- KorAPConnection(accessToken = NULL, verbose = FALSE)
10 dir <- file.path(tempdir(), "cacheAsTest")
11 dir.create(dir, showWarnings = FALSE)
12 on.exit(unlink(dir, recursive = TRUE), add = TRUE)
13
14 # every query function that takes cacheAs, so that one added later without a
15 # cache file of its own does not go unnoticed
16 queries <- list(
17 "frequencyQuery" = function(f) frequencyQuery(kco, "Ameisenplage", cacheAs = f),
18 "corpusStats" = function(f) corpusStats(kco, vc = "pubDate since 2020", as.df = TRUE, cacheAs = f),
19 "collocationScoreQuery" = function(f) collocationScoreQuery(kco, "Grund", "triftiger", cacheAs = f),
20 "textMetadata" = function(f) textMetadata(kco, "WPD17/L79/98721", cacheAs = f)
21 )
22
23 for (name in names(queries)) {
24 query <- queries[[name]]
25 file <- file.path(dir, name)
26
27 fresh <- query(file)
28 expect_true(file.exists(paste0(file, ".rds")), info = name)
29
30 fromCache <- local({
31 testthat::local_mocked_bindings(
32 apiCall = function(...) stop("server must not be contacted"),
33 .package = "RKorAPClient"
34 )
35 query(file)
36 })
37 expect_equal(fromCache, fresh, info = name)
38
39 # what produced the result lives in the file only, not in the value
40 expect_null(attr(fromCache, RKorAPClient:::cacheAsAttribute), info = name)
41 expect_equal(
42 attr(readRDS(paste0(file, ".rds")), RKorAPClient:::cacheAsAttribute)$scoreVersion,
43 RKorAPClient:::cacheAsScoreVersion,
44 info = name
45 )
46 }
47})
48
49test_that("a cache file written before the scores were corrected is refused", {
50 skip_if_offline()
51 kco <- KorAPConnection(accessToken = NULL, verbose = FALSE)
52 file <- tempfile(fileext = ".rds")
53 on.exit(unlink(file), add = TRUE)
54
55 frequencyQuery(kco, "Ameisenplage", cacheAs = file)
56 aged <- readRDS(file)
57 record <- attr(aged, RKorAPClient:::cacheAsAttribute)
58 # as a file from before the corrections has it: an older score generation, and
59 # an older version to go with it
60 record$scoreVersion <- "1.3.0"
61 record$packageVersion <- "1.3.0"
62 attr(aged, RKorAPClient:::cacheAsAttribute) <- record
63 saveRDS(aged, file)
64
65 expect_warning(frequencyQuery(kco, "Ameisenplage", cacheAs = file), "1\\.3\\.0")
66 # and the refused file is replaced by a current one, so the warning comes once
67 expect_silent(frequencyQuery(kco, "Ameisenplage", cacheAs = file))
68})
69
70test_that("a query that differs is recomputed rather than read back", {
71 skip_if_offline()
72 kco <- KorAPConnection(accessToken = NULL, verbose = FALSE)
73 file <- tempfile(fileext = ".rds")
74 on.exit(unlink(file), add = TRUE)
75
76 frequencyQuery(kco, "Ameisenplage", cacheAs = file)
77 expect_warning(frequencyQuery(kco, "Heuschreckenplage", cacheAs = file), "query")
78})
79
80test_that("verbosity is not part of what a cache file records", {
81 skip_if_offline()
82 kco <- KorAPConnection(accessToken = NULL, verbose = FALSE)
83 file <- tempfile(fileext = ".rds")
84 on.exit(unlink(file), add = TRUE)
85
86 corpusStats(kco, vc = "pubDate since 2020", as.df = TRUE, cacheAs = file)
87 # how loud a query is does not change what it returns, so it must not make
88 # the file be thrown away
89 expect_silent(
90 corpusStats(kco, vc = "pubDate since 2020", as.df = TRUE, verbose = TRUE, cacheAs = file)
91 )
92})