Skip to content

Commit

Permalink
Refactor Protobuf test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszKubuszok committed Sep 25, 2023
1 parent b17cc21 commit 8ae8524
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.scalaland.chimney.{partial, PartialTransformer}

// TODO: not yet published
trait ProtobufsPartialTransformerImplicits {

final type IsEmpty = scalapb.GeneratedOneof { type ValueType = Nothing }
// TODO: not yet published
implicit def partialTransformerEmptyOneOfInstance[From <: IsEmpty, To]: PartialTransformer[From, To] =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.scalaland.chimney

// format: off
import io.scalaland.chimney.dsl._
// format: on
import io.scalaland.chimney.examples.pb
import io.scalaland.chimney.fixtures.{addressbook, order}

class ProtobufBuildInSpec extends ChimneySpec {

test("transform value classes between their primitive representations") {

addressbook.PersonName("John").transformInto[String] ==> "John"
addressbook.PersonId(5).transformInto[Int] ==> 5
addressbook.Email("[email protected]").transformInto[String] ==> "[email protected]"
}

test("not compile if target type is wrong for value class") {

compileErrorsFixed(""" addressbook.PersonName("John").transformInto[Int] """)
.check(
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.addressbook.PersonName to scala.Int"
)

compileErrorsFixed(""" addressbook.PersonId(5).transformInto[String] """)
.check(
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.addressbook.PersonId to java.lang.String"
)

compileErrorsFixed(""" addressbook.Email("[email protected]").transformInto[Float] """)
.check(
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.addressbook.Email to scala.Float"
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.scalaland.chimney

// format: off
import io.scalaland.chimney.dsl._
// format: on
import io.scalaland.chimney.examples.pb
import io.scalaland.chimney.fixtures.{addressbook, order}

class ProtobufEnumSpec extends ChimneySpec {

test("transform enum represented as sealed trait hierarchy") {

(addressbook.MOBILE: addressbook.PhoneType)
.transformInto[pb.addressbook.PhoneType] ==> pb.addressbook.PhoneType.MOBILE
(addressbook.HOME: addressbook.PhoneType).transformInto[pb.addressbook.PhoneType] ==> pb.addressbook.PhoneType.HOME
(addressbook.WORK: addressbook.PhoneType).transformInto[pb.addressbook.PhoneType] ==> pb.addressbook.PhoneType.WORK
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,7 @@ import io.scalaland.chimney.dsl._
import io.scalaland.chimney.examples.pb
import io.scalaland.chimney.fixtures.{addressbook, order}

class PBTransformationSpec extends ChimneySpec {

test("transform value classes between their primitive representations") {

addressbook.PersonName("John").transformInto[String] ==> "John"
addressbook.PersonId(5).transformInto[Int] ==> 5
addressbook.Email("[email protected]").transformInto[String] ==> "[email protected]"
}

test("not compile if target type is wrong for value class") {

compileErrorsFixed(""" addressbook.PersonName("John").transformInto[Int] """)
.check(
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.addressbook.PersonName to scala.Int"
)

compileErrorsFixed(""" addressbook.PersonId(5).transformInto[String] """)
.check(
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.addressbook.PersonId to java.lang.String"
)

compileErrorsFixed(""" addressbook.Email("[email protected]").transformInto[Float] """)
.check(
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.addressbook.Email to scala.Float"
)
}

test("transform enum represented as sealed trait hierarchy") {

(addressbook.MOBILE: addressbook.PhoneType)
.transformInto[pb.addressbook.PhoneType] ==> pb.addressbook.PhoneType.MOBILE
(addressbook.HOME: addressbook.PhoneType).transformInto[pb.addressbook.PhoneType] ==> pb.addressbook.PhoneType.HOME
(addressbook.WORK: addressbook.PhoneType).transformInto[pb.addressbook.PhoneType] ==> pb.addressbook.PhoneType.WORK
}
class ProtobufMessageSpec extends ChimneySpec {

group("transform bigger case classes") {

Expand Down Expand Up @@ -172,51 +139,4 @@ class PBTransformationSpec extends ChimneySpec {
}
}
}

group("transformer sealed traits generated from oneof") {

test("AddressBookType (oneof value - sealed contains single-value wrappers around actual products)") {
val domainType: addressbook.AddressBookType = addressbook.AddressBookType.Private("test")
val pbType: pb.addressbook.AddressBookType =
pb.addressbook.AddressBookType.of(
pb.addressbook.AddressBookType.Value.Private(pb.addressbook.AddressBookType.Private.of("test"))
)

domainType.into[pb.addressbook.AddressBookType.Value].transform ==> pbType.value

pbType.value
.intoPartial[addressbook.AddressBookType]
.withCoproductInstancePartial[pb.addressbook.AddressBookType.Value.Empty.type](_ => partial.Result.fromEmpty)
.transform
.asOption ==> Some(domainType)
locally {
// format: off
import protobufs._
// format: on
pbType.value.intoPartial[addressbook.AddressBookType].transform.asOption ==> Some(domainType)
}
}

test("CustomerStatus (oneof sealed_value - flat representation with additional Empty vase)") {
val domainStatus: order.CustomerStatus = order.CustomerStatus.CustomerRegistered
val pbStatus: pb.order.CustomerStatus = pb.order.CustomerRegistered()

domainStatus.into[pb.order.CustomerStatus].transform ==> pbStatus

pbStatus
.intoPartial[order.CustomerStatus]
.withCoproductInstancePartial[pb.order.CustomerStatus.Empty.type](_ => partial.Result.fromEmpty)
.withCoproductInstance[pb.order.CustomerStatus.NonEmpty](_.transformInto[order.CustomerStatus])
.transform
.asOption ==> Some(domainStatus)
}

test("PaymentStatus (oneof sealed_value_optional - flat representation wrapped in Option)") {
val domainStatus: Option[order.PaymentStatus] = Option(order.PaymentStatus.PaymentRequested)
val pbStatus: Option[pb.order.PaymentStatus] = Option(pb.order.PaymentRequested())

domainStatus.into[Option[pb.order.PaymentStatus]].transform ==> pbStatus
pbStatus.into[Option[order.PaymentStatus]].transform ==> domainStatus
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.scalaland.chimney

// format: off
import io.scalaland.chimney.dsl._
// format: on
import io.scalaland.chimney.examples.pb
import io.scalaland.chimney.fixtures.{addressbook, order}

class ProtobufOneOfSpec extends ChimneySpec {

group("transformer sealed traits generated from oneof") {

test("AddressBookType (oneof value - sealed contains single-value wrappers around actual products)") {
val domainType: addressbook.AddressBookType = addressbook.AddressBookType.Private("test")
val pbType: pb.addressbook.AddressBookType =
pb.addressbook.AddressBookType.of(
pb.addressbook.AddressBookType.Value.Private(pb.addressbook.AddressBookType.Private.of("test"))
)

domainType.into[pb.addressbook.AddressBookType.Value].transform ==> pbType.value

pbType.value
.intoPartial[addressbook.AddressBookType]
.withCoproductInstancePartial[pb.addressbook.AddressBookType.Value.Empty.type](_ => partial.Result.fromEmpty)
.transform
.asOption ==> Some(domainType)
locally {
// format: off
import protobufs._
// format: on
pbType.value.intoPartial[addressbook.AddressBookType].transform.asOption ==> Some(domainType)
}
}

test("CustomerStatus (oneof sealed_value - flat representation with additional Empty vase)") {
val domainStatus: order.CustomerStatus = order.CustomerStatus.CustomerRegistered
val pbStatus: pb.order.CustomerStatus = pb.order.CustomerRegistered()

domainStatus.into[pb.order.CustomerStatus].transform ==> pbStatus

pbStatus
.intoPartial[order.CustomerStatus]
.withCoproductInstancePartial[pb.order.CustomerStatus.Empty.type](_ => partial.Result.fromEmpty)
.withCoproductInstance[pb.order.CustomerStatus.NonEmpty](_.transformInto[order.CustomerStatus])
.transform
.asOption ==> Some(domainStatus)
}

test("PaymentStatus (oneof sealed_value_optional - flat representation wrapped in Option)") {
val domainStatus: Option[order.PaymentStatus] = Option(order.PaymentStatus.PaymentRequested)
val pbStatus: Option[pb.order.PaymentStatus] = Option(pb.order.PaymentRequested())

domainStatus.into[Option[pb.order.PaymentStatus]].transform ==> pbStatus
pbStatus.into[Option[order.PaymentStatus]].transform ==> domainStatus
}
}
}

0 comments on commit 8ae8524

Please sign in to comment.