Skip to content

Commit

Permalink
new ecoregion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MAKassien committed Feb 11, 2025
1 parent 6b12d0e commit cd2676b
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions tests/testthat/test-ecoregion.R
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,46 @@ testthat::test_that("calculate_ecoregion", {
geom = TRUE
)
)
# Ensure that unmatched locations are handled properly
testthat::expect_true(nrow(ecor_res) == nrow(site_faux))

# Test unmatched locations are correctly reintroduced with NA values in ecoregion columns
site_unmatched <-
data.frame(
site_id = "99999999999999",
lon = -999.999,
lat = -99.999,
date = as.Date("2022-01-01")
)
site_unmatched <-
terra::vect(
site_unmatched,
geom = c("lon", "lat"),
keepgeom = TRUE,
crs = "EPSG:4326")
site_unmatched <- terra::project(site_unmatched, "EPSG:5070")

site_combined <- rbind(sf::st_drop_geometry(site_faux), site_unmatched)
site_combined <- sf::st_as_sf(site_combined, coords = c("lon", "lat"), crs = "EPSG:4326")

ecor_res2 <- calculate_ecoregion(
from = erras,
locs = site_combined,
locs_id = "site_id"
)

testthat::expect_true("99999999999999" %in% ecor_res2$site_id,
info = "Unmatched site should be present.")

extra_cols <- setdiff(colnames(ecor_res2), colnames(sf::st_drop_geometry(site_combined)))
unmatched_row <- ecor_res2[ecor_res2$site_id == "99999999999999", ]

testthat::expect_true(all(is.na(unmatched_row[, extra_cols])),
info = "Unmatched locations should have NA in ecoregion-related columns.")

# Ensure the warning message appears when unmatched locations exist
testthat::expect_message(
calculate_ecoregion(from = erras, locs = site_combined, locs_id = "site_id"),
"Warning: only .* locations provided had matching ecoregions.",
fixed = FALSE
)
})
# nolint end

0 comments on commit cd2676b

Please sign in to comment.