Skip to content

Commit

Permalink
feat: add detritus cleaning but switch off during testing
Browse files Browse the repository at this point in the history
  • Loading branch information
salvafern committed Apr 26, 2023
1 parent fc1df1a commit 38909b7
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 41 deletions.
28 changes: 20 additions & 8 deletions R/08_mrp_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ mrp_get <- function(layer, path = getOption("mregions2.download_path", tempdir()
hash <- glue::glue('{layer}-{hash}')
cached_zip_path <- file.path(path, glue::glue('{hash}.zip'))
cached_unzip_path <- file.path(path, hash)
dir.create(cached_unzip_path, showWarnings = FALSE)
cached_file_path <- file.path(cached_unzip_path, glue::glue('{layer}.shp'))

do_request <- TRUE
Expand All @@ -136,14 +135,14 @@ mrp_get <- function(layer, path = getOption("mregions2.download_path", tempdir()
httr2::req_perform(path = cached_zip_path) %>%
mrp_get_sanity_check()

dir.create(cached_unzip_path, showWarnings = FALSE)
utils::unzip(zipfile = cached_zip_path, exdir = cached_unzip_path, overwrite = TRUE)

# suppressWarnings({
# try({file.remove(cached_zip_path)})
# })

if(!is_test()) try_clean_up(cached_zip_path)
}

check_server_warning(cached_unzip_path)

mrp_list <- NULL # Avoid R CMD Check note

out <- sf::st_read(cached_file_path, quiet = TRUE, stringsAsFactors = FALSE)
Expand All @@ -152,7 +151,7 @@ mrp_get <- function(layer, path = getOption("mregions2.download_path", tempdir()
}

cache_max_time <- function(){
weeks <- getOption("TESTPKG.CACHETIME", 4)
weeks <- Sys.getenv("TESTPKG.CACHETIME", 4)
weeks
}

Expand All @@ -177,17 +176,30 @@ mrp_get_sanity_check <- function(resp){

msg <- c(msg,
"i" = "Exception Code: {.emph {exception_code}}",
"i" = "Exception text: {.emph {exception_text}}"
"i" = "Exception Text: {.emph {exception_text}}"
)

# try({file.remove(resp$body)})
if(!is_test()) try_clean_up(resp$body)
})
cli::cli_abort(msg)
}

resp
}

try_clean_up <- function(path) try({file.remove(path)}, silent = TRUE)

check_server_warning <- function(cached_unzip_path){
readme <- file.path(cached_unzip_path, "README.txt")

if(file.exists(readme)){
msg <- readLines(readme, warn = FALSE, skipNul = TRUE)
msg <- paste0(msg, collapse = "; ")
warning(msg, call. = FALSE)
}

invisible(NULL)
}

.mrp_colnames <- function(layer){

Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,7 @@ assert_mrgid_exists <- function(mrgid){
gaz_rest_names_by_mrgid(mrgid)
invisible(NULL)
}

is_test <- function(){
nzchar(Sys.getenv("TESTPKG.ISTEST"))
}
24 changes: 20 additions & 4 deletions real-tests/testthat/test-real-prod.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# httptest2::with_mock_dir("prod/fail/", {
test_that("mrp_get: Bad filters errors surfaced", {
# withr::local_options("mregions2.download_path" = "./prod/fail/geo")
withr::local_envvar("TESTPKG.ISTEST" = "true")
withr::local_envvar("TESTPKG.CACHETIME" = 0)

.f <- function() mrp_get("eez", filter="<Filter>")
expect_error(.f(), "XML getFeature request SAX parsing error")

Expand All @@ -21,6 +25,17 @@
expect_error(.f(), "NoApplicableCode")

})

test_that("mrp_get: Warnings coming from the server are surfaced", {
# withr::local_options("mregions2.download_path" = "./prod/fail/geo")
withr::local_envvar("TESTPKG.ISTEST" = "true")
withr::local_envvar("TESTPKG.CACHETIME" = 0)

.f <- function() mrp_get("eez", cql_filter = "mrgid = -1")
expect_warning(.f(), regexp = "empty", fixed = TRUE)
})


# })

# httptest2::with_mock_dir("prod/ok/", {
Expand Down Expand Up @@ -96,6 +111,7 @@

test_that("mrp_get() works", {
# withr::local_options("mregions2.download_path" = "./prod/ok/geo")
withr::local_envvar("TESTPKG.ISTEST" = "true")

expect_sf <- function(x){
expect_type(x, "list")
Expand All @@ -105,8 +121,8 @@
}


# Check without caching
withr::local_options("TESTPKG.CACHETIME" = 0)
# Mock HTTP request like if there was no cache
withr::local_envvar("TESTPKG.CACHETIME" = 0)

# Mexican ECS Deposit
.f1 <- function() mrp_get("ecs", cql_filter = "mrgid = 64123")
Expand All @@ -118,8 +134,8 @@
expect_sf(.f2())
expect_s3_class(sf::st_geometry(.f2()), "sfc_LINESTRING")

# Check with caching
# withr::local_options("TESTPKG.CACHETIME" = Inf)
# Actually reading from cache without HTTP request
# withr::local_envvar("TESTPKG.CACHETIME" = Inf)
#
# expect_message(.f1(), "Cache", fixed = TRUE)
# expect_sf(.f1())
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/prod/fail/geo/eez-165a0cf8.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?><ows:ExceptionReport xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.0" xsi:schemaLocation="http://www.opengis.net/ows/1.1 https://geo.vliz.be/geoserver/schemas/ows/1.1.0/owsAll.xsd">
<ows:Exception exceptionCode="InvalidParameterValue" locator="GetFeature">
<ows:ExceptionText>Illegal property name: notvalidparameter for feature type MarineRegions:eez</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>
Binary file added tests/testthat/prod/fail/geo/eez-18c40a19.zip
Binary file not shown.
1 change: 1 addition & 0 deletions tests/testthat/prod/fail/geo/eez-18c40a19/README.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The query result is empty, and the geometric type of the features is unknwon:an empty point shapefile has been created to fill the zip file
1 change: 1 addition & 0 deletions tests/testthat/prod/fail/geo/eez-18c40a19/eez.cst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ISO-8859-1
Binary file not shown.
1 change: 1 addition & 0 deletions tests/testthat/prod/fail/geo/eez-18c40a19/eez.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["WGS 84", DATUM["World Geodetic System 1984", SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic latitude", NORTH], AXIS["Geodetic longitude", EAST], AUTHORITY["EPSG","4326"]]
Binary file added tests/testthat/prod/fail/geo/eez-18c40a19/eez.shp
Binary file not shown.
Binary file added tests/testthat/prod/fail/geo/eez-18c40a19/eez.shx
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/testthat/prod/fail/geo/eez-5664f407.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?><ows:ExceptionReport xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.0" xsi:schemaLocation="http://www.opengis.net/ows/1.1 https://geo.vliz.be/geoserver/schemas/ows/1.1.0/owsAll.xsd">
<ows:Exception exceptionCode="XML getFeature request SAX parsing error" locator="org.geoserver.ows.XmlRequestReader">
<ows:ExceptionText>org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 9; XML document structures must start and end within the same entity.
XML document structures must start and end within the same entity.</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>
5 changes: 5 additions & 0 deletions tests/testthat/prod/fail/geo/eez-9700bc98.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?><ows:ExceptionReport xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.0" xsi:schemaLocation="http://www.opengis.net/ows/1.1 https://geo.vliz.be/geoserver/schemas/ows/1.1.0/owsAll.xsd">
<ows:Exception exceptionCode="InvalidParameterValue" locator="GetFeature">
<ows:ExceptionText>Illegal property name: notvalidparameter for feature type MarineRegions:eez</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>
6 changes: 6 additions & 0 deletions tests/testthat/prod/fail/geo/eez-f5f8f76d.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?><ows:ExceptionReport xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.0" xsi:schemaLocation="http://www.opengis.net/ows/1.1 https://geo.vliz.be/geoserver/schemas/ows/1.1.0/owsAll.xsd">
<ows:Exception exceptionCode="NoApplicableCode">
<ows:ExceptionText>java.lang.ClassCastException: Cannot cast java.lang.String to java.lang.Integer
Cannot cast java.lang.String to java.lang.Integer</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>
6 changes: 6 additions & 0 deletions tests/testthat/prod/fail/geo/ows-8fcb79.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
structure(list(method = "GET", url = "geo/ows?service=wfs&version=2.0.0&request=GetFeature&typeName=MarineRegions%3Aeez&cql_filter=mrgid%20%3D%20-1&outputFormat=SHAPE-ZIP",
status_code = 200L, headers = structure(list(`Access-Control-Allow-Origin` = "*",
`X-Frame-Options` = "SAMEORIGIN", `Content-Disposition` = "attachment; filename=eez.zip",
`Content-Type` = "application/zip", `Transfer-Encoding` = "chunked",
Date = "Wed, 26 Apr 2023 08:26:02 GMT"), class = "httr2_headers"),
body = as.raw(c(0x50, 0x4b, 0x03, 0x04, 0x14))), class = "httr2_response")
2 changes: 1 addition & 1 deletion tests/testthat/prod/fail/geo/ows-a366ca.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ structure(list(method = "GET", url = "geo/ows?service=wfs&version=2.0.0&request=
status_code = 400L, headers = structure(list(`Access-Control-Allow-Origin` = "*",
`X-Frame-Options` = "SAMEORIGIN", `Content-Encoding` = "gzip",
`Content-Type` = "application/xml", `Transfer-Encoding` = "chunked",
Date = "Mon, 24 Apr 2023 14:48:21 GMT"), class = "httr2_headers"),
Date = "Wed, 26 Apr 2023 08:25:25 GMT"), class = "httr2_headers"),
body = charToRaw("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ows:ExceptionReport xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:ows=\"http://www.opengis.net/ows/1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.0.0\" xsi:schemaLocation=\"http://www.opengis.net/ows/1.1 geo/schemas/ows/1.1.0/owsAll.xsd\">\n<ows:Exception exceptionCode=\"InvalidParameterValue\" locator=\"GetFeature\">\n<ows:ExceptionText>Illegal property name: notvalidparameter for feature type MarineRegions:eez</ows:ExceptionText>\n</ows:Exception>\n</ows:ExceptionReport>\n")), class = "httr2_response")
2 changes: 1 addition & 1 deletion tests/testthat/prod/fail/geo/ows-af137d.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ structure(list(method = "GET", url = "geo/ows?service=wfs&version=2.0.0&request=
status_code = 400L, headers = structure(list(`Access-Control-Allow-Origin` = "*",
`X-Frame-Options` = "SAMEORIGIN", `Content-Encoding` = "gzip",
`Content-Type` = "application/xml", `Transfer-Encoding` = "chunked",
Date = "Mon, 24 Apr 2023 14:48:21 GMT"), class = "httr2_headers"),
Date = "Wed, 26 Apr 2023 08:25:15 GMT"), class = "httr2_headers"),
body = charToRaw("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ows:ExceptionReport xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:ows=\"http://www.opengis.net/ows/1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.0.0\" xsi:schemaLocation=\"http://www.opengis.net/ows/1.1 geo/schemas/ows/1.1.0/owsAll.xsd\">\n<ows:Exception exceptionCode=\"XML getFeature request SAX parsing error\" locator=\"org.geoserver.ows.XmlRequestReader\">\n<ows:ExceptionText>org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 9; XML document structures must start and end within the same entity.\nXML document structures must start and end within the same entity.</ows:ExceptionText>\n</ows:Exception>\n</ows:ExceptionReport>\n")), class = "httr2_response")
2 changes: 1 addition & 1 deletion tests/testthat/prod/fail/geo/ows-bf6baa.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ structure(list(method = "GET", url = "geo/ows?service=wfs&version=2.0.0&request=
status_code = 400L, headers = structure(list(`Access-Control-Allow-Origin` = "*",
`X-Frame-Options` = "SAMEORIGIN", `Content-Encoding` = "gzip",
`Content-Type` = "application/xml", `Transfer-Encoding` = "chunked",
Date = "Mon, 24 Apr 2023 14:48:22 GMT"), class = "httr2_headers"),
Date = "Wed, 26 Apr 2023 08:25:29 GMT"), class = "httr2_headers"),
body = charToRaw("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ows:ExceptionReport xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:ows=\"http://www.opengis.net/ows/1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.0.0\" xsi:schemaLocation=\"http://www.opengis.net/ows/1.1 geo/schemas/ows/1.1.0/owsAll.xsd\">\n<ows:Exception exceptionCode=\"NoApplicableCode\">\n<ows:ExceptionText>java.lang.ClassCastException: Cannot cast java.lang.String to java.lang.Integer\nCannot cast java.lang.String to java.lang.Integer</ows:ExceptionText>\n</ows:Exception>\n</ows:ExceptionReport>\n")), class = "httr2_response")
2 changes: 1 addition & 1 deletion tests/testthat/prod/fail/geo/ows-cfb5b6.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ structure(list(method = "GET", url = "geo/ows?service=wfs&version=2.0.0&request=
status_code = 400L, headers = structure(list(`Access-Control-Allow-Origin` = "*",
`X-Frame-Options` = "SAMEORIGIN", `Content-Encoding` = "gzip",
`Content-Type` = "application/xml", `Transfer-Encoding` = "chunked",
Date = "Mon, 24 Apr 2023 14:48:21 GMT"), class = "httr2_headers"),
Date = "Wed, 26 Apr 2023 08:25:28 GMT"), class = "httr2_headers"),
body = charToRaw("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ows:ExceptionReport xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:ows=\"http://www.opengis.net/ows/1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.0.0\" xsi:schemaLocation=\"http://www.opengis.net/ows/1.1 geo/schemas/ows/1.1.0/owsAll.xsd\">\n<ows:Exception exceptionCode=\"InvalidParameterValue\" locator=\"GetFeature\">\n<ows:ExceptionText>Illegal property name: notvalidparameter for feature type MarineRegions:eez</ows:ExceptionText>\n</ows:Exception>\n</ows:ExceptionReport>\n")), class = "httr2_response")
21 changes: 0 additions & 21 deletions tests/testthat/prod/fail/geo/wfs-f93f5d.R

This file was deleted.

24 changes: 20 additions & 4 deletions tests/testthat/test-prod.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ test_that("mrp_get() assertions work", {

httptest2::with_mock_dir("prod/fail/", {
test_that("mrp_get: Bad filters errors surfaced", {
withr::local_options("mregions2.download_path" = "./prod/fail/geo")
withr::local_envvar("TESTPKG.ISTEST" = "true")
withr::local_envvar("TESTPKG.CACHETIME" = 0)

.f <- function() mrp_get("eez", filter="<Filter>")
expect_error(.f(), "XML getFeature request SAX parsing error")

Expand All @@ -222,6 +226,17 @@ httptest2::with_mock_dir("prod/fail/", {
expect_error(.f(), "NoApplicableCode")

})

test_that("mrp_get: Warnings coming from the server are surfaced", {
withr::local_options("mregions2.download_path" = "./prod/fail/geo")
withr::local_envvar("TESTPKG.ISTEST" = "true")
withr::local_envvar("TESTPKG.CACHETIME" = 0)

.f <- function() mrp_get("eez", cql_filter = "mrgid = -1")
expect_warning(.f(), regexp = "empty", fixed = TRUE)
})


})

httptest2::with_mock_dir("prod/ok/", {
Expand Down Expand Up @@ -297,6 +312,7 @@ httptest2::with_mock_dir("prod/ok/", {

test_that("mrp_get() works", {
withr::local_options("mregions2.download_path" = "./prod/ok/geo")
withr::local_envvar("TESTPKG.ISTEST" = "true")

expect_sf <- function(x){
expect_type(x, "list")
Expand All @@ -306,8 +322,8 @@ httptest2::with_mock_dir("prod/ok/", {
}


# Check without caching
withr::local_options("TESTPKG.CACHETIME" = 0)
# Mock HTTP request like if there was no cache
withr::local_envvar("TESTPKG.CACHETIME" = 0)

# Mexican ECS Deposit
.f1 <- function() mrp_get("ecs", cql_filter = "mrgid = 64123")
Expand All @@ -319,8 +335,8 @@ httptest2::with_mock_dir("prod/ok/", {
expect_sf(.f2())
expect_s3_class(sf::st_geometry(.f2()), "sfc_LINESTRING")

# Check with caching
withr::local_options("TESTPKG.CACHETIME" = Inf)
# Actually reading from cache without HTTP request
withr::local_envvar("TESTPKG.CACHETIME" = Inf)

expect_message(.f1(), "Cache", fixed = TRUE)
expect_sf(.f1())
Expand Down

0 comments on commit 38909b7

Please sign in to comment.