Skip to content

Commit 509f60b

Browse files
authored
Merge pull request #48 from BiodiversiteQuebec/refactor_req_tests
Migrate to newapi V2
2 parents 2e870eb + 08b5569 commit 509f60b

File tree

3 files changed

+38
-71
lines changed

3 files changed

+38
-71
lines changed

R/coleo_api_utils.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ server <- function(){
1717
base <- function() {
1818
path <- Sys.getenv("COLEOAPI_PATH")
1919
if (path==''){
20-
path="/newapi/v1"
20+
path="/newapi/v2"
2121
}
2222
return(path)
2323
}

tests/testthat/test-coleo_api_utils.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ test_that("api utils do the right thing", {
33
setup_call <- coleo_begin_req("api")
44
expect_s3_class(setup_call, class = "httr2_request")
55

6-
expect_equal(setup_call$url, "https://coleo.biodiversite-quebec.ca/newapi/v1")
6+
expect_equal(setup_call$url, "https://coleo.biodiversite-quebec.ca/newapi/v2")
77

88
})
99

tests/testthat/test-coleo_request.R

+36-69
Original file line numberDiff line numberDiff line change
@@ -4,99 +4,66 @@ without_internet({
44
test_that("coleo_request makes a good request", {
55

66
expect_GET(coleo_request_general(endpoint = "cells", response_as_df = FALSE, schema = "public", 'cell_code' = "eq.foo"),
7-
url = "https://coleo.biodiversite-quebec.ca/newapi/v1/cells?cell_code=eq.foo")
7+
url = "https://coleo.biodiversite-quebec.ca/newapi/v2/cells?cell_code=eq.foo")
88
})
99
})
1010

1111

12-
with_mock_dir("coleo_request", {
13-
test_that("no answer for a nonsense code", {
14-
nonsense_request <- coleo_request_general(endpoint = "cells", response_as_df = FALSE, schema = "public", 'cell_code' = "eq.foo")
15-
16-
expect_equal(httr2::resp_body_json(nonsense_request), list())
17-
})
12+
test_that("no answer for a nonsense code", {
13+
nonsense_request <- coleo_request_general(endpoint = "cells", response_as_df = FALSE, schema = "public", 'cell_code' = "eq.foo")
14+
expect_equal(httr2::resp_body_json(nonsense_request), list())
1815
})
1916

2017
# query for a real site
21-
22-
with_mock_dir("coleo_request_real", {
23-
test_that("returns answer for a real code",{
24-
real_site <- coleo_request_general(endpoint = "cells", schema = "public", "cell_code" = "eq.139_87")
25-
26-
answer <- httr2::resp_body_json(real_site)[[1]]
27-
28-
expect_equal(length(answer), 6)
29-
30-
})
18+
test_that("returns answer for a real code",{
19+
real_site <- coleo_request_general(endpoint = "cells", schema = "public", "cell_code" = "eq.139_87")
20+
answer <- httr2::resp_body_json(real_site)[[1]]
21+
expect_equal(length(answer), 6)
3122
})
3223

3324

34-
with_mock_dir("coleo_request_real", {
35-
real_cell <- coleo_request_general(endpoint = "cells", schema = "public", "cell_code" = "eq.139_87")
36-
real_cell_df <- coleo_request_general(endpoint = "cells", schema = "public", "cell_code" = "eq.139_87", response_as_df = TRUE)
37-
38-
resp_body <- httr2::resp_body_json(real_cell)[[1]]
39-
40-
41-
test_that("response is in expected format", {
42-
expect_equal(length(resp_body), 6)
43-
44-
# can pluck one id successfully from site request
45-
expect_equal(coleo_extract_id(real_cell), 161)
46-
47-
expect_type(real_cell_df, "list")
48-
49-
})
50-
25+
real_cell <- coleo_request_general(endpoint = "cells", schema = "public", "cell_code" = "eq.139_87")
26+
real_cell_df <- coleo_request_general(endpoint = "cells", schema = "public", "cell_code" = "eq.139_87", response_as_df = TRUE)
27+
resp_body <- httr2::resp_body_json(real_cell)[[1]]
28+
test_that("response is in expected format", {
29+
expect_equal(length(resp_body), 6)
30+
# can pluck one id successfully from site request
31+
expect_equal(coleo_extract_id(real_cell), 161)
32+
expect_type(real_cell_df, "list")
5133
})
5234

5335
test_that("errors for bad endpoint", {
5436
expect_error(coleo_request_general(endpoint = "not_an_endpoint"))
5537
})
5638

57-
with_mock_dir("site id download and extraction", {
58-
59-
real_site <- coleo_request_general(endpoint = "sites", schema = "public", "site_code" = "eq.139_87_F01")
60-
61-
test_that("request_general works for a valid site", {
62-
63-
64-
resp_body <- httr2::resp_body_json(real_site)[[1]]
65-
66-
# should be 11 columns of info for this site
67-
expect_equal(length(names(resp_body)), 11)
68-
69-
})
70-
71-
test_that("can coleo_pluck_id correctly", {
72-
expect_equal(coleo_extract_id(real_site), 94)
73-
})
74-
75-
})
76-
77-
78-
with_mock_dir("Site processing works correctly", {
79-
80-
resp_df <- coleo_request_general(endpoint = "sites", response_as_df = TRUE, schema = "public", "site_code" = "eq.137_107_H02")
81-
82-
test_that("resp is easily processed", {
83-
# This processing step returns everything in one row
84-
expect_equal(nrow(resp_df), 1)
85-
# things should be unnested now; here is one example
86-
expect_equal(length(resp_df$id), 1)
87-
# coluns should be of the right class
88-
expect_type(resp_df$type, "character")
89-
})
90-
39+
# Site id download and extraction
40+
real_site <- coleo_request_general(endpoint = "sites", schema = "public", "site_code" = "eq.139_87_F01")
41+
test_that("request_general works for a valid site", {
42+
resp_body <- httr2::resp_body_json(real_site)[[1]]
43+
# should be 11 columns of info for this site
44+
expect_equal(length(names(resp_body)), 12)
45+
})
46+
test_that("can coleo_pluck_id correctly", {
47+
expect_equal(coleo_extract_id(real_site), 94)
48+
})
9149

50+
# Site processing works correctly
51+
resp_df <- coleo_request_general(endpoint = "sites", response_as_df = TRUE, schema = "public", "site_code" = "eq.137_107_H02")
52+
test_that("resp is easily processed", {
53+
# This processing step returns everything in one row
54+
expect_equal(nrow(resp_df), 1)
55+
# things should be unnested now; here is one example
56+
expect_equal(length(resp_df$id), 1)
57+
# coluns should be of the right class
58+
expect_type(resp_df$type, "character")
9259
})
9360

9461

9562
test_that("coleo_request_data returns expected output", {
9663
# Test case 1: survey_type = 'vegetation', view = 'short'
9764
result1 <- coleo_request_data(survey_type = 'vegetation', view = 'short')
9865
expect_is(result1, "tbl_df")
99-
expect_equal(ncol(result1), 18) # Assuming there are 18 columns of data for vegetation inventory
66+
expect_equal(ncol(result1), 20) # Assuming there are 18 columns of data for vegetation inventory
10067

10168
# Test case 2: survey_type = 'acoustique_anoures', view = 'long'
10269
result2 <- coleo_request_data(survey_type = 'acoustique_anoures', view = 'long')

0 commit comments

Comments
 (0)