Skip to content

Commit

Permalink
adding more tests and revising docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrapyre committed May 4, 2024
1 parent 9253c10 commit cdd623a
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 33 deletions.
2 changes: 1 addition & 1 deletion docs/basic-building-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 0 additions & 1 deletion tests/shared/src/test/scala-2/zio/schema/SchemaGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
60 changes: 30 additions & 30 deletions tests/shared/src/test/scala-2/zio/schema/StandardTypeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zio.schema.codec

import java.time.{ ZoneId, ZoneOffset }
import java.util.UUID

import scala.collection.immutable.ListMap

Expand Down Expand Up @@ -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")(
Expand Down Expand Up @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit cdd623a

Please sign in to comment.