Skip to content

Commit

Permalink
allow empty glossary
Browse files Browse the repository at this point in the history
ref #452
  • Loading branch information
wibeasley committed Oct 23, 2022
1 parent 0b098f4 commit 52109c2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
19 changes: 17 additions & 2 deletions R/create-batch-glossary.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
#' @examples
#' REDCapR::create_batch_glossary(100, 50)
#' REDCapR::create_batch_glossary(100, 25)
#' REDCapR::create_batch_glossary(100, 3)
#' REDCapR::create_batch_glossary(100, 3)
#' REDCapR::create_batch_glossary( 0, 3)
#' d <- data.frame(
#' record_id = 1:100,
#' iv = sample(x=4, size=100, replace=TRUE),
Expand All @@ -51,9 +52,23 @@

#' @export
create_batch_glossary <- function(row_count, batch_size) {
checkmate::assert_integerish(row_count , any.missing=FALSE, len=1L, lower=1L)
checkmate::assert_integerish(row_count , any.missing=FALSE, len=1L, lower=0L)
checkmate::assert_integerish(batch_size, any.missing=FALSE, len=1L, lower=1L)

if (0L == row_count) {
return(
tibble::tibble(
id = integer(0),
start_index = integer(0),
stop_index = integer(0),
index_pretty = character(0),
start_index_pretty = character(0),
stop_index_pretty = character(0),
label = character(0),
)
)
}

start_index <- base::seq.int(from=1, to=row_count, by=batch_size)

ds_batch <-
Expand Down
31 changes: 24 additions & 7 deletions tests/testthat/test-create-batch-glossary.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,30 @@ test_that("N50B10", {

ds_result <- REDCapR::create_batch_glossary(row_count=row_count, batch_size=batch_size) # dput(ds_result)
ds_expected <- structure(list(id = 1:5, start_index = c(1, 11, 21, 31, 41),
stop_index = c(10, 20, 30, 40, 50), index_pretty = c("1",
"2", "3", "4", "5"), start_index_pretty = c("01", "11", "21",
"31", "41"), stop_index_pretty = c("10", "20", "30", "40",
"50"), label = c("1_01_10", "2_11_20", "3_21_30", "4_31_40",
"5_41_50")), .Names = c("id", "start_index", "stop_index",
"index_pretty", "start_index_pretty", "stop_index_pretty", "label"
), row.names = c(NA, -5L), class = "data.frame")
stop_index = c(10, 20, 30, 40, 50), index_pretty = c("1",
"2", "3", "4", "5"), start_index_pretty = c("01", "11", "21",
"31", "41"), stop_index_pretty = c("10", "20", "30", "40",
"50"), label = c("1_01_10", "2_11_20", "3_21_30", "4_31_40",
"5_41_50")), .Names = c("id", "start_index", "stop_index",
"index_pretty", "start_index_pretty", "stop_index_pretty", "label"
), row.names = c(NA, -5L), class = "data.frame")
expect_equal(object=nrow(ds_result), expected=expected_glossary_count, info="The number of batches should be correct.")
expect_equal(object=colnames(ds_result), expected=expected_column_names, info="The column names should be correct.")
expect_equal(object=ds_result, expected=ds_expected, info="The returned batch glossary should be correct.")
})

test_that("N0B10", {
row_count <- 0
batch_size <- 10
expected_glossary_count <- 0

ds_result <- REDCapR::create_batch_glossary(row_count=row_count, batch_size=batch_size) # dput(ds_result)
ds_expected <-
structure(list(id = integer(0), start_index = integer(0), stop_index = integer(0),
index_pretty = character(0), start_index_pretty = character(0),
stop_index_pretty = character(0), label = character(0)), class = c("tbl_df",
"tbl", "data.frame"), row.names = integer(0))

expect_equal(object=nrow(ds_result), expected=expected_glossary_count, info="The number of batches should be correct.")
expect_equal(object=colnames(ds_result), expected=expected_column_names, info="The column names should be correct.")
expect_equal(object=ds_result, expected=ds_expected, info="The returned batch glossary should be correct.")
Expand Down

0 comments on commit 52109c2

Please sign in to comment.