add tests for nested oneOf fromJSONString (#12740)

This commit is contained in:
William Cheng 2022-06-30 23:08:49 +08:00 committed by GitHub
parent 3a2bbbb850
commit 08108cc4ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,7 +15,6 @@ result <- pet_api$AddPet(pet)
test_that("AddPet", { test_that("AddPet", {
expect_equal(pet_id, 123321) expect_equal(pet_id, 123321)
#expect_equal(result, NULL)
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"}') 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"}')
}) })
@ -228,15 +227,20 @@ test_that("Tests oneOf", {
expect_equal(pig2$actual_instance$color, "red") expect_equal(pig2$actual_instance$color, "red")
expect_equal(pig2$actual_instance$className, "BasquePig") expect_equal(pig2$actual_instance$className, "BasquePig")
expect_equal(pig2$toJSONString(), original_basque_pig$toJSONString()) expect_equal(pig2$toJSONString(), original_basque_pig$toJSONString())
expect_error(Pig$new(instance = basque_pig), 'Failed to initialize Pig with oneOf schemas BasquePig, DanishPig. Provided class name: Pig') expect_error(Pig$new(instance = basque_pig), 'Failed to initialize Pig with oneOf schemas BasquePig, DanishPig. Provided class name: Pig')
# test nested oneOf # test nested oneOf toJSONString
nested_oneof <- NestedOneOf$new() nested_oneof <- NestedOneOf$new()
nested_oneof$nested_pig <- pig nested_oneof$nested_pig <- pig
nested_oneof$size <- 15 nested_oneof$size <- 15
expect_equal(nested_oneof$toJSONString(), '{"size":15,"nested_pig":{"className":"BasquePig","color":"red"}}') expect_equal(nested_oneof$toJSONString(), '{"size":15,"nested_pig":{"className":"BasquePig","color":"red"}}')
# test fromJSONString with nested oneOf
nested_json_str <- '{"size":15,"nested_pig":{"className":"BasquePig","color":"red"}}'
nested_oneof2 <- NestedOneOf$new()$fromJSONString(nested_json_str)
expect_equal(nested_oneof2$toJSONString(), '{"size":15,"nested_pig":{"className":"BasquePig","color":"red"}}')
# test toString # test toString
expect_equal(as.character(jsonlite::minify(pig$toString())), "{\"actual_instance\":{\"className\":\"BasquePig\",\"color\":\"red\"},\"actual_type\":\"BasquePig\",\"one_of\":\"BasquePig, DanishPig\"}") expect_equal(as.character(jsonlite::minify(pig$toString())), "{\"actual_instance\":{\"className\":\"BasquePig\",\"color\":\"red\"},\"actual_type\":\"BasquePig\",\"one_of\":\"BasquePig, DanishPig\"}")
expect_equal(as.character(jsonlite::minify(Pig$new()$toString())), "{\"one_of\":\"BasquePig, DanishPig\"}") expect_equal(as.character(jsonlite::minify(Pig$new()$toString())), "{\"one_of\":\"BasquePig, DanishPig\"}")
@ -270,7 +274,7 @@ test_that("Tests anyOf", {
expect_equal(pig$actual_instance$size, 7) expect_equal(pig$actual_instance$size, 7)
expect_equal(pig$actual_instance$className, "DanishPig") expect_equal(pig$actual_instance$className, "DanishPig")
# test toJSON # test toJSONString
expect_equal(danish_pig$toJSONString(), original_danish_pig$toJSONString()) expect_equal(danish_pig$toJSONString(), original_danish_pig$toJSONString())
basque_pig <- pig$fromJSON(basque_pig_json) basque_pig <- pig$fromJSON(basque_pig_json)