# Tests for caching functionality test_that("enable_cache sets cache directory", { temp_dir <- file.path(tempdir(), "nomisdata_test_enable") if (dir.exists(temp_dir)) unlink(temp_dir, recursive = TRUE) # Save original option orig_cache <- getOption("nomisdata.cache_dir") on.exit({ options(nomisdata.cache_dir = orig_cache) if (dir.exists(temp_dir)) unlink(temp_dir, recursive = TRUE) }) enable_cache(temp_dir) expect_equal(getOption("nomisdata.cache_dir"), temp_dir) expect_true(dir.exists(temp_dir)) }) test_that("enable_cache creates directory if needed", { temp_dir <- file.path(tempdir(), "nomisdata_test_create") if (dir.exists(temp_dir)) unlink(temp_dir, recursive = TRUE) orig_cache <- getOption("nomisdata.cache_dir") on.exit({ options(nomisdata.cache_dir = orig_cache) if (dir.exists(temp_dir)) unlink(temp_dir, recursive = TRUE) }) enable_cache(temp_dir) cache_dir <- getOption("nomisdata.cache_dir") expect_type(cache_dir, "character") expect_true(dir.exists(cache_dir)) }) test_that("clear_cache removes cache directory", { temp_dir <- file.path(tempdir(), "nomisdata_test_clear") if (dir.exists(temp_dir)) unlink(temp_dir, recursive = TRUE) dir.create(temp_dir, recursive = TRUE) orig_cache <- getOption("nomisdata.cache_dir") on.exit({ options(nomisdata.cache_dir = orig_cache) if (dir.exists(temp_dir)) unlink(temp_dir, recursive = TRUE) }) options(nomisdata.cache_dir = temp_dir) clear_cache() # The directory should be removed expect_false(dir.exists(temp_dir)) }) test_that("cache_data and get_cached_data work together", { temp_dir <- file.path(tempdir(), "nomisdata_test_cache_ops") if (dir.exists(temp_dir)) unlink(temp_dir, recursive = TRUE) dir.create(temp_dir, recursive = TRUE, showWarnings = FALSE) orig_cache <- getOption("nomisdata.cache_dir") on.exit({ options(nomisdata.cache_dir = orig_cache) if (dir.exists(temp_dir)) unlink(temp_dir, recursive = TRUE) }) options(nomisdata.cache_dir = temp_dir) cache_key <- "test_key_123" test_data <- data.frame(x = 1:3, y = letters[1:3]) # Cache the data - ensure directory exists cache_file <- file.path(temp_dir, paste0(cache_key, ".rds")) cache_dir_for_file <- dirname(cache_file) if (!dir.exists(cache_dir_for_file)) { dir.create(cache_dir_for_file, recursive = TRUE, showWarnings = FALSE) } cache_data(cache_key, test_data) # Retrieve the data retrieved <- get_cached_data(cache_key) expect_equal(retrieved, test_data) }) test_that("get_cached_data respects max_age", { temp_dir <- file.path(tempdir(), "nomisdata_test_maxage") if (dir.exists(temp_dir)) unlink(temp_dir, recursive = TRUE) dir.create(temp_dir, recursive = TRUE, showWarnings = FALSE) orig_cache <- getOption("nomisdata.cache_dir") on.exit({ options(nomisdata.cache_dir = orig_cache) if (dir.exists(temp_dir)) unlink(temp_dir, recursive = TRUE) }) options(nomisdata.cache_dir = temp_dir) cache_key <- "old_data" test_data <- data.frame(x = 1) # Ensure directory exists cache_file <- file.path(temp_dir, paste0(cache_key, ".rds")) cache_dir_for_file <- dirname(cache_file) if (!dir.exists(cache_dir_for_file)) { dir.create(cache_dir_for_file, recursive = TRUE, showWarnings = FALSE) } cache_data(cache_key, test_data) # Should retrieve when fresh expect_equal(get_cached_data(cache_key, max_age = 3600), test_data) # Should return NULL when max_age = 0 (expired) expect_null(get_cached_data(cache_key, max_age = 0)) }) test_that("caching respects disabled cache", { # This test verifies cache functions handle NULL gracefully # Note: With the get_nomis_cache_dir() fallback, cache may still work # but it should not error orig_cache <- getOption("nomisdata.cache_dir") on.exit(options(nomisdata.cache_dir = orig_cache)) options(nomisdata.cache_dir = NULL) # Should not error when cache directory is NULL expect_no_error(cache_data("key", data.frame(x = 1))) # Result depends on implementation - either NULL or uses fallback # Both behaviors are acceptable result <- get_cached_data("key") expect_true(is.null(result) || is.data.frame(result)) })