Skip to content

Commit

Permalink
test: add dilute tests
Browse files Browse the repository at this point in the history
Also fixes:
- checking the length of an argument in `length_is_recyclable` instead
of checking the argument itself
  • Loading branch information
KaiAragaki committed Nov 5, 2023
1 parent 1af6e0f commit 4738078
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions R/qp_dilute.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)
}
38 changes: 38 additions & 0 deletions tests/testthat/test-qp_dilute.R
Original file line number Diff line number Diff line change
@@ -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))
})

0 comments on commit 4738078

Please sign in to comment.