Skip to content

Commit

Permalink
add unit tests (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cidree committed Sep 19, 2024
1 parent f6b0329 commit 4fd249a
Show file tree
Hide file tree
Showing 9 changed files with 700 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/testthat/test-GLAD.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

# 1. Forest Extent --------------------------------------------------------

## 1.1. Get data --------------------

## Using coords (all years)
galicia_coords_all_years <- fd_forest_extent_glad(
lat = -7,
lon = 43,
year = "all"
)

## 1.2. Unit tests --------------------

## Check that data is properly downloaded and tiles are merged
test_that("Data is properly downloaded", {
expect_s4_class(galicia_coords_all_years, "SpatRaster")
})

test_that("Tiles are the correct ones", {

expect_equal(
basename(terra::sources(galicia_coords_all_years)),
rep("00N_040E.tif", 2)
)

expect_equal(
names(galicia_coords_all_years),
c("tile_2000", "tile_2020")
)
})

38 changes: 38 additions & 0 deletions tests/testthat/test-allometry.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# 1. TALLO database -------------------------------------------------------

## Get data
tallo_tbl <- fd_allometry_tallo()
tallo_sf <- fd_allometry_tallo(spatial = TRUE)
tallo_country_sf <- fd_allometry_tallo(country = "Spain")
tallo_iso2_sf <- fd_allometry_tallo(country = "ES")
tallo_iso3_sf <- fd_allometry_tallo(country = "ESP")
tallo_2countries <- fd_allometry_tallo(country = c("ES", "PO"))

## Check formats
test_that("Database is downloaded in correct format", {
expect_equal(class(tallo_tbl)[1], "tbl_df")
expect_equal(class(tallo_sf)[1], "sf")
expect_equal(class(tallo_country_sf)[1], "sf")
expect_equal(class(tallo_iso2_sf)[1], "sf")
expect_equal(class(tallo_iso3_sf)[1], "sf")
expect_equal(class(tallo_2countries)[1], "sf")
})

## Data is downloaded
test_that("Data is downloaded", {
expect_gt(nrow(tallo_tbl), 1)
expect_gt(nrow(tallo_sf), 1)
expect_gt(nrow(tallo_country_sf), 1)
expect_gt(nrow(tallo_iso2_sf), 1)
expect_gt(nrow(tallo_iso3_sf), 1)
expect_gt(nrow(tallo_2countries), 1)
})

## ISO2, ISO3 and country works properly
test_that("Data is the same", {
expect_equal(tallo_country_sf, tallo_iso2_sf)
expect_equal(tallo_iso3_sf, tallo_iso2_sf)
})


70 changes: 70 additions & 0 deletions tests/testthat/test-canopy-height.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

library(terra)

# 1. Sentinel 10m ---------------------------------------------------------

## 1.1. Get data ------------------

## Create polygon spanning 2 tiles
polygon_2tiles_sf <- sf::st_sfc(sf::st_point(c(-7, 42)), crs = 4326) |>
sf::st_buffer(1000) |>
sf::st_as_sf()

polygon_sf <- sf::st_sfc(sf::st_point(c(-8, 43)), crs = 4326) |>
sf::st_buffer(1000) |>
sf::st_as_sf() |>
terra::vect()

## Download 2 tiles
ch_2tiles_sr <- fd_canopy_height(polygon_2tiles_sf)

## Download 1 tile
ch_polygon_sr <- fd_canopy_height(polygon_sf)
ch_polygon_crop_sr <- fd_canopy_height(polygon_sf, crop = TRUE)

## Download 1 tiles using coords
ch_coords_sr <- fd_canopy_height(lon = -8, lat = 43)

## Download other layers
std_coords_sr <- fd_canopy_height(lon = -8, lat = 43, layer = "std")
all_coords_sr <- fd_canopy_height(lon = -8, lat = 43, layer = "all")

## 1.2. Unit tests ------------------

## Check that data is properly downloaded (tiles are merged)
test_that("Data is properly downloaded", {
expect_s4_class(ch_2tiles_sr, "SpatRaster")
expect_s4_class(ch_polygon_sr, "SpatRaster")
expect_s4_class(ch_polygon_crop_sr, "SpatRaster")
expect_s4_class(ch_coords_sr, "SpatRaster")
expect_s4_class(std_coords_sr, "SpatRaster")
expect_s4_class(all_coords_sr, "SpatRaster")
})

## Check that polygon was properly cropped
test_that("Crop works", {
expect_equal(terra::ext(polygon_sf)[1], terra::ext(ch_polygon_crop_sr)[1], tolerance = 1e-1)
expect_equal(terra::ext(polygon_sf)[2], terra::ext(ch_polygon_crop_sr)[2], tolerance = 1e-1)
expect_equal(terra::ext(polygon_sf)[3], terra::ext(ch_polygon_crop_sr)[3], tolerance = 1e-1)
expect_equal(terra::ext(polygon_sf)[4], terra::ext(ch_polygon_crop_sr)[4], tolerance = 1e-1)
})

## Test that not cropping and taking coordinate of same tile is same object
test_that("Objects are the same", {
expect_equal(terra::ext(ch_polygon_sr)[1], terra::ext(ch_coords_sr)[1], tolerance = 1e-1)
expect_equal(terra::ext(ch_polygon_sr)[2], terra::ext(ch_coords_sr)[2], tolerance = 1e-1)
expect_equal(terra::ext(ch_polygon_sr)[3], terra::ext(ch_coords_sr)[3], tolerance = 1e-1)
expect_equal(terra::ext(ch_polygon_sr)[4], terra::ext(ch_coords_sr)[4], tolerance = 1e-1)
})

## Check layer argument
test_that("Layers are the same", {
expect_equal(
names(all_coords_sr),
names(c(ch_coords_sr, std_coords_sr))
)
expect_equal(
names(all_coords_sr),
c("chm", "std")
)
})
47 changes: 47 additions & 0 deletions tests/testthat/test-chorological-maps.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

# 1. Chronological maps -----------------------------------------------------

## 1.1. Get data --------------------

cs_syn <- fd_forest_chorological(
species = "Castanea sativa",
range = "syn"
)

cs_nat <- fd_forest_chorological(
species = "Castanea sativa",
range = "nat"
)

## 1.2. Unit tests ------------------

test_that("All natural range data in downloaded", {
expect_no_error(
purrr::map(
metadata_forestdata$chorological_species[1:20],
fd_forest_chorological
)
)
})

test_that("Synanthropic range data is downloaded", {
expect_no_error(
fd_forest_chorological(
species = "Castanea sativa",
range = "syn"
)
)
})

test_that("Polygons are downloaded, not points", {

expect_equal(
sf::st_geometry_type(cs_nat, by_geometry = FALSE) |> as.character(),
"POLYGON"
)

expect_equal(
sf::st_geometry_type(cs_syn, by_geometry = FALSE) |> as.character(),
"POLYGON"
)
})
168 changes: 168 additions & 0 deletions tests/testthat/test-eutrees4f.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@

# 1. EU-Trees4F -----------------------------------------------------------

## 1.1. Get data ------------------------

## Base with all periods
abies_alba_base <- fd_forest_eutrees4f(
species = metadata_forestdata$eutrees4f_species[1]
)

## Download only one period
acer_campestre_1_period <- purrr::map(
c("2005", "2035", "2065", "2065"),
\(x) fd_forest_eutrees4f(
species = metadata_forestdata$eutrees4f_species[2],
period = x
)
)

## Scenario argument
betula_pendula_scenario <- purrr::map(
c("rcp45", "rcp85"),
\(x) fd_forest_eutrees4f(
species = metadata_forestdata$eutrees4f_species[10],
period = "2035",
scenario = x
)
)

## Type and model argument
quercus_ilex_type <- purrr::map(
c("bin", "prob", "std"),
\(x) fd_forest_eutrees4f(
species = "Quercus ilex",
period = "2095",
model = "sdms",
type = x
)
)

## Distrib argument
tilia_cordata_distrib <- purrr::map(
c("disp_lu", "disp", "pot"),
\(x) fd_forest_eutrees4f(
species = "Tilia cordata",
period = "2035",
type = "bin",
distrib = x
)
)

tilia_cordata_distrib_nat <-
fd_forest_eutrees4f(
"Abies alba",
type = "bin",
period = "2005",
distrib = "nat"
)


## 1.2. Unit tests ----------------------

## Check that all periods are downloaded properly
test_that("4 layers are downloaded", {

expect_equal(terra::nlyr(abies_alba_base), 4)

expect_equal(
basename(terra::sources(abies_alba_base)),
c(
"Abies_alba_ens-clim_cur2005_bin_pot.tif",
"Abies_alba_ens-clim_rcp45_fut2035_bin_pot.tif",
"Abies_alba_ens-clim_rcp45_fut2065_bin_pot.tif",
"Abies_alba_ens-clim_rcp45_fut2095_bin_pot.tif"
)
)

})

## Check that only 1 layer is downloaded when 1 year specified
test_that("There is only 1 layer per raster", {
expect_equal(
purrr::map_int(acer_campestre_1_period, terra::nlyr),
rep(1, 4)
)
})

## Check scenario argument
test_that("Scenario argument works properly", {

expect_equal(
purrr::map_chr(betula_pendula_scenario, \(x) basename(terra::sources(x))),
c(
"Betula_pendula_ens-clim_rcp45_fut2035_bin_pot.tif",
"Betula_pendula_ens-clim_rcp85_fut2035_bin_pot.tif"
)
)

})

## Check type and model argument
test_that("Type and model arguments work properly", {

expect_equal(
purrr::map_chr(quercus_ilex_type, \(x) basename(terra::sources(x))),
c(
"Quercus_ilex_ens-sdms_rcp45_fut2095_bin_pot.tif",
"Quercus_ilex_ens-sdms_rcp45_fut2095_prob_pot.tif",
"Quercus_ilex_ens-sdms_rcp45_fut2095_std_pot.tif"
)
)
})

## Check distrib argument
test_that("Distrib argument behaves correctly", {
expect_equal(
purrr::map_chr(tilia_cordata_distrib, \(x) basename(terra::sources(x))),
c(
"Tilia_cordata_ens-clim_rcp45_fut2035_bin_disp_lu.tif",
"Tilia_cordata_ens-clim_rcp45_fut2035_bin_disp.tif",
"Tilia_cordata_ens-clim_rcp45_fut2035_bin_pot.tif"
)
)

expect_equal(
basename(terra::sources(tilia_cordata_distrib_nat)),
"Abies_alba_ens-clim_cur2005_bin_nat.tif"
)
})

## Test errors
test_that("Errors are working properly", {

expect_error(fd_forest_eutrees4f("Abies alba", model = "clim", type = "std"))
expect_error(fd_forest_eutrees4f("Abies alba", type = "prob", distrib = "nat"))
expect_error(fd_forest_eutrees4f("Abies alba", type = "std", distrib = "nat"))
expect_error(fd_forest_eutrees4f("Abies alba", type = "std", period = 2005))
expect_error(fd_forest_eutrees4f("Abies alba", distrib = "disp", type = "std"))
expect_error(fd_forest_eutrees4f("Abies alba", distrib = "disp_lu", type = "std"))
expect_error(fd_forest_eutrees4f("Abies alba", distrib = "nat", period = 2065))

})


























Loading

0 comments on commit 4fd249a

Please sign in to comment.