Skip to content

Commit cf5c798

Browse files
committed
test: replace deprecated with_mock() with with_mocked_bindings() in all test cases
1 parent bd264a1 commit cf5c798

16 files changed

+155
-119
lines changed

hereR.Rproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version: 1.0
2+
ProjectId: 5727d5b4-fdcf-4de3-8771-c889c90ac857
23

34
RestoreWorkspace: Default
45
SaveWorkspace: Default

tests/testthat/test-autosuggest.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ test_that("autosuggest works", {
1212
expect_error(autosuggest(" "), "'address' contains empty strings.")
1313

1414
# Test with API response mock
15-
with_mock(
16-
"hereR:::.async_request" = function(url, rps) {
15+
with_mocked_bindings(
16+
.async_request = function(url, rps) {
1717
hereR:::mock$autosuggest_response
1818
},
19-
suggestion <- autosuggest(address = poi$city),
19+
{
20+
suggestion <- autosuggest(address = poi$city)
2021

21-
# Tests
22-
expect_s3_class(suggestion, "data.frame", exact = TRUE),
23-
expect_equal(length(unique(suggestion$id)), length(poi$city))
22+
# Tests
23+
expect_s3_class(suggestion, "data.frame", exact = TRUE)
24+
expect_equal(length(unique(suggestion$id)), length(poi$city))
25+
}
2426
)
2527
})

tests/testthat/test-connection.R

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,30 @@ test_that("connection works", {
2222

2323
## Test with API response mock
2424
# Route segments: "summary = FALSE"
25-
with_mock(
26-
"hereR:::.async_request" = function(url, rps) {
25+
with_mocked_bindings(
26+
.async_request = function(url, rps) {
2727
hereR:::mock$connection_response
2828
},
29-
connections <- connection(origin = poi[3:4, ], destination = poi[5:6, ], summary = FALSE),
29+
{
30+
connections <- connection(origin = poi[3:4, ], destination = poi[5:6, ], summary = FALSE)
3031

31-
# Tests
32-
expect_equal(any(sf::st_geometry_type(connections) != "LINESTRING"), FALSE),
33-
expect_equal(length(unique(connections$id)), 2)
32+
# Tests
33+
expect_equal(any(sf::st_geometry_type(connections) != "LINESTRING"), FALSE)
34+
expect_equal(length(unique(connections$id)), 2)
35+
}
3436
)
3537

3638
# Route summary: "summary = FALSE"
37-
with_mock(
38-
"hereR:::.async_request" = function(url, rps) {
39+
with_mocked_bindings(
40+
.async_request = function(url, rps) {
3941
hereR:::mock$connection_response
4042
},
41-
connections <- connection(origin = poi[3:4, ], destination = poi[5:6, ], summary = TRUE),
43+
{
44+
connections <- connection(origin = poi[3:4, ], destination = poi[5:6, ], summary = TRUE)
4245

43-
# Tests
44-
expect_equal(any(sf::st_geometry_type(connections) != "MULTILINESTRING"), FALSE),
45-
expect_equal(length(unique(connections$id)), 2)
46+
# Tests
47+
expect_equal(any(sf::st_geometry_type(connections) != "MULTILINESTRING"), FALSE)
48+
expect_equal(length(unique(connections$id)), 2)
49+
}
4650
)
4751
})

tests/testthat/test-flow.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ test_that("flow works", {
2222
expect_is(flow(aoi = aoi[aoi$code == "LI", ], url_only = TRUE), "character")
2323

2424
# Test with API response mock
25-
with_mock(
26-
"hereR:::.async_request" = function(url, rps) {
25+
with_mocked_bindings(
26+
.async_request = function(url, rps) {
2727
hereR:::mock$flow_response
2828
},
29-
flows <- flow(aoi = aoi[aoi$code == "LI", ]),
29+
{
30+
flows <- flow(aoi = aoi[aoi$code == "LI", ])
3031

31-
# Tests
32-
expect_equal(class(flows), c("sf", "data.frame")),
33-
expect_equal(any(sf::st_geometry_type(flows) != "MULTILINESTRING"), FALSE)
32+
# Tests
33+
expect_equal(class(flows), c("sf", "data.frame"))
34+
expect_equal(any(sf::st_geometry_type(flows) != "MULTILINESTRING"), FALSE)
35+
}
3436
)
3537
})

tests/testthat/test-geocode.R

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,33 @@ test_that("geocode works", {
4040

4141

4242
# Test with API response mock
43-
with_mock(
44-
"hereR:::.async_request" = function(url, rps) {
43+
with_mocked_bindings(
44+
.async_request = function(url, rps) {
4545
hereR:::mock$geocode_response
4646
},
47-
geocoded <- geocode(address = poi$city),
47+
{
48+
geocoded <- geocode(address = poi$city)
4849

49-
# Tests
50-
expect_s3_class(geocoded, c("sf", "data.frame"), exact = TRUE),
51-
expect_true(all(sf::st_geometry_type(geocoded) == "POINT")),
52-
expect_equal(nrow(geocoded), length(poi$city))
50+
# Tests
51+
expect_s3_class(geocoded, c("sf", "data.frame"), exact = TRUE)
52+
expect_true(all(sf::st_geometry_type(geocoded) == "POINT"))
53+
expect_equal(nrow(geocoded), length(poi$city))
54+
}
5355
)
54-
with_mock(
55-
"hereR:::.async_request" = function(url, rps) {
56+
with_mocked_bindings(
57+
.async_request = function(url, rps) {
5658
hereR:::mock$geocode_response
5759
},
58-
geocoded <- geocode(address = poi$city, alternatives = TRUE, sf = FALSE),
60+
{
61+
geocoded <- geocode(address = poi$city, alternatives = TRUE, sf = FALSE)
5962

60-
# Tests
61-
expect_s3_class(geocoded, "data.frame", exact = TRUE),
62-
expect_gt(nrow(geocoded), length(poi$city)),
63-
expect_type(geocoded[["lat_position"]], "double"),
64-
expect_type(geocoded[["lng_position"]], "double"),
65-
expect_type(geocoded[["lat_access"]], "double"),
66-
expect_type(geocoded[["lng_access"]], "double")
63+
# Tests
64+
expect_s3_class(geocoded, "data.frame", exact = TRUE)
65+
expect_gt(nrow(geocoded), length(poi$city))
66+
expect_type(geocoded[["lat_position"]], "double")
67+
expect_type(geocoded[["lng_position"]], "double")
68+
expect_type(geocoded[["lat_access"]], "double")
69+
expect_type(geocoded[["lng_access"]], "double")
70+
}
6771
)
6872
})

tests/testthat/test-incident.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ test_that("incidents works", {
1616
), "character")
1717

1818
# Test with API response mock
19-
with_mock(
20-
"hereR:::.async_request" = function(url, rps) {
19+
with_mocked_bindings(
20+
.async_request = function(url, rps) {
2121
hereR:::mock$incident_response
2222
},
23-
incidents <- incident(aoi = aoi),
23+
{
24+
incidents <- incident(aoi = aoi)
2425

25-
# Tests
26-
expect_equal(class(incidents), c("sf", "data.frame")),
27-
expect_equal(any(sf::st_geometry_type(incidents) != "MULTILINESTRING"), FALSE)
26+
# Tests
27+
expect_equal(class(incidents), c("sf", "data.frame"))
28+
expect_equal(any(sf::st_geometry_type(incidents) != "MULTILINESTRING"), FALSE)
29+
}
2830
)
2931
})

tests/testthat/test-intermodal_route.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ test_that("intermodal_route works", {
1616
expect_error(intermodal_route(origin = poi, destination = poi, url_only = "not_a_bool"), "'url_only' must be a 'boolean' value.")
1717

1818
# Test with API response mock
19-
with_mock(
20-
"hereR:::.async_request" = function(url, rps) {
19+
with_mocked_bindings(
20+
.async_request = function(url, rps) {
2121
hereR:::mock$intermodal_route_response
2222
},
23-
intermodal_routes <- intermodal_route(origin = poi[1:2, ], destination = poi[3:4, ]),
23+
{
24+
intermodal_routes <- intermodal_route(origin = poi[1:2, ], destination = poi[3:4, ])
2425

25-
# Tests
26-
expect_s3_class(intermodal_routes, c("sf", "data.frame"), exact = TRUE),
27-
expect_true(all(sf::st_geometry_type(intermodal_routes) == "LINESTRING"))
26+
# Tests
27+
expect_s3_class(intermodal_routes, c("sf", "data.frame"), exact = TRUE)
28+
expect_true(all(sf::st_geometry_type(intermodal_routes) == "LINESTRING"))
29+
}
2830
)
2931
})

tests/testthat/test-isoline.R

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ test_that("isoline works", {
1818
expect_error(isoline(poi = poi, url_only = "not_a_bool"), "'url_only' must be a 'boolean' value.")
1919

2020
# Test with API response mock
21-
with_mock(
22-
"hereR:::.async_request" = function(url, rps) {
21+
with_mocked_bindings(
22+
.async_request = function(url, rps) {
2323
hereR:::mock$isoline_response
2424
},
25+
{
26+
# With and without aggregation
27+
isolines_aggr <- isoline(poi = poi, aggregate = TRUE)
28+
isolines_mult <- isoline(poi = poi, aggregate = FALSE)
2529

26-
# With and without aggregation
27-
isolines_aggr <- isoline(poi = poi, aggregate = TRUE),
28-
isolines_mult <- isoline(poi = poi, aggregate = FALSE),
29-
30-
# Tests
31-
expect_equal(any(sf::st_geometry_type(isolines_aggr) != "MULTIPOLYGON"), FALSE),
32-
expect_equal(any(sf::st_geometry_type(isolines_mult) != "POLYGON"), FALSE)
30+
# Tests
31+
expect_equal(any(sf::st_geometry_type(isolines_aggr) != "MULTIPOLYGON"), FALSE)
32+
expect_equal(any(sf::st_geometry_type(isolines_mult) != "POLYGON"), FALSE)
33+
}
3334
)
3435
})

tests/testthat/test-reverse_geocode.R

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,33 @@ test_that("reverse_geocode works", {
1919
expect_error(reverse_geocode(poi = poi, results = "-100"), "'results' must be of type 'numeric'.")
2020

2121
# Test with API response mock: sf
22-
with_mock(
23-
"hereR:::.async_request" = function(url, rps) {
22+
with_mocked_bindings(
23+
.async_request = function(url, rps) {
2424
hereR:::mock$reverse_geocode_response
2525
},
26-
reverse <- reverse_geocode(poi = poi, results = 3, sf = TRUE),
26+
{
27+
reverse <- reverse_geocode(poi = poi, results = 3, sf = TRUE)
2728

28-
# Tests
29-
expect_s3_class(reverse, c("sf", "data.frame"), exact = TRUE),
30-
expect_true(all(sf::st_geometry_type(reverse) == "POINT"))
29+
# Tests
30+
expect_s3_class(reverse, c("sf", "data.frame"), exact = TRUE)
31+
expect_true(all(sf::st_geometry_type(reverse) == "POINT"))
32+
}
3133
)
3234

3335
# Test with API response mock: data.frame
34-
with_mock(
35-
"hereR:::.async_request" = function(url, rps) {
36+
with_mocked_bindings(
37+
.async_request = function(url, rps) {
3638
hereR:::mock$reverse_geocode
3739
},
38-
reverse <- reverse_geocode(poi = poi, results = 3, sf = FALSE),
40+
{
41+
reverse <- reverse_geocode(poi = poi, results = 3, sf = FALSE)
3942

40-
# Tests
41-
expect_s3_class(reverse, "data.frame", exact = TRUE),
42-
expect_type(reverse[["lat_position"]], "double"),
43-
expect_type(reverse[["lng_position"]], "double"),
44-
expect_type(reverse[["lat_access"]], "double"),
45-
expect_type(reverse[["lng_access"]], "double")
43+
# Tests
44+
expect_s3_class(reverse, "data.frame", exact = TRUE)
45+
expect_type(reverse[["lat_position"]], "double")
46+
expect_type(reverse[["lng_position"]], "double")
47+
expect_type(reverse[["lat_access"]], "double")
48+
expect_type(reverse[["lng_access"]], "double")
49+
}
4650
)
4751
})

tests/testthat/test-route.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ test_that("route works", {
2525
expect_false(all(grepl(",tolls&tolls[summaries]=total&tolls[vignettes]=all", route(origin = poi, destination = poi, transport_mode = "bicycle", vignettes = FALSE, url_only = TRUE), fixed = TRUE)))
2626

2727
# Test with API response mock
28-
with_mock(
29-
"hereR:::.async_request" = function(url, rps) {
28+
with_mocked_bindings(
29+
.async_request = function(url, rps) {
3030
hereR:::mock$route_response
3131
},
32-
routes <- route(origin = poi[1:2, ], destination = poi[3:4, ]),
32+
{
33+
routes <- route(origin = poi[1:2, ], destination = poi[3:4, ])
3334

34-
# Tests
35-
expect_equal(any(sf::st_geometry_type(routes) != "LINESTRING"), FALSE),
36-
expect_equal(nrow(routes), nrow(poi[1:2, ]))
35+
# Tests
36+
expect_equal(any(sf::st_geometry_type(routes) != "LINESTRING"), FALSE)
37+
expect_equal(nrow(routes), nrow(poi[1:2, ]))
38+
}
3739
)
3840
})

0 commit comments

Comments
 (0)