|
| 1 | + |
| 2 | +# Tests from the other files in this directory that are masking API errors |
| 3 | +# This file will be submitted to the API team |
| 4 | + |
| 5 | +eps <- (get_endpoints()) |
| 6 | + |
| 7 | +# from test-api-returns.R |
| 8 | +test_that("API returns all requested groups", { |
| 9 | + skip_on_cran() |
| 10 | + skip_on_ci() |
| 11 | + |
| 12 | + # endpoints where an error is thrown if we request all the fields listed |
| 13 | + # in the OpenAPI object |
| 14 | + |
| 15 | + bad_eps <- c("cpc_subclasses" |
| 16 | + , "locations" # Error: Invalid field: location_latitude |
| 17 | + , "uspc_subclasses" # Error: Internal Server Error |
| 18 | + , "uspc_mainclasses" # Error: Internal Server Error |
| 19 | + , "wipo" # Error: Internal Server Error |
| 20 | + , "claims" # Error: Invalid field: claim_dependent |
| 21 | + , "draw_desc_texts" # Error: Invalid field: description_sequence |
| 22 | + ) |
| 23 | + |
| 24 | + # this will fail when the api is fixed |
| 25 | + z <- lapply(bad_eps, function(x) { |
| 26 | + print(x) |
| 27 | + expect_error( |
| 28 | + j <- search_pv(query = TEST_QUERIES[[x]], endpoint = x, fields = get_fields(x)) |
| 29 | + ) |
| 30 | + }) |
| 31 | + |
| 32 | + # endpoints where we request all fields but not all the expected groups |
| 33 | + # are returned |
| 34 | + mismatched_returns <- c( |
| 35 | + "patents", |
| 36 | + "publications" |
| 37 | + ) |
| 38 | + |
| 39 | + # this will fail when the API is fixed |
| 40 | + z <- lapply(mismatched_returns, function(x) { |
| 41 | + print(x) |
| 42 | + res <- search_pv( |
| 43 | + query = TEST_QUERIES[[x]], |
| 44 | + endpoint = x, |
| 45 | + fields = get_fields(x) |
| 46 | + ) |
| 47 | + |
| 48 | + dl <- unnest_pv_data(res$data) |
| 49 | + |
| 50 | + actual_groups <- names(dl) |
| 51 | + expected_groups <- unique(fieldsdf[fieldsdf$endpoint == x, "group"]) |
| 52 | + |
| 53 | + # we now need to unnest the endpoints for the comparison to work |
| 54 | + expected_groups <- sub("patent/","",expected_groups) |
| 55 | + expected_groups <- sub("publication/","",expected_groups) |
| 56 | + |
| 57 | + # better way to do this? want to expect_set_not_equal |
| 58 | + expect_false(isTRUE(all.equal(length(actual_groups), length(expected_groups)))) |
| 59 | + |
| 60 | + }) |
| 61 | + |
| 62 | +# test-fetch-each-field.R |
| 63 | +test_that("each field in fieldsdf can be retrieved", { |
| 64 | + # Iterate through fieldsdf, requesting one field at a time to see if the field |
| 65 | + # really can be retrieved. What fields work and don't work is constantly changing |
| 66 | + # as the new version of the api is being developed |
| 67 | + |
| 68 | + # maybe have the return be the fields that failed? "endpoint" "field" |
| 69 | + count <- 0 |
| 70 | + |
| 71 | + dev_null <- sapply(get_endpoints(), function(endpoint) { |
| 72 | + fields <- fieldsdf[fieldsdf$endpoint == endpoint, c("field")] |
| 73 | + |
| 74 | + # here we want to remove nested fields like assignees.assignee_id |
| 75 | + fields <- fields[!fields %in% fields[grepl("\\.", fields)]] |
| 76 | + |
| 77 | + # should also test that there are unique values, some fields come back all NULLS |
| 78 | + |
| 79 | + lapply(fields, function(field) { |
| 80 | + tryCatch( |
| 81 | + expr = { |
| 82 | + # try adding the primary key to fields to see if that stops the 500s- helped some but not all |
| 83 | + # pk <- get_ok_pk(endpoint) |
| 84 | + pv_out <- search_pv(query = TEST_QUERIES[[endpoint]], endpoint = endpoint, fields = c(field)) |
| 85 | + |
| 86 | + # see if the field actually came back - a fair amount don't come back |
| 87 | + # make sure pv_out$query_results$total_hits >= 1 first |
| 88 | + if(pv_out$query_results$total_hits == 0) |
| 89 | + print(paste(endpoint,"zero hits")) |
| 90 | + else |
| 91 | + { |
| 92 | + found <- FALSE |
| 93 | + if(! field %in% colnames(pv_out$data[[1]])) { |
| 94 | + # check for the _id thing, ex requested assignee_id but got assignee back |
| 95 | + |
| 96 | + if(grepl("_id", field)) { |
| 97 | + idless <- sub("_id","",field) |
| 98 | + |
| 99 | + found <- idless %in% colnames(pv_out$data[[1]]) |
| 100 | + if(found) |
| 101 | + print(paste("id dance on ", endpoint, field)) |
| 102 | + } |
| 103 | + if(!found) |
| 104 | + print(paste(endpoint, field,"not returned")) |
| 105 | + } |
| 106 | + } |
| 107 | + NA |
| 108 | + }, |
| 109 | + error = function(e) { |
| 110 | + print(paste("error",endpoint, field)) |
| 111 | + print(e) |
| 112 | + count <<- count + 1 |
| 113 | + c(endpoint, field) |
| 114 | + } |
| 115 | + ) |
| 116 | + }) |
| 117 | + }) |
| 118 | + |
| 119 | + expect_true(count > 0) # would fail when the API doesn't throw errors |
| 120 | +}) |
| 121 | + |
| 122 | +# from test-search-pv.R |
| 123 | +test_that("We can call all the legitimate HATEOAS endpoints", { |
| 124 | + skip_on_cran() |
| 125 | + |
| 126 | + # these currently throw Error: Internal Server Error |
| 127 | + broken_single_item_queries <- c( |
| 128 | + "cpc_subclass/A01B/", |
| 129 | + "uspc_mainclass/30/", |
| 130 | + "uspc_subclass/30:100/", |
| 131 | + "wipo/1/" |
| 132 | + ) |
| 133 | + |
| 134 | + |
| 135 | + # TODO: remove when this is fixed |
| 136 | + # we'll know the api is fixed when this fails |
| 137 | + dev_null <- lapply(broken_single_item_queries, function(q) { |
| 138 | + expect_error( |
| 139 | + j <- retrieve_linked_data(add_base_url(q)) |
| 140 | + ) |
| 141 | + }) |
| 142 | + |
| 143 | + # We'll make a call to get an inventor and assignee HATEOAS link |
| 144 | + # in case their ids are not persistent |
| 145 | + # new weirdness: we request inventor_id and assignee_id but the |
| 146 | + # fields come back without the _id |
| 147 | + res <- search_pv('{"patent_id":"10000000"}', |
| 148 | + fields = c("inventors.inventor_id", "assignees.assignee_id") |
| 149 | + ) |
| 150 | + |
| 151 | + assignee <- retrieve_linked_data(res$data$patents$assignees[[1]]$assignee) |
| 152 | + expect_true(assignee$query_results$total_hits == 1) |
| 153 | + |
| 154 | + inventor <- retrieve_linked_data(res$data$patents$inventors[[1]]$inventor) |
| 155 | + expect_true(inventor$query_results$total_hits == 1) |
| 156 | + |
| 157 | +}) |
| 158 | + |
| 159 | +# from test-search-pv.R |
| 160 | +test_that("individual fields are still broken", { |
| 161 | + skip_on_cran() |
| 162 | + |
| 163 | + # Sample fields that cause 500 errors when requested by themselves. |
| 164 | + # Some don't throw errors when included in get_fields() but they do if |
| 165 | + # they are the only field requested. Other individual fields at these |
| 166 | + # same endpoints throw errors. Check fields again when these fail. |
| 167 | + sample_bad_fields <- c( |
| 168 | + "assignee_organization" = "assignees", |
| 169 | + "inventor_lastknown_longitude" = "inventors", |
| 170 | + "inventor_gender_code" = "inventors", |
| 171 | + "location_name" = "locations", |
| 172 | + "attorney_name_last" = "patent/attorneys", |
| 173 | + "citation_country" = "patent/foreign_citations", |
| 174 | + "ipc_id" = "ipcs" |
| 175 | + ) |
| 176 | + |
| 177 | + dev_null <- lapply(names(sample_bad_fields), function(x) { |
| 178 | + endpoint <- sample_bad_fields[[x]] |
| 179 | + expect_error( |
| 180 | + out <- search_pv(query = TEST_QUERIES[[endpoint]], endpoint = endpoint, fields = c(x)) |
| 181 | + ) |
| 182 | + }) |
| 183 | +}) |
| 184 | + |
| 185 | +}) |
0 commit comments