Skip to content

Commit

Permalink
handle warnings correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
wlangera committed Jul 22, 2024
1 parent 27c01e5 commit 43361b1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
20 changes: 10 additions & 10 deletions tests/testthat/test-grid_designation.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,29 +177,29 @@ test_that("correct column names present", {
})

test_that("no minimal coordinate uncertainty for empty grid cells", {
suppressWarnings({
grid_designation_df1 <- grid_designation(observations_sf1, grid = grid_df1)
grid_designation_df2 <- grid_designation(observations_sf2, grid = grid_df1)
grid_designation_df3 <- grid_designation(observations_sf1, grid = grid_df1,
randomisation = "normal")
grid_designation_df4 <- grid_designation(observations_sf2, grid = grid_df1,
randomisation = "normal")
})

# randomisation is "uniform"
suppressWarnings({
expect_equal(sum(grid_designation_df1$n == 0),
sum(is.na(grid_designation_df1$min_coord_uncertainty))
)
})
expect_equal(sum(grid_designation_df1$n == 0),
sum(is.na(grid_designation_df1$min_coord_uncertainty))
)

expect_equal(sum(grid_designation_df2$n == 0),
sum(is.na(grid_designation_df2$min_coord_uncertainty))
)

# randomisation is "normal"
suppressWarnings({
expect_equal(sum(grid_designation_df3$n == 0),
sum(is.na(grid_designation_df3$min_coord_uncertainty))
)
})
expect_equal(sum(grid_designation_df3$n == 0),
sum(is.na(grid_designation_df3$min_coord_uncertainty))
)

expect_equal(sum(grid_designation_df4$n == 0),
sum(is.na(grid_designation_df4$min_coord_uncertainty))
)
Expand Down
31 changes: 18 additions & 13 deletions tests/testthat/test-sample_from_binormal_circle.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,52 +38,57 @@ test_that("arguments are of the right class", {

# pnorm is numeric between 0 and 1
expect_error(sample_from_binormal_circle(observations_sf1, p_norm = "0.95"),
regexp = "`p_norm` must be a numeric vector of length 1.",
regexp = "`pnorm` must be a numeric value between 0 and 1.",
fixed = TRUE)
expect_error(sample_from_binormal_circle(observations_sf2, p_norm = "0.95"),
regexp = "`p_norm` must be a numeric vector of length 1.",
regexp = "`pnorm` must be a numeric value between 0 and 1.",
fixed = TRUE)
expect_error(sample_from_binormal_circle(observations_sf1, p_norm = -0.5),
regexp = "`p_norm` must be a single value between 0 and 1.",
regexp = "`pnorm` must be a numeric value between 0 and 1.",
fixed = TRUE)
expect_error(sample_from_binormal_circle(observations_sf2, p_norm = -0.5),
regexp = "`p_norm` must be a single value between 0 and 1.",
regexp = "`pnorm` must be a numeric value between 0 and 1.",
fixed = TRUE)

# seed is numeric
expect_error(sample_from_binormal_circle(observations_sf1, seed = "123"),
regexp = "`seed` must be a numeric vector of length 1.",
regexp = "`seed` must be a numeric vector of length 1 or NA.",
fixed = TRUE)
expect_error(sample_from_binormal_circle(observations_sf2, seed = "123"),
regexp = "`seed` must be a numeric vector of length 1.",
regexp = "`seed` must be a numeric vector of length 1 or NA.",
fixed = TRUE)
})

test_that("arguments are of the right length", {
# pnorm is length 1
expect_error(sample_from_binormal_circle(observations_sf1,
p_norm = seq(0.1, 0.3, 0.1)),
regexp = "`p_norm` must be a vector of length 1.",
regexp = "`pnorm` must be a numeric value between 0 and 1.",
fixed = TRUE)
expect_error(sample_from_binormal_circle(observations_sf2,
p_norm = seq(0.1, 0.3, 0.1)),
regexp = "`p_norm` must be a vector of length 1.",
regexp = "`pnorm` must be a numeric value between 0 and 1.",
fixed = TRUE)

# seed has length 1
expect_error(sample_from_binormal_circle(observations_sf1, seed = 1:3),
regexp = "`seed` must be a numeric vector of length 1.",
regexp = "`seed` must be a numeric vector of length 1 or NA.",
fixed = TRUE)
expect_error(sample_from_binormal_circle(observations_sf2, seed = 1:3),
regexp = "`seed` must be a numeric vector of length 1.",
regexp = "`seed` must be a numeric vector of length 1 or NA.",
fixed = TRUE)
})

## expect warnings
test_that("warning if coordinateUncertaintyInMeters column is not present", {
expect_warning(sample_from_binormal_circle(observations_sf1),
regexp = "No column `coordinateUncertaintyInMeters` present!",
fixed = TRUE)
expect_warning(
sample_from_binormal_circle(observations_sf1),
regexp = paste(
"No column `coordinateUncertaintyInMeters` present!",
"Assuming no uncertainty around observations.",
sep = "\n"
),
fixed = TRUE)
})

## expected outputs
Expand Down
19 changes: 12 additions & 7 deletions tests/testthat/test-sample_from_uniform_circle.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,33 @@ test_that("arguments are of the right class", {

# seed is numeric
expect_error(sample_from_uniform_circle(observations_sf1, seed = "123"),
regexp = "`seed` must be a numeric vector of length 1.",
regexp = "`seed` must be a numeric vector of length 1 or NA.",
fixed = TRUE)
expect_error(sample_from_uniform_circle(observations_sf2, seed = "123"),
regexp = "`seed` must be a numeric vector of length 1.",
regexp = "`seed` must be a numeric vector of length 1 or NA.",
fixed = TRUE)
})

test_that("arguments are of the right length", {
# seed has length 1
expect_error(sample_from_uniform_circle(observations_sf1, seed = 1:3),
regexp = "`seed` must be a numeric vector of length 1.",
regexp = "`seed` must be a numeric vector of length 1 or NA.",
fixed = TRUE)
expect_error(sample_from_uniform_circle(observations_sf2, seed = 1:3),
regexp = "`seed` must be a numeric vector of length 1.",
regexp = "`seed` must be a numeric vector of length 1 or NA.",
fixed = TRUE)
})

## expect warnings
test_that("warning if coordinateUncertaintyInMeters column is not present", {
expect_warning(sample_from_uniform_circle(observations_sf1),
regexp = "No column `coordinateUncertaintyInMeters` present!",
fixed = TRUE)
expect_warning(
sample_from_uniform_circle(observations_sf1),
regexp = paste(
"No column `coordinateUncertaintyInMeters` present!",
"Assuming no uncertainty around observations.",
sep = "\n"
),
fixed = TRUE)
})

## expected outputs
Expand Down

0 comments on commit 43361b1

Please sign in to comment.