Skip to content

Commit

Permalink
test: unit tests for read_*_data()
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Feb 15, 2024
1 parent 16d8949 commit 8e69090
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/testthat/test-read_cpr_north_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ test_that("Test read_cpr_north_data() for success", {

expect_true("data_type" %in% colnames(x))
expect_true("CPR North" %in% x$"data_type")

expect_message({ x <- read_cpr_north_data(version = "07") },
paste0("A newer version of the FORCIS database is available. ",
"Use 'download_forcis_db(version = NULL)' to download ",
"it."),
fixed = TRUE)
})
52 changes: 52 additions & 0 deletions tests/testthat/test-read_cpr_south_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Data for tests ----

df <- data.frame("crassula" = rep(1, 2),
"dehiscens" = rep(1, 2))


## read_cpr_south_data() ----

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

create_tempdir()

expect_error(read_cpr_south_data(version = "07"),
paste0("The directory './forcis-db/version-07' does not exist. ",
"Please check the argument 'path' or use the function ",
"'download_forcis_db()'."),
fixed = TRUE)

dir.create(file.path("forcis-db", "version-07"), recursive = TRUE)

expect_error(read_cpr_south_data(version = "07"),
paste0("The South CPR dataset does not exist. Please use the ",
"function 'download_forcis_db()'."),
fixed = TRUE)
})

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

create_tempdir()

dir.create(file.path("forcis-db", "version-07"), recursive = TRUE)

write.csv2(df,
file = file.path("forcis-db", "version-07",
"FORCIS_cpr_south_test.csv"),
row.names = FALSE)

x <- read_cpr_south_data(version = "07", check_for_update = FALSE)

expect_equal(class(x), "data.frame")
expect_equal(ncol(x), 3L)
expect_equal(nrow(x), 2L)

expect_true("data_type" %in% colnames(x))
expect_true("CPR South" %in% x$"data_type")

expect_message({ x <- read_cpr_south_data(version = "07") },
paste0("A newer version of the FORCIS database is available. ",
"Use 'download_forcis_db(version = NULL)' to download ",
"it."),
fixed = TRUE)
})
52 changes: 52 additions & 0 deletions tests/testthat/test-read_plankton_nets_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Data for tests ----

df <- data.frame("crassula" = rep(1, 2),
"dehiscens" = rep(1, 2))


## read_plankton_nets_data() ----

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

create_tempdir()

expect_error(read_plankton_nets_data(version = "07"),
paste0("The directory './forcis-db/version-07' does not exist. ",
"Please check the argument 'path' or use the function ",
"'download_forcis_db()'."),
fixed = TRUE)

dir.create(file.path("forcis-db", "version-07"), recursive = TRUE)

expect_error(read_plankton_nets_data(version = "07"),
paste0("The Plankton net dataset does not exist. Please use the ",
"function 'download_forcis_db()'."),
fixed = TRUE)
})

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

create_tempdir()

dir.create(file.path("forcis-db", "version-07"), recursive = TRUE)

write.csv2(df,
file = file.path("forcis-db", "version-07",
"FORCIS_net_test.csv"),
row.names = FALSE)

x <- read_plankton_nets_data(version = "07", check_for_update = FALSE)

expect_equal(class(x), "data.frame")
expect_equal(ncol(x), 3L)
expect_equal(nrow(x), 2L)

expect_true("data_type" %in% colnames(x))
expect_true("Net" %in% x$"data_type")

expect_message({ x <- read_plankton_nets_data(version = "07") },
paste0("A newer version of the FORCIS database is available. ",
"Use 'download_forcis_db(version = NULL)' to download ",
"it."),
fixed = TRUE)
})
52 changes: 52 additions & 0 deletions tests/testthat/test-read_pump_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Data for tests ----

df <- data.frame("crassula" = rep(1, 2),
"dehiscens" = rep(1, 2))


## read_pump_data() ----

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

create_tempdir()

expect_error(read_pump_data(version = "07"),
paste0("The directory './forcis-db/version-07' does not exist. ",
"Please check the argument 'path' or use the function ",
"'download_forcis_db()'."),
fixed = TRUE)

dir.create(file.path("forcis-db", "version-07"), recursive = TRUE)

expect_error(read_pump_data(version = "07"),
paste0("The Pump dataset does not exist. Please use the ",
"function 'download_forcis_db()'."),
fixed = TRUE)
})

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

create_tempdir()

dir.create(file.path("forcis-db", "version-07"), recursive = TRUE)

write.csv2(df,
file = file.path("forcis-db", "version-07",
"FORCIS_pump_test.csv"),
row.names = FALSE)

x <- read_pump_data(version = "07", check_for_update = FALSE)

expect_equal(class(x), "data.frame")
expect_equal(ncol(x), 3L)
expect_equal(nrow(x), 2L)

expect_true("data_type" %in% colnames(x))
expect_true("Pump" %in% x$"data_type")

expect_message({ x <- read_pump_data(version = "07") },
paste0("A newer version of the FORCIS database is available. ",
"Use 'download_forcis_db(version = NULL)' to download ",
"it."),
fixed = TRUE)
})
52 changes: 52 additions & 0 deletions tests/testthat/test-read_sediment_trap_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Data for tests ----

df <- data.frame("crassula" = rep(1, 2),
"dehiscens" = rep(1, 2))


## read_sediment_trap() ----

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

create_tempdir()

expect_error(read_sediment_trap_data(version = "07"),
paste0("The directory './forcis-db/version-07' does not exist. ",
"Please check the argument 'path' or use the function ",
"'download_forcis_db()'."),
fixed = TRUE)

dir.create(file.path("forcis-db", "version-07"), recursive = TRUE)

expect_error(read_sediment_trap_data(version = "07"),
paste0("The Sediment trap dataset does not exist. Please use the ",
"function 'download_forcis_db()'."),
fixed = TRUE)
})

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

create_tempdir()

dir.create(file.path("forcis-db", "version-07"), recursive = TRUE)

write.csv2(df,
file = file.path("forcis-db", "version-07",
"FORCIS_trap_test.csv"),
row.names = FALSE)

x <- read_sediment_trap_data(version = "07", check_for_update = FALSE)

expect_equal(class(x), "data.frame")
expect_equal(ncol(x), 3L)
expect_equal(nrow(x), 2L)

expect_true("data_type" %in% colnames(x))
expect_true("Sediment trap" %in% x$"data_type")

expect_message({ x <- read_sediment_trap_data(version = "07") },
paste0("A newer version of the FORCIS database is available. ",
"Use 'download_forcis_db(version = NULL)' to download ",
"it."),
fixed = TRUE)
})

0 comments on commit 8e69090

Please sign in to comment.