Skip to content

Commit

Permalink
Add property based tests for generated Dynamic Value
Browse files Browse the repository at this point in the history
  • Loading branch information
BijenderKumar1 committed Aug 16, 2024
1 parent 2ccf9b6 commit e630292
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
25 changes: 25 additions & 0 deletions tests/shared/src/test/scala-2/zio/schema/DynamicValueGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ object DynamicValueGen {
}
}

// Method to generate arbitrary DynamicValue
def anyDynamicValue: Gen[Sized, DynamicValue] =
Gen.oneOf(
anyPrimitiveDynamicValue(StandardType.StringType),
anyPrimitiveDynamicValue(StandardType.IntType),
anyPrimitiveDynamicValue(StandardType.BoolType),
anyPrimitiveDynamicValue(StandardType.DoubleType),
anyPrimitiveDynamicValue(StandardType.UUIDType),
anyPrimitiveDynamicValue(StandardType.LocalDateType),
anyPrimitiveDynamicValue(StandardType.LocalDateTimeType),
anyPrimitiveDynamicValue(StandardType.LocalTimeType),
anyPrimitiveDynamicValue(StandardType.InstantType),
anyPrimitiveDynamicValue(StandardType.DurationType),
anyPrimitiveDynamicValue(StandardType.ZoneIdType),
anyPrimitiveDynamicValue(StandardType.ZoneOffsetType),
anyPrimitiveDynamicValue(StandardType.ZonedDateTimeType),
anyPrimitiveDynamicValue(StandardType.YearType),
anyPrimitiveDynamicValue(StandardType.YearMonthType),
anyPrimitiveDynamicValue(StandardType.MonthType),
anyPrimitiveDynamicValue(StandardType.MonthDayType),
anyPrimitiveDynamicValue(StandardType.DayOfWeekType),
anyPrimitiveDynamicValue(StandardType.BigDecimalType),
anyPrimitiveDynamicValue(StandardType.BigIntegerType)
)

//scalafmt: { maxColumn = 400 }
def anyDynamicValueOfSchema[A](schema: Schema[A]): Gen[Sized, DynamicValue] =
schema match {
Expand Down
44 changes: 10 additions & 34 deletions tests/shared/src/test/scala-2/zio/schema/DynamicValueSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ import zio.test.{ Sized, TestConfig, _ }

object DynamicValueSpec extends ZIOSpecDefault {

case class Person(name: String, age: Int)

object Person {
implicit val schema: Schema[Person] = DeriveSchema.gen[Person]
}

case class User(name: String, age: Int)

object User {
implicit val schema: Schema[User] = DeriveSchema.gen[User]
}

def spec: Spec[Environment, Any] =
suite("DynamicValueSpec")(
suite("round-trip")(
Expand Down Expand Up @@ -115,29 +103,18 @@ object DynamicValueSpec extends ZIOSpecDefault {
),
suite("hashCode and equality consistency")(
test("hashCode does not change") {
val primitive1 = DynamicValue.Primitive(123, StandardType.IntType)
val hash1 = primitive1.hashCode()
val hash2 = primitive1.hashCode()
assert(hash1)(equalTo(hash2))
check(DynamicValueGen.anyDynamicValue) { dynamicValue =>
val hash1 = dynamicValue.hashCode()
val hash2 = dynamicValue.hashCode()
assert(hash1)(equalTo(hash2))
}
},
test("equivalent DynamicValues have same hashCode and equality") {
val person1 = Person("John Doe", 42)
val person2 = Person("John Doe", 42)

val dynamicPerson1 = DynamicValue.fromSchemaAndValue(Person.schema, person1)
val dynamicPerson2 = DynamicValue.fromSchemaAndValue(Person.schema, person2)

assert(dynamicPerson1.hashCode())(equalTo(dynamicPerson2.hashCode())) &&
assert(dynamicPerson1)(equalTo(dynamicPerson2))
},
test("different DynamicValues are not equal") {
val person1 = Person("John Doe", 42)
val person2 = Person("Jane Doe", 42)

val dynamicPerson1 = DynamicValue.fromSchemaAndValue(Person.schema, person1)
val dynamicPerson2 = DynamicValue.fromSchemaAndValue(Person.schema, person2)

assert(dynamicPerson1)(not(equalTo(dynamicPerson2)))
check(DynamicValueGen.anyDynamicValue) { dynamicValue =>
val dynamicValueCopy = dynamicValue // Assuming the generation ensures equivalence
assert(dynamicValue.hashCode())(equalTo(dynamicValueCopy.hashCode())) &&
assert(dynamicValue)(equalTo(dynamicValueCopy))
}
}
)
)
Expand All @@ -153,5 +130,4 @@ object DynamicValueSpec extends ZIOSpecDefault {
check(gen) { a =>
assert(schema.fromDynamic(schema.toDynamic(a)))(isRight(equalTo(a)))
}

}

0 comments on commit e630292

Please sign in to comment.