From 1d375d94793ea3e9879eb00b750406e475e705bb Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Gupta Date: Wed, 12 Jun 2024 09:55:47 +0000 Subject: [PATCH] add test case for BigDecimal's codec (#663) --- .../schema/codec/BsonSchemaCodecSpec.scala | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/zio-schema-bson/src/test/scala/zio/schema/codec/BsonSchemaCodecSpec.scala b/zio-schema-bson/src/test/scala/zio/schema/codec/BsonSchemaCodecSpec.scala index 0ee22827b..941dc1450 100644 --- a/zio-schema-bson/src/test/scala/zio/schema/codec/BsonSchemaCodecSpec.scala +++ b/zio-schema-bson/src/test/scala/zio/schema/codec/BsonSchemaCodecSpec.scala @@ -2,12 +2,12 @@ package zio.schema.codec import scala.reflect.{ ClassTag, classTag } -import org.bson._ import org.bson.codecs.configuration.CodecRegistry import org.bson.codecs.{ Codec => BCodec, DecoderContext, EncoderContext } import org.bson.conversions.Bson import org.bson.io.BasicOutputBuffer -import org.bson.types.ObjectId +import org.bson.types.{ Decimal128, ObjectId } +import org.bson.{ BsonDecimal128, _ } import zio.bson.BsonBuilder._ import zio.bson._ @@ -24,6 +24,13 @@ object BsonSchemaCodecSpec extends ZIOSpecDefault { implicit lazy val codec: BsonCodec[SimpleClass] = BsonSchemaCodec.bsonCodec(schema) } + case class BigDecimalClass(value: BigDecimal) + + object BigDecimalClass { + implicit val schema: Schema[BigDecimalClass] = DeriveSchema.gen + implicit lazy val codec: BsonCodec[BigDecimalClass] = BsonSchemaCodec.bsonCodec(schema) + } + sealed trait Tree object Tree { @@ -79,6 +86,10 @@ object BsonSchemaCodecSpec extends ZIOSpecDefault { } yield Customer(id, name, age, friends) } + // Custom generator for BigDecimal values with rounding to ensure exact representation as Decimal128 + def genRoundedBigDecimal(scale: Int): Gen[Any, BigDecimal] = + Gen.double.map(d => BigDecimal(d).setScale(scale, BigDecimal.RoundingMode.HALF_UP)) + def spec: Spec[TestEnvironment with Scope, Any] = suite("BsonSchemaCodecSpec")( suite("round trip")( roundTripTest("SimpleClass")( @@ -107,6 +118,12 @@ object BsonSchemaCodecSpec extends ZIOSpecDefault { Customer.example.invitedFriends.map(_.value.toBsonValue): _* ) ) + ), + roundTripTest("BigDecimalClass")( + // 14 decimal places in the assert value below + genRoundedBigDecimal(14).map(BigDecimalClass(_)), + BigDecimalClass(BigDecimal("279.00000000000000")), + doc("value" -> new BsonDecimal128(Decimal128.parse("279.00000000000000"))) ) ), suite("configuration")(