-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve check + add unit tests in filter_by_species()
- Loading branch information
Showing
2 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
## Data for tests ---- | ||
|
||
df_cpr <- data.frame("data_type" = rep("CPR North", 5), | ||
"species" = c("n_pachyderma", "n_pachyderma", | ||
"conglobatus", "g_rubescens", | ||
"g_rubescens")) | ||
|
||
df_net <- data.frame("data_type" = rep("Net", 5), | ||
"t_parkerae_VT" = c(1:5), | ||
"conglobatus" = c(1:5), | ||
"g_rubescens" = c(1:5)) | ||
|
||
df <- data.frame("data_type" = rep("Net", 5), | ||
"taxa" = c("n_pachyderma", "n_pachyderma", | ||
"conglobatus", "g_rubescens", | ||
"g_rubescens"), | ||
"counts" = c(1:4, NA)) | ||
|
||
df2 <- data.frame("data_type" = rep("Net", 5), | ||
"taxa" = c("n_pachyderma", "n_pachyderma", | ||
"conglobatus", "g_rubescens", | ||
"g_rubescens"), | ||
"counts" = c(1:3, NA, NA)) | ||
|
||
|
||
## filter_by_species() ---- | ||
|
||
test_that("Test filter_by_species() for error", { | ||
|
||
expect_error(filter_by_species(df_cpr), | ||
"This function is not designed to work with 'CPR North' data", | ||
fixed = TRUE) | ||
|
||
expect_error(filter_by_species(df_net), | ||
paste0("This function requires data in long format. Please use ", | ||
"the function 'reshape_data()'"), | ||
fixed = TRUE) | ||
|
||
expect_error(filter_by_species(df), | ||
"Argument 'species' is required", | ||
fixed = TRUE) | ||
|
||
expect_error(filter_by_species(df, species = 1), | ||
"Argument 'species' must be a character of length >= 1", | ||
fixed = TRUE) | ||
|
||
expect_error(filter_by_species(df, species = "toto"), | ||
"The species provided are absent from 'data'", | ||
fixed = TRUE) | ||
|
||
expect_error(filter_by_species(df, species = c("toto", "titi")), | ||
"The species provided are absent from 'data'", | ||
fixed = TRUE) | ||
}) | ||
|
||
test_that("Test filter_by_species() for success", { | ||
|
||
expect_silent(res <- filter_by_species(df, species = c("conglobatus"))) | ||
|
||
expect_true(is.data.frame(res)) | ||
expect_equal(ncol(res), ncol(df)) | ||
expect_equal(nrow(res), 1L) | ||
|
||
expect_silent(res <- filter_by_species(df, species = c("conglobatus", | ||
"g_rubescens"))) | ||
|
||
expect_true(is.data.frame(res)) | ||
expect_equal(ncol(res), ncol(df)) | ||
expect_equal(nrow(res), 3L) | ||
|
||
expect_silent(res <- filter_by_species(df, species = c("conglobatus", | ||
"g_rubescens"), | ||
rm_na = TRUE)) | ||
|
||
expect_true(is.data.frame(res)) | ||
expect_equal(ncol(res), ncol(df)) | ||
expect_equal(nrow(res), 2L) | ||
|
||
expect_silent(res <- filter_by_species(df2, species = "g_rubescens", | ||
rm_na = TRUE)) | ||
|
||
expect_true(is.data.frame(res)) | ||
expect_equal(ncol(res), ncol(df)) | ||
expect_equal(nrow(res), 0L) | ||
|
||
expect_silent(res <- filter_by_species(df, species = c("conglobatus", | ||
"g_rubescens", | ||
"titi"))) | ||
|
||
expect_true(is.data.frame(res)) | ||
expect_equal(ncol(res), ncol(df)) | ||
expect_equal(nrow(res), 3L) | ||
}) |