Skip to content

Commit 152fe14

Browse files
committed
request fields with _id
1 parent ca8d014 commit 152fe14

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

R/search-pv.R

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,32 @@ search_pv <- function(query,
346346
return(first_res) # else we iterate through pages below
347347
}
348348

349-
unique_sort_keys <- rep("asc", length(pk))
350-
names(unique_sort_keys) <- pk
351-
arg_list$sort <- unique_sort_keys
349+
if(is.null(fields))
350+
{
351+
# request the default fields returned by the first call, otherwise adding the pk below
352+
# will just return that/thoses fields.
353+
fields <- names(first_res$data[[1]])
354+
}
355+
356+
# add pk here instead - pk fields not added to first_res
357+
# We need pk to be in the result for all_pages to work with ease, hence adding
358+
# it below
359+
pk <- get_ok_pk(endpoint)
360+
361+
fields <- unique(c(pk, fields))
362+
abbreviated_fields <- sub_grp_names_for_fields(endpoint, fields)
363+
arg_list$fields <- abbreviated_fields
364+
365+
required_paging_keys <- rep("asc", length(pk))
366+
names(required_paging_keys) <- pk
367+
368+
# append required_paging_keys to user's sort if needed when supplied
369+
if(is.null(sort)) {
370+
arg_list$sort <- required_paging_keys
371+
} else {
372+
non_duplicate_keys <- setdiff(names(required_paging_keys), names(sort))
373+
arg_list$sort <- c(sort, required_paging_keys[non_duplicate_keys])
374+
}
352375

353376
paged_data <- request_apply(
354377
first_res, method, query, base_url, arg_list, api_key, ...

tests/testthat/test-api-bugs.R

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@ test_that("Queries returning non-utility patents page well", {
1818

1919
all_types_res <- search_pv(only_utility_qry, all_pages = TRUE)
2020
expect_true(
21-
nrow(all_types_res$data$patents) != all_types_res$query_results$total_hits
21+
nrow(all_types_res$data$patents) == all_types_res$query_results$total_hits
2222
)
2323
})
2424

25-
test_that("inventors.inventor and assignees.assignee aren't returned
25+
test_that("inventors.inventor_id and assignees.assignee_id are returned
2626
from patent endpoint when specified exactly", {
2727
skip_on_cran()
2828
query <- TEST_QUERIES[["patent"]]
2929
# Should return inventors and assignee list cols
30-
wrong_res <- search_pv(
31-
query, fields = c("inventors.inventor", "assignees.assignee")
30+
res <- search_pv(
31+
query, fields = c("inventors.inventor_id", "assignees.assignee_id")
3232
)
33-
# ...But they don't
34-
expect_equal(colnames(wrong_res$data$patents), "patent_id")
33+
# ...But they do, just without the _id
34+
expect_no_error(res$data$patents$inventors[[1]]$inventor)
35+
expect_no_error(res$data$patents$assignees[[1]]$assignee)
3536

3637
# Good result when not specifying nested-level fields explicitly
3738
good_res <- search_pv(

0 commit comments

Comments
 (0)