Skip to content

Commit

Permalink
test: add unit tests - compute_*()
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Feb 23, 2024
1 parent a9679ca commit d4dcc00
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/testthat/test-compute_abundances.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Data for test ----

df <- vroom::vroom(system.file(file.path("extdata", "FORCIS_pump_sample.csv"),
package = "forcis"),
delim = ";", altrep = FALSE, show_col_types = FALSE)

df <- add_data_type(df, "Pump")
df <- select_taxonomy(df, "VT")

df2 <- df
df2 <- add_data_type(df2, "CPR North")


## compute_abundances() ----

test_that("Test compute_abundances() for error", {

expect_error(compute_abundances(df2),
paste0("This function is not designed to work with 'CPR North' ",
"or 'Sediment trap' data"),
fixed = TRUE)
})

test_that("Test compute_abundances() for success", {

expect_message(res <- compute_abundances(df))

expect_true(is.data.frame(res))
expect_equal(ncol(res), 69L)
expect_equal(nrow(res), 3024L)

expect_message(res <- compute_abundances(df, aggregate = FALSE))

expect_true(is.data.frame(res))
expect_equal(ncol(res), 72L)
expect_equal(nrow(res), 3024L)
})
49 changes: 49 additions & 0 deletions tests/testthat/test-compute_concentrations.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## Data for test ----

df <- vroom::vroom(system.file(file.path("extdata", "FORCIS_pump_sample.csv"),
package = "forcis"),
delim = ";", altrep = FALSE, show_col_types = FALSE)

df <- add_data_type(df, "Pump")
df <- select_taxonomy(df, "VT")

df2 <- df
df2 <- add_data_type(df2, "Sediment trap")

df3 <- vroom::vroom(system.file(file.path("extdata", "FORCIS_cpr_north_sample.csv"),
package = "forcis"),
delim = ";", altrep = FALSE, show_col_types = FALSE)

df3 <- add_data_type(df3, "CPR North")


## compute_concentrations() ----

test_that("Test compute_concentrations() for error", {

expect_error(compute_concentrations(df2),
paste0("This function is not designed to work with ",
"'Sediment trap' data"),
fixed = TRUE)
})

test_that("Test compute_concentrations() for success", {

expect_message(res <- compute_concentrations(df))

expect_true(is.data.frame(res))
expect_equal(ncol(res), 69L)
expect_equal(nrow(res), 3024L)

expect_message(res <- compute_concentrations(df, aggregate = FALSE))

expect_true(is.data.frame(res))
expect_equal(ncol(res), 72L)
expect_equal(nrow(res), 7280L)

res <- compute_concentrations(df3)

expect_true(is.data.frame(res))
expect_equal(ncol(res), 78L)
expect_equal(nrow(res), 440L)
})
37 changes: 37 additions & 0 deletions tests/testthat/test-compute_frequencies.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Data for test ----

df <- vroom::vroom(system.file(file.path("extdata", "FORCIS_pump_sample.csv"),
package = "forcis"),
delim = ";", altrep = FALSE, show_col_types = FALSE)

df <- add_data_type(df, "Pump")
df <- select_taxonomy(df, "VT")

df2 <- df
df2 <- add_data_type(df2, "CPR North")


## compute_frequencies() ----

test_that("Test compute_frequencies() for error", {

expect_error(compute_frequencies(df2),
paste0("This function is not designed to work with 'CPR North' ",
"or 'Sediment trap' data"),
fixed = TRUE)
})

test_that("Test compute_frequencies() for success", {

expect_message(res <- compute_frequencies(df))

expect_true(is.data.frame(res))
expect_equal(ncol(res), 68L)
expect_equal(nrow(res), 6832L)

expect_message(res <- compute_frequencies(df, aggregate = FALSE))

expect_true(is.data.frame(res))
expect_equal(ncol(res), 71L)
expect_equal(nrow(res), 6832L)
})

0 comments on commit d4dcc00

Please sign in to comment.