Skip to content

Commit 053c108

Browse files
committed
Added mock API testing to test-find_land
1 parent ddddcd3 commit 053c108

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ Suggests:
3636
knitr (>= 1.46),
3737
rgbif,
3838
BAMMtools,
39-
curl
39+
curl,
40+
httptest,
41+
spdep
4042
Config/testthat/edition: 3
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {
7+
"osm_type": "W",
8+
"osm_id": 303268103,
9+
"osm_key": "place",
10+
"osm_value": "island",
11+
"type": "other",
12+
"countrycode": "EC",
13+
"name": "Isla Pinzón",
14+
"country": "Ecuador",
15+
"city": "Parroquia Santa Rosa",
16+
"state": "Provincia de Galápagos",
17+
"county": "Cantón Santa Cruz",
18+
"extent": [
19+
-90.6884183,
20+
-0.5873741,
21+
-90.6423652,
22+
-0.6344752
23+
]
24+
},
25+
"geometry": {
26+
"type": "Point",
27+
"coordinates": [
28+
-90.66598885769616,
29+
-0.61100745
30+
]
31+
}
32+
}
33+
]
34+
}

tests/testthat/test-find_land.R

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
library(httptest)
2+
13
# Test dataframe for find_land occs input without values
24
occs <- as.data.frame(matrix(ncol = 6, nrow = 2))
35
colnames(occs) <- c("decimalLongitude", "decimalLatitude",
@@ -72,16 +74,6 @@ test_that("When occurrence record dataframe input has no rows,
7274
expect_error(find_land(occs))
7375
})
7476

75-
if (!curl::has_internet()) {
76-
cli::cli_alert_info("No internet connection. Cannot run Photon tests.")
77-
} else {
78-
test_that("Fillgaps attempts to get information for a datapoint
79-
unsupported by map.where", {
80-
expect_message(find_land(occs_mystery, fillgaps = TRUE),
81-
"Filling gaps using Photon...")
82-
})
83-
}
84-
8577
test_that("Inputting a dataframe without the correct column names will cause
8678
an error", {
8779
expect_error(find_land(occ_name))
@@ -91,3 +83,18 @@ test_that("Inputting a dataframe without the correct types will cause an error",
9183
{
9284
expect_error(find_land(occ_types))
9385
})
86+
87+
##### API testing #####
88+
# Using a mock API through httptest so that Photon doesn't need to be called
89+
# directly.
90+
# Mock API response saved in \tests\testthat\photon.komoot.io
91+
92+
# Wrap with the "with_mock_api" function
93+
with_mock_api({
94+
test_that("A location not found with the maps package can be found with
95+
Photon when using fillgaps = TRUE",
96+
{
97+
result <- find_land(occs_mystery, fillgaps = TRUE)
98+
expect_equal(result$second[1], "Isla Pinzón")
99+
})
100+
})

0 commit comments

Comments
 (0)