Commit 33b7b63e authored by Facundo Muñoz's avatar Facundo Muñoz ®️
Browse files

use testdata for consistent data-loading tests

parent 8eb0daa0
## check existence of testdata directory and create it if necessary
if (
!file.exists(
tdp <- file.path(system.file(package = "mapMCDA"), "testdata")
)
) {
dir.create(tdp)
}
testdata <- tdp
## Fake animal mobility data
## For testing network data
## Fake vector, raster and network data
## For testing lading
if (!file.exists(system.file("testdata", "mobility.csv", package = "mapMCDA"))) {
if (!file.exists(file.path(testdata, "points.shp"))) {
vector <- SpatialPointsDataFrame(
cbind(1:5, 1:5),
data.frame(a = 1:5)
)
writeOGR(vector, testdata, "points", driver = "ESRI Shapefile")
}
if (!file.exists(vf <- file.path(testdata, "points.gpkg"))) {
vector <- SpatialPointsDataFrame(
cbind(1:5, 1:5),
data.frame(a = 1:5)
)
writeOGR(vector, vf, layer = "points", driver = "GPKG")
}
if (!file.exists(rf <- file.path(testdata, "raster.tif"))) {
writeRaster(raster(matrix(1:4, 2, 2)), rf)
}
if (!file.exists(nf <- file.path(testdata, "mobility.csv"))) {
set.seed(20190115)
n_markets <- 5
markets <- spsample(rgeos::gUnaryUnion(admin), n_markets, type = "random")
......@@ -31,8 +54,5 @@ if (!file.exists(system.file("testdata", "mobility.csv", package = "mapMCDA")))
rownames(mobility) <- NULL
if (!file.exists(tdp <- file.path(system.file(package = "mapMCDA"), "testdata"))) {
dir.create(tdp)
}
write.csv(mobility, file.path(tdp, "mobility.csv"), row.names = FALSE, quote = FALSE)
write.csv(mobility, nf, row.names = FALSE, quote = FALSE)
}
context("load")
tdir <- tempfile()
dir.create(tdir)
vector <- SpatialPointsDataFrame(
cbind(1:5, 1:5),
data.frame(a = 1:5)
)
writeOGR(vector, tdir, "points", driver = "ESRI Shapefile")
writeRaster(raster(matrix(1:4, 2, 2)), file.path(tdir, "raster.tif"))
context("Data loading")
test_that("layer_type() correctly identifies file extensions", {
......@@ -28,18 +18,41 @@ test_that("layer_type() correctly identifies file extensions", {
expect_identical(names(file_types), as.character(file_types))
})
test_that("load_layer() reads shapefiles", {
expect_error(load_layer(file.path(tdir, "points.shp")), NA)
test_that("load_layer() reads known formats", {
expect_load <- function(x) {
eval(bquote(expect_error(load_layer(.(file.path(testdata, x))), NA)))
}
expect_load("points.shp") # Shapefile
expect_load("points.gpkg") # GeoPackage
expect_load("raster.tif") # tiff
expect_load("mobility.csv") # network
})
test_that("load_dir() skips unknnown file formats", {
res <- load_dir(tdir)
res <- load_dir(testdata)
test_files <- c("mobility", "points", "points", "raster")
expect_is(res, "list")
expect_identical(names(res), c("points", "raster"))
expect_is(res[[1]], "SpatialPointsDataFrame")
expect_is(res[[2]], "RasterLayer")
expect_identical(names(res), test_files)
expect_identical(
vapply(res, class, "char")[test_files],
setNames(
c("igraph", "SpatialPointsDataFrame", "SpatialPointsDataFrame", "RasterLayer"),
test_files
)
)
})
test_that("Interpret network data", {
nf <- system.file("testdata", "mobility.csv", package = "mapMCDA")
expect_error(x <- read_network(nf), NA)
expect_s3_class(x, "igraph")
})
......@@ -14,10 +14,3 @@ admin <- mapMCDA_datasets()$cmr_admin3
## lat_dest numeric (decimal degrees, WGS84)
## volume Optional. directed flux in some consistent unit.
test_that("Interpret network data", {
nf <- system.file("testdata", "mobility.csv", package = "mapMCDA")
expect_error(x <- read_network(nf), NA)
expect_s3_class(x, "igraph")
})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment