Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThierryO committed Sep 8, 2023
1 parent d5f516f commit ce2fcfa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions R/aggregate_impute.R
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ setMethod(
Covariate = data |>
select(!!!grouping) |>
as.data.frame(),
Imputation = ncol(imputation) |>
seq_len() |>
sprintf(fmt = "Imputation%04i") |>
Imputation = imputation |>
select(starts_with("Imputation")) |>
colnames() |>
list() |>
c(list(character(0))) |>
rev() |>
matrix(
data = NA_real_, nrow = 0, ncol = ncol(imputation),
data = NA_real_, nrow = 0, ncol = ncol(imputation) - 1,
byrow = FALSE
)
)
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test_bbb_aggregate_impute.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ test_that("aggregate_impute", {
),
"aggregatedImputed"
)

# handles empty datasets
empty_imputed <- impute(data = dataset[integer(0), ], model = model)
empty_aggr <- aggregate_impute(empty_imputed, grouping = grouping, fun = fun)
expect_is(empty_aggr, "aggregatedImputed")
expect_identical(colnames(empty_aggr@Covariate), grouping)
expect_identical(nrow(empty_aggr@Covariate), nrow(empty_aggr@Imputation))
expect_identical(ncol(empty_imputed@Imputation), ncol(empty_aggr@Imputation))
})

test_that("aggregate_impute() works on aggregatedImputed objects", {
Expand All @@ -112,4 +120,13 @@ test_that("aggregate_impute() works on aggregatedImputed objects", {
aggr2 <- aggregate_impute(aggr, grouping = grouping2, fun = sum),
"aggregatedImputed"
)

# handles empty datasets
empty_imputed <- impute(data = dataset[integer(0), ], model = model)
empty_aggr <- aggregate_impute(empty_imputed, grouping = grouping, fun = fun)
empty_aggr2 <- aggregate_impute(empty_aggr, grouping = grouping2, fun = fun)
expect_is(empty_aggr2, "aggregatedImputed")
expect_identical(colnames(empty_aggr2@Covariate), grouping2)
expect_identical(nrow(empty_aggr2@Covariate), nrow(empty_aggr2@Imputation))
expect_identical(ncol(empty_aggr@Imputation), ncol(empty_aggr2@Imputation))
})

0 comments on commit ce2fcfa

Please sign in to comment.