fix assignment, clean up tests, use !!! operator (#13075)

This commit is contained in:
William Cheng 2022-08-05 14:09:24 +08:00 committed by GitHub
parent a4a89caf73
commit 247574aa0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 53 deletions

View File

@ -189,34 +189,29 @@ ApiClient <- R6::R6Class(
#' @export
Execute = function(url, method, query_params, header_params, form_params, file_params,
accepts, content_types, body, stream_callback = NULL, ...) {
# set the URL
req <- request(url)
## add headers and default headers
if (!is.null(header_params) && length(header_params) != 0) {
req <- req %>% req_headers(!!!header_params)
}
if (!is.null(self$default_headers) && length(self$default_headers) != 0) {
req <- req %>% req_headers(!!!self$default_headers)
}
## add headers
req <- req %>% req_headers(!!!header_params)
## add default headers
req <- req %>% req_headers(!!!self$default_headers)
# set HTTP accept header
accept = self$select_header(accepts)
accept <- self$select_header(accepts)
if (!is.null(accept)) {
req <- req %>% req_headers("Accept" = accept)
}
# set HTTP content-type header
content_type = self$select_header(content_types)
content_type <- self$select_header(content_types)
if (!is.null(content_type)) {
req <- req %>% req_headers("Content-Type" = content_type)
}
## add query parameters
if (length(query_params) != 0) {
for (query_param in names(query_params)) {
req <- req %>% req_url_query(query_param = query_params[query_param])
}
}
req <- req %>% req_url_query(!!!query_params)
# has file upload?
if (!is.null(file_params) && length(file_params) != 0) {

View File

@ -194,34 +194,29 @@ ApiClient <- R6::R6Class(
#' @export
Execute = function(url, method, query_params, header_params, form_params, file_params,
accepts, content_types, body, stream_callback = NULL, ...) {
# set the URL
req <- request(url)
## add headers and default headers
if (!is.null(header_params) && length(header_params) != 0) {
req <- req %>% req_headers(!!!header_params)
}
if (!is.null(self$default_headers) && length(self$default_headers) != 0) {
req <- req %>% req_headers(!!!self$default_headers)
}
## add headers
req <- req %>% req_headers(!!!header_params)
## add default headers
req <- req %>% req_headers(!!!self$default_headers)
# set HTTP accept header
accept = self$select_header(accepts)
accept <- self$select_header(accepts)
if (!is.null(accept)) {
req <- req %>% req_headers("Accept" = accept)
}
# set HTTP content-type header
content_type = self$select_header(content_types)
content_type <- self$select_header(content_types)
if (!is.null(content_type)) {
req <- req %>% req_headers("Content-Type" = content_type)
}
## add query parameters
if (length(query_params) != 0) {
for (query_param in names(query_params)) {
req <- req %>% req_url_query(query_param = query_params[query_param])
}
}
req <- req %>% req_url_query(!!!query_params)
# has file upload?
if (!is.null(file_params) && length(file_params) != 0) {

View File

@ -1,5 +1,6 @@
context("basic functionality")
## create a new pet and add to petstore server
pet_api <- PetApi$new()
pet_id <- 123321
pet <- Pet$new("name_test",
@ -15,12 +16,12 @@ pet_api$api_client$username <- "username123"
pet_api$api_client$password <- "password123"
result <- pet_api$add_pet(pet)
test_that("add_pet", {
test_that("Test toJSON toJSONString fromJSON fromJSONString", {
# test pet
expect_equal(pet_id, 123321)
expect_equal(pet$toJSONString(), '{"id":123321,"category":{"id":450,"name":"test_cat"},"name":"name_test","photoUrls":["photo_test","second test"],"tags":[{"id":123,"name":"tag_test"},{"id":456,"name":"unknown"}],"status":"available"}')
})
test_that("Test toJSON toJSONString fromJSON fromJSONString", {
# tests for other pet objects
pet0 <- Pet$new()
jsonpet <- pet0$toJSON()
pet2 <- pet0$fromJSON(
@ -350,25 +351,3 @@ test_that("Tests anyOf", {
expect_error(pig$validateJSON('{}'), 'No match found when deserializing the payload into AnyOfPig with anyOf schemas BasquePig, DanishPig. Details: The JSON input ` \\{\\} ` is invalid for BasquePig: the required field `className` is missing\\., The JSON input ` \\{\\} ` is invalid for DanishPig: the required field `className` is missing\\.')
})
#test_that("GetPetById", {
# pet.id <- pet.id
# pet <- Pet$new(pet.id, NULL, "name_test2",
# list("photo_test2", "second test2"),
# NULL, NULL)
# result <-pet_api$AddPet(pet)
#
# response <- pet_api$GetPetById(pet.id)
#
# expect_equal(response$id, pet.id)
# expect_equal(response$name, "name_test2")
# #expect_equal(response$category, Category$new(450,"test_cat"))
# expect_equal(response$photoUrls, list("photo_test2", "second test2"))
# expect_equal(response$status, NULL)
# #expect_equal(response$tags, list(Tag$new(123, "tag_test"), Tag$new(456, "unknown")))
#})
#test_that("updatePetWithForm", {
# response <- pet_api$updatePetWithForm(pet_id, "test", "sold")
# expect_equal(response, "Pet updated")
#})