diff --git a/R/qp_dilute.R b/R/qp_dilute.R index 0d827c7..7daec27 100644 --- a/R/qp_dilute.R +++ b/R/qp_dilute.R @@ -29,12 +29,12 @@ qp_dilute.data.frame <- function(x, target_conc = NULL, target_vol = 15, if (remove_standards) x <- x |> dplyr::filter(.data$sample_type != "standard") - if (!length_is_recyclable(target_conc, x)) { + if (!length_is_recyclable(length(target_conc), x)) { rlang::abort("`target_conc` length is not 1 or nrow(x)") } conc_col_name <- ifelse( - ".mean_pred_conc" %in% colnames(x), ".mean_pred_conc", ".pred_conc" + has_cols(x, ".mean_pred_conc"), ".mean_pred_conc", ".pred_conc" ) if (is.null(target_conc)) { @@ -64,5 +64,5 @@ qp_dilute.list <- function(x, target_conc = NULL, target_vol = 15, } length_is_recyclable <- function(n, x) { - is.null(n) || n == 1 || n == nrow(x) + n == 0 || n == 1 || n == nrow(x) } diff --git a/tests/testthat/test-qp_dilute.R b/tests/testthat/test-qp_dilute.R new file mode 100644 index 0000000..e8c7269 --- /dev/null +++ b/tests/testthat/test-qp_dilute.R @@ -0,0 +1,38 @@ +test_that("multiplication works", { + expect_equal(2 * 2, 4) +}) + +test_that("length_is_recyclable works", { + expect_true(length_is_recyclable(1, iris[1, ])) + expect_true(length_is_recyclable(1, iris[1:2, ])) + expect_true(length_is_recyclable(2, iris[1:2, ])) + expect_true(length_is_recyclable(0, iris[1:2, ])) + expect_false(length_is_recyclable(2, iris)) +}) + +test_that("dilution prefers .mean_pred_conc", { + data <- data.frame( + .pred_conc = c(1, 2), + .mean_pred_conc = c(5, 10) + ) + out <- qp_dilute(data, target_conc = 5, target_vol = 2) + expect_equal(out$sample_to_add, c(2, 1)) + expect_equal(out$add_to, c(0, 1)) +}) + +test_that("dilution can use .pred_conc", { + data <- data.frame(.pred_conc = c(1, 2)) + out <- qp_dilute(data, target_conc = 1, target_vol = 2) + expect_equal(out$sample_to_add, c(2, 1)) + expect_equal(out$add_to, c(0, 1)) +}) + +test_that("NULL target_conc uses lowest concentration", { + data <- data.frame( + sample_type = c("standard", "standard", "unknown", "unknown"), + .pred_conc = 1:4 + ) + out <- qp_dilute(data, target_vol = 2) + expect_equal(out$sample_to_add, c(6, 3, 2, 1.5)) + expect_equal(out$add_to, c(-4, -1, 0, 0.5)) +})