Skip to content

Commit

Permalink
Merge branch 'master' of github.com:moia-oss/scynamo
Browse files Browse the repository at this point in the history
  • Loading branch information
CristinaHG committed Apr 22, 2021
2 parents c0bd701 + c9263c5 commit e05abd9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/main/scala/scynamo/Scynamo.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scynamo

import cats.data.EitherNec
import software.amazon.awssdk.services.dynamodb.model.{GetItemResponse, QueryResponse}
import software.amazon.awssdk.services.dynamodb.model.{GetItemResponse, QueryResponse, ScanResponse}
import cats.syntax.all._

import scala.jdk.CollectionConverters._
Expand All @@ -16,4 +16,8 @@ trait ScynamoFunctions {

def decodeQueryResponse[A: ObjectScynamoDecoder](response: QueryResponse): EitherNec[ScynamoDecodeError, List[A]] =
response.items().asScala.toList.traverse(ObjectScynamoDecoder[A].decodeMap(_))

def decodeScanResponse[A: ObjectScynamoDecoder](response: ScanResponse): EitherNec[ScynamoDecodeError, List[A]] =
response.items().asScala.toList.traverse(ObjectScynamoDecoder[A].decodeMap(_))

}
28 changes: 26 additions & 2 deletions src/test/scala/scynamo/ScynamoTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package scynamo

import software.amazon.awssdk.services.dynamodb.model.{AttributeValue, GetItemResponse, QueryResponse}
import software.amazon.awssdk.services.dynamodb.model.{AttributeValue, GetItemResponse, QueryResponse, ScanResponse}

class ScynamoTest extends UnitTest {
"Scynamo" should {
Expand All @@ -23,6 +23,16 @@ class ScynamoTest extends UnitTest {
result should ===(Right(List.empty))
}

"return an empty List if the scan response has no items" in {
val response = ScanResponse.builder().build()

val result = for {
result <- Scynamo.decodeScanResponse[Map[String, AttributeValue]](response)
} yield result

result should ===(Right(List.empty))
}

"return the decoded result if it has an item that is well formed" in {
import scynamo.syntax.encoder._
val input = Map("foo" -> "bar")
Expand All @@ -36,7 +46,7 @@ class ScynamoTest extends UnitTest {
result should ===(Right(Some(input)))
}

"return the decoded result if it has multiple items that are well formed" in {
"return the decoded query result if it has multiple items that are well formed" in {
import scynamo.syntax.encoder._
val input1 = Map("foo" -> "bar")
val input2 = Map("Miami" -> "Ibiza")
Expand All @@ -49,5 +59,19 @@ class ScynamoTest extends UnitTest {
} yield result
result should ===(Right(List(input1, input2)))
}

"return the decoded scan result if it has multiple items that are well formed" in {
import scynamo.syntax.encoder._
val input1 = Map("foo" -> "bar")
val input2 = Map("Miami" -> "Ibiza")

val result = for {
encodedInput1 <- input1.encodedMap
encodedInput2 <- input2.encodedMap
response = ScanResponse.builder().items(encodedInput1, encodedInput2).build()
result <- Scynamo.decodeScanResponse[Map[String, String]](response)
} yield result
result should ===(Right(List(input1, input2)))
}
}
}

0 comments on commit e05abd9

Please sign in to comment.