Skip to content

Commit

Permalink
Add clio new fields annotation check
Browse files Browse the repository at this point in the history
* allow_new_fields defaults to FALSE
* if new fields are spotted then checks to see if they already exist
* includes test
  • Loading branch information
jefferis committed Dec 22, 2024
1 parent 03e3328 commit f523ccd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
14 changes: 13 additions & 1 deletion R/annotate.R
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,10 @@ parse_query <- function(query, version) {
#' \bold{NB only applies when \code{x} is a data.frame}.
#' @param query Special query to pass to Clio API. If TRUE, then the default is
#' passed with Clio version, FALSE means no query, and list is allowed for a
#' customized behavior.
#' customized behaviour.
#' @param allow_new_fields Whether to allow creation of new clio fields. Default
#' \code{FALSE} will produce an error encouraging you to check the field
#' names.
#' @param ... Additional parameters passed to \code{pbapply::\link{pbsapply}}
#'
#' @return \code{NULL} invisibly on success. Errors out on failure.
Expand Down Expand Up @@ -283,6 +286,7 @@ parse_query <- function(query, version) {
#' }
manc_annotate_body <- function(x, test=TRUE, version=NULL,
write_empty_fields=FALSE,
allow_new_fields=FALSE,
designated_user=NULL,
protect=c("user"), chunksize=50, query=TRUE, ...) {
query=parse_query(query, version=version)
Expand All @@ -295,6 +299,14 @@ manc_annotate_body <- function(x, test=TRUE, version=NULL,
fafbseg:::check_package_available('purrr')
if(!is.character(x)) {
if(is.data.frame(x)) {
cf=clio_fields()
new_fields=setdiff(colnames(x), cf)
if(isFALSE(allow_new_fields)) {
if(length(new_fields)>0)
stop("If you are sure you want to add new fields: \n ",
paste(new_fields, collapse = ', '),
"\nafter appropriate discussion then please set `allow_new_fields=TRUE`")
}
x <- clioannotationdf2list(x, write_empty_fields = write_empty_fields)
if (length(x) > 0)
x <- compute_clio_delta(x, test = test, write_empty_fields = write_empty_fields)
Expand Down
7 changes: 6 additions & 1 deletion man/manc_annotate_body.Rd

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

5 changes: 5 additions & 0 deletions tests/testthat/test-annotations.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ test_that("parse_query", {
expect_equal(res$version, "v9.9.9")
expect_true("abc" %in% names(res))
})

test_that("new field check", {
df=data.frame(bodyid=10002, new_field='rhubarb', new_field2='crumble')
expect_error(manc_annotate_body(df, allow_new_fields = FALSE))
})

0 comments on commit f523ccd

Please sign in to comment.