Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shackett committed Sep 25, 2024
1 parent 205ab5a commit ec18cd0
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 0 deletions.
3 changes: 3 additions & 0 deletions R/mutates.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ center <- function(x) {
#' mutate(new_sample_var = "foo") %>%
#' select(-DR)
#' new_variable_tables <- c("new_sample_var" = "samples")
#'
#' update_tidy_omic(tidy_omic, updated_tidy_data, new_variable_tables)
#'
#' @export
update_tidy_omic <- function(
tidy_omic,
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@
9 sample sample_primary_key measurements
10 expression numeric measurements

# Catch malformed design objects

Code
check_design(malformed_design)
Condition
Error in `check_design()`:
! The following unexpected attributes were found in the design: foo

---

Code
check_design(malformed_design)
Condition
Error in `check_design()`:
! The following attributes were missing in the design: feature_pk

20 changes: 20 additions & 0 deletions tests/testthat/_snaps/dim_reduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Sample mahalanobis distances are calculated

Code
pc_distances
Output
# A tibble: 36 x 4
sample nutrient DR pc_distance
<chr> <chr> <dbl> <dbl>
1 G0.05 G 0.05 188.
2 G0.1 G 0.1 125.
3 G0.15 G 0.15 128.
4 G0.2 G 0.2 125.
5 G0.25 G 0.25 83.4
6 G0.3 G 0.3 101.
7 N0.05 N 0.05 371.
8 N0.1 N 0.1 226.
9 N0.15 N 0.15 123.
10 N0.2 N 0.2 100.
# i 26 more rows

52 changes: 52 additions & 0 deletions tests/testthat/_snaps/mutates.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,55 @@
! bar is not present in measurements, valid value_vars include:
expression

# Factor levels can be updated using a list of factor orders

Code
brauer_w_mystery_nutrients <- update_sample_factors(brauer_2008_tidy_w_NAs,
list(nutrient = NUTRIENT_ORDER))
Message
! NA was present in the sample metadata's nutrient field but did not have a corresponding factor level in the `factor_levels` list. They will be added to the end of the specified factor levels
! The nutrient field in the sample metadata contains 2 NA values. These entries will be replaced with an "unspecified" level.

---

Code
reordered_tidy <- update_sample_factors(brauer_2008_tidy, list(nutrient = CONFUSED_NUTRIENT_ORDER))
Message
! "G" was present in the sample metadata's nutrient field but did not have a corresponding factor level in the `factor_levels` list. They will be added to the end of the specified factor levels
! "C" was present in `factor_levels` for nutrient but did not have a corresponding entry in the sample metadata.

---

Code
update_sample_factors(brauer_2008_tidy, list(nutrient = 1:5))
Condition
Error in `set_factor_levels()`:
! The factor levels for nutrient were "integer". This should be a character vector.

---

Code
update_sample_factors(brauer_2008_tidy, list(nutrient = c("G", "G", "N", "L")))
Condition
Error in `set_factor_levels()`:
! 1 factor levels was duplicated in the `factor_levels` specification for "nutrient": G

---

Code
update_sample_factors(brauer_2008_tidy, list(DR = seq(0.05, 0.3, by = 0.05)))
Condition
Error in `set_factor_levels()`:
! The factor levels for DR were "numeric". This should be a character vector.

# Update tidy omics with new added variables

Code
update_tidy_omic(tidy_omic, updated_tidy_data, c())
Condition
Error in `update_tidy_omic()`:
! updated_tidy_data contains 1
- new fields: new_sample_var.
- Add these to "new_variable_tables" so that romic know how to
- use them.

28 changes: 28 additions & 0 deletions tests/testthat/test-design.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,31 @@ test_that("extract design as a table", {
expect_invisible(check_design_in_tomic(brauer_2008_tidy))
})

test_that("get_design_tbl() works when directly passing a design instead of a tomic", {
expect_equal(
get_design_tbl(brauer_2008_tidy),
get_design_tbl(brauer_2008_tidy$design)
)
})

test_that("Catch malformed design objects", {

malformed_design <- brauer_2008_tidy$design
malformed_design$foo <- "bar"

expect_snapshot(
check_design(malformed_design),
error = TRUE
)


malformed_design <- brauer_2008_tidy$design
malformed_design$feature_pk <- NULL

expect_snapshot(
check_design(malformed_design),
error = TRUE
)

})

7 changes: 7 additions & 0 deletions tests/testthat/test-dim_reduction.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ test_that("Matrices keys are reconstructed with appropriate classes", {
}

})

test_that("Sample mahalanobis distances are calculated", {

pc_distances <- calculate_sample_mahalanobis_distances(brauer_2008_tidy)
expect_snapshot(pc_distances)

})
84 changes: 84 additions & 0 deletions tests/testthat/test-mutates.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,87 @@ test_that("Sort tomic applies a sort to features and/or samples", {
expect_equal("fully sorted")
})

test_that("Factor levels can be updated using a list of factor orders", {

NUTRIENT_ORDER <- c("G", "N", "P", "S", "L", "U")

reordered_tidy <- update_sample_factors(
brauer_2008_tidy,
list(nutrient = NUTRIENT_ORDER)
)

expect_equal(levels(reordered_tidy$data$nutrient), NUTRIENT_ORDER)

# test NA handling
brauer_samples <- get_tomic_table(brauer_2008_tidy, "samples")
brauer_samples$nutrient[1:2] <- NA

brauer_2008_tidy_w_NAs <- update_tomic(
brauer_2008_tidy,
brauer_samples
)

expect_snapshot(
brauer_w_mystery_nutrients <- update_sample_factors(
brauer_2008_tidy_w_NAs,
list(nutrient = NUTRIENT_ORDER)
)
)

expect_equal(
levels(brauer_w_mystery_nutrients$data$nutrient),
c(NUTRIENT_ORDER, "unspecified")
)

# W/ unexpected levels

CONFUSED_NUTRIENT_ORDER <- c("C", "N", "P", "S", "L", "U")

expect_snapshot(
reordered_tidy <- update_sample_factors(
brauer_2008_tidy,
list(nutrient = CONFUSED_NUTRIENT_ORDER)
))

expect_equal(levels(reordered_tidy$data$nutrient), c(CONFUSED_NUTRIENT_ORDER, "G"))

# invalid factor specifications

expect_snapshot(
update_sample_factors(brauer_2008_tidy, list(nutrient = 1:5)),
error = TRUE
)

expect_snapshot(
update_sample_factors(brauer_2008_tidy, list(nutrient = c("G", "G", "N", "L"))),
error = TRUE
)

expect_snapshot(
update_sample_factors(brauer_2008_tidy, list(DR = seq(0.05, 0.3, by = 0.05))),
error = TRUE
)

})

test_that("Update tidy omics with new added variables", {

tidy_omic <- brauer_2008_tidy
updated_tidy_data <- tidy_omic$data %>%
dplyr::mutate(new_sample_var = "foo") %>%
dplyr::select(-DR)

new_variable_tables <- c("new_sample_var" = "samples")

tidy_w_updated_samples <- update_tidy_omic(tidy_omic, updated_tidy_data, new_variable_tables)
expect_equal(
tidy_w_updated_samples$design$samples$variable,
c('sample', "nutrient", "new_sample_var")
)

expect_snapshot(
update_tidy_omic(tidy_omic, updated_tidy_data, c()),
error = TRUE
)

})

0 comments on commit ec18cd0

Please sign in to comment.