Skip to content

Commit

Permalink
test: RequireAllSourceFieldsUsed validation in product-to-product module
Browse files Browse the repository at this point in the history
  • Loading branch information
nimatrueway committed Sep 29, 2024
1 parent b0bf68e commit f770b01
Showing 1 changed file with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,91 @@ class TotalTransformerProductSpec extends ChimneySpec {
}
}

group("""setting .requireSourceFieldsUsedExcept(_.field1, _.field2)""") {
import shapes1.{Point, Rectangle, Triangle}

test("should fail if not all required source fields are used") {
compileErrors(
"""
Triangle(p1 = Point(0, 0), p2 = Point(2, 2), p3 = Point(2, 0))
.into[Rectangle]
.requireSourceFieldsUsedExcept()
.transform
"""
).check(
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.shapes1.Triangle to io.scalaland.chimney.fixtures.shapes1.Rectangle",
"source field(s) p3 are required to be used!"
)
}

test("should pass if all required source fields are either used or included in exceptions") {
Triangle(p1 = Point(0, 0), p2 = shapes1.Point(2, 2), p3 = shapes1.Point(2, 0))
.into[Rectangle]
.requireSourceFieldsUsedExcept(_.p3)
.transform ==> Rectangle(p1 = Point(0, 0), p2 = Point(2, 2))
}

test("should pass if all required source fields are used (withFieldRenamed)") {
case class AnotherRectangle(p1: Point, PPPP: Point)

Rectangle(p1 = Point(0, 0), p2 = Point(2, 2))
.into[AnotherRectangle]
.withFieldRenamed(_.p2, _.PPPP)
.requireSourceFieldsUsedExcept()
.transform ==> AnotherRectangle(p1 = Point(0, 0), PPPP = Point(2, 2))
}

test("should pass if all required source fields are used (enableCustomFieldNameComparison)") {
case class AnotherRectangle(p1: Point, P2: Point)

Rectangle(p1 = Point(0, 0), p2 = Point(2, 2))
.into[AnotherRectangle]
.requireSourceFieldsUsedExcept()
.enableCustomFieldNameComparison(TransformedNamesComparison.CaseInsensitiveEquality)
.transform ==> AnotherRectangle(p1 = Point(0, 0), P2 = Point(2, 2))
}

test("should pass if all required source fields are used (withFieldRenamed + enableCustomFieldNameComparison)") {
case class AnotherTriangle(p1: Point, P2: Point, PPPP: Point)

Triangle(p1 = Point(0, 0), p2 = Point(2, 2), p3 = Point(2, 0))
.into[AnotherTriangle]
.withFieldRenamed(_.p3, _.PPPP)
.requireSourceFieldsUsedExcept()
.enableCustomFieldNameComparison(TransformedNamesComparison.CaseInsensitiveEquality)
.transform ==> AnotherTriangle(p1 = Point(0, 0), P2 = Point(2, 2), PPPP = Point(2, 0))
}

test("should pass if all required source fields are either used or included in exceptions (withFieldRenamed + enableCustomFieldNameComparison)") {
case class AnotherTriangle(p11111: Point, P2: Point, PPPP: Point)

// without p1 in exceptions
compileErrors(
"""
Triangle(p1 = Point(0, 0), p2 = Point(2, 2), p3 = Point(2, 0))
.into[AnotherTriangle]
.withFieldRenamed(_.p3, _.PPPP)
.withFieldConst(_.p11111, Point(0, 0))
.requireSourceFieldsUsedExcept()
.enableCustomFieldNameComparison(TransformedNamesComparison.CaseInsensitiveEquality)
.transform ==> AnotherTriangle(p11111 = Point(0, 0), P2 = Point(2, 2), PPPP = Point(2, 0))
"""
).check(
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.shapes1.Triangle to io.scalaland.chimney.TotalTransformerProductSpec.AnotherTriangle",
"source field(s) p1 are required to be used!"
)

// with p1 in exceptions
Triangle(p1 = Point(0, 0), p2 = Point(2, 2), p3 = Point(2, 0))
.into[AnotherTriangle]
.withFieldRenamed(_.p3, _.PPPP)
.withFieldConst(_.p11111, Point(0, 0))
.requireSourceFieldsUsedExcept(_.p1)
.enableCustomFieldNameComparison(TransformedNamesComparison.CaseInsensitiveEquality)
.transform ==> AnotherTriangle(p11111 = Point(0, 0), P2 = Point(2, 2), PPPP = Point(2, 0))
}
}

group("flag .enableDefaultValues") {

test("should be disabled by default") {
Expand Down

0 comments on commit f770b01

Please sign in to comment.