Skip to content

Commit

Permalink
All the tests updated to the latest conventions (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil authored Dec 11, 2024
1 parent bedafb6 commit 759dcba
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 42 deletions.
9 changes: 4 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ kotlin {
commonTest {
dependencies {
implementation(libs.kotlin.test)
implementation(libs.kotest.assertions.core)
implementation(libs.xemantic.kotlin.test)
implementation(libs.kotest.assertions.json)
implementation(libs.kotlinx.datetime)
implementation(libs.bignum)
Expand Down Expand Up @@ -201,10 +201,9 @@ tasks.withType<Test> {
}

powerAssert {
// power assert temporarily switched off for kotest, since it stopped working with kotlin 2.1
// functions = listOf(
// "io.kotest.matchers.shouldBe"
// )
functions = listOf(
"com.xemantic.kotlin.test.have"
)
}

// https://kotlinlang.org/docs/dokka-migration.html#adjust-configuration-options
Expand Down
6 changes: 5 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ javaTarget = "17"
kotlin = "2.1.0"
kotlinxSerialization = "1.7.3"
kotlinxDatetime = "0.6.1"

xemanticKotlinTest = "0.1.1"

kotest = "6.0.0.M1"

bignum = "0.3.10"
Expand All @@ -20,7 +23,8 @@ kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotl
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerialization" }
kotlinx-datetime = { module="org.jetbrains.kotlinx:kotlinx-datetime", version.ref="kotlinxDatetime" }

kotest-assertions-core = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" }
xemantic-kotlin-test = { module="com.xemantic.kotlin:xemantic-kotlin-test", version.ref="xemanticKotlinTest" }

kotest-assertions-json = { module = "io.kotest:kotest-assertions-json", version.ref = "kotest" }
bignum = { module = "com.ionspin.kotlin:bignum", version.ref = "bignum" }

Expand Down
50 changes: 26 additions & 24 deletions src/commonTest/kotlin/JsonSchemaTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package com.xemantic.ai.tool.schema

import io.kotest.assertions.json.shouldEqualJson
import io.kotest.assertions.throwables.shouldThrowWithMessage
import com.xemantic.kotlin.test.should
import com.xemantic.kotlin.test.have
import kotlin.test.Test
import kotlin.test.assertFailsWith

class JsonSchemaTest {

Expand All @@ -33,7 +35,7 @@ class JsonSchemaTest {
)
required = listOf("name")
additionalProperties = false
}.toString() shouldEqualJson """
}.toString() shouldEqualJson /* language=json */ """
{
"type": "object",
"title": "Person",
Expand Down Expand Up @@ -68,7 +70,7 @@ class JsonSchemaTest {
)
}
)
}.toString() shouldEqualJson $$"""
}.toString() shouldEqualJson /* language=json */ $$"""
{
"type": "object",
"title": "Person",
Expand Down Expand Up @@ -99,7 +101,7 @@ class JsonSchemaTest {

@Test
fun `should create empty ObjectSchema`() {
ObjectSchema {}.toString() shouldEqualJson """{"type": "object"}"""
ObjectSchema {}.toString() shouldEqualJson /* language=json */ """{"type": "object"}"""
}

@Test
Expand All @@ -110,7 +112,7 @@ class JsonSchemaTest {
minLength = 3
maxLength = 20
pattern = "^[a-zA-Z0-9_]+$"
}.toString() shouldEqualJson """
}.toString() shouldEqualJson /* language=json */ """
{
"type": "string",
"title": "Username",
Expand All @@ -130,7 +132,7 @@ class JsonSchemaTest {
minLength = 3
maxLength = 100
format(StringFormat.EMAIL)
}.toString() shouldEqualJson """
}.toString() shouldEqualJson /* language=json */ """
{
"type": "string",
"title": "Email",
Expand All @@ -147,7 +149,7 @@ class JsonSchemaTest {
StringSchema {
title = "Color"
enum = listOf("red", "green", "blue")
}.toString() shouldEqualJson """
}.toString() shouldEqualJson /* language=json */ """
{
"type": "string",
"title": "Color",
Expand All @@ -163,20 +165,20 @@ class JsonSchemaTest {
description = "User's avatar image"
contentEncoding = ContentEncoding.BASE64
contentMediaType = "image/png"
}.toString() shouldEqualJson """
}.toString() shouldEqualJson /* language=json */ """
{
"type": "string",
"title": "Image",
"description": "User's avatar image",
"contentEncoding": "base64",
"contentMediaType": "image/png"
}
}
"""
}

@Test
fun `should create empty StringSchema`() {
StringSchema {}.toString() shouldEqualJson """{"type": "string"}"""
StringSchema {}.toString() shouldEqualJson /* language=json */ """{"type": "string"}"""
}

@Test
Expand All @@ -188,7 +190,7 @@ class JsonSchemaTest {
minItems = 1
maxItems = 10
uniqueItems = true
}.toString() shouldEqualJson """
}.toString() shouldEqualJson /* language=json */ """
{
"type": "array",
"title": "Numbers",
Expand All @@ -213,7 +215,7 @@ class JsonSchemaTest {
"name" to StringSchema {}
)
}
}.toString() shouldEqualJson """
}.toString() shouldEqualJson /* language=json */ """
{
"type": "array",
"title": "Users",
Expand All @@ -240,7 +242,7 @@ class JsonSchemaTest {
minimum = 0.0
maximum = 1000.0
multipleOf = 0.01
}.toString() shouldEqualJson """
}.toString() shouldEqualJson /* language=json */ """
{
"type": "number",
"title": "Price",
Expand All @@ -259,7 +261,7 @@ class JsonSchemaTest {
description = "A price value"
exclusiveMinimum = 0.0
exclusiveMaximum = 1000.0
}.toString() shouldEqualJson """
}.toString() shouldEqualJson /* language=json */ """
{
"type": "number",
"title": "Price",
Expand All @@ -272,7 +274,7 @@ class JsonSchemaTest {

@Test
fun `should create empty NumberSchema`() {
NumberSchema {}.toString() shouldEqualJson """{"type": "number"}"""
NumberSchema {}.toString() shouldEqualJson /* language=json */ """{"type": "number"}"""
}

@Test
Expand All @@ -283,7 +285,7 @@ class JsonSchemaTest {
minimum = 0
maximum = 120
multipleOf = 1
}.toString() shouldEqualJson """
}.toString() shouldEqualJson /* language=json */ """
{
"type": "integer",
"title": "Age",
Expand All @@ -302,7 +304,7 @@ class JsonSchemaTest {
description = "A person's age"
exclusiveMinimum = 0
exclusiveMaximum = 120
}.toString() shouldEqualJson """
}.toString() shouldEqualJson /* language=json */ """
{
"type": "integer",
"title": "Age",
Expand All @@ -315,15 +317,15 @@ class JsonSchemaTest {

@Test
fun `should create empty IntegerSchema`() {
IntegerSchema {}.toString() shouldEqualJson """{"type": "integer"}"""
IntegerSchema {}.toString() shouldEqualJson /* language=json */ """{"type": "integer"}"""
}

@Test
fun `should create BooleanSchema`() {
BooleanSchema {
title = "Is Active"
description = "Whether the user is active"
}.toString() shouldEqualJson """
}.toString() shouldEqualJson /* language=json */ """
{
"type": "boolean",
"title": "Is Active",
Expand All @@ -334,12 +336,12 @@ class JsonSchemaTest {

@Test
fun `should create empty BooleanSchema`() {
BooleanSchema {}.toString() shouldEqualJson """{"type": "boolean"}"""
BooleanSchema {}.toString() shouldEqualJson /* language=json */ """{"type": "boolean"}"""
}

@Test
fun `should create JsonSchemaRef`() {
JsonSchema.Ref("#/definitions/address").toString() shouldEqualJson $$"""
JsonSchema.Ref("#/definitions/address").toString() shouldEqualJson /* language=json */ $$"""
{
"$ref": "#/definitions/address"
}
Expand All @@ -348,10 +350,10 @@ class JsonSchemaTest {

@Test
fun `should throw Exception for invalid JSON Pointer passed to JsonSchemaRef`() {
shouldThrowWithMessage<IllegalArgumentException>(
"The 'ref' must start with '#/'"
) {
assertFailsWith<IllegalArgumentException> {
JsonSchema.Ref("invalid_ref")
} should {
have(message == "The 'ref' must start with '#/'")
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/commonTest/kotlin/generator/JsonSchemaGeneratorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class JsonSchemaGeneratorTest {
@Test
fun `generate JSON Schema for Address`() {
val schema = jsonSchemaOf<Address>()
testJson.encodeToString(schema) shouldEqualJson """
testJson.encodeToString(schema) shouldEqualJson /* language=json */ """
{
"type": "object",
"title": "The full address",
Expand Down Expand Up @@ -181,7 +181,7 @@ class JsonSchemaGeneratorTest {
val schemaJson = testJson.encodeToString(schema)

// then
schemaJson shouldEqualJson $$"""
schemaJson shouldEqualJson /* language=json */ $$"""
{
"type": "object",
"description": "Personal data",
Expand Down Expand Up @@ -346,7 +346,7 @@ class JsonSchemaGeneratorTest {
@Test
fun `should prioritize title and description set on property over the one set for the whole class`() {
val schema = jsonSchemaOf<Foo>()
testJson.encodeToString(schema) shouldEqualJson $$"""
testJson.encodeToString(schema) shouldEqualJson /* language=json */ $$"""
{
"type": "object",
"description": "A container of monetary amounts",
Expand Down Expand Up @@ -377,7 +377,7 @@ class JsonSchemaGeneratorTest {
val schema = jsonSchemaOf<Foo>(
suppressDescription = true
)
testJson.encodeToString(schema) shouldEqualJson $$"""
testJson.encodeToString(schema) shouldEqualJson /* language=json */ $$"""
{
"type": "object",
"properties": {
Expand Down Expand Up @@ -416,7 +416,7 @@ class JsonSchemaGeneratorTest {
val schema = jsonSchemaOf<Bar>(
outputAdditionalPropertiesFalse = true
)
testJson.encodeToString(schema) shouldEqualJson $$"""
testJson.encodeToString(schema) shouldEqualJson /* language=json */ $$"""
{
"type": "object",
"properties": {
Expand Down
20 changes: 14 additions & 6 deletions src/commonTest/kotlin/serialization/JsonSchemaSerializerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,31 @@ package com.xemantic.ai.tool.schema.serialization

import com.xemantic.ai.tool.schema.JsonSchema
import com.xemantic.ai.tool.schema.test.testJson
import com.xemantic.kotlin.test.be
import com.xemantic.kotlin.test.have
import com.xemantic.kotlin.test.should
import io.kotest.assertions.json.shouldEqualJson
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.instanceOf
import kotlin.test.Test

class JsonSchemaSerializerTest {

@Test
fun `should decode JsonSchema reference`() {
/* language=json */
val json = $$"""
{
"$ref": "#/definitions/foo"
}
""".trimIndent()
val ref = testJson.decodeFromString<JsonSchema>(json)
ref shouldBe instanceOf<JsonSchema.Ref>()
(ref as JsonSchema.Ref).ref shouldBe "#/definitions/foo"
"""
testJson.decodeFromString<JsonSchema>(json) should {
be<JsonSchema.Ref>()
have(ref == "#/definitions/foo")
}
}

@Test
fun `decode JSON Schema from JSON`() {
/* language=json */
val json = $$"""
{
"type": "object",
Expand Down Expand Up @@ -181,7 +185,11 @@ class JsonSchemaSerializerTest {
}
}
"""

// when
val schema = testJson.decodeFromString<JsonSchema>(json)

// then
schema.toString() shouldEqualJson json
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class JavaBigDecimalToSchemaTest {
@Test
fun `should represent Java BigDecimal as String with pattern and description JSON Schema`() {
val schema = jsonSchemaOf<FinancialReport>()
testJson.encodeToString(schema) shouldEqualJson $$"""
testJson.encodeToString(schema) shouldEqualJson /* language=json */ $$"""
{
"type": "object",
"properties": {
Expand Down

0 comments on commit 759dcba

Please sign in to comment.