Skip to content

Commit

Permalink
verify that field specs are all EpidataFieldInfo objs
Browse files Browse the repository at this point in the history
  • Loading branch information
nmdefries committed Nov 29, 2023
1 parent 4228e79 commit 8e4fb6c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
5 changes: 3 additions & 2 deletions R/epidatacall.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@
#' @return
#' - For `create_epidata_call`: an `epidata_call` object
#'
#' @importFrom purrr map_chr
#' @importFrom purrr map_chr map_lgl
create_epidata_call <- function(endpoint, params, meta = NULL,
only_supports_classic = FALSE) {
stopifnot(is.character(endpoint), length(endpoint) == 1)
stopifnot(is.list(params))
stopifnot(is.null(meta) || is.list(meta))
stopifnot(all(map_lgl(meta, ~ inherits(.x, "EpidataFieldInfo"))))
stopifnot(is.logical(only_supports_classic), length(only_supports_classic) == 1)

if (length(unique(meta)) != length(meta)) {
Expand All @@ -60,7 +61,7 @@ create_epidata_call <- function(endpoint, params, meta = NULL,
)
}

meta_field_names <- map_chr(meta, ~ .x$name)
meta_field_names <- map_chr(meta, "name")
if (length(meta_field_names) != length(unique(meta_field_names))) {
cli::cli_abort(
c(
Expand Down
2 changes: 1 addition & 1 deletion R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ parse_data_frame <- function(epidata_call, df, disable_date_parsing = FALSE) {
return(df)
}

meta_field_names <- map_chr(meta, ~ .x$name)
meta_field_names <- map_chr(meta, "name")
missing_fields <- setdiff(names(df), meta_field_names)
if (
length(missing_fields) != 0
Expand Down
26 changes: 20 additions & 6 deletions tests/testthat/test-epidatacall.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ test_that("create_epidata_call basic behavior", {
params <- list()

# Success
meta <- list(list(name = "time_value", class = "date"), list(name = "value", class = "double"))
meta <- list(
create_epidata_field_info("time_value", "date"),
create_epidata_field_info("value", "float")
)
expected <- list(
endpoint = endpoint,
params = params,
Expand All @@ -175,7 +178,6 @@ test_that("create_epidata_call basic behavior", {
only_supports_classic = FALSE
)
class(expected) <- "epidata_call"

expect_identical(create_epidata_call(endpoint, params, meta = meta), expected)

expected$meta <- list()
Expand All @@ -189,10 +191,22 @@ test_that("create_epidata_call fails when meta arg contains duplicates", {
params <- list()

# Duplicate names
meta <- list(list(name = "time_value", class = "date"), list(name = "time_value", class = "int"))
expect_error(create_epidata_call(endpoint, params, meta = meta), class = "epidatr__duplicate_meta_names")
meta <- list(
create_epidata_field_info("time_value", "date"),
create_epidata_field_info("time_value", "int")
)
expect_error(
create_epidata_call(endpoint, params, meta = meta),
class = "epidatr__duplicate_meta_names"
)

# Duplicate entries
meta <- list(list(name = "time_value", class = "date"), list(name = "time_value", class = "date"))
expect_error(create_epidata_call(endpoint, params, meta = meta), class = "epidatr__duplicate_meta_entries")
meta <- list(
create_epidata_field_info("time_value", "date"),
create_epidata_field_info("time_value", "date")
)
expect_error(
create_epidata_call(endpoint, params, meta = meta),
class = "epidatr__duplicate_meta_entries"
)
})

0 comments on commit 8e4fb6c

Please sign in to comment.