How to use json method of io.kotest.assertions.json.schema.ObjectSchemaTest class

Best Kotest code snippet using io.kotest.assertions.json.schema.ObjectSchemaTest.json

ObjectSchemaTest.kt

Source:ObjectSchemaTest.kt Github

copy

Full Screen

1package io.kotest.assertions.json.schema2import io.kotest.assertions.shouldFail3import io.kotest.core.spec.style.FunSpec4import io.kotest.matchers.shouldBe5import org.intellij.lang.annotations.Language6class ObjectSchemaTest : FunSpec(7 {8 fun json(@Language("JSON") raw: String) = raw9 val personSchemaAllowingExtraProperties = jsonSchema {10 obj {11 withProperty("name", required = true) { string() }12 withProperty("initials", required = false) { string() }13 withProperty("age", required = true) { number() }14 }15 }16 val personSchema = parseSchema(17 json(18 """19 {20 "type": "object",21 "properties": {22 "name": { "type": "string" },23 "initials": { "type": "string" },24 "age": { "type": "number" }25 },26 "requiredProperties": [ "name", "age" ],27 "additionalProperties": false28 }29 """30 )31 )32 test("matching object passes") {33 """{ "name": "John", "age": 27.2 }""" shouldMatchSchema personSchema34 }35 test("mismatching property type causes failure") {36 shouldFail {37 json("""{ "name": "John", "age": "twentyseven" }""") shouldMatchSchema personSchema38 }.message shouldBe """39 $.age => Expected number, but was string40 """.trimIndent()41 }42 test("primitive instead of object causes failure") {43 shouldFail {44 "\"hello\"" shouldMatchSchema personSchema45 }.message shouldBe """46 $ => Expected object, but was string47 """.trimIndent()48 }49 test("Extra property causes failure") {50 shouldFail {51 json("""{ "name": "John", "age": 27.2, "profession": "T800" }""") shouldMatchSchema personSchema52 }.message shouldBe """53 $.profession => Key undefined in schema, and schema is set to disallow extra keys54 """.trimIndent()55 }56 test("Missing required property causes failure") {57 shouldFail {58 json("""{ "name": "John" }""") shouldMatchSchema personSchema59 }.message shouldBe """60 $.age => Expected number, but was undefined61 """.trimIndent()62 }63 test("Extra property causes failure for scheam disallowing it") {64 shouldFail {65 json("""{ "name": "John", "favorite_pet": "Cat", "age": 2 }""") shouldMatchSchema personSchema66 }.message shouldBe """67 $.favorite_pet => Key undefined in schema, and schema is set to disallow extra keys68 """.trimIndent()69 }70 test("Extra property is OK when schema allows it") {71 json("""{ "name": "John", "favorite_pet": "Cat", "age": 2 }""") shouldMatchSchema personSchemaAllowingExtraProperties72 }73 test("Problems compound") {74 shouldFail {75 json("""{ "name": 5, "age": "twentyseven" }""") shouldMatchSchema personSchema76 }.message shouldBe """77 $.name => Expected string, but was number78 $.age => Expected number, but was string79 """.trimIndent()80 }81 context("nested objects") {82 val companySchema = jsonSchema {83 obj {84 withProperty("owner") { personSchema.root }85 withProperty("employees") {86 array {87 personSchema.root // TODO: Should be possible to compose schemas without explicitly unpacking boxing element88 }89 }90 }91 }92 test("matching object passes") {93 json("""{ "owner": { "name": "Emil", "age": 34.1 }, "employees": [] }""") shouldMatchSchema94 companySchema95 }96 test("Mismatch gives good message") {97 shouldFail {98 json("""{ "owner": { "name": 5, "age": 34.1 }, "employees": [] }""") shouldMatchSchema99 companySchema100 }.message shouldBe """101 $.owner.name => Expected string, but was number102 """.trimIndent()103 }104 }105 }106)...

Full Screen

Full Screen

json

Using AI Code Generation

copy

Full Screen

1import io.kotest.assertions.json.schema.jsonSchema2import io.kotest.assertions.json.schema.jsonSchemaObject3import io.kotest.assertions.json.schema.jsonSchemaString4import io.kotest.assertions.json.schema.jsonSchemaValue5import io.kotest.core.spec.style.FunSpec6import io.kotest.matchers.shouldBe7import io.kotest.matchers.shouldNotBe8import io.kotest.matchers.types.shouldBeTypeOf9class ObjectSchemaTest : FunSpec({10 test("simple object") {11 val schema = jsonSchemaObject {12 "name" to jsonSchemaString()13 "age" to jsonSchemaValue(18)14 }15 val json = """{"name":"Joe","age":18}"""16 val result = schema.validate(json)17 }18 test("nested object") {19 val schema = jsonSchemaObject {20 "name" to jsonSchemaString()21 "age" to jsonSchemaValue(18)22 "address" to jsonSchemaObject {23 "street" to jsonSchemaString()24 "city" to jsonSchemaString()25 "zip" to jsonSchemaValue(12345)26 }27 }28 val json = """{"name":"Joe","age":18,"address":{"street":"Main St","city":"Anytown","zip":12345}}"""29 val result = schema.validate(json)30 }31 test("array of objects") {32 val schema = jsonSchemaObject {33 "name" to jsonSchemaString()34 "age" to jsonSchemaValue(18)35 "friends" to jsonSchemaArray(jsonSchemaObject {36 "name" to jsonSchemaString()37 "age" to jsonSchemaValue(18)38 })39 }40 val json = """{"name":"Joe","age":18,"friends":[{"name":"Joe","age":18},{"name":"Joe","age":18}]}"""41 val result = schema.validate(json)42 }43 test("array of objects with optional property") {44 val schema = jsonSchemaObject {45 "name" to jsonSchemaString()46 "age" to jsonSchemaValue(18)47 "friends" to jsonSchemaArray(jsonSchemaObject {48 "name" to jsonSchemaString()49 "age" to jsonSchemaValue(18)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in ObjectSchemaTest

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful