From 9ed1e0e570957cada9635828d5c39706c91a0614 Mon Sep 17 00:00:00 2001 From: wlangera Date: Wed, 24 Jul 2024 16:53:16 +0200 Subject: [PATCH] add expect error tests --- tests/testthat/test-grid_designation.R | 34 +++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-grid_designation.R b/tests/testthat/test-grid_designation.R index a77a35d..967fd1f 100644 --- a/tests/testthat/test-grid_designation.R +++ b/tests/testthat/test-grid_designation.R @@ -44,7 +44,6 @@ grid_df3 <- grid_df1 %>% sf::st_drop_geometry() # Unit tests -## expect warnings test_that("unique ids if id column is provided", { expect_warning( grid_designation(observations_sf2, @@ -348,3 +347,36 @@ test_that("CRS of input observations and output are the same", { # Check if CRS is the same expect_equal(sf::st_crs(observations_sf_custom_crs), sf::st_crs(output_sf)) }) + +test_that("grid_designation raises an error for incorrect observations type", { + expect_error(grid_designation(observations_sf3, grid_df1), + "`observations` must be an sf object.") +}) + +test_that("grid_designation raises an error for incorrect grid type", { + expect_error(grid_designation(observations_sf2, grid_df3), + "`grid` must be an sf object.") +}) + +test_that("grid_designation raises an error for incorrect id_col type", { + expect_error(grid_designation(observations_sf2, grid_df1, id_col = 123), + "`id_col` must be a character vector of length 1.") +}) + +test_that("grid_designation raises an error for incorrect aggregate type", { + expect_error( + grid_designation(observations_sf2, grid_df1, aggregate = "not_logical"), + "`aggregate` must be a logical vector of length 1.") +}) + +test_that("grid_designation raises an error for different CRS", { + grid_df1_different_crs <- st_transform(grid_df1, crs = 4326) + expect_error(grid_designation(observations_sf2, grid_df1_different_crs), + "`grid` must have the same CRS as `observations`.") +}) + +test_that("grid_designation raises an error for invalid randomisation value", { + expect_error( + grid_designation(observations_sf2, grid_df1, randomisation = "invalid"), + "`randomisation` must be one of 'uniform', 'normal'.") +})