From cdd623a90efa5b3475ea2ae7a0ece19f2ce205b0 Mon Sep 17 00:00:00 2001 From: Andrapyre <42009361+Andrapyre@users.noreply.github.com> Date: Sat, 4 May 2024 03:09:55 +0200 Subject: [PATCH] adding more tests and revising docs --- docs/basic-building-blocks.md | 2 +- .../test/scala-2/zio/schema/SchemaGen.scala | 1 - .../scala-2/zio/schema/StandardTypeGen.scala | 60 +++++++++---------- .../zio/schema/codec/AvroCodecSpec.scala | 12 ++++ .../zio/schema/codec/JsonCodecSpec.scala | 20 ++++++- .../zio/schema/codec/ProtobufCodecSpec.scala | 8 +++ .../zio/schema/codec/ThriftCodecSpec.scala | 7 +++ .../main/scala/zio/schema/DynamicValue.scala | 1 + 8 files changed, 78 insertions(+), 33 deletions(-) diff --git a/docs/basic-building-blocks.md b/docs/basic-building-blocks.md index c75099a92..8e0c2aa76 100644 --- a/docs/basic-building-blocks.md +++ b/docs/basic-building-blocks.md @@ -28,7 +28,7 @@ case class Primitive[A](standardType: StandardType[A]) extends Schema[A] Primitive values are represented using the `Primitive[A]` type class and represent the elements, that we cannot further define through other means. If we visualize our data structure as a tree, primitives are the leaves. -ZIO Schema provides a number of built-in primitive types, that we can use to represent our data. These can be found in the [`StandardType`](https://github.com/zio/zio-schema/blob/main/zio-schema/shared/src/main/scala/zio/schema/StandardType.scala) companion-object: +ZIO Schema provides a number of built-in primitive types, that we can use to represent our data. These can be found in the [`StandardType`](https://github.com/zio/zio-schema/blob/main/zio-schema/jvm/src/main/scala/zio/schema/StandardType.scala) companion-object: ```scala sealed trait StandardType[A] diff --git a/tests/shared/src/test/scala-2/zio/schema/SchemaGen.scala b/tests/shared/src/test/scala-2/zio/schema/SchemaGen.scala index c7d7e5700..ebf06762c 100644 --- a/tests/shared/src/test/scala-2/zio/schema/SchemaGen.scala +++ b/tests/shared/src/test/scala-2/zio/schema/SchemaGen.scala @@ -2,7 +2,6 @@ package zio.schema import scala.collection.immutable.ListMap import zio.Chunk -import zio.schema.SchemaGen.SchemaTest import zio.test.{ Gen, Sized } object SchemaGen { diff --git a/tests/shared/src/test/scala-2/zio/schema/StandardTypeGen.scala b/tests/shared/src/test/scala-2/zio/schema/StandardTypeGen.scala index e85b4c8b0..1106f08d9 100644 --- a/tests/shared/src/test/scala-2/zio/schema/StandardTypeGen.scala +++ b/tests/shared/src/test/scala-2/zio/schema/StandardTypeGen.scala @@ -6,39 +6,39 @@ import zio.test.{ Gen, Sized } object StandardTypeGen { + //IMPORTANT! - Updating the following list without updating the schema primitive case set in zio.schema.DynamicValue.schema will trigger a set of very obscure test failures val anyStandardType: Gen[Any, StandardType[_]] = Gen.fromIterable( List( - (StandardType.StringType), - (StandardType.BoolType), - (StandardType.ShortType), - (StandardType.IntType), - (StandardType.LongType), - (StandardType.FloatType), - (StandardType.DoubleType), - (StandardType.BinaryType), - (StandardType.BigDecimalType), - (StandardType.BigIntegerType), - (StandardType.CharType), - (StandardType.UUIDType), - (StandardType.DayOfWeekType), - (StandardType.DurationType), - (StandardType.InstantType), - (StandardType.LocalDateType), - (StandardType.LocalDateTimeType), - (StandardType.LocalTimeType), - (StandardType.MonthType), - (StandardType.MonthDayType), - (StandardType.OffsetDateTimeType), - (StandardType.OffsetTimeType), - (StandardType.PeriodType), - (StandardType.YearType), - (StandardType.YearMonthType), - (StandardType.ZonedDateTimeType), - (StandardType.ZoneIdType), - (StandardType.CurrencyType) + StandardType.StringType, + StandardType.BoolType, + StandardType.ShortType, + StandardType.IntType, + StandardType.LongType, + StandardType.FloatType, + StandardType.DoubleType, + StandardType.BinaryType, + StandardType.BigDecimalType, + StandardType.BigIntegerType, + StandardType.CharType, + StandardType.UUIDType, + StandardType.DayOfWeekType, + StandardType.DurationType, + StandardType.InstantType, + StandardType.LocalDateType, + StandardType.LocalDateTimeType, + StandardType.LocalTimeType, + StandardType.MonthType, + StandardType.MonthDayType, + StandardType.OffsetDateTimeType, + StandardType.OffsetTimeType, + StandardType.PeriodType, + StandardType.YearType, + StandardType.YearMonthType, + StandardType.ZonedDateTimeType, + StandardType.ZoneIdType, + StandardType.CurrencyType, + StandardType.ZoneOffsetType ) - //FIXME For some reason adding this causes other unrelated tests to break. -// Gen.const(StandardType.ZoneOffset) ) val javaBigInt: Gen[Any, JBigInt] = diff --git a/zio-schema-avro/src/test/scala-2/zio/schema/codec/AvroCodecSpec.scala b/zio-schema-avro/src/test/scala-2/zio/schema/codec/AvroCodecSpec.scala index 3e91df872..11096603a 100644 --- a/zio-schema-avro/src/test/scala-2/zio/schema/codec/AvroCodecSpec.scala +++ b/zio-schema-avro/src/test/scala-2/zio/schema/codec/AvroCodecSpec.scala @@ -194,6 +194,11 @@ object AvroCodecSpec extends ZIOSpecDefault { val bytes = codec.encode(UUID.randomUUID()) assertTrue(bytes.length == 37) }, + test("Encode Currency") { + val codec = AvroCodec.schemaBasedBinaryCodec[java.util.Currency] + val bytes = codec.encode(java.util.Currency.getInstance("USD")) + assertTrue(bytes.length == 4) + }, test("Encode BigDecimal") { val codec = AvroCodec.schemaBasedBinaryCodec[BigDecimal] val bytes = codec.encode(BigDecimal.valueOf(42.0)) @@ -468,6 +473,13 @@ object AvroCodecSpec extends ZIOSpecDefault { val result = codec.decode(bytes) assertTrue(result == Right(uuid)) }, + test("Decode Currency") { + val codec = AvroCodec.schemaBasedBinaryCodec[java.util.Currency] + val currency = java.util.Currency.getInstance(java.util.Locale.getDefault) + val bytes = codec.encode(currency) + val result = codec.decode(bytes) + assertTrue(result == Right(currency)) + }, test("Decode BigDecimal") { val codec = AvroCodec.schemaBasedBinaryCodec[BigDecimal] val bigDecimal = BigDecimal(42) diff --git a/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala b/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala index 923c19da5..f4f576458 100644 --- a/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala +++ b/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala @@ -1,6 +1,7 @@ package zio.schema.codec import java.time.{ ZoneId, ZoneOffset } +import java.util.UUID import scala.collection.immutable.ListMap @@ -46,6 +47,15 @@ object JsonCodecSpec extends ZIOSpecDefault { }, test("ZoneId") { assertEncodesJson(Schema.Primitive(StandardType.ZoneIdType), ZoneId.systemDefault()) + }, + test("UUID") { + assertEncodesJson(Schema.Primitive(StandardType.UUIDType), UUID.randomUUID()) + }, + test("Currency") { + assertEncodesJson( + Schema.Primitive(StandardType.CurrencyType), + java.util.Currency.getInstance(java.util.Locale.getDefault) + ) } ), suite("fallback")( @@ -415,7 +425,15 @@ object JsonCodecSpec extends ZIOSpecDefault { test("any") { check(Gen.string)(s => assertDecodes(Schema[String], s, stringify(s))) } - ) + ), + test("UUID") { + check(Gen.uuid)(uuid => assertDecodes(Schema[UUID], uuid, stringify(uuid.toString))) + }, + test("Currency") { + check(Gen.currency)( + currency => assertDecodes(Schema[java.util.Currency], currency, stringify(currency.getCurrencyCode)) + ) + } ), suite("generic record")( test("with extra fields") { diff --git a/zio-schema-protobuf/shared/src/test/scala-2/zio/schema/codec/ProtobufCodecSpec.scala b/zio-schema-protobuf/shared/src/test/scala-2/zio/schema/codec/ProtobufCodecSpec.scala index dd4a94db7..a0c9c482d 100644 --- a/zio-schema-protobuf/shared/src/test/scala-2/zio/schema/codec/ProtobufCodecSpec.scala +++ b/zio-schema-protobuf/shared/src/test/scala-2/zio/schema/codec/ProtobufCodecSpec.scala @@ -264,6 +264,14 @@ object ProtobufCodecSpec extends ZIOSpecDefault { } yield assert(ed)(equalTo(Chunk(value))) && assert(ed2)(equalTo(value)) } }, + test("currencies") { + check(Gen.currency) { value => + for { + ed <- encodeAndDecode(Schema[java.util.Currency], value) + ed2 <- encodeAndDecodeNS(Schema[java.util.Currency], value) + } yield assert(ed)(equalTo(Chunk(value))) && assert(ed2)(equalTo(value)) + } + }, test("day of weeks") { check(Gen.dayOfWeek) { value => for { diff --git a/zio-schema-thrift/src/test/scala-2/zio/schema/codec/ThriftCodecSpec.scala b/zio-schema-thrift/src/test/scala-2/zio/schema/codec/ThriftCodecSpec.scala index a45fb452b..1047810a5 100644 --- a/zio-schema-thrift/src/test/scala-2/zio/schema/codec/ThriftCodecSpec.scala +++ b/zio-schema-thrift/src/test/scala-2/zio/schema/codec/ThriftCodecSpec.scala @@ -273,6 +273,13 @@ object ThriftCodecSpec extends ZIOSpecDefault { ed2 <- encodeAndDecodeNS(Schema[UUID], value) } yield assert(ed)(equalTo(Chunk(value))) && assert(ed2)(equalTo(value)) }, + test("currencies") { + val value = java.util.Currency.getInstance(java.util.Locale.getDefault) + for { + ed <- encodeAndDecode(Schema[java.util.Currency], value) + ed2 <- encodeAndDecodeNS(Schema[java.util.Currency], value) + } yield assert(ed)(equalTo(Chunk(value))) && assert(ed2)(equalTo(value)) + }, test("day of weeks") { val value = DayOfWeek.of(3) for { diff --git a/zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala b/zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala index 6d34df523..75289f3e0 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala @@ -239,6 +239,7 @@ object DynamicValue { lazy val typeId: TypeId = TypeId.parse("zio.schema.DynamicValue") + //TODO: Refactor case set so that adding a new type to StandardType without updating the below list will trigger a compile error lazy val schema: Schema[DynamicValue] = Schema.EnumN( typeId,