R/cache.R was a stub, so a remote session read the ten metadata tables (about 3 MB) over HTTP every time. cog_open() now keeps them in USCOGDATA_CACHE_DIR/corpus/ and points the metadata views there. A cached file is used only while its sha256 matches the session's manifest; a download is checked before it replaces anything. Any failure (an HTTP error such as Hugging Face's 429, a mismatch, an unwritable directory) falls back to remote reads with a message. A local corpus is read in place and never cached. The yearly partitions stay remote; cog_mirror() is still the tool for repeat work on them. Against the public corpus, 2026-11-12, one run each, fresh cache: session open 1.76 s cold -> 0.38 s warm first query 1.16 s cold -> 0.68 s warm cog_spending() for Madison (552025209777), 2012 and 2020, returns the same 46 rows and the same digest as main. Live-corpus tests pass. Closes #64 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
133 lines
4.9 KiB
R
133 lines
4.9 KiB
R
# tests/testthat/test-metadata-cache.R
|
|
#
|
|
# uscogdata#64. On the remote path the ten metadata tables (about 3 MB) were
|
|
# read over HTTP every session. Kept in the cache directory and checked
|
|
# against the manifest's sha256, they cut session open from 1.7 s to 0.4 s
|
|
# against the public corpus (2026-11-12, one run per arm).
|
|
#
|
|
# The fixture is local, and a local corpus is never cached. These tests treat
|
|
# the fixture as if it were remote by handing .cache_metadata_tables() a
|
|
# fetch function that copies from it, so they run offline.
|
|
|
|
fake_remote <- "https://example.invalid/corpus/"
|
|
|
|
# A fetch that serves the fixture's bytes for the fake remote URL, and counts
|
|
# its calls.
|
|
counting_fetch <- function() {
|
|
env <- new.env(); env$n <- 0L
|
|
env$fetch <- function(src, dest) {
|
|
env$n <- env$n + 1L
|
|
rel <- sub(fake_remote, "", src, fixed = TRUE)
|
|
stopifnot(file.copy(file.path(fixture_corpus_path(), rel), dest,
|
|
overwrite = TRUE))
|
|
}
|
|
env
|
|
}
|
|
|
|
fixture_manifest <- function() {
|
|
jsonlite::fromJSON(file.path(fixture_corpus_path(), "manifest.json"),
|
|
simplifyVector = FALSE)
|
|
}
|
|
|
|
test_that("a local corpus is read in place, never cached (#64)", {
|
|
skip_if_no_corpus()
|
|
cd <- withr::local_tempdir()
|
|
url <- paste0(fixture_corpus_path(), "/")
|
|
expect_identical(.cache_metadata_tables(url, fixture_manifest(), cd), url)
|
|
expect_false(dir.exists(file.path(cd, "corpus")))
|
|
})
|
|
|
|
test_that("remote metadata tables are cached and verified (#64)", {
|
|
skip_if_no_corpus()
|
|
cd <- withr::local_tempdir()
|
|
m <- fixture_manifest()
|
|
f <- counting_fetch()
|
|
base <- .cache_metadata_tables(fake_remote, m, cd, fetch = f$fetch)
|
|
expect_false(identical(base, fake_remote))
|
|
expect_match(base, "/$")
|
|
expect_equal(f$n, length(m$files$metadata))
|
|
for (e in m$files$metadata) {
|
|
p <- file.path(cd, "corpus", e$path)
|
|
expect_true(file.exists(p), info = e$path)
|
|
expect_equal(digest::digest(p, algo = "sha256", file = TRUE), e$sha256)
|
|
}
|
|
|
|
# Second session: nothing fetched.
|
|
expect_identical(.cache_metadata_tables(fake_remote, m, cd, fetch = f$fetch),
|
|
base)
|
|
expect_equal(f$n, length(m$files$metadata))
|
|
})
|
|
|
|
test_that("a cached file that no longer matches the manifest is fetched again (#64)", {
|
|
skip_if_no_corpus()
|
|
cd <- withr::local_tempdir()
|
|
m <- fixture_manifest()
|
|
f <- counting_fetch()
|
|
.cache_metadata_tables(fake_remote, m, cd, fetch = f$fetch)
|
|
writeLines("stale", file.path(cd, "corpus", m$files$metadata[[1]]$path))
|
|
.cache_metadata_tables(fake_remote, m, cd, fetch = f$fetch)
|
|
expect_equal(f$n, length(m$files$metadata) + 1L)
|
|
p <- file.path(cd, "corpus", m$files$metadata[[1]]$path)
|
|
expect_equal(digest::digest(p, algo = "sha256", file = TRUE),
|
|
m$files$metadata[[1]]$sha256)
|
|
})
|
|
|
|
test_that("a download that fails its sha256 check falls back to remote (#64)", {
|
|
skip_if_no_corpus()
|
|
cd <- withr::local_tempdir()
|
|
m <- fixture_manifest()
|
|
bad_fetch <- function(src, dest) writeLines("not parquet", dest)
|
|
expect_message(
|
|
base <- .cache_metadata_tables(fake_remote, m, cd, fetch = bad_fetch),
|
|
"sha256"
|
|
)
|
|
expect_identical(base, fake_remote)
|
|
# Nothing unverified is left where a later session would find it.
|
|
left <- list.files(file.path(cd, "corpus"), recursive = TRUE)
|
|
expect_length(left, 0L)
|
|
})
|
|
|
|
test_that("a failed download falls back to remote (#64)", {
|
|
skip_if_no_corpus()
|
|
cd <- withr::local_tempdir()
|
|
err_fetch <- function(src, dest) stop("HTTP 429 Too Many Requests")
|
|
expect_message(
|
|
base <- .cache_metadata_tables(fake_remote, fixture_manifest(), cd,
|
|
fetch = err_fetch),
|
|
"429"
|
|
)
|
|
expect_identical(base, fake_remote)
|
|
})
|
|
|
|
test_that("a manifest entry without a sha256 disables the cache (#64)", {
|
|
skip_if_no_corpus()
|
|
cd <- withr::local_tempdir()
|
|
m <- fixture_manifest()
|
|
m$files$metadata[[1]]$sha256 <- NULL
|
|
f <- counting_fetch()
|
|
expect_identical(.cache_metadata_tables(fake_remote, m, cd, fetch = f$fetch),
|
|
fake_remote)
|
|
expect_equal(f$n, 0L)
|
|
})
|
|
|
|
test_that("views read metadata from the cache and partitions from the corpus (#64)", {
|
|
skip_if_no_corpus()
|
|
cd <- withr::local_tempdir()
|
|
m <- fixture_manifest()
|
|
meta <- .cache_metadata_tables(fake_remote, m, cd,
|
|
fetch = counting_fetch()$fetch)
|
|
url <- paste0(fixture_corpus_path(), "/")
|
|
con <- DBI::dbConnect(duckdb::duckdb())
|
|
on.exit(DBI::dbDisconnect(con, shutdown = TRUE), add = TRUE)
|
|
.register_views(con, url, m, meta_url = meta)
|
|
|
|
sql_of <- function(view) DBI::dbGetQuery(con, sprintf(
|
|
"SELECT sql FROM duckdb_views() WHERE view_name = '%s'", view))$sql
|
|
expect_match(sql_of("summary_categories"),
|
|
file.path(normalizePath(cd, winslash = "/"), "corpus"),
|
|
fixed = TRUE)
|
|
expect_match(sql_of("long"), fixture_corpus_path(), fixed = TRUE)
|
|
n <- DBI::dbGetQuery(con, "SELECT COUNT(*) AS n FROM spending_long")$n
|
|
expect_gt(n, 0)
|
|
})
|