Skip to content

Commit

Permalink
added test for enum & const checking
Browse files Browse the repository at this point in the history
  • Loading branch information
ghik committed Apr 22, 2024
1 parent 4b82ab1 commit 2cc0172
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sttp.apispec.validation

import sttp.apispec.{Pattern, Schema, SchemaType}
import sttp.apispec.{ExampleSingleValue, ExampleValue, Pattern, Schema, SchemaType}

sealed abstract class SchemaCompatibilityIssue extends Product {
def description: String
Expand Down Expand Up @@ -47,8 +47,8 @@ case class TypeMismatch(

case class EnumMismatch(
// None indicates that the writer schema has no enum values
incompatibleWriterValues: Option[List[Any]],
readerValues: List[Any]
incompatibleWriterValues: Option[List[ExampleValue]],
readerValues: List[ExampleValue]
) extends SchemaCompatibilityIssue {
def description: String = {
val writerValuesRepr =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sttp.apispec.validation

import org.scalatest.funsuite.AnyFunSuite
import sttp.apispec.{Pattern, Schema, SchemaType}
import sttp.apispec.{ExampleSingleValue, Pattern, Schema, SchemaType}

import scala.collection.immutable.ListMap

Expand Down Expand Up @@ -130,5 +130,36 @@ class SchemaComparatorTest extends AnyFunSuite {
))
}

test("enum and const") {
def enums(values: Any*): List[ExampleSingleValue] =
values.toList.map(ExampleSingleValue)

def enumSchema(values: String*): Schema = values.toList match {
case single :: Nil => stringSchema.copy(enum = Some(List(single).map(ExampleSingleValue)))
case multiple => stringSchema.copy(enum = Some(multiple.map(ExampleSingleValue)))
}

assert(compare(enumSchema("a"), stringSchema) == Nil)
assert(compare(enumSchema("a"), enumSchema("a")) == Nil)
assert(compare(enumSchema("a"), enumSchema("a", "b")) == Nil)
assert(compare(enumSchema("a", "b"), enumSchema("a", "b", "c")) == Nil)

assert(compare(stringSchema, enumSchema("a", "b")) == List(
EnumMismatch(None, enums("a", "b"))
))
assert(compare(enumSchema("a"), enumSchema("b")) == List(
EnumMismatch(Some(enums("a")), enums("b"))
))
assert(compare(enumSchema("a"), enumSchema("b", "c")) == List(
EnumMismatch(Some(enums("a")), enums("b", "c"))
))
assert(compare(enumSchema("a", "b"), enumSchema("c")) == List(
EnumMismatch(Some(enums("a", "b")), enums("c"))
))
assert(compare(enumSchema("a", "b"), enumSchema("b", "c")) == List(
EnumMismatch(Some(enums("a")), enums("b", "c"))
))
}

//TODO significantly more tests
}

0 comments on commit 2cc0172

Please sign in to comment.