Skip to content

Commit

Permalink
test: implement tests for check functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed May 2, 2024
1 parent e53277b commit 7a74761
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/testthat/test-error_if_field_not_in_df.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## Test error_if_field_not_in_df() ----

### Data for tests ----

test_error_if_field_not_in_df <- function(data, column) {
error_if_field_not_in_df(data, column)
invisible(NULL)
}

good_data <- sf::st_read(system.file(file.path("extdata",
"marine_fish_richness.gpkg"),
package = "rphenofish"), quiet = TRUE)
bad_col <- "missing_column"
good_col <- "ECOREGION"


### Tests for errors ----

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

expect_error(error_if_field_not_in_df(good_data, bad_col),
"The column listed in 'bad_col' is absent from 'good_data'",
fixed = TRUE)

expect_error(test_error_if_field_not_in_df(good_data, bad_col),
"The column listed in 'column' is absent from 'data'",
fixed = TRUE)

expect_error(error_if_field_not_in_df(good_data, c(good_col, good_col)),
"Argument 'c(good_col, good_col)' must be a `character` of length 1",
fixed = TRUE)

expect_error(test_error_if_field_not_in_df(good_data, c(good_col, good_col)),
"Argument 'column' must be a `character` of length 1",
fixed = TRUE)
})


### Tests for success ----

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

expect_silent(error_if_field_not_in_df(good_data, good_col))
expect_silent(test_error_if_field_not_in_df(good_data, good_col))

expect_invisible(x <- error_if_field_not_in_df(good_data, good_col))
expect_invisible(x <- test_error_if_field_not_in_df(good_data, good_col))
})
34 changes: 34 additions & 0 deletions tests/testthat/test-error_if_missing.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## Test error_if_missing() ----

### Data for tests ----

test_error_if_missing <- function(data) {
error_if_missing(data)
invisible(NULL)
}


### Tests for errors ----

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

expect_error(error_if_missing(),
"Argument '' is required",
fixed = TRUE)

expect_error(test_error_if_missing(),
"Argument 'data' is required",
fixed = TRUE)
})


### Tests for success ----

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

expect_silent(error_if_missing(iris))
expect_silent(test_error_if_missing(iris))

expect_invisible(x <- error_if_missing(iris))
expect_invisible(x <- test_error_if_missing(iris))
})
50 changes: 50 additions & 0 deletions tests/testthat/test-error_if_not_character.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## Test error_if_not_character() ----

### Data for tests ----

test_error_if_not_character <- function(data) {
error_if_not_character(data)
invisible(NULL)
}


### Tests for errors ----

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

expect_error(error_if_not_character(1),
"Argument '1' must be a `character`",
fixed = TRUE)

expect_error(error_if_not_character(iris),
"Argument 'iris' must be a `character`",
fixed = TRUE)

expect_error(error_if_not_character(NULL),
"Argument 'NULL' must be a `character`",
fixed = TRUE)

expect_error(test_error_if_not_character(1),
"Argument 'data' must be a `character`",
fixed = TRUE)

expect_error(test_error_if_not_character(iris),
"Argument 'data' must be a `character`",
fixed = TRUE)

expect_error(test_error_if_not_character(NULL),
"Argument 'data' must be a `character`",
fixed = TRUE)
})


### Tests for success ----

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

expect_silent(error_if_not_character("this is a character"))
expect_silent(test_error_if_not_character("this is a character"))

expect_invisible(x <- error_if_not_character("this is a character"))
expect_invisible(x <- test_error_if_not_character("this is a character"))
})
81 changes: 81 additions & 0 deletions tests/testthat/test-error_if_not_length_of.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
## Test error_if_not_length_of() ----

### Data for tests ----

test_error_if_not_length_of <- function(data, n) {
error_if_not_length_of(data, n)
invisible(NULL)
}



### Tests for errors ----

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

expect_error(error_if_not_length_of("length_1", n = "length_1"),
"Argument 'n' must be a `numeric`",
fixed = TRUE)

expect_error(error_if_not_length_of("length_1", n = NULL),
"Argument 'n' must be a `numeric`",
fixed = TRUE)

expect_error(error_if_not_length_of("length_1", n = NA),
"Argument 'n' must be a `numeric`",
fixed = TRUE)

expect_error(error_if_not_length_of("length_1", n = 1:2),
"Argument 'n' must be a `numeric` of length 1",
fixed = TRUE)

expect_error(error_if_not_length_of("length_1", n = 2),
"Argument '\"length_1\"' must be of length 2",
fixed = TRUE)

expect_error(error_if_not_length_of(rep("length_1", 2), n = 1),
"Argument 'rep(\"length_1\", 2)' must be of length 1",
fixed = TRUE)

expect_error(test_error_if_not_length_of("length_1", n = "length_1"),
"Argument 'n' must be a `numeric`",
fixed = TRUE)

expect_error(test_error_if_not_length_of("length_1", n = NULL),
"Argument 'n' must be a `numeric`",
fixed = TRUE)

expect_error(test_error_if_not_length_of("length_1", n = NA),
"Argument 'n' must be a `numeric`",
fixed = TRUE)

expect_error(test_error_if_not_length_of("length_1", n = 1:2),
"Argument 'n' must be a `numeric` of length 1",
fixed = TRUE)

expect_error(test_error_if_not_length_of("length_1", n = 2),
"Argument 'data' must be of length 2",
fixed = TRUE)

expect_error(test_error_if_not_length_of(rep("length_1", 2), n = 1),
"Argument 'data' must be of length 1",
fixed = TRUE)
})


### Tests for success ----

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

expect_silent(error_if_not_length_of("length_1", 1))
expect_silent(test_error_if_not_length_of("length_1", 1))

expect_invisible(x <- error_if_not_length_of("length_1", 1))
expect_invisible(x <- test_error_if_not_length_of("length_1", 1))

expect_silent(error_if_not_length_of(rep("length_1", 2), 2))
expect_silent(test_error_if_not_length_of(rep("length_1", 2), 2))

expect_invisible(x <- error_if_not_length_of(rep("length_1", 2), 2))
expect_invisible(x <- test_error_if_not_length_of(rep("length_1", 2), 2))
})
47 changes: 47 additions & 0 deletions tests/testthat/test-error_if_not_sf.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## Test error_if_not_sf() ----

### Data for tests ----

test_error_if_not_sf <- function(data) {
error_if_not_sf(data)
invisible(NULL)
}

bad_data <- iris
good_data <- sf::st_read(system.file(file.path("extdata",
"marine_fish_richness.gpkg"),
package = "rphenofish"), quiet = TRUE)


### Tests for errors ----

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

expect_error(error_if_not_sf(bad_data),
"Argument 'bad_data' must be an `sf` object",
fixed = TRUE)

expect_error(test_error_if_not_sf(bad_data),
"Argument 'data' must be an `sf` object",
fixed = TRUE)

expect_error(error_if_not_sf(good_data[0, ]),
"Argument 'good_data[0, ]' must have at least one row",
fixed = TRUE)

expect_error(test_error_if_not_sf(good_data[0, ]),
"Argument 'data' must have at least one row",
fixed = TRUE)
})


### Tests for success ----

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

expect_silent(error_if_not_sf(good_data))
expect_silent(test_error_if_not_sf(good_data))

expect_invisible(x <- error_if_not_sf(good_data))
expect_invisible(x <- test_error_if_not_sf(good_data))
})

0 comments on commit 7a74761

Please sign in to comment.