Skip to content

Commit b3dae91

Browse files
committed
bug fix when posts had no fields/f: or sort/s:
1 parent d308be0 commit b3dae91

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

R/search-pv.R

+9-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ get_get_url <- function(query, base_url, arg_list) {
2727
base_url,
2828
"?q=", utils::URLencode(query, reserved = TRUE),
2929
"&f=", tojson_2(arg_list$fields),
30-
"&o=", tojson_2(arg_list$opts, auto_unbox = TRUE),
31-
"&s=", tojson_2(arg_list$sort, auto_unbox = TRUE)
30+
"&s=", tojson_2(arg_list$sort, auto_unbox = TRUE),
31+
"&o=", tojson_2(arg_list$opts, auto_unbox = TRUE)
3232
)
33+
3334
utils::URLencode(j)
3435
}
3536

@@ -39,11 +40,14 @@ get_post_body <- function(query, arg_list) {
3940
"{",
4041
'"q":', query, ",",
4142
'"f":', tojson_2(arg_list$fields), ",",
42-
'"o":', tojson_2(arg_list$opts, auto_unbox = TRUE), ",",
43-
'"s":', tojson_2(arg_list$sort, auto_unbox = TRUE),
43+
'"s":', tojson_2(arg_list$sort, auto_unbox = TRUE), ",",
44+
'"o":', tojson_2(arg_list$opts, auto_unbox = TRUE),
4445
"}"
4546
)
46-
gsub('(,"[fs]":)([,}])', paste0("\\1", "{}", "\\2"), body)
47+
# The API can now act weirdly if we pass f:{},s:{} as we did in the past.
48+
# (Weirdly in that the post results may not equal the get results or posts error out)
49+
# Now we'd remove "f":, and "s":, We're guaranteed to have q: and at least "size":1000 as o:
50+
gsub('("[fs]":,)', "", body)
4751
}
4852

4953
#' @noRd

tests/testthat/test-search-pv.R

+42
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,45 @@ test_that("individual fields are still broken", {
297297
# make it noticeable that all is not right with the API
298298
skip("Skip for API bugs") # TODO: remove when the API is fixed
299299
})
300+
301+
# Make sure gets and posts return the same data.
302+
# Posts had issues that went undetected for a while using the new API
303+
# (odd results with posts when either no fields or sort was passed
304+
# see get_post_body in search-pv.R)
305+
306+
test_that("posts and gets return the same data", {
307+
bad_eps <- c(
308+
"cpc_subclasses",
309+
"location" # Error: Invalid field: location_latitude
310+
, "uspc_subclasse" # Error: Internal Server Error
311+
, "uspc_mainclass" # Error: Internal Server Error
312+
, "wipo" # Error: Internal Server Error
313+
, "claim" # Error: Invalid field: claim_dependent
314+
, "draw_desc_text" # Error: Invalid field: description_sequence
315+
, "cpc_subclass" # 404? check the test query
316+
, "uspc_subclass" # 404
317+
)
318+
319+
good_eps <- eps[!eps %in% bad_eps]
320+
321+
z <- lapply(good_eps, function(x) {
322+
print(x)
323+
get_res <- search_pv(
324+
query = TEST_QUERIES[[x]],
325+
endpoint = x,
326+
method = "GET"
327+
)
328+
329+
g <- unnest_pv_data(get_res$data)
330+
331+
post_res <- search_pv(
332+
query = TEST_QUERIES[[x]],
333+
endpoint = x,
334+
method = "POST"
335+
)
336+
337+
p <- unnest_pv_data(post_res$data)
338+
339+
expect_equal(g, p)
340+
})
341+
})

0 commit comments

Comments
 (0)