fix toJSON, add tests (#12541)

This commit is contained in:
William Cheng 2022-06-06 23:17:35 +08:00 committed by GitHub
parent 6948f15514
commit 5db8cd0eff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 14 deletions

View File

@ -19,10 +19,6 @@
) { ) {
local_optional_var <- list(...) local_optional_var <- list(...)
}, },
toJSON = function() {
{{classname}}_object <- list()
{{classname}}_object
},
fromJSON = function(input) { fromJSON = function(input) {
matched <- 0 # match counter matched <- 0 # match counter
matched_schemas <- list() #names of matched schemas matched_schemas <- list() #names of matched schemas
@ -59,6 +55,13 @@
} }
self self
},
toJSON = function() {
if (!is.null(self$actual_instance)) {
self$actual_instance$toJSONString()
} else {
NULL
}
} }
) )
) )

View File

@ -26,10 +26,6 @@ Pig <- R6::R6Class(
) { ) {
local_optional_var <- list(...) local_optional_var <- list(...)
}, },
toJSON = function() {
Pig_object <- list()
Pig_object
},
fromJSON = function(input) { fromJSON = function(input) {
matched <- 0 # match counter matched <- 0 # match counter
matched_schemas <- list() #names of matched schemas matched_schemas <- list() #names of matched schemas
@ -79,6 +75,13 @@ Pig <- R6::R6Class(
} }
self self
},
toJSON = function() {
if (!is.null(self$actual_instance)) {
self$actual_instance$toJSONString()
} else {
NULL
}
} }
) )
) )

View File

@ -56,15 +56,17 @@ json <-
{}, {},
{"Name" : "Ada", "Occupation" : "Engineer"} {"Name" : "Ada", "Occupation" : "Engineer"}
]' ]'
print("==========")
pig <- Pig$new() pig <- Pig$new()
basque_pig <- pig$fromJSON(basque_pig_json) basque_pig <- pig$fromJSON(basque_pig_json)
#print(basque_pig$actual_instance$color) #print(basque_pig$actual_instance$color)
#expect_equal(basque_pig$actual_type, "BasquePig") #expect_equal(basque_pig$actual_type, "BasquePig")
i <- pig$fromJSON(danish_pig_json) pig$fromJSON(danish_pig_json)
#i <- pig$fromJSON(wrong_json) #pig$fromJSON(wrong_json)
pig$toJSON()
d <- DanishPig$new() #d <- DanishPig$new()
dp <- d$validateJSON(danish_pig_json) #dp <- d$validateJSON(danish_pig_json)

View File

@ -1,4 +1,5 @@
context("basic functionality") context("basic functionality")
pet_api <- PetApi$new() pet_api <- PetApi$new()
pet_id <- 123321 pet_id <- 123321
pet <- Pet$new("name_test", pet <- Pet$new("name_test",
@ -152,20 +153,34 @@ test_that("Tests oneOf", {
{"Name" : "Ada", "Occupation" : "Engineer"} {"Name" : "Ada", "Occupation" : "Engineer"}
]' ]'
original_danish_pig = DanishPig$new()$fromJSON(danish_pig_json)
original_basque_pig = BasquePig$new()$fromJSON(basque_pig_json)
# test fromJSNO, actual_tpye, actual_instance
pig <- Pig$new() pig <- Pig$new()
danish_pig <- pig$fromJSON(danish_pig_json) danish_pig <- pig$fromJSON(danish_pig_json)
expect_equal(danish_pig$actual_type, "DanishPig") expect_equal(danish_pig$actual_type, "DanishPig")
expect_equal(danish_pig$actual_instance$size, 7) expect_equal(danish_pig$actual_instance$size, 7)
expect_equal(danish_pig$actual_instance$className, "DanishPig") expect_equal(danish_pig$actual_instance$className, "DanishPig")
expect_equal(pig$actual_type, "DanishPig")
expect_equal(pig$actual_instance$size, 7)
expect_equal(pig$actual_instance$className, "DanishPig")
# test toJSON
#expect_equal(danish_pig$toJSON(), "")
expect_equal(danish_pig$toJSON(), original_danish_pig$toJSONString())
basque_pig <- pig$fromJSON(basque_pig_json) basque_pig <- pig$fromJSON(basque_pig_json)
expect_equal(basque_pig$actual_type, "BasquePig") expect_equal(basque_pig$actual_type, "BasquePig")
expect_equal(basque_pig$actual_instance$color, "red") expect_equal(basque_pig$actual_instance$color, "red")
expect_equal(basque_pig$actual_instance$className, "BasquePig") expect_equal(basque_pig$actual_instance$className, "BasquePig")
expect_equal(basque_pig$toJSON(), original_basque_pig$toJSONString())
#expect_error(pig$fromJSON(wrong_json), "No match found when deserializing the payload into Pig with oneOf schemas BasquePig, DanishPig. Details: The JSON input ` [\n {\"Name\" : \"Tom\", \"Age\" : 32, \"Occupation\" : \"Consultant\"}, \n {},\n {\"Name\" : \"Ada\", \"Occupation\" : \"Engineer\"}\n ] ` is invalid for BasquePig: the required field `className` is missing., The JSON input ` [\n {\"Name\" : \"Tom\", \"Age\" : 32, \"Occupation\" : \"Consultant\"}, \n {},\n {\"Name\" : \"Ada\", \"Occupation\" : \"Engineer\"}\n ] ` is invalid for DanishPig: the required field `className` is missing.") # test exception when no matche found
expect_error(pig$fromJSON('{}'), 'No match found when deserializing the payload into Pig with oneOf 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\\.') expect_error(pig$fromJSON('{}'), 'No match found when deserializing the payload into Pig with oneOf 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", { #test_that("GetPetById", {