Marc Kupietz | 7de5f32 | 2025-06-04 17:17:22 +0200 | [diff] [blame^] | 1 | test_that("textMetadata includes expected metadata fields", { |
Marc Kupietz | 6ddece4 | 2023-12-18 17:02:36 +0100 | [diff] [blame] | 2 | skip_if_offline() |
Marc Kupietz | 7de5f32 | 2025-06-04 17:17:22 +0200 | [diff] [blame^] | 3 | kco <- RKorAPClient::KorAPConnection(accessToken = NULL) |
| 4 | skip_if_offline() |
| 5 | kco <- RKorAPClient::KorAPConnection(accessToken = NULL) |
| 6 | m <- textMetadata(kco, "WUD17/B96/57558") |
| 7 | |
| 8 | expect_s3_class(m, "tbl_df") |
| 9 | expect_true("textSigle" %in% names(m)) |
| 10 | expect_true("requestUrl" %in% names(m)) |
| 11 | expect_true("webUIRequestUrl" %in% names(m)) |
| 12 | expect_equal(m$textSigle[1], "WUD17/B96/57558") |
| 13 | expect_true(nrow(m) == 1) |
Marc Kupietz | 6ddece4 | 2023-12-18 17:02:36 +0100 | [diff] [blame] | 14 | }) |
| 15 | |
Marc Kupietz | 7de5f32 | 2025-06-04 17:17:22 +0200 | [diff] [blame^] | 16 | test_that("textMetadata works with multiple text sigles", { |
| 17 | skip_if_offline() |
| 18 | kco <- RKorAPClient::KorAPConnection(accessToken = NULL) |
| 19 | m <- textMetadata(kco, c("WUD17/B96/57558", "WUD17/A97/08541")) |
| 20 | |
| 21 | expect_s3_class(m, "tbl_df") |
| 22 | expect_true("textSigle" %in% names(m)) |
| 23 | expect_true(nrow(m) == 2) |
| 24 | expect_true(all(c("WUD17/B96/57558", "WUD17/A97/08541") %in% m$textSigle)) |
| 25 | }) |
| 26 | |
| 27 | test_that("textMetadata includes expected metadata columns", { |
| 28 | skip_if_offline() |
| 29 | kco <- KorAPConnection(accessToken = NULL) |
| 30 | m <- textMetadata(kco, "WUD17/B96/57558") |
| 31 | |
| 32 | # Check for common metadata fields |
| 33 | expect_true("textType" %in% names(m)) |
| 34 | expect_true("textSigle" %in% names(m)) |
| 35 | expect_true("requestUrl" %in% names(m)) |
| 36 | expect_true("webUIRequestUrl" %in% names(m)) |
| 37 | }) |
Marc Kupietz | 6ddece4 | 2023-12-18 17:02:36 +0100 | [diff] [blame] | 38 | |
| 39 | test_that("textMetadata works for unknown text sigles", { |
| 40 | skip_if_offline() |
Marc Kupietz | 7de5f32 | 2025-06-04 17:17:22 +0200 | [diff] [blame^] | 41 | kco <- KorAPConnection(accessToken = NULL) |
| 42 | m <- textMetadata(kco, c("WUD17/B96/57558", "unknownsigle")) |
| 43 | |
| 44 | expect_true("errors" %in% names(m)) |
| 45 | expect_true(nrow(m) >= 1) |
| 46 | |
| 47 | # Test with only unknown sigle |
| 48 | m_unknown <- textMetadata(kco, "completely/unknown/sigle") |
| 49 | expect_true("errors" %in% names(m_unknown)) |
Marc Kupietz | 6ddece4 | 2023-12-18 17:02:36 +0100 | [diff] [blame] | 50 | }) |
Marc Kupietz | 06e5b97 | 2024-12-07 17:22:06 +0100 | [diff] [blame] | 51 | |
| 52 | test_that("textMetadata works with list valued fields", { |
| 53 | skip_if_offline() |
Marc Kupietz | 7de5f32 | 2025-06-04 17:17:22 +0200 | [diff] [blame^] | 54 | kco <- KorAPConnection(accessToken = NULL) |
| 55 | m <- textMetadata(kco, "WUD17/B96/57558") |
| 56 | |
| 57 | expect_true("staat-gesellschaft\\tbiographien-interviews" == m$textClass[1] || |
| 58 | grepl("\\t", m$textClass[1]), |
| 59 | "multiple text classes / domains should be tab separated") |
| 60 | }) |
| 61 | |
| 62 | test_that("textMetadata respects verbose parameter", { |
| 63 | skip_if_offline() |
| 64 | kco <- KorAPConnection(accessToken = NULL, verbose = FALSE) |
| 65 | |
| 66 | # Test with verbose = FALSE (default from connection) |
| 67 | expect_silent(m1 <- textMetadata(kco, "WUD17/B96/57558")) |
| 68 | |
| 69 | # Test with explicit verbose = TRUE |
| 70 | expect_output(m2 <- textMetadata(kco, "WUD17/B96/57558", verbose = TRUE), |
| 71 | "Getting metadata") |
| 72 | |
| 73 | # Results should be the same regardless of verbosity |
| 74 | expect_equal(m1$textSigle, m2$textSigle) |
| 75 | }) |
| 76 | |
| 77 | test_that("textMetadata handles URL encoding correctly", { |
| 78 | skip_if_offline() |
| 79 | kco <- KorAPConnection(accessToken = NULL) |
| 80 | |
| 81 | # Test with a text sigle that contains special characters |
| 82 | m <- textMetadata(kco, "WUD17/B96/57558") |
| 83 | |
| 84 | expect_true(grepl("corpus/WUD17%2FB96%2F57558", m$requestUrl[1])) |
| 85 | expect_true(grepl("textSigle", m$webUIRequestUrl[1])) |
| 86 | }) |
| 87 | |
| 88 | test_that("textMetadata returns proper data types", { |
| 89 | skip_if_offline() |
| 90 | kco <- KorAPConnection(accessToken = NULL) |
| 91 | m <- textMetadata(kco, "WUD17/B96/57558") |
| 92 | |
| 93 | # All columns should be character type due to mutate(across(everything(), as.character)) |
| 94 | expect_true(all(sapply(m, is.character))) |
| 95 | }) |
| 96 | |
| 97 | test_that("textMetadata column ordering", { |
| 98 | skip_if_offline() |
| 99 | kco <- KorAPConnection(accessToken = NULL) |
| 100 | m <- textMetadata(kco, "WUD17/B96/57558") |
| 101 | |
| 102 | # textSigle should be the first column due to relocate(textSigle) |
| 103 | expect_equal(names(m)[1], "textSigle") |
| 104 | }) |
| 105 | |
| 106 | test_that("textMetadata handles special characters in sigle", { |
| 107 | skip_if_offline() |
| 108 | kco <- KorAPConnection(accessToken = NULL) |
| 109 | |
| 110 | # Test with valid sigle containing numbers and special format |
| 111 | m <- textMetadata(kco, "WUD17/A97/08542") |
| 112 | |
| 113 | expect_true("textSigle" %in% names(m)) |
| 114 | expect_equal(m$textSigle[1], "WUD17/A97/08542") |
| 115 | }) |
| 116 | |
| 117 | test_that("textMetadata error handling for API failures", { |
| 118 | # Mock a connection with invalid API URL to test error handling |
| 119 | kco <- KorAPConnection(KorAPUrl = "https://invalid.url", accessToken = NULL) |
| 120 | |
| 121 | # This should handle API failures gracefully |
| 122 | m <- textMetadata(kco, "WUD17/B96/57558") |
| 123 | expect_true("errors" %in% names(m) || nrow(m) == 0) |
| 124 | }) |
| 125 | |
| 126 | test_that("textMetadata preserves request information", { |
| 127 | skip_if_offline() |
| 128 | kco <- KorAPConnection(accessToken = NULL) |
| 129 | test_sigle <- "WUD17/B96/57558" |
| 130 | m <- textMetadata(kco, test_sigle) |
| 131 | |
| 132 | expect_true("requestUrl" %in% names(m)) |
| 133 | expect_true("webUIRequestUrl" %in% names(m)) |
| 134 | expect_true(grepl(URLencode(test_sigle, reserved = TRUE), m$requestUrl[1])) |
| 135 | expect_true(grepl("textSigle", m$webUIRequestUrl[1])) |
| 136 | }) |
| 137 | |
| 138 | test_that("textMetadata batch processing consistency", { |
| 139 | skip_if_offline() |
| 140 | kco <- KorAPConnection(accessToken = NULL) |
| 141 | |
| 142 | # Get metadata individually |
| 143 | m1 <- textMetadata(kco, "WUD17/B96/57558") |
| 144 | m2 <- textMetadata(kco, "WUD17/A97/08541") |
| 145 | |
| 146 | # Get metadata in batch |
| 147 | m_batch <- textMetadata(kco, c("WUD17/B96/57558", "WUD17/A97/08541")) |
| 148 | |
| 149 | # Should have same number of rows |
| 150 | expect_equal(nrow(m_batch), 2) |
| 151 | |
| 152 | # Should contain both sigles |
| 153 | expect_true(all(c("WUD17/B96/57558", "WUD17/A97/08541") %in% m_batch$textSigle)) |
Marc Kupietz | 06e5b97 | 2024-12-07 17:22:06 +0100 | [diff] [blame] | 154 | }) |