-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
dynamodb/src/it/scala/zio/dynamodb/TypeSafeApiNarrowSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package zio.dynamodb | ||
|
||
import zio.dynamodb.DynamoDBQuery.{ getItem, put } | ||
import zio.Scope | ||
import zio.test.Spec | ||
import zio.test.assertTrue | ||
import zio.test.TestEnvironment | ||
import zio.schema.Schema | ||
import zio.schema.DeriveSchema | ||
import zio.schema.annotation.discriminatorName | ||
import zio.test.TestAspect | ||
import zio.dynamodb.DynamoDBQuery.getWithNarrow | ||
|
||
object TypeSafeApiNarrowSpec extends DynamoDBLocalSpec { | ||
|
||
object dynamo { | ||
@discriminatorName("invoiceType") | ||
sealed trait Invoice { | ||
def id: String | ||
} | ||
object Invoice { | ||
final case class Unpaid(id: String) extends Invoice | ||
object Unpaid { | ||
implicit val schema: Schema.CaseClass1[String, Unpaid] = DeriveSchema.gen[Unpaid] | ||
val id = ProjectionExpression.accessors[Unpaid] | ||
} | ||
final case class Paid(id: String, amount: Int) extends Invoice | ||
object Paid { | ||
implicit val schema: Schema.CaseClass2[String, Int, Paid] = DeriveSchema.gen[Paid] | ||
val (id, amount) = ProjectionExpression.accessors[Paid] | ||
} | ||
implicit val schema: Schema.Enum2[Unpaid, Paid, Invoice] = | ||
DeriveSchema.gen[Invoice] | ||
val (unpaid, paid) = ProjectionExpression.accessors[Invoice] | ||
} | ||
|
||
} | ||
|
||
override def spec: Spec[Environment with TestEnvironment with Scope, Any] = | ||
suite("TypeSafeApiMappingSpec")( | ||
topLevelSumTypeDiscriminatorNameSuite | ||
) @@ TestAspect.nondeterministic | ||
|
||
val topLevelSumTypeDiscriminatorNameSuite = suite("with @discriminatorName annotation")( | ||
test("getWithNarrow succeeds in narrowing an Invoice to Unpaid") { | ||
withSingleIdKeyTable { invoiceTable => | ||
val key: ProjectionExpression[dynamo.Invoice, String] = dynamo.Invoice.unpaid >>> dynamo.Invoice.Unpaid.id | ||
val keyCond: KeyConditionExpr.PartitionKeyEquals[dynamo.Invoice] = key.partitionKey === "1" | ||
for { | ||
_ <- put[dynamo.Invoice](invoiceTable, dynamo.Invoice.Unpaid("1")).execute | ||
unpaid <- getWithNarrow(dynamo.Invoice.unpaid)(invoiceTable)(keyCond).execute.absolve | ||
item <- getItem(invoiceTable, PrimaryKey("id" -> "1")).execute | ||
} yield { | ||
val unpaid2: dynamo.Invoice.Unpaid = unpaid | ||
assertTrue( | ||
unpaid2 == dynamo.Invoice.Unpaid("1") && item == Some( | ||
Item("id" -> "1", "invoiceType" -> "Unpaid") | ||
) | ||
) | ||
} | ||
} | ||
} | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters