blob: e8356fe2d5b614356424f4eb6ae3803234690847 [file] [log] [blame]
Marc Kupietz6a16d7d2026-08-31 21:36:01 +02001withOfflineKorAPEnv <- function(code, verboseEnv = NA_character_) {
2 oldUrl <- Sys.getenv("KORAP_URL", unset = NA_character_)
3 oldVerbose <- Sys.getenv("KORAP_VERBOSE", unset = NA_character_)
4 on.exit(
5 {
6 if (is.na(oldUrl)) Sys.unsetenv("KORAP_URL") else Sys.setenv(KORAP_URL = oldUrl)
7 if (is.na(oldVerbose)) {
8 Sys.unsetenv("KORAP_VERBOSE")
9 } else {
10 Sys.setenv(KORAP_VERBOSE = oldVerbose)
11 }
12 },
13 add = TRUE
14 )
15 Sys.setenv(KORAP_URL = "https://example.invalid/")
16 if (is.na(verboseEnv)) Sys.unsetenv("KORAP_VERBOSE") else Sys.setenv(KORAP_VERBOSE = verboseEnv)
17 force(code)
18}
19
20test_that("the documented KorAPConnection() defaults are the ones actually used", {
21 # KorAPConnection() only forwards the arguments that were supplied, so its
22 # own defaults are never evaluated. They mirror those of the initialize
23 # method to document them, and this test makes sure that the two cannot
24 # drift apart silently.
25 withOfflineKorAPEnv({
26 kco <- KorAPConnection(accessToken = NULL)
27 documented <- formals(KorAPConnection)
28
29 # accessToken (keyring), apiUrl (derived) and authorizationSupported
30 # (answered by the server) are covered by the tests below instead
31 for (parameter in c(
32 "KorAPUrl", "apiVersion", "oauthClient", "oauthScope",
33 "userAgent", "timeout", "verbose", "cache"
34 )) {
35 expect_equal(
36 slot(kco, parameter),
37 eval(documented[[parameter]]),
38 info = parameter
39 )
40 }
41 })
42})
43
44test_that("KorAPConnection() forwards only the arguments that were supplied", {
45 # `missing()` based overrides in the initialize method must keep working
46 withOfflineKorAPEnv(
47 {
48 expect_true(KorAPConnection(accessToken = NULL)@verbose)
49 # an explicitly passed verbose argument still wins over the env var
50 expect_false(KorAPConnection(accessToken = NULL, verbose = FALSE)@verbose)
51 },
52 verboseEnv = "true"
53 )
54})
55
56test_that("KorAPConnection() derives apiUrl unless it is given explicitly", {
57 kco <- KorAPConnection(KorAPUrl = "https://example.invalid/", accessToken = NULL)
58 expect_equal(kco@apiUrl, "https://example.invalid/api/v1.0/")
59
60 kco <- KorAPConnection(
61 KorAPUrl = "https://example.invalid/",
62 apiUrl = "https://other.invalid/api/",
63 accessToken = NULL
64 )
65 expect_equal(kco@apiUrl, "https://other.invalid/api/")
66})
67
68test_that("KorAPConnection objects can still be created with new()", {
69 kco <- methods::new(
70 "KorAPConnection",
71 KorAPUrl = "https://example.invalid/",
72 apiUrl = "https://example.invalid/api/v1.0/",
73 accessToken = NULL,
74 verbose = FALSE
75 )
76 expect_s4_class(kco, "KorAPConnection")
77 expect_equal(kco@apiUrl, "https://example.invalid/api/v1.0/")
78})
79
80test_that("defaultKorAPUrl() honours KORAP_URL", {
81 old <- Sys.getenv("KORAP_URL", unset = NA_character_)
82 on.exit(
83 if (is.na(old)) Sys.unsetenv("KORAP_URL") else Sys.setenv(KORAP_URL = old),
84 add = TRUE
85 )
86
87 Sys.setenv(KORAP_URL = "https://example.invalid/")
88 expect_equal(defaultKorAPUrl(), "https://example.invalid/")
89
90 # an empty KORAP_URL counts as unset
91 Sys.setenv(KORAP_URL = "")
92 expect_equal(defaultKorAPUrl(), "https://korap.ids-mannheim.de/")
93
94 Sys.unsetenv("KORAP_URL")
95 expect_equal(defaultKorAPUrl(), "https://korap.ids-mannheim.de/")
96})