Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Ingest return value #1923

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions R/util-ApplySpec.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ ApplySpec <- function(dfSource, columnSpecs, domain) {
}

# write a query to select the columns from the source
columnMapping <- columnSpecs %>% imap(
function(spec, name) {
mapping <- list(target = name)
mapping$source <- spec$source_col %||% name
mapping$type <- spec$type %||% NULL
return(mapping)
}
)
columnMapping <- columnSpecs %>%
imap(
function(spec, name) {
mapping <- list(target = name)
mapping$source <- spec$source_col %||% name
mapping$type <- spec$type %||% NULL
mapping$required <- spec$required %||% FALSE
return(mapping)
}
) %>%
# Drop non-required columns that aren't in dfSource.
purrr::keep(~.x$required || .x$source %in% colnames(dfSource))

# check that the source columns exists in the source data
# check that the required columns exists in the source data
sourceCols <- columnMapping %>% map("source")
if (!all(sourceCols %in% names(dfSource))) {
missingCols <- sourceCols[!sourceCols %in% names(dfSource)]
Expand Down
4 changes: 2 additions & 2 deletions R/util-CombineSpecs.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ combine_domain <- function(domain_specs) {
#' @return A list containing the updated column specification.
#' @keywords internal
update_column <- function(existing_col, new_col, col_name) {
if (!is.null(existing_col)) {
if (length(existing_col)) {
# Handle required conflict
existing_col$required <- existing_col$required || new_col$required
existing_col$required <- isTRUE(existing_col$required) || isTRUE(new_col$required)

# Handle type conflict with a warning when available
if (!is.null(existing_col$type) && !is.null(new_col$type)) {
Expand Down
2 changes: 1 addition & 1 deletion R/util-Ingest.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' @param lSpec `list` A named list of column specifications.
#' @param strDomain `character` Domain name to add to the data frames after ingestions. Default: "Raw"
#'
#' @return `list` A named list of data frames, where each data frame corresponds to a domain in the
#' @return `list` A named list of data frames, where each data frame corresponds to a domain in the
#' specification.
#'
#' @examples
Expand Down
9 changes: 5 additions & 4 deletions man/Ingest.Rd

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

105 changes: 105 additions & 0 deletions tests/testthat/test-util-CombineSpecs.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,108 @@ test_that("warning if type doesn't match first instance", {

expect_equal(combined, expected)
})

test_that("CombineSpecs works with optoinal columns", {
spec1 <- list(
df1 = list(
col1 = list(required = TRUE)
)
)
spec2 <- list(
df1 = list(
col2 = list(required = TRUE)
)
)
expected <- list(
df1 = list(
col1 = list(required = TRUE),
col2 = list(required = TRUE)
)
)
expect_no_error({
test_result <- CombineSpecs(list(spec1, spec2), bIsWorkflow = FALSE)
})
expect_identical(test_result, expected)

spec1 <- list(
df1 = list(
col1 = list(required = FALSE)
)
)
spec2 <- list(
df1 = list(
col2 = list(required = TRUE)
)
)
expected <- list(
df1 = list(
col1 = list(required = FALSE),
col2 = list(required = TRUE)
)
)
expect_no_error({
test_result <- CombineSpecs(list(spec1, spec2), bIsWorkflow = FALSE)
})
expect_identical(test_result, expected)

spec1 <- list(
df1 = list(
col1 = list(required = TRUE)
)
)
spec2 <- list(
df1 = list(
col2 = list(required = FALSE)
)
)
expected <- list(
df1 = list(
col1 = list(required = TRUE),
col2 = list(required = FALSE)
)
)
expect_no_error({
test_result <- CombineSpecs(list(spec1, spec2), bIsWorkflow = FALSE)
})
expect_identical(test_result, expected)

spec1 <- list(
df1 = list(
col1 = list(required = FALSE)
)
)
spec2 <- list(
df1 = list(
col1 = list(required = TRUE)
)
)
expected <- list(
df1 = list(
col1 = list(required = TRUE)
)
)
expect_no_error({
test_result <- CombineSpecs(list(spec1, spec2), bIsWorkflow = FALSE)
})
expect_identical(test_result, expected)

spec1 <- list(
df1 = list(
col1 = list(required = TRUE)
)
)
spec2 <- list(
df1 = list(
col1 = list(required = FALSE)
)
)
expected <- list(
df1 = list(
col1 = list(required = TRUE)
)
)
expect_no_error({
test_result <- CombineSpecs(list(spec1, spec2), bIsWorkflow = FALSE)
})
expect_identical(test_result, expected)
})
22 changes: 22 additions & 0 deletions tests/testthat/test-util-Ingest.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
test_that("Ingest works with optional columns", {
lSourceData <- list(
df1 = data.frame(
a = 1:10,
b = letters[1:10]
)
)
lSpec <- list(
df1 = list(
a = list(required = TRUE, type = "integer"),
b = list(required = TRUE, type = "character"),
c = list(required = FALSE, type = "character")
)
)
expect_no_error(expect_message(expect_message({
test_result <- Ingest(lSourceData, lSpec)
})))
expected_result <- list(
Raw_df1 = lSourceData$df1
)
expect_identical(test_result, expected_result)
})
Loading