From 62826d13e1ca0ce6ec1b70048e50dd9c33f31f6d Mon Sep 17 00:00:00 2001 From: BijenderKumar1 Date: Sun, 28 Jul 2024 10:02:47 +0000 Subject: [PATCH] Add property based tests for generated Dynamic Value --- .../scala-2/zio/schema/DynamicValueGen.scala | 25 +++++++++++ .../scala-2/zio/schema/DynamicValueSpec.scala | 44 +++++-------------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/tests/shared/src/test/scala-2/zio/schema/DynamicValueGen.scala b/tests/shared/src/test/scala-2/zio/schema/DynamicValueGen.scala index d5a5e73d4..8be9d9b25 100644 --- a/tests/shared/src/test/scala-2/zio/schema/DynamicValueGen.scala +++ b/tests/shared/src/test/scala-2/zio/schema/DynamicValueGen.scala @@ -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 { diff --git a/tests/shared/src/test/scala-2/zio/schema/DynamicValueSpec.scala b/tests/shared/src/test/scala-2/zio/schema/DynamicValueSpec.scala index f3c990dd4..d518bfcc4 100644 --- a/tests/shared/src/test/scala-2/zio/schema/DynamicValueSpec.scala +++ b/tests/shared/src/test/scala-2/zio/schema/DynamicValueSpec.scala @@ -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")( @@ -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)) + } } ) ) @@ -153,5 +130,4 @@ object DynamicValueSpec extends ZIOSpecDefault { check(gen) { a => assert(schema.fromDynamic(schema.toDynamic(a)))(isRight(equalTo(a))) } - }