Skip to content

Commit

Permalink
Split tests for primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
nomisRev committed Jul 9, 2024
1 parent 3d8d418 commit 3253088
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,18 @@ private class OpenAPITransformer(private val openAPI: OpenAPI) {
is Type.Basic ->
when (type) {
Type.Basic.Array -> collection(context)
Type.Basic.Boolean -> Primitive.Boolean(default?.toString()?.toBoolean(), description)
Type.Basic.Boolean -> Primitive.Boolean(default?.toString()?.toBooleanStrictOrNull(), description)
Type.Basic.Integer -> Primitive.Int(default?.toString()?.toIntOrNull(), description)
Type.Basic.Number -> Primitive.Double(default?.toString()?.toDoubleOrNull(), description)
Type.Basic.String ->
if (format == "binary") Model.OctetStream(description)
else Primitive.String(default?.toString(), description)
else Primitive.String(
when(val default = default) {
is ExampleValue.Multiple ->
default.values.joinToString()
is ExampleValue.Single -> default.value
null -> null
}, description)
Type.Basic.Object -> toObject(context)
Type.Basic.Null -> TODO("Schema.Type.Basic.Null")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,34 +314,6 @@ class ModelTest {
assertEquals(setOf(expected), actual)
}

@Test
fun int() {
val actual =
testAPI
.copy(
components =
Components(schemas = mapOf("Primitive.Int" to value(Schema(type = Type.Basic.Integer))))
)
.models()
val expected = Primitive.Int(default = null, description = null)
assertEquals(setOf(expected), actual)
}

@Test
fun string() {
val actual =
testAPI
.copy(
components =
Components(
schemas = mapOf("Primitive.String" to value(Schema(type = Type.Basic.String)))
)
)
.models()
val expected = Primitive.String(default = null, description = null)
assertEquals(setOf(expected), actual)
}

@Test
fun binary() {
val actual =
Expand All @@ -360,35 +332,8 @@ class ModelTest {
assertEquals(setOf(expected), actual)
}

@Test
fun boolean() {
val actual =
testAPI
.copy(
components =
Components(
schemas = mapOf("Primitive.Boolean" to value(Schema(type = Type.Basic.Boolean)))
)
)
.models()
val expected = Primitive.Boolean(default = null, description = null)
assertEquals(setOf(expected), actual)
}

@Test
fun double() {
val actual =
testAPI
.copy(
components =
Components(
schemas = mapOf("Primitive.Number" to value(Schema(type = Type.Basic.Number)))
)
)
.models()
val expected = Primitive.Double(default = null, description = null)
assertEquals(setOf(expected), actual)
}


@Test
fun defaultArrayIsList() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package io.github.nomisrev.openapi

import io.github.nomisrev.openapi.Model.Primitive
import io.github.nomisrev.openapi.ReferenceOr.Companion.value
import io.github.nomisrev.openapi.Schema.Type
import org.junit.jupiter.api.assertThrows
import kotlin.test.Test
import kotlin.test.assertEquals

class PrimitiveTest {
@Test
fun double() {
primitive(
Schema(
type = Type.Basic.Number,
default = ExampleValue("1"),
description = "My Desc"
),
Primitive.Double(1.0, description = "My Desc")
)
}

@Test
fun doubleIncorrectDefault() {
primitive(
Schema(
type = Type.Basic.Number,
default = ExampleValue("Nonsense Value"),
description = "My Desc"
),
Primitive.Double(null, description = "My Desc")
)
}

@Test
fun doubleIncorrectDefaultMultiple() {
primitive(
Schema(
type = Type.Basic.Number,
default = ExampleValue.Multiple(listOf("Nonsense", "Value")),
description = "My Desc"
),
Primitive.Double(null, description = "My Desc")
)
}

@Test
fun boolean() {
primitive(
Schema(
type = Type.Basic.Boolean,
default = ExampleValue("true"),
description = "My Desc"
),
Primitive.Boolean(true, description = "My Desc")
)
}

@Test
fun booleanIncorrectDefault() {
primitive(
Schema(
type = Type.Basic.Boolean,
default = ExampleValue("Nonsense Value"),
description = "My Desc"
),
Primitive.Boolean(null, description = "My Desc")
)
}

@Test
fun booleanIncorrectDefaultMultiple() {
primitive(
Schema(
type = Type.Basic.Boolean,
default = ExampleValue.Multiple(listOf("Nonsense", "Value")),
description = "My Desc"
),
Primitive.Boolean(null, description = "My Desc")
)
}

@Test
fun integer() {
primitive(
Schema(
type = Type.Basic.Integer,
default = ExampleValue("2"),
description = "My Desc"
),
Primitive.Int(2, description = "My Desc")
)
}

@Test
fun integerIncorrectDefault() {
primitive(
Schema(
type = Type.Basic.Integer,
default = ExampleValue("Nonsense Value"),
description = "My Desc"
),
Primitive.Int(null, description = "My Desc")
)
}

@Test
fun integerIncorrectDefaultMultiple() {
primitive(
Schema(
type = Type.Basic.Integer,
default = ExampleValue.Multiple(listOf("Nonsense", "Value")),
description = "My Desc"
),
Primitive.Int(null, description = "My Desc")
)
}

@Test
fun string() {
primitive(
Schema(
type = Type.Basic.String,
default = ExampleValue("Some Text"),
description = "My Desc"
),
Primitive.String("Some Text", description = "My Desc")
)
}

@Test
fun stringIntegerDefaultRemainsString() {
primitive(
Schema(
type = Type.Basic.String,
default = ExampleValue("999"),
description = "My Desc"
),
Primitive.String("999", description = "My Desc")
)
}

@Test
fun stringIncorrectDefaultMultiple() {
primitive(
Schema(
type = Type.Basic.String,
default = ExampleValue.Multiple(listOf("Nonsense", "Value")),
description = "My Desc"
),
Primitive.String("Nonsense, Value", description = "My Desc")
)
}

@Test
fun nullNotSupported() {
assertThrows<NotImplementedError> {
primitive(
Schema(
type = Type.Basic.Null,
default = ExampleValue.Multiple(listOf("Nonsense", "Value")),
description = "My Desc"
),
Primitive.String("Nonsense, Value", description = "My Desc")
)
}
}

private fun primitive(
schema: Schema,
model: Model
) {
val actual =
testAPI
.copy(components = Components(schemas = mapOf("Primitive" to value(schema))))
.models()
assertEquals(setOf(model), actual)
}
}

0 comments on commit 3253088

Please sign in to comment.