-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: implement tests for check functions
- Loading branch information
Showing
5 changed files
with
260 additions
and
0 deletions.
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
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)) | ||
}) |
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,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)) | ||
}) |
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,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")) | ||
}) |
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,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)) | ||
}) |
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,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)) | ||
}) |