[R] Enum R6Class Support, closes #3367 (#5728)

* wip: fix documentation tags

* update model.mustache

* update template for documentation

* update mustache templates

* update mustache templates

* run ./bin/r-petstore and update R pkg docs

* use loadNamespace instead of package:pkgName string

* update R package code

* wip: enum

* update enum function

* use httr::content to unwrap response

* update enum table and functions

* include simplifyVector and auto_unbox arguments

* remove mapping and return strings

* use triple stash values and update enum class methods

* remove extra comma

* minor: formatting

* update and run ./bin/r-petstore.sh

* move helper for template

- export tag does not coincide with original model function
- move helper down so it does

* update tests based on generator

* Revert "update tests based on generator"

This reverts commit b6314c8927a8411561577c0dfd100018947de42e.

* Update tests with engine

- preserve test_petstore.R

* restore apiResponse

* restore simplifyVector to TRUE value

* update api.mustache templates

* fix and add tests to package

Co-authored-by: William Cheng <wing328hk@gmail.com>
This commit is contained in:
Marcel Ramos 2020-07-01 03:48:09 -04:00 committed by GitHub
parent fa72c63b62
commit 8e95298653
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 254 additions and 111 deletions

View File

@ -271,7 +271,9 @@
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) { if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
{{#returnType}} {{#returnType}}
{{#isPrimitiveType}} {{#isPrimitiveType}}
content <- httr::content(resp, "text", encoding = "UTF-8") content <- httr::content(
resp, "text", encoding = "UTF-8", simplifyVector = FALSE
)
ApiResponse$new(content,resp) ApiResponse$new(content,resp)
{{/isPrimitiveType}} {{/isPrimitiveType}}
{{^isPrimitiveType}} {{^isPrimitiveType}}

View File

@ -166,7 +166,9 @@ ApiClient <- R6::R6Class(
else if (exists(returnType, pkgEnv) && !(c(returnType) %in% primitiveTypes)) { else if (exists(returnType, pkgEnv) && !(c(returnType) %in% primitiveTypes)) {
returnType <- get(returnType, envir = as.environment(pkgEnv)) returnType <- get(returnType, envir = as.environment(pkgEnv))
returnObj <- returnType$new() returnObj <- returnType$new()
returnObj$fromJSON(jsonlite::toJSON(obj, digits = NA)) returnObj$fromJSON(
jsonlite::toJSON(obj, digits = NA, auto_unbox = TRUE)
)
} }
# To handle primitive type # To handle primitive type

View File

@ -17,7 +17,7 @@ test_that("{{{operationId}}}", {
# {{notes}} # {{notes}}
{{/notes}} {{/notes}}
{{#allParams}} {{#allParams}}
# @param {{{dataType}}} {{{paramName}}} {{{description}}} {{^required}} (optional){{/required}} # @param {{{paramName}}} {{{dataType}}} {{{description}}}{{^required}} (optional){{/required}}
{{/allParams}} {{/allParams}}
# @return [{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}] # @return [{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}]

View File

@ -3,15 +3,21 @@
{{>partial_header}} {{>partial_header}}
#' @docType class #' @docType class
#' @title {{classname}} #' @title {{classname}}
#'
#' @description {{classname}} Class #' @description {{classname}} Class
#'
#' @format An \code{R6Class} generator object #' @format An \code{R6Class} generator object
#'
{{#vars}} {{#vars}}
#' @field {{baseName}} {{title}} {{#isContainer}}{{#isListContainer}}list( {{/isListContainer}}{{#isMapContainer}}named list( {{/isMapContainer}}{{/isContainer}}{{^isPrimitiveType}}\link{{=<% %>=}}{<%/isPrimitiveType%><%={{ }}=%>{{#isContainer}}{{#items}}{{dataType}}{{/items}}{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}{{=<% %>=}}<%^isPrimitiveType%>}<%={{ }}=%>{{/isPrimitiveType}}{{#isContainer}}{{#isListContainer}} ){{/isListContainer}}{{#isMapContainer}} ){{/isMapContainer}}{{/isContainer}} {{^required}}[optional]{{/required}} #' @field {{baseName}} {{title}} {{#isContainer}}{{#isListContainer}}list( {{/isListContainer}}{{#isMapContainer}}named list( {{/isMapContainer}}{{/isContainer}}{{^isPrimitiveType}}\link{{=<% %>=}}{<%/isPrimitiveType%><%={{ }}=%>{{#isContainer}}{{#items}}{{dataType}}{{/items}}{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}{{=<% %>=}}<%^isPrimitiveType%>}<%={{ }}=%>{{/isPrimitiveType}}{{#isContainer}}{{#isListContainer}} ){{/isListContainer}}{{#isMapContainer}} ){{/isMapContainer}}{{/isContainer}} {{^required}}[optional]{{/required}}
#' #'
{{/vars}} {{/vars}}
#'
#' @importFrom R6 R6Class #' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON #' @importFrom jsonlite fromJSON toJSON
{{#isEnum}}
{{>modelEnum}}
{{/isEnum}}
{{^isEnum}}
#' @export #' @export
{{classname}} <- R6::R6Class( {{classname}} <- R6::R6Class(
'{{classname}}', '{{classname}}',
@ -19,7 +25,9 @@
{{#vars}} {{#vars}}
`{{{baseName}}}` = NULL, `{{{baseName}}}` = NULL,
{{/vars}} {{/vars}}
initialize = function({{#requiredVars}}`{{baseName}}`{{#hasMore}}, {{/hasMore}}{{/requiredVars}}{{#optionalVars}}{{#-first}}{{#requiredVars.0}}, {{/requiredVars.0}}{{/-first}}`{{baseName}}`={{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}NULL{{/defaultValue}}{{^-last}}, {{/-last}}{{/optionalVars}}, ...){ initialize = function(
{{#requiredVars}}`{{baseName}}`, {{/requiredVars}}{{#optionalVars}}`{{baseName}}`={{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}NULL{{/defaultValue}}, {{/optionalVars}}...
) {
local.optional.var <- list(...) local.optional.var <- list(...)
{{#requiredVars}} {{#requiredVars}}
if (!missing(`{{baseName}}`)) { if (!missing(`{{baseName}}`)) {
@ -159,6 +167,7 @@
{{/isContainer}} {{/isContainer}}
} }
{{/vars}} {{/vars}}
self
}, },
toJSONString = function() { toJSONString = function() {
jsoncontent <- c( jsoncontent <- c(
@ -241,5 +250,7 @@
} }
) )
) )
{{/isEnum}}
{{/model}} {{/model}}
{{/models}} {{/models}}

View File

@ -0,0 +1,48 @@
#' @export
{{#allowableValues}}
{{classname}} <- R6::R6Class(
"{{classname}}",
public = list(
initialize = function(...) {
local.optional.var <- list(...)
val <- unlist(local.optional.var)
enumvec <- .parse_{{name}}()
stopifnot(length(val) == 1L)
if (!val %in% enumvec)
stop("Use one of the valid values: ",
paste0(enumvec, collapse = ", "))
private$value <- val
},
toJSON = function() {
jsonlite::toJSON(private$value, auto_unbox = TRUE)
},
fromJSON = function({{classname}}Json) {
private$value <- jsonlite::fromJSON({{classname}}Json,
simplifyVector = FALSE)
self
},
toJSONString = function() {
as.character(jsonlite::toJSON(private$value,
auto_unbox = TRUE))
},
fromJSONString = function({{classname}}Json) {
private$value <- jsonlite::fromJSON({{classname}}Json,
simplifyVector = FALSE)
self
}
),
private = list(
value = NULL
)
)
# add to utils.R
.parse_{{name}} <- function(vals) {
res <- gsub("^\\[|\\]$", "",
"{{{values}}}"
)
unlist(strsplit(res, ", "))
}
{{/allowableValues}}

View File

@ -164,7 +164,9 @@ ApiClient <- R6::R6Class(
else if (exists(returnType, pkgEnv) && !(c(returnType) %in% primitiveTypes)) { else if (exists(returnType, pkgEnv) && !(c(returnType) %in% primitiveTypes)) {
returnType <- get(returnType, envir = as.environment(pkgEnv)) returnType <- get(returnType, envir = as.environment(pkgEnv))
returnObj <- returnType$new() returnObj <- returnType$new()
returnObj$fromJSON(jsonlite::toJSON(obj, digits = NA)) returnObj$fromJSON(
jsonlite::toJSON(obj, digits = NA, auto_unbox = TRUE)
)
} }
# To handle primitive type # To handle primitive type

View File

@ -8,13 +8,15 @@
#' @docType class #' @docType class
#' @title Category #' @title Category
#'
#' @description Category Class #' @description Category Class
#'
#' @format An \code{R6Class} generator object #' @format An \code{R6Class} generator object
#'
#' @field id integer [optional] #' @field id integer [optional]
#' #'
#' @field name character [optional] #' @field name character [optional]
#' #'
#'
#' @importFrom R6 R6Class #' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON #' @importFrom jsonlite fromJSON toJSON
#' @export #' @export
@ -23,7 +25,9 @@ Category <- R6::R6Class(
public = list( public = list(
`id` = NULL, `id` = NULL,
`name` = NULL, `name` = NULL,
initialize = function(`id`=NULL, `name`=NULL, ...){ initialize = function(
`id`=NULL, `name`=NULL, ...
) {
local.optional.var <- list(...) local.optional.var <- list(...)
if (!is.null(`id`)) { if (!is.null(`id`)) {
stopifnot(is.numeric(`id`), length(`id`) == 1) stopifnot(is.numeric(`id`), length(`id`) == 1)
@ -55,6 +59,7 @@ Category <- R6::R6Class(
if (!is.null(CategoryObject$`name`)) { if (!is.null(CategoryObject$`name`)) {
self$`name` <- CategoryObject$`name` self$`name` <- CategoryObject$`name`
} }
self
}, },
toJSONString = function() { toJSONString = function() {
jsoncontent <- c( jsoncontent <- c(
@ -84,3 +89,4 @@ Category <- R6::R6Class(
} }
) )
) )

View File

@ -8,15 +8,17 @@
#' @docType class #' @docType class
#' @title ModelApiResponse #' @title ModelApiResponse
#'
#' @description ModelApiResponse Class #' @description ModelApiResponse Class
#'
#' @format An \code{R6Class} generator object #' @format An \code{R6Class} generator object
#'
#' @field code integer [optional] #' @field code integer [optional]
#' #'
#' @field type character [optional] #' @field type character [optional]
#' #'
#' @field message character [optional] #' @field message character [optional]
#' #'
#'
#' @importFrom R6 R6Class #' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON #' @importFrom jsonlite fromJSON toJSON
#' @export #' @export
@ -26,7 +28,9 @@ ModelApiResponse <- R6::R6Class(
`code` = NULL, `code` = NULL,
`type` = NULL, `type` = NULL,
`message` = NULL, `message` = NULL,
initialize = function(`code`=NULL, `type`=NULL, `message`=NULL, ...){ initialize = function(
`code`=NULL, `type`=NULL, `message`=NULL, ...
) {
local.optional.var <- list(...) local.optional.var <- list(...)
if (!is.null(`code`)) { if (!is.null(`code`)) {
stopifnot(is.numeric(`code`), length(`code`) == 1) stopifnot(is.numeric(`code`), length(`code`) == 1)
@ -69,6 +73,7 @@ ModelApiResponse <- R6::R6Class(
if (!is.null(ModelApiResponseObject$`message`)) { if (!is.null(ModelApiResponseObject$`message`)) {
self$`message` <- ModelApiResponseObject$`message` self$`message` <- ModelApiResponseObject$`message`
} }
self
}, },
toJSONString = function() { toJSONString = function() {
jsoncontent <- c( jsoncontent <- c(
@ -106,3 +111,4 @@ ModelApiResponse <- R6::R6Class(
} }
) )
) )

View File

@ -8,8 +8,11 @@
#' @docType class #' @docType class
#' @title Order #' @title Order
#'
#' @description Order Class #' @description Order Class
#'
#' @format An \code{R6Class} generator object #' @format An \code{R6Class} generator object
#'
#' @field id integer [optional] #' @field id integer [optional]
#' #'
#' @field petId integer [optional] #' @field petId integer [optional]
@ -22,7 +25,6 @@
#' #'
#' @field complete character [optional] #' @field complete character [optional]
#' #'
#'
#' @importFrom R6 R6Class #' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON #' @importFrom jsonlite fromJSON toJSON
#' @export #' @export
@ -35,7 +37,9 @@ Order <- R6::R6Class(
`shipDate` = NULL, `shipDate` = NULL,
`status` = NULL, `status` = NULL,
`complete` = NULL, `complete` = NULL,
initialize = function(`id`=NULL, `petId`=NULL, `quantity`=NULL, `shipDate`=NULL, `status`=NULL, `complete`=FALSE, ...){ initialize = function(
`id`=NULL, `petId`=NULL, `quantity`=NULL, `shipDate`=NULL, `status`=NULL, `complete`=FALSE, ...
) {
local.optional.var <- list(...) local.optional.var <- list(...)
if (!is.null(`id`)) { if (!is.null(`id`)) {
stopifnot(is.numeric(`id`), length(`id`) == 1) stopifnot(is.numeric(`id`), length(`id`) == 1)
@ -110,6 +114,7 @@ Order <- R6::R6Class(
if (!is.null(OrderObject$`complete`)) { if (!is.null(OrderObject$`complete`)) {
self$`complete` <- OrderObject$`complete` self$`complete` <- OrderObject$`complete`
} }
self
}, },
toJSONString = function() { toJSONString = function() {
jsoncontent <- c( jsoncontent <- c(
@ -171,3 +176,4 @@ Order <- R6::R6Class(
} }
) )
) )

View File

@ -8,8 +8,11 @@
#' @docType class #' @docType class
#' @title Pet #' @title Pet
#'
#' @description Pet Class #' @description Pet Class
#'
#' @format An \code{R6Class} generator object #' @format An \code{R6Class} generator object
#'
#' @field id integer [optional] #' @field id integer [optional]
#' #'
#' @field category \link{Category} [optional] #' @field category \link{Category} [optional]
@ -22,7 +25,6 @@
#' #'
#' @field status character [optional] #' @field status character [optional]
#' #'
#'
#' @importFrom R6 R6Class #' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON #' @importFrom jsonlite fromJSON toJSON
#' @export #' @export
@ -35,7 +37,9 @@ Pet <- R6::R6Class(
`photoUrls` = NULL, `photoUrls` = NULL,
`tags` = NULL, `tags` = NULL,
`status` = NULL, `status` = NULL,
initialize = function(`name`, `photoUrls`, `id`=NULL, `category`=NULL, `tags`=NULL, `status`=NULL, ...){ initialize = function(
`name`, `photoUrls`, `id`=NULL, `category`=NULL, `tags`=NULL, `status`=NULL, ...
) {
local.optional.var <- list(...) local.optional.var <- list(...)
if (!missing(`name`)) { if (!missing(`name`)) {
stopifnot(is.character(`name`), length(`name`) == 1) stopifnot(is.character(`name`), length(`name`) == 1)
@ -115,6 +119,7 @@ Pet <- R6::R6Class(
if (!is.null(PetObject$`status`)) { if (!is.null(PetObject$`status`)) {
self$`status` <- PetObject$`status` self$`status` <- PetObject$`status`
} }
self
}, },
toJSONString = function() { toJSONString = function() {
jsoncontent <- c( jsoncontent <- c(
@ -176,3 +181,4 @@ Pet <- R6::R6Class(
} }
) )
) )

View File

@ -8,13 +8,15 @@
#' @docType class #' @docType class
#' @title Tag #' @title Tag
#'
#' @description Tag Class #' @description Tag Class
#'
#' @format An \code{R6Class} generator object #' @format An \code{R6Class} generator object
#'
#' @field id integer [optional] #' @field id integer [optional]
#' #'
#' @field name character [optional] #' @field name character [optional]
#' #'
#'
#' @importFrom R6 R6Class #' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON #' @importFrom jsonlite fromJSON toJSON
#' @export #' @export
@ -23,7 +25,9 @@ Tag <- R6::R6Class(
public = list( public = list(
`id` = NULL, `id` = NULL,
`name` = NULL, `name` = NULL,
initialize = function(`id`=NULL, `name`=NULL, ...){ initialize = function(
`id`=NULL, `name`=NULL, ...
) {
local.optional.var <- list(...) local.optional.var <- list(...)
if (!is.null(`id`)) { if (!is.null(`id`)) {
stopifnot(is.numeric(`id`), length(`id`) == 1) stopifnot(is.numeric(`id`), length(`id`) == 1)
@ -55,6 +59,7 @@ Tag <- R6::R6Class(
if (!is.null(TagObject$`name`)) { if (!is.null(TagObject$`name`)) {
self$`name` <- TagObject$`name` self$`name` <- TagObject$`name`
} }
self
}, },
toJSONString = function() { toJSONString = function() {
jsoncontent <- c( jsoncontent <- c(
@ -84,3 +89,4 @@ Tag <- R6::R6Class(
} }
) )
) )

View File

@ -8,8 +8,11 @@
#' @docType class #' @docType class
#' @title User #' @title User
#'
#' @description User Class #' @description User Class
#'
#' @format An \code{R6Class} generator object #' @format An \code{R6Class} generator object
#'
#' @field id integer [optional] #' @field id integer [optional]
#' #'
#' @field username character [optional] #' @field username character [optional]
@ -26,7 +29,6 @@
#' #'
#' @field userStatus integer [optional] #' @field userStatus integer [optional]
#' #'
#'
#' @importFrom R6 R6Class #' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON #' @importFrom jsonlite fromJSON toJSON
#' @export #' @export
@ -41,7 +43,9 @@ User <- R6::R6Class(
`password` = NULL, `password` = NULL,
`phone` = NULL, `phone` = NULL,
`userStatus` = NULL, `userStatus` = NULL,
initialize = function(`id`=NULL, `username`=NULL, `firstName`=NULL, `lastName`=NULL, `email`=NULL, `password`=NULL, `phone`=NULL, `userStatus`=NULL, ...){ initialize = function(
`id`=NULL, `username`=NULL, `firstName`=NULL, `lastName`=NULL, `email`=NULL, `password`=NULL, `phone`=NULL, `userStatus`=NULL, ...
) {
local.optional.var <- list(...) local.optional.var <- list(...)
if (!is.null(`id`)) { if (!is.null(`id`)) {
stopifnot(is.numeric(`id`), length(`id`) == 1) stopifnot(is.numeric(`id`), length(`id`) == 1)
@ -139,6 +143,7 @@ User <- R6::R6Class(
if (!is.null(UserObject$`userStatus`)) { if (!is.null(UserObject$`userStatus`)) {
self$`userStatus` <- UserObject$`userStatus` self$`userStatus` <- UserObject$`userStatus`
} }
self
}, },
toJSONString = function() { toJSONString = function() {
jsoncontent <- c( jsoncontent <- c(
@ -216,3 +221,4 @@ User <- R6::R6Class(
} }
) )
) )

View File

@ -12,11 +12,11 @@ test_that("id", {
#expect_equal(model.instance$`id`, "EXPECTED_RESULT") #expect_equal(model.instance$`id`, "EXPECTED_RESULT")
}) })
test_that("pet_id", { test_that("petId", {
# tests for the property `pet_id` (integer) # tests for the property `petId` (integer)
# uncomment below to test the property # uncomment below to test the property
#expect_equal(model.instance$`pet_id`, "EXPECTED_RESULT") #expect_equal(model.instance$`petId`, "EXPECTED_RESULT")
}) })
test_that("quantity", { test_that("quantity", {
@ -26,11 +26,11 @@ test_that("quantity", {
#expect_equal(model.instance$`quantity`, "EXPECTED_RESULT") #expect_equal(model.instance$`quantity`, "EXPECTED_RESULT")
}) })
test_that("ship_date", { test_that("shipDate", {
# tests for the property `ship_date` (character) # tests for the property `shipDate` (character)
# uncomment below to test the property # uncomment below to test the property
#expect_equal(model.instance$`ship_date`, "EXPECTED_RESULT") #expect_equal(model.instance$`shipDate`, "EXPECTED_RESULT")
}) })
test_that("status", { test_that("status", {

View File

@ -26,15 +26,15 @@ test_that("name", {
#expect_equal(model.instance$`name`, "EXPECTED_RESULT") #expect_equal(model.instance$`name`, "EXPECTED_RESULT")
}) })
test_that("photo_urls", { test_that("photoUrls", {
# tests for the property `photo_urls` (character) # tests for the property `photoUrls` (array[character])
# uncomment below to test the property # uncomment below to test the property
#expect_equal(model.instance$`photo_urls`, "EXPECTED_RESULT") #expect_equal(model.instance$`photoUrls`, "EXPECTED_RESULT")
}) })
test_that("tags", { test_that("tags", {
# tests for the property `tags` (Tag) # tests for the property `tags` (array[Tag])
# uncomment below to test the property # uncomment below to test the property
#expect_equal(model.instance$`tags`, "EXPECTED_RESULT") #expect_equal(model.instance$`tags`, "EXPECTED_RESULT")

View File

@ -7,9 +7,9 @@ api.instance <- PetApi$new()
test_that("AddPet", { test_that("AddPet", {
# tests for AddPet # tests for AddPet
# base path: http://petstore.swagger.io/v2
# Add a new pet to the store # Add a new pet to the store
# @param body Pet object that needs to be added to the store # @param body Pet Pet object that needs to be added to the store
# @param [Hash] opts the optional parameters
# @return [Void] # @return [Void]
# uncomment below to test the operation # uncomment below to test the operation
@ -18,10 +18,10 @@ test_that("AddPet", {
test_that("DeletePet", { test_that("DeletePet", {
# tests for DeletePet # tests for DeletePet
# base path: http://petstore.swagger.io/v2
# Deletes a pet # Deletes a pet
# @param pet.id Pet id to delete # @param pet.id integer Pet id to delete
# @param [Hash] opts the optional parameters # @param api.key character (optional)
# @option opts [character] :api.key
# @return [Void] # @return [Void]
# uncomment below to test the operation # uncomment below to test the operation
@ -30,11 +30,11 @@ test_that("DeletePet", {
test_that("FindPetsByStatus", { test_that("FindPetsByStatus", {
# tests for FindPetsByStatus # tests for FindPetsByStatus
# base path: http://petstore.swagger.io/v2
# Finds Pets by status # Finds Pets by status
# Multiple status values can be provided with comma separated strings # Multiple status values can be provided with comma separated strings
# @param status Status values that need to be considered for filter # @param status array[character] Status values that need to be considered for filter
# @param [Hash] opts the optional parameters # @return [array[Pet]]
# @return [Pet]
# uncomment below to test the operation # uncomment below to test the operation
#expect_equal(result, "EXPECTED_RESULT") #expect_equal(result, "EXPECTED_RESULT")
@ -42,11 +42,11 @@ test_that("FindPetsByStatus", {
test_that("FindPetsByTags", { test_that("FindPetsByTags", {
# tests for FindPetsByTags # tests for FindPetsByTags
# base path: http://petstore.swagger.io/v2
# Finds Pets by tags # Finds Pets by tags
# Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. # Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
# @param tags Tags to filter by # @param tags array[character] Tags to filter by
# @param [Hash] opts the optional parameters # @return [array[Pet]]
# @return [Pet]
# uncomment below to test the operation # uncomment below to test the operation
#expect_equal(result, "EXPECTED_RESULT") #expect_equal(result, "EXPECTED_RESULT")
@ -54,10 +54,10 @@ test_that("FindPetsByTags", {
test_that("GetPetById", { test_that("GetPetById", {
# tests for GetPetById # tests for GetPetById
# base path: http://petstore.swagger.io/v2
# Find pet by ID # Find pet by ID
# Returns a single pet # Returns a single pet
# @param pet.id ID of pet to return # @param pet.id integer ID of pet to return
# @param [Hash] opts the optional parameters
# @return [Pet] # @return [Pet]
# uncomment below to test the operation # uncomment below to test the operation
@ -66,9 +66,9 @@ test_that("GetPetById", {
test_that("UpdatePet", { test_that("UpdatePet", {
# tests for UpdatePet # tests for UpdatePet
# base path: http://petstore.swagger.io/v2
# Update an existing pet # Update an existing pet
# @param body Pet object that needs to be added to the store # @param body Pet Pet object that needs to be added to the store
# @param [Hash] opts the optional parameters
# @return [Void] # @return [Void]
# uncomment below to test the operation # uncomment below to test the operation
@ -77,11 +77,11 @@ test_that("UpdatePet", {
test_that("UpdatePetWithForm", { test_that("UpdatePetWithForm", {
# tests for UpdatePetWithForm # tests for UpdatePetWithForm
# base path: http://petstore.swagger.io/v2
# Updates a pet in the store with form data # Updates a pet in the store with form data
# @param pet.id ID of pet that needs to be updated # @param pet.id integer ID of pet that needs to be updated
# @param [Hash] opts the optional parameters # @param name character Updated name of the pet (optional)
# @option opts [character] :name Updated name of the pet # @param status character Updated status of the pet (optional)
# @option opts [character] :status Updated status of the pet
# @return [Void] # @return [Void]
# uncomment below to test the operation # uncomment below to test the operation
@ -90,11 +90,11 @@ test_that("UpdatePetWithForm", {
test_that("UploadFile", { test_that("UploadFile", {
# tests for UploadFile # tests for UploadFile
# base path: http://petstore.swagger.io/v2
# uploads an image # uploads an image
# @param pet.id ID of pet to update # @param pet.id integer ID of pet to update
# @param [Hash] opts the optional parameters # @param additional.metadata character Additional data to pass to server (optional)
# @option opts [character] :additional.metadata Additional data to pass to server # @param file data.frame file to upload (optional)
# @option opts [data.frame] :file file to upload
# @return [ModelApiResponse] # @return [ModelApiResponse]
# uncomment below to test the operation # uncomment below to test the operation

View File

@ -1,31 +1,67 @@
context("basic functionality") context("basic functionality")
petApi <- PetApi$new() petApi <- PetApi$new()
petId <- 123321 petId <- 123321
test_that("AddPet", { pet <- Pet$new("name_test",
pet <- Pet$new("name_test", list("photo_test", "second test"), photoUrls = list("photo_test", "second test"),
petId, Category$new(id=450, name="test_cat"), category = Category$new(id=450, name="test_cat"),
list(Tag$new(id=123, name="tag_test"), Tag$new(id=456, name="unknown")), id = petId,
"available") tags = list(
#pet <- Pet$new("name_test", Tag$new(id=123, name="tag_test"), Tag$new(id=456, name="unknown")
# list("photo_test", "second test"), ),
# category=Category$new(id=450, name="test_cat"), status = "available"
# id=petId, )
# tags=list(Tag$new(id=123, name="tag_test"), Tag$new(id=456, name="unknown")),
# status="available")
result <- petApi$AddPet(pet) result <- petApi$AddPet(pet)
test_that("AddPet", {
expect_equal(petId, 123321) expect_equal(petId, 123321)
expect_equal(result, NULL) expect_equal(result, NULL)
}) })
test_that("Test Pet", { test_that("Test toJSON toJSON fromJSON fromJSONString", {
pet <- Pet$new("name_test", list("photo_test", "second test"), pet0 <- Pet$new()
petId, Category$new(id=450, name="test_cat"), jsonpet <- pet0$toJSON()
list(Tag$new(id=123, name="tag_test"), Tag$new(id=456, name="unknown")), pet2 <- pet0$fromJSON(
"available") jsonlite::toJSON(jsonpet, auto_unbox=TRUE)
)
expect_equal(pet0, pet2)
jsonpet <- pet0$toJSONString()
pet2 <- pet0$fromJSON(
jsonpet
)
expect_equal(pet0, pet2)
pet2 <- Pet$new() jsonpet <- pet0$toJSONString()
#pet2$fromJSON(jsonlite::toJSON(pet$toJSON(), auto_unbox=TRUE)) pet2 <- pet0$fromJSONString(
#expect_equal(pet, pet2) jsonpet
)
expect_equal(pet0, pet2)
pet1 <- Pet$new("name_test",
list("photo_test", "second test"),
category = Category$new(id=450, name="test_cat"),
id = petId,
tags = list(
Tag$new(id=123, name="tag_test"), Tag$new(id=456, name="unknown")
),
status = "available"
)
jsonpet <- pet1$toJSON()
pet2 <- pet1$fromJSON(
jsonlite::toJSON(jsonpet, auto_unbox=TRUE)
)
expect_equal(pet1, pet2)
jsonpet <- pet1$toJSONString()
pet2 <- pet1$fromJSON(
jsonpet
)
expect_equal(pet1, pet2)
jsonpet <- pet1$toJSONString()
pet2 <- pet1$fromJSONString(
jsonpet
)
expect_equal(pet1, pet2)
}) })
test_that("Test Category", { test_that("Test Category", {
@ -33,16 +69,26 @@ test_that("Test Category", {
c2 <- Category$new() c2 <- Category$new()
c2$fromJSON(jsonlite::toJSON(c1$toJSON(), auto_unbox=TRUE)) c2$fromJSON(jsonlite::toJSON(c1$toJSON(), auto_unbox=TRUE))
expect_equal(c1, c2) expect_equal(c1, c2)
c2$fromJSONString(c1$toJSONString())
expect_equal(c1, c2)
}) })
test_that("GetPetById", { test_that("GetPetById", {
response <- petApi$GetPetById(petId) response <- petApi$GetPetById(petId)
expect_equal(response$id, petId) expect_equal(response$id, petId)
expect_equal(response$name, "name_test") expect_equal(response$name, "name_test")
#expect_equal(response$category, Category$new(id=450, name="test_cat")) expect_equal(
expect_equal(response$photoUrls, list("photo_test", "second test")) response$photoUrls,
list("photo_test", "second test")
)
expect_equal(response$status, "available") expect_equal(response$status, "available")
expect_equal(response$tags, list(Tag$new(id=123, name="tag_test"), Tag$new(id=456, name="unknown"))) expect_equal(response$category, Category$new(id=450, name="test_cat"))
expect_equal(pet$tags, response$tags)
expect_equal(
response$tags,
list(Tag$new(id=123, name="tag_test"), Tag$new(id=456, name="unknown"))
)
}) })
#test_that("GetPetById", { #test_that("GetPetById", {

View File

@ -7,10 +7,10 @@ api.instance <- StoreApi$new()
test_that("DeleteOrder", { test_that("DeleteOrder", {
# tests for DeleteOrder # tests for DeleteOrder
# base path: http://petstore.swagger.io/v2
# Delete purchase order by ID # Delete purchase order by ID
# For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors # For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
# @param order.id ID of the order that needs to be deleted # @param order.id character ID of the order that needs to be deleted
# @param [Hash] opts the optional parameters
# @return [Void] # @return [Void]
# uncomment below to test the operation # uncomment below to test the operation
@ -19,10 +19,10 @@ test_that("DeleteOrder", {
test_that("GetInventory", { test_that("GetInventory", {
# tests for GetInventory # tests for GetInventory
# base path: http://petstore.swagger.io/v2
# Returns pet inventories by status # Returns pet inventories by status
# Returns a map of status codes to quantities # Returns a map of status codes to quantities
# @param [Hash] opts the optional parameters # @return [map(integer)]
# @return [integer]
# uncomment below to test the operation # uncomment below to test the operation
#expect_equal(result, "EXPECTED_RESULT") #expect_equal(result, "EXPECTED_RESULT")
@ -30,10 +30,10 @@ test_that("GetInventory", {
test_that("GetOrderById", { test_that("GetOrderById", {
# tests for GetOrderById # tests for GetOrderById
# base path: http://petstore.swagger.io/v2
# Find purchase order by ID # Find purchase order by ID
# For valid response try integer IDs with value &lt;&#x3D; 5 or &gt; 10. Other values will generated exceptions # For valid response try integer IDs with value &lt;&#x3D; 5 or &gt; 10. Other values will generated exceptions
# @param order.id ID of pet that needs to be fetched # @param order.id integer ID of pet that needs to be fetched
# @param [Hash] opts the optional parameters
# @return [Order] # @return [Order]
# uncomment below to test the operation # uncomment below to test the operation
@ -42,9 +42,9 @@ test_that("GetOrderById", {
test_that("PlaceOrder", { test_that("PlaceOrder", {
# tests for PlaceOrder # tests for PlaceOrder
# base path: http://petstore.swagger.io/v2
# Place an order for a pet # Place an order for a pet
# @param body order placed for purchasing the pet # @param body Order order placed for purchasing the pet
# @param [Hash] opts the optional parameters
# @return [Order] # @return [Order]
# uncomment below to test the operation # uncomment below to test the operation

View File

@ -19,18 +19,18 @@ test_that("username", {
#expect_equal(model.instance$`username`, "EXPECTED_RESULT") #expect_equal(model.instance$`username`, "EXPECTED_RESULT")
}) })
test_that("first_name", { test_that("firstName", {
# tests for the property `first_name` (character) # tests for the property `firstName` (character)
# uncomment below to test the property # uncomment below to test the property
#expect_equal(model.instance$`first_name`, "EXPECTED_RESULT") #expect_equal(model.instance$`firstName`, "EXPECTED_RESULT")
}) })
test_that("last_name", { test_that("lastName", {
# tests for the property `last_name` (character) # tests for the property `lastName` (character)
# uncomment below to test the property # uncomment below to test the property
#expect_equal(model.instance$`last_name`, "EXPECTED_RESULT") #expect_equal(model.instance$`lastName`, "EXPECTED_RESULT")
}) })
test_that("email", { test_that("email", {
@ -54,11 +54,11 @@ test_that("phone", {
#expect_equal(model.instance$`phone`, "EXPECTED_RESULT") #expect_equal(model.instance$`phone`, "EXPECTED_RESULT")
}) })
test_that("user_status", { test_that("userStatus", {
# tests for the property `user_status` (integer) # tests for the property `userStatus` (integer)
# User Status # User Status
# uncomment below to test the property # uncomment below to test the property
#expect_equal(model.instance$`user_status`, "EXPECTED_RESULT") #expect_equal(model.instance$`userStatus`, "EXPECTED_RESULT")
}) })

View File

@ -7,10 +7,10 @@ api.instance <- UserApi$new()
test_that("CreateUser", { test_that("CreateUser", {
# tests for CreateUser # tests for CreateUser
# base path: http://petstore.swagger.io/v2
# Create user # Create user
# This can only be done by the logged in user. # This can only be done by the logged in user.
# @param body Created user object # @param body User Created user object
# @param [Hash] opts the optional parameters
# @return [Void] # @return [Void]
# uncomment below to test the operation # uncomment below to test the operation
@ -19,9 +19,9 @@ test_that("CreateUser", {
test_that("CreateUsersWithArrayInput", { test_that("CreateUsersWithArrayInput", {
# tests for CreateUsersWithArrayInput # tests for CreateUsersWithArrayInput
# base path: http://petstore.swagger.io/v2
# Creates list of users with given input array # Creates list of users with given input array
# @param body List of user object # @param body array[User] List of user object
# @param [Hash] opts the optional parameters
# @return [Void] # @return [Void]
# uncomment below to test the operation # uncomment below to test the operation
@ -30,9 +30,9 @@ test_that("CreateUsersWithArrayInput", {
test_that("CreateUsersWithListInput", { test_that("CreateUsersWithListInput", {
# tests for CreateUsersWithListInput # tests for CreateUsersWithListInput
# base path: http://petstore.swagger.io/v2
# Creates list of users with given input array # Creates list of users with given input array
# @param body List of user object # @param body array[User] List of user object
# @param [Hash] opts the optional parameters
# @return [Void] # @return [Void]
# uncomment below to test the operation # uncomment below to test the operation
@ -41,10 +41,10 @@ test_that("CreateUsersWithListInput", {
test_that("DeleteUser", { test_that("DeleteUser", {
# tests for DeleteUser # tests for DeleteUser
# base path: http://petstore.swagger.io/v2
# Delete user # Delete user
# This can only be done by the logged in user. # This can only be done by the logged in user.
# @param username The name that needs to be deleted # @param username character The name that needs to be deleted
# @param [Hash] opts the optional parameters
# @return [Void] # @return [Void]
# uncomment below to test the operation # uncomment below to test the operation
@ -53,9 +53,9 @@ test_that("DeleteUser", {
test_that("GetUserByName", { test_that("GetUserByName", {
# tests for GetUserByName # tests for GetUserByName
# base path: http://petstore.swagger.io/v2
# Get user by user name # Get user by user name
# @param username The name that needs to be fetched. Use user1 for testing. # @param username character The name that needs to be fetched. Use user1 for testing.
# @param [Hash] opts the optional parameters
# @return [User] # @return [User]
# uncomment below to test the operation # uncomment below to test the operation
@ -64,10 +64,10 @@ test_that("GetUserByName", {
test_that("LoginUser", { test_that("LoginUser", {
# tests for LoginUser # tests for LoginUser
# base path: http://petstore.swagger.io/v2
# Logs user into the system # Logs user into the system
# @param username The user name for login # @param username character The user name for login
# @param password The password for login in clear text # @param password character The password for login in clear text
# @param [Hash] opts the optional parameters
# @return [character] # @return [character]
# uncomment below to test the operation # uncomment below to test the operation
@ -76,8 +76,8 @@ test_that("LoginUser", {
test_that("LogoutUser", { test_that("LogoutUser", {
# tests for LogoutUser # tests for LogoutUser
# base path: http://petstore.swagger.io/v2
# Logs out current logged in user session # Logs out current logged in user session
# @param [Hash] opts the optional parameters
# @return [Void] # @return [Void]
# uncomment below to test the operation # uncomment below to test the operation
@ -86,11 +86,11 @@ test_that("LogoutUser", {
test_that("UpdateUser", { test_that("UpdateUser", {
# tests for UpdateUser # tests for UpdateUser
# base path: http://petstore.swagger.io/v2
# Updated user # Updated user
# This can only be done by the logged in user. # This can only be done by the logged in user.
# @param username name that need to be deleted # @param username character name that need to be deleted
# @param body Updated user object # @param body User Updated user object
# @param [Hash] opts the optional parameters
# @return [Void] # @return [Void]
# uncomment below to test the operation # uncomment below to test the operation

View File

@ -1,4 +0,0 @@
library(testthat)
library(petstore)
test_check("petstore")