Skip to content

Commit

Permalink
Merge pull request #30 from mrc-ide/mrc-4384
Browse files Browse the repository at this point in the history
New function for reading metadata
  • Loading branch information
r-ash authored Jul 12, 2023
2 parents 6b84e73 + c48e0e5 commit fa403cc
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export(outpack_log)
export(outpack_log_debug)
export(outpack_log_info)
export(outpack_log_trace)
export(outpack_metadata)
export(outpack_metadata_read)
export(outpack_packet_add_custom)
export(outpack_packet_cancel)
Expand Down
24 changes: 24 additions & 0 deletions R/outpack_metadata.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
##' Read metadata for a particular id. You may want to use
##' [orderly2::outpack_search] to find an id corresponding to a
##' particular query.
##'
##' @title Read outpack metadata
##'
##' @param id The id to fetch metadata for. An error will be thrown if
##' this id is not known
##'
##' @param root The outpack root, typically the path to the root. The
##' default value of `NULL` looks for the root from the current
##' directory which is typically correct.
##'
##' @return A list of metadata. See the outpack schema for details
##' (https://github.com/mrc-ide/outpack)
##'
##' @export
outpack_metadata <- function(id, root = NULL) {
validate_outpack_id(id)
root <- outpack_root_open(root, locate = TRUE)
root$metadata(id, full = TRUE)
}


##' Low-level function for reading metadata and deserialising it. This
##' function can be used to directly read a metadata json file without
##' reference to a root which contains it. It may be useful in the
Expand Down
25 changes: 25 additions & 0 deletions man/outpack_metadata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion tests/testthat/test-outpack-metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ test_that("Validate hashes", {
test_that("reading metadata via top-level function is same as from root", {
root <- create_temporary_root(use_file_store = TRUE)
id <- create_random_packet(root)
meta <- root$metadata(id, TRUE)
expect_identical(
outpack_metadata_read(file.path(root$path, ".outpack", "metadata", id)),
root$metadata(id, TRUE))
meta)
expect_identical(outpack_metadata(id, root), meta)
})


Expand All @@ -71,3 +73,20 @@ test_that("Sensible error if metadata file not found", {
outpack_metadata_read(tempfile()),
"File does not exist: ")
})


test_that("Sensible error if metadata file not found", {
root <- create_temporary_root(use_file_store = TRUE)
expect_error(
outpack_metadata(1, root),
"'id' must be character")
expect_error(
outpack_metadata(letters, root),
"'id' must be a scalar")
expect_error(
outpack_metadata("some-id", root),
"Malformed id 'some-id'")
expect_error(
outpack_metadata(outpack_id(), root),
"id '.+' not found in index")
})

0 comments on commit fa403cc

Please sign in to comment.