Skip to content

Commit

Permalink
use new dataset in tests/
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Apr 15, 2024
1 parent 4ee3dd1 commit 5cd2af3
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 90 deletions.
22 changes: 11 additions & 11 deletions tests/testthat/test-fb_aggregate_site_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ test_that("fb_aggregate_site_data() works", {
)

expect_s4_class(ras, "SpatRaster")
expect_named(ras, c("sp_001", "sp_002"))
expect_equal(dim(ras), c(290, 405, 2))
expect_equal(ras[][1816], 1, tolerance = 0.000001)
expect_named(ras, c("country", "CGRA", "CLAC"))
expect_equal(dim(ras), c(290, 405, 3))
expect_equal(ras[][82006], 0, tolerance = 0.000001)


# Projected raster
Expand All @@ -101,7 +101,7 @@ test_that("fb_aggregate_site_data() works", {
)

expect_s4_class(ras, "SpatRaster")
expect_named(ras, c("sp_001", "sp_002"))
expect_named(ras, c("country", "CGRA", "CLAC"))


## Polygon spatial data
Expand All @@ -111,17 +111,17 @@ test_that("fb_aggregate_site_data() works", {
)

expect_s4_class(ras, "SpatRaster")
expect_named(ras, c("sp_001", "sp_002"))
expect_equal(dim(ras), c(290, 405, 2))
expect_equal(ras[][1816], 1, tolerance = 0.000001)
expect_named(ras, c("country", "CGRA", "CLAC"))
expect_equal(dim(ras), c(290, 405, 3))
expect_equal(ras[][82006], 0, tolerance = 0.000001)

# Projected Raster
expect_silent(
ras <- fb_aggregate_site_data(site_locations, site_species[, 1:3], tavg_prj)
)

expect_s4_class(ras, "SpatRaster")
expect_named(ras, c("sp_001", "sp_002"))
expect_named(ras, c("country", "CGRA", "CLAC"))


## Multiline spatial data
Expand All @@ -131,14 +131,14 @@ test_that("fb_aggregate_site_data() works", {
)

expect_s4_class(ras, "SpatRaster")
expect_named(ras, c("sp_001", "sp_002"))
expect_equal(dim(ras), c(290, 405, 2))
expect_named(ras, c("country", "CGRA", "CLAC"))
expect_equal(dim(ras), c(290, 405, 3))

# Projected Raster
expect_silent(
ras <- fb_aggregate_site_data(site_lines, site_species[, 1:3], tavg_prj)
)

expect_s4_class(ras, "SpatRaster")
expect_named(ras, c("sp_001", "sp_002"))
expect_named(ras, c("country", "CGRA", "CLAC"))
})
8 changes: 4 additions & 4 deletions tests/testthat/test-fb_format_site_locations.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Initial data -----------------------------------------------------------------

filename <- system.file("extdata", "raw_mammals_data.csv",
filename <- system.file("extdata", "funbiogeo_raw_data.csv",
package = "funbiogeo")
all_data <- read.csv(filename)

Expand Down Expand Up @@ -230,10 +230,10 @@ test_that("fb_format_site_locations() works with valid input", {
site_locations <- fb_format_site_locations(all_data, "site", "longitude",
"latitude")
expect_true(is(site_locations, "sf"))
expect_equal(nrow(site_locations), 32L)
expect_equal(nrow(site_locations), 4723L)
expect_equal(ncol(site_locations), 2L)
expect_true("site" %in% colnames(site_locations))
expect_equal(sf::st_coordinates(site_locations)[1, 1][[1]], 59.097357)
expect_equal(sf::st_coordinates(site_locations)[1, 1][[1]], 1955000)


# Including NA values
Expand All @@ -244,5 +244,5 @@ test_that("fb_format_site_locations() works with valid input", {
site_locations <- fb_format_site_locations(all_data_test, "site",
"longitude", "latitude",
na_rm = TRUE)
expect_equal(nrow(site_locations), 32L)
expect_equal(nrow(site_locations), 4722L)
})
14 changes: 7 additions & 7 deletions tests/testthat/test-fb_format_site_species.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("fb_format_site_species() works", {

filename <- system.file("extdata", "raw_mammals_data.csv",
filename <- system.file("extdata", "funbiogeo_raw_data.csv",
package = "funbiogeo")
all_data <- read.csv(filename)

Expand Down Expand Up @@ -110,8 +110,8 @@ test_that("fb_format_site_species() works", {
)

expect_error(
fb_format_site_species(all_data, "site", "genus"),
"The column 'genus' is absent from 'data'",
fb_format_site_species(all_data, "site", "class"),
"The column 'class' is absent from 'data'",
fixed = TRUE
)

Expand Down Expand Up @@ -193,12 +193,12 @@ test_that("fb_format_site_species() works", {
site_species <- fb_format_site_species(all_data, "site", "species", "count",
na_to_zero = TRUE)

expect_equal(nrow(site_species), 32L)
expect_equal(ncol(site_species), 66L)
expect_equal(nrow(site_species), 4723L)
expect_equal(ncol(site_species), 27L)
expect_true("site" %in% colnames(site_species))
expect_equal(site_species[1, 3], 0L)
expect_equal(site_species[1, 3], 1L)

site_species <- fb_format_site_species(all_data, "site", "species", "count",
na_to_zero = FALSE)
expect_equal(site_species[1, 3], NA_real_)
expect_equal(site_species[1, 3], 1L)
})
12 changes: 6 additions & 6 deletions tests/testthat/test-fb_format_species_categories.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("fb_format_species_categories() works", {

filename <- system.file("extdata", "raw_mammals_data.csv",
filename <- system.file("extdata", "funbiogeo_raw_data.csv",
package = "funbiogeo")
all_data <- read.csv(filename)

Expand Down Expand Up @@ -120,22 +120,22 @@ test_that("fb_format_species_categories() works", {
# Non-unique categories per species ----

test_data <- all_data
test_data[2, 2] <- "Carnivora"
test_data[1, "genus"] <- "Pyrus"

expect_error(
fb_format_species_categories(test_data, "species", "order"),
fb_format_species_categories(test_data, "species", "genus"),
"Some species have non-unique category values",
fixed = TRUE
)

# Working ----

species_categories <- fb_format_species_categories(
all_data, "species", "order"
all_data, "species", "genus"
)
expect_true(is.data.frame(species_categories))
expect_equal(nrow(species_categories), 65L)
expect_equal(nrow(species_categories), 26L)
expect_equal(ncol(species_categories), 2L)
expect_true("species" %in% colnames(species_categories))
expect_equal(species_categories[1, 2],"Cetartiodactyla")
expect_equal(species_categories[1, 2],"Crataegus")
})
36 changes: 17 additions & 19 deletions tests/testthat/test-fb_format_species_traits.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("fb_format_species_traits() works", {

filename <- system.file("extdata", "raw_mammals_data.csv",
filename <- system.file("extdata", "funbiogeo_raw_data.csv",
package = "funbiogeo")
all_data <- read.csv(filename)

Expand Down Expand Up @@ -120,30 +120,26 @@ test_that("fb_format_species_traits() works", {

all_data_test <- all_data
all_data_test <- rbind(all_data_test[1, ], all_data_test)
all_data_test[1, "adult_body_mass"] <- 9999
all_data_test[1, "plant_height"] <- 9999

expect_error(
fb_format_species_traits(all_data_test, "species", c("adult_body_mass",
"gestation_length",
"litter_size",
"max_longevity",
"sexual_maturity_age",
"diet_breadth")),
fb_format_species_traits(all_data_test, "species", c("plant_height",
"seed_mass",
"sla",
"wood_density")),
"Some species have non-unique trait values",
fixed = TRUE
)

all_data_test <- all_data
all_data_test <- rbind(all_data_test[1, ], all_data_test)
all_data_test[1, "adult_body_mass"] <- NA
all_data_test[1, "plant_height"] <- NA

expect_error(
fb_format_species_traits(all_data_test, "species", c("adult_body_mass",
"gestation_length",
"litter_size",
"max_longevity",
"sexual_maturity_age",
"diet_breadth")),
fb_format_species_traits(all_data_test, "species", c("plant_height",
"seed_mass",
"sla",
"wood_density")),
"Some species have non-unique trait values",
fixed = TRUE
)
Expand All @@ -153,12 +149,14 @@ test_that("fb_format_species_traits() works", {

species_traits <- fb_format_species_traits(
all_data, "species",
c("adult_body_mass", "gestation_length", "litter_size", "max_longevity",
"sexual_maturity_age", "diet_breadth")
c("plant_height",
"seed_mass",
"sla",
"wood_density")
)
expect_true(is.data.frame(species_traits))
expect_equal(nrow(species_traits), 65L)
expect_equal(ncol(species_traits), 7L)
expect_equal(nrow(species_traits), 26L)
expect_equal(ncol(species_traits), 5L)
expect_true("species" %in% colnames(species_traits))
expect_equal(species_traits[1, 3], 235, tolerance = 7)
})
22 changes: 11 additions & 11 deletions tests/testthat/test-fb_get_all_trait_coverages_by_site.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ test_that(
)

expect_s3_class(res, "data.frame")
expect_equal(dim(res), c(1505, 8))
expect_equal(dim(res), c(4723, 6))
expect_equal(
colnames(res), c("site", "all_traits", colnames(species_traits)[-1])
)
expect_equal(round(res[1, 2], 2), 0.73)
expect_equal(round(res[2, 5], 2), 0.97)
expect_equal(round(res[1, 2], 2), 1.0)
expect_equal(round(res[2, 5], 2), 1.0)

# Without all traits
expect_silent(
Expand All @@ -74,11 +74,11 @@ test_that(
)

expect_s3_class(res, "data.frame")
expect_equal(dim(res), c(1505, 7))
expect_equal(dim(res), c(0, 5))
expect_equal(
colnames(res), c("site", colnames(species_traits)[-1])
)
expect_equal(round(res[2, 4], 2), 0.91)
expect_equal(round(res[2, 4], 2), NA_real_)

}
)
Expand All @@ -94,7 +94,7 @@ test_that("fb_get_all_trait_coverages_by_site() works for edge cases", {
)
)
expect_s3_class(res, "data.frame")
expect_equal(dim(res), c(1505, 8))
expect_equal(dim(res), c(4723, 6))
expect_equal(
colnames(res), c("site", "all_traits", colnames(species_traits)[-1])
)
Expand All @@ -106,7 +106,7 @@ test_that("fb_get_all_trait_coverages_by_site() works for edge cases", {
)
)
expect_s3_class(res, "data.frame")
expect_equal(dim(res), c(1505, 8))
expect_equal(dim(res), c(4723, 6))
expect_equal(
colnames(res), c("site", "all_traits", colnames(species_traits)[-1])
)
Expand All @@ -118,7 +118,7 @@ test_that("fb_get_all_trait_coverages_by_site() works for edge cases", {
)
)
expect_s3_class(res, "data.frame")
expect_equal(dim(res), c(1505, 8))
expect_equal(dim(res), c(4723, 6))
expect_equal(
colnames(res), c("site", "all_traits", colnames(species_traits)[-1])
)
Expand All @@ -132,7 +132,7 @@ test_that("fb_get_all_trait_coverages_by_site() works for edge cases", {
)
)
expect_s3_class(res, "data.frame")
expect_equal(dim(res), c(1505, 7))
expect_equal(dim(res), c(0, 5))
expect_equal(
colnames(res), c("site", colnames(species_traits)[-1])
)
Expand All @@ -144,7 +144,7 @@ test_that("fb_get_all_trait_coverages_by_site() works for edge cases", {
)
)
expect_s3_class(res, "data.frame")
expect_equal(dim(res), c(1505, 7))
expect_equal(dim(res), c(0, 5))
expect_equal(
colnames(res), c("site", colnames(species_traits)[-1])
)
Expand All @@ -156,7 +156,7 @@ test_that("fb_get_all_trait_coverages_by_site() works for edge cases", {
)
)
expect_s3_class(res, "data.frame")
expect_equal(dim(res), c(1505, 7))
expect_equal(dim(res), c(0, 5))
expect_equal(
colnames(res), c("site", colnames(species_traits)[-1])
)
Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test-fb_get_environment.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ test_that("fb_get_environment() works", {

expect_s3_class(env_value, "data.frame")
expect_named(env_value, c("site", "annual_mean_temp", "annual_tot_prec"))
expect_equal(dim(env_value), c(1505, 3))
expect_equal(round(env_value[["annual_tot_prec"]][[1]]), 2620)
expect_equal(dim(env_value), c(4723, 3))
expect_equal(round(env_value[["annual_tot_prec"]][[3]]), 692)

# Projected layers
rob <- "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
Expand All @@ -99,8 +99,8 @@ test_that("fb_get_environment() works", {

expect_s3_class(env_value, "data.frame")
expect_named(env_value, c("site", "annual_mean_temp", "annual_tot_prec"))
expect_equal(dim(env_value), c(1505, 3))
expect_equal(env_value[["annual_mean_temp"]][[1]], 6.3, tolerance = 0.01)
expect_equal(dim(env_value), c(4723, 3))
expect_equal(env_value[["annual_mean_temp"]][[1]], 15.7, tolerance = 0.01)


## 'sf' polygons
Expand All @@ -109,16 +109,16 @@ test_that("fb_get_environment() works", {

expect_s3_class(env_value, "data.frame")
expect_named(env_value, c("site", "annual_mean_temp", "annual_tot_prec"))
expect_equal(dim(env_value), c(1505, 3))
expect_equal(round(env_value[["annual_tot_prec"]][[1]]), 2480)
expect_equal(dim(env_value), c(4723, 3))
expect_equal(round(env_value[["annual_tot_prec"]][[1]]), 671)

# Projected Layer
expect_silent(env_value <- fb_get_environment(site_locations, layers_prj))

expect_s3_class(env_value, "data.frame")
expect_named(env_value, c("site", "annual_mean_temp", "annual_tot_prec"))
expect_equal(dim(env_value), c(1505, 3))
expect_equal(env_value[["annual_mean_temp"]][[1]], 6.6, tolerance = 0.01)
expect_equal(dim(env_value), c(4723, 3))
expect_equal(env_value[["annual_mean_temp"]][[1]], 15.7, tolerance = 0.01)


## 'sf' line
Expand All @@ -128,13 +128,13 @@ test_that("fb_get_environment() works", {
expect_s3_class(env_value, "data.frame")
expect_named(env_value, c("site", "annual_mean_temp", "annual_tot_prec"))
expect_equal(dim(env_value), c(1, 3))
expect_equal(env_value[["annual_mean_temp"]][[1]], 6.3, tolerance = 0.01)
expect_equal(env_value[["annual_mean_temp"]][[1]], 15.7, tolerance = 0.01)

# Projected layer
expect_silent(env_value <- fb_get_environment(site_lines, layers_prj))

expect_s3_class(env_value, "data.frame")
expect_named(env_value, c("site", "annual_mean_temp", "annual_tot_prec"))
expect_equal(dim(env_value), c(1, 3))
expect_equal(env_value[["annual_mean_temp"]][[1]], 6.38, tolerance = 0.01)
expect_equal(env_value[["annual_mean_temp"]][[1]], 15.9, tolerance = 0.01)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-fb_get_trait_combination_coverage.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test_that("fb_get_trait_combination_coverage() works with good inputs", {
"trait_coverage")
)
expect_equal(res[1, 2], 1)
expect_equal(res[1, 3], "adult_body_mass")
expect_equal(res[1, 3], "plant_height")

# Given trait combination
expect_silent(
Expand All @@ -66,5 +66,5 @@ test_that("fb_get_trait_combination_coverage() works with good inputs", {
"trait_coverage")
)
expect_equal(res[1, 2], 2)
expect_equal(res[1, 3], "adult_body_mass__gestation_length")
expect_equal(res[1, 3], "plant_height__seed_mass")
})
Loading

0 comments on commit 5cd2af3

Please sign in to comment.