Skip to content

Commit

Permalink
test: add unit tests - filter_*()
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Feb 22, 2024
1 parent d7e89d5 commit 509fa01
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tests/testthat/test-filter_by_bbox.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## Data for tests ----

df <- data.frame(matrix(rep(1, 21), nrow = 1))
colnames(df) <- get_required_columns()

df <- rbind(df, df, df, df, df)

df[ , "site_lat_start_decimal"] <- c( 45.26, 34.18, -04.56, NA, -04.56)
df[ , "site_lon_start_decimal"] <- c(-34.25, -48.29, 74.01, 74.01, NA)

bbox_num <- c(-64.48, 16.05, -10.16, 54.08)

bbox <- sf::st_bbox(c(xmin = bbox_num[1], ymin = bbox_num[2],
xmax = bbox_num[3], ymax = bbox_num[4]),
crs = sf::st_crs(4326))

bbox_0 <- sf::st_bbox(c(xmin = bbox_num[1], ymin = bbox_num[2],
xmax = bbox_num[3], ymax = bbox_num[4]),
crs = NA)

bbox_num_2 <- c(64.48, 16.05, 10.16, 54.08)


## filter_by_bbox() ----

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

expect_error(filter_by_bbox(df),
"Argument 'bbox' is required",
fixed = TRUE)

expect_error(filter_by_bbox(df, 1),
"The object 'bbox' must a numeric of length 4 or a bbox object",
fixed = TRUE)

expect_error(filter_by_bbox(df, iho_boundaries),
"The object 'bbox' must a numeric of length 4 or a bbox object",
fixed = TRUE)

expect_error(filter_by_bbox(df, bbox_0),
"The object 'bbox' must have a CRS",
fixed = TRUE)
})


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

expect_silent(df_sub <- filter_by_bbox(df, bbox_num))

expect_true(is.data.frame(df_sub))
expect_equal(ncol(df_sub), ncol(df))
expect_equal(nrow(df_sub), 2L)

expect_silent(df_sub <- filter_by_bbox(df, bbox))

expect_true(is.data.frame(df_sub))
expect_equal(ncol(df_sub), ncol(df))
expect_equal(nrow(df_sub), 2L)

expect_silent(df_sub <- filter_by_bbox(df, bbox_num_2))

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

df <- data.frame(matrix(rep(1, 21), nrow = 1))
colnames(df) <- get_required_columns()

df <- rbind(df, df, df, df, df)

df[ , "site_lat_start_decimal"] <- c( 45.26, 34.18, -04.56, NA, -04.56)
df[ , "site_lon_start_decimal"] <- c(-34.25, -48.29, 74.01, 74.01, NA)



## filter_by_ocean() ----

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

expect_error(filter_by_ocean(df),
"Argument 'ocean' is required",
fixed = TRUE)

expect_error(filter_by_ocean(df, 1),
"Argument 'ocean' must be a character of length >= 1",
fixed = TRUE)

expect_error(filter_by_ocean(df, "Indian"),
paste0("Some ocean names are mispelled. Please use ",
"'get_ocean_names()' to find the correct spelling"),
fixed = TRUE)

expect_error(filter_by_ocean(df, c("Indian", "Arctic Ocean")),
paste0("Some ocean names are mispelled. Please use ",
"'get_ocean_names()' to find the correct spelling"),
fixed = TRUE)
})


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

expect_silent(df_sub <- filter_by_ocean(df, "Indian Ocean"))

expect_true(is.data.frame(df_sub))
expect_equal(ncol(df_sub), ncol(df))
expect_equal(nrow(df_sub), 1L)

expect_silent(df_sub <- filter_by_ocean(df, "North Atlantic Ocean"))

expect_true(is.data.frame(df_sub))
expect_equal(ncol(df_sub), ncol(df))
expect_equal(nrow(df_sub), 2L)

expect_silent(df_sub <- filter_by_ocean(df, "South Atlantic Ocean"))

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

df <- data.frame(matrix(rep(1, 21), nrow = 1))
colnames(df) <- get_required_columns()

df <- rbind(df, df, df, df, df)

df[ , "site_lat_start_decimal"] <- c( 45.26, 34.18, -04.56, NA, -04.56)
df[ , "site_lon_start_decimal"] <- c(-34.25, -48.29, 74.01, 74.01, NA)

pol <- iho_boundaries[1, ]

pol_1 <- pol
sf::st_crs(pol_1) <- NA

pts <- data_to_sf(df)


## filter_by_polygon() ----

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

expect_error(filter_by_polygon(df),
"Argument 'polygon' is required",
fixed = TRUE)

expect_error(filter_by_polygon(df, df),
"Argument 'polygon' must be an 'sf' object",
fixed = TRUE)

expect_error(filter_by_polygon(df, pts),
"Argument 'polygon' must be a 'POLYGON' or a 'MULTIPOLYGON'",
fixed = TRUE)

expect_error(filter_by_polygon(df, pol_1),
"The object 'polygon' must have a CRS",
fixed = TRUE)
})

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

expect_silent(df_sub <- filter_by_polygon(df, iho_boundaries[2, ]))

expect_true(is.data.frame(df_sub))
expect_equal(ncol(df_sub), ncol(df))
expect_equal(nrow(df_sub), 1L)

expect_silent(df_sub <- filter_by_polygon(df, iho_boundaries[4, ]))

expect_true(is.data.frame(df_sub))
expect_equal(ncol(df_sub), ncol(df))
expect_equal(nrow(df_sub), 2L)

expect_silent(df_sub <- filter_by_polygon(df, iho_boundaries[1, ]))

expect_true(is.data.frame(df_sub))
expect_equal(ncol(df_sub), ncol(df))
expect_equal(nrow(df_sub), 0L)
})

0 comments on commit 509fa01

Please sign in to comment.