-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJsonToFromDynamoSpec.scala
192 lines (177 loc) · 6.15 KB
/
JsonToFromDynamoSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.lifeway.play.dynamo
import com.lifeway.play.dynamo
import org.scalactic._
import org.scalatest.{MustMatchers, WordSpec}
import play.api.libs.json.{JsObject, Json}
class JsonToFromDynamoSpec extends WordSpec with MustMatchers {
case class NestedObject(title: String, age: Byte)
object NestedObject {
implicit val reads = Json.reads[NestedObject]
implicit val writes = Json.writes[NestedObject]
}
case class TestSample(name: String,
enabled: Boolean = true,
age: Byte,
emails: Set[String],
contactTimes: Set[Long],
stringList: Seq[String],
numList: Seq[Int],
objectType: NestedObject,
nestedObjectSet: Set[NestedObject],
nestedObjectSeq: Seq[NestedObject])
object TestSample {
implicit val reads = Json.reads[TestSample]
implicit val writes = Json.writes[TestSample]
}
"A Full Example with nested objects, sets, and sequences" should {
val sampleJson = Json.parse("""
|{
| "name": {
| "S": "Test User Id"
| },
| "enabled": {
| "BOOL": true
| },
| "age": {
| "N": "31"
| },
| "emails": {
| "L": [
| { "S": "[email protected]" },
| { "S": "[email protected]" }
| ]
| },
| "contactTimes": {
| "L": [
| { "N": "6442450941" },
| { "N": "12884901882" }
| ]
| },
| "stringList": {
| "L": [
| { "S": "item-1-string" },
| { "S": "item-2-string" }
| ]
| },
| "numList": {
| "L": [
| { "N": "12345"},
| { "N": "45678"}
| ]
| },
| "objectType": {
| "M": {
| "title": {
| "S": "title-val"
| },
| "age" : {
| "N": "31"
| }
| }
| },
| "nestedObjectSet": {
| "L": [
| {
| "M": {
| "title": {
| "S": "title-val-a"
| },
| "age": {
| "N": "31"
| }
| }
| },
| {
| "M": {
| "title": {
| "S": "title-val-b"
| },
| "age": {
| "N": "32"
| }
| }
| }
| ]
| },
| "nestedObjectSeq": {
| "L": [
| {
| "M": {
| "title": {
| "S": "title-val-c"
| },
| "age": {
| "N": "33"
| }
| }
| },
| {
| "M": {
| "title": {
| "S": "title-val-d"
| },
| "age": {
| "N": "34"
| }
| }
| }
| ]
| }
|}
""".stripMargin)
val sampleVal = TestSample("Test User Id",
enabled = true,
31,
Set("[email protected]", "[email protected]"),
Set(6442450941l, 12884901882l),
Seq("item-1-string", "item-2-string"),
Seq(12345, 45678),
NestedObject("title-val", 31),
Set(NestedObject("title-val-a", 31), NestedObject("title-val-b", 32)),
Seq(NestedObject("title-val-c", 33), NestedObject("title-val-d", 34)))
"read from DynamoDB Json through direct Json Converters to standard case class readers" in {
import DynamoJsonConverters.Converters
val result: TestSample Or Every[ErrorMessage] = sampleJson.as[JsObject].fromDynamoJson.map(_.as[TestSample])
result.isGood mustEqual true
result.get mustEqual sampleVal
}
"read from an invalid DynamoDB JSON through direct Json Converters should return with accumulated error messages in the event of non-dynamo Json" in {
import DynamoJsonConverters.Converters
val json = Json.parse("""
|{
| "key": "This is normal Json",
| "otherThing": true,
| "nestedType": {
| "someKey" : "This is bad"
| },
| "someThing": {
| "L": [ true, false ]
| },
| "anotherArray" : {
| "L": [
| {
| "S": "A valid DynamoDB value just for good measure...."
| },
| {
| "B": "Some data type that is not yet supported..."
| }
| ]
| }
|}
""".stripMargin)
val result: TestSample Or Every[ErrorMessage] = json.as[JsObject].fromDynamoJson.map(_.as[TestSample])
result.isGood mustEqual false
result.swap.get mustEqual Many(
"The value for field `key` is not a valid DynamoDB type.",
"The value for field `otherThing` is not a valid DynamoDB type.",
"The field `someKey` under field `nestedType` is not a valid / supported DynamoDB type",
"`true` is not a valid / supported DynamoDB type in a DynamoDB array",
"`false` is not a valid / supported DynamoDB type in a DynamoDB array",
"`B` is not a valid / supported DynamoDB type in an array")
}
"write to DynamoDB Json through direct Json converters that occur after the standard case class Json writers" in {
import dynamo.DynamoJsonConverters.Converters
Json.toJson(sampleVal).as[JsObject].toDynamoJson mustEqual sampleJson
}
}
}