@@ -10,6 +10,10 @@ import {
10
10
arrayToSetAttribute ,
11
11
listAttributeToArray ,
12
12
setAttributeToArray ,
13
+ wrapMapperForDynamoListJsArray ,
14
+ wrapMapperForDynamoListJsSet ,
15
+ wrapMapperForDynamoSetJsArray ,
16
+ wrapMapperForDynamoSetJsSet ,
13
17
} from './wrap-mapper-for-collection.function'
14
18
15
19
class MyNumber {
@@ -28,108 +32,189 @@ const myCharToNumberAttrMapper: MapperForType<MyChar, NumberAttribute> = {
28
32
toDb : propertyValue => ( { N : `${ propertyValue . value . charCodeAt ( 0 ) } ` } ) ,
29
33
fromDb : attributeValue => ( { value : String . fromCharCode ( parseInt ( attributeValue . N , 10 ) ) } ) ,
30
34
}
31
-
32
- describe ( 'arrayToListAttribute' , ( ) => {
33
- it ( 'should map empty array to empty (L)ist' , ( ) => {
34
- expect ( arrayToListAttribute ( myNumberToStringAttrMapper ) ( [ ] ) ) . toEqual ( { L : [ ] } )
35
- } )
36
- it ( 'should map array to list with given mapper' , ( ) => {
37
- expect ( arrayToListAttribute ( myNumberToStringAttrMapper ) ( [ { value : 7 } ] ) ) . toEqual ( { L : [ { S : '7' } ] } )
35
+ describe ( 'wrap mapper for collection' , ( ) => {
36
+ describe ( 'arrayToListAttribute' , ( ) => {
37
+ it ( 'should map empty array to empty (L)ist' , ( ) => {
38
+ expect ( arrayToListAttribute ( myNumberToStringAttrMapper ) ( [ ] ) ) . toEqual ( { L : [ ] } )
39
+ } )
40
+ it ( 'should map array to list with given mapper' , ( ) => {
41
+ expect ( arrayToListAttribute ( myNumberToStringAttrMapper ) ( [ { value : 7 } ] ) ) . toEqual ( { L : [ { S : '7' } ] } )
42
+ } )
38
43
} )
39
- } )
40
44
41
- describe ( 'listAttributeToArray' , ( ) => {
42
- it ( 'should parse empty list to empty array' , ( ) => {
43
- expect ( listAttributeToArray ( myNumberToStringAttrMapper ) ( { L : [ ] } ) ) . toEqual ( [ ] )
44
- } )
45
- it ( 'should parse list to array with given mapper' , ( ) => {
46
- expect ( listAttributeToArray ( myNumberToStringAttrMapper ) ( { L : [ { S : '7' } ] } ) ) . toEqual ( [ { value : 7 } ] )
45
+ describe ( 'listAttributeToArray' , ( ) => {
46
+ it ( 'should parse empty list to empty array' , ( ) => {
47
+ expect ( listAttributeToArray ( myNumberToStringAttrMapper ) ( { L : [ ] } ) ) . toEqual ( [ ] )
48
+ } )
49
+ it ( 'should parse list to array with given mapper' , ( ) => {
50
+ expect ( listAttributeToArray ( myNumberToStringAttrMapper ) ( { L : [ { S : '7' } ] } ) ) . toEqual ( [ { value : 7 } ] )
51
+ } )
47
52
} )
48
- } )
49
53
50
- describe ( 'arrayToSetAttribute' , ( ) => {
51
- it ( 'should map empty array to null' , ( ) => {
52
- expect ( arrayToSetAttribute ( myNumberToStringAttrMapper ) ( [ ] ) ) . toEqual ( null )
53
- } )
54
- it ( 'should map array to (S)et' , ( ) => {
55
- expect ( arrayToSetAttribute ( myNumberToStringAttrMapper ) ( [ { value : 7 } ] ) ) . toEqual ( { SS : [ '7' ] } )
56
- expect ( arrayToSetAttribute ( myCharToNumberAttrMapper ) ( [ { value : 'A' } ] ) ) . toEqual ( { NS : [ '65' ] } )
54
+ describe ( 'arrayToSetAttribute' , ( ) => {
55
+ it ( 'should map empty array to null' , ( ) => {
56
+ expect ( arrayToSetAttribute ( myNumberToStringAttrMapper ) ( [ ] ) ) . toEqual ( null )
57
+ } )
58
+ it ( 'should map array to (S)et' , ( ) => {
59
+ expect ( arrayToSetAttribute ( myNumberToStringAttrMapper ) ( [ { value : 7 } ] ) ) . toEqual ( { SS : [ '7' ] } )
60
+ expect ( arrayToSetAttribute ( myCharToNumberAttrMapper ) ( [ { value : 'A' } ] ) ) . toEqual ( { NS : [ '65' ] } )
61
+ } )
57
62
} )
58
- } )
59
63
60
- describe ( 'setAttributeToArray' , ( ) => {
61
- it ( 'should parse (S)et to array' , ( ) => {
62
- expect ( setAttributeToArray ( myNumberToStringAttrMapper ) ( { SS : [ '7' ] } ) ) . toEqual ( [ { value : 7 } ] )
63
- expect ( setAttributeToArray ( myCharToNumberAttrMapper ) ( { NS : [ '65' ] } ) ) . toEqual ( [ { value : 'A' } ] )
64
+ describe ( 'setAttributeToArray' , ( ) => {
65
+ it ( 'should parse (S)et to array' , ( ) => {
66
+ expect ( setAttributeToArray ( myNumberToStringAttrMapper ) ( { SS : [ '7' ] } ) ) . toEqual ( [ { value : 7 } ] )
67
+ expect ( setAttributeToArray ( myCharToNumberAttrMapper ) ( { NS : [ '65' ] } ) ) . toEqual ( [ { value : 'A' } ] )
68
+ } )
64
69
} )
65
- } )
66
70
67
- describe ( 'for collection wrapped mappers' , ( ) => {
68
- describe ( 'fromDb' , ( ) => {
69
- let aFormId : FormId
70
- beforeEach ( ( ) => {
71
- aFormId = new FormId ( FormType . REQUEST , 55 , 2020 )
71
+ describe ( 'wrapMapperForDynamoSetJsArray' , ( ) => {
72
+ const wrappedMapper = wrapMapperForDynamoSetJsArray ( myNumberToStringAttrMapper )
73
+ it ( 'maps correctly toDb' , ( ) => {
74
+ const dbVal = wrappedMapper . toDb ( [ { value : 5 } ] )
75
+ expect ( dbVal ) . toEqual ( { SS : [ '5' ] } )
76
+ } )
77
+ it ( 'toDb throws if not an array is given' , ( ) => {
78
+ expect ( ( ) => wrappedMapper . toDb ( < any > new Set ( [ { value : 5 } ] ) ) ) . toThrow ( )
79
+ } )
80
+ it ( 'maps correctly fromDb' , ( ) => {
81
+ const jsVal = wrappedMapper . fromDb ( { SS : [ '5' , '1' ] } )
82
+ expect ( jsVal ) . toEqual ( [ { value : 5 } , { value : 1 } ] )
72
83
} )
84
+ it ( 'fromDb throws if not a Set was given' , ( ) => {
85
+ // it does not throw, if it is a wrong set --> this should do the single item mapper
86
+ // it only throws if it is not a set at all
87
+ expect ( ( ) => wrappedMapper . fromDb ( < any > { S : '5' } ) ) . toThrow ( )
88
+ } )
89
+ } )
73
90
74
- it ( 'array to (L)ist (itemMapper, sorted)' , ( ) => {
75
- const dbObj : Attributes < ModelWithCollections > = {
76
- arrayOfFormIdToListWithStrings : { L : [ formIdMapper . toDb ( aFormId ) ] } ,
77
- }
78
- expect ( fromDb ( dbObj , ModelWithCollections ) ) . toEqual ( { arrayOfFormIdToListWithStrings : [ aFormId ] } )
91
+ describe ( 'wrapMapperForDynamoSetJsSet' , ( ) => {
92
+ const wrappedMapper = wrapMapperForDynamoSetJsSet ( myNumberToStringAttrMapper )
93
+ it ( 'maps correctly toDb' , ( ) => {
94
+ const dbVal = wrappedMapper . toDb ( new Set ( [ { value : 5 } ] ) )
95
+ expect ( dbVal ) . toEqual ( { SS : [ '5' ] } )
96
+ } )
97
+ it ( 'toDb throws if not a set is given' , ( ) => {
98
+ expect ( ( ) => wrappedMapper . toDb ( < any > [ { value : 5 } ] ) ) . toThrow ( )
79
99
} )
80
- it ( 'set to (L)ist (itemMapper, sorted)' , ( ) => {
81
- const dbObj : Attributes < ModelWithCollections > = {
82
- setOfFormIdToListWithStrings : { L : [ formIdMapper . toDb ( aFormId ) ] } ,
83
- }
84
- expect ( fromDb ( dbObj , ModelWithCollections ) ) . toEqual ( { setOfFormIdToListWithStrings : new Set ( [ aFormId ] ) } )
100
+ it ( 'maps correctly fromDb' , ( ) => {
101
+ const jsVal = wrappedMapper . fromDb ( { SS : [ '5' , '1' ] } )
102
+ expect ( jsVal ) . toEqual ( new Set ( [ { value : 5 } , { value : 1 } ] ) )
85
103
} )
104
+ it ( 'fromDb throws if not a Set was given' , ( ) => {
105
+ // it does not throw, if it is a wrong set --> this should do the single item mapper
106
+ // it only throws if it is not a set at all
107
+ expect ( ( ) => wrappedMapper . fromDb ( < any > { S : '5' } ) ) . toThrow ( )
108
+ } )
109
+ } )
86
110
87
- it ( 'array to (S)et (itemMapper)' , ( ) => {
88
- const dbObj : Attributes < ModelWithCollections > = { arrayOfFormIdToSet : { SS : [ FormId . unparse ( aFormId ) ] } }
89
- expect ( fromDb ( dbObj , ModelWithCollections ) ) . toEqual ( { arrayOfFormIdToSet : [ aFormId ] } )
111
+ describe ( 'wrapMapperForDynamoListJsArray' , ( ) => {
112
+ const wrappedMapper = wrapMapperForDynamoListJsArray ( myNumberToStringAttrMapper )
113
+ it ( 'maps correctly toDb' , ( ) => {
114
+ const dbVal = wrappedMapper . toDb ( [ { value : 5 } ] )
115
+ expect ( dbVal ) . toEqual ( { L : [ { S : '5' } ] } )
90
116
} )
91
- it ( 'set to (S)et (itemMapper)' , ( ) => {
92
- const dbObj : Attributes < ModelWithCollections > = { setOfFormIdToSet : { SS : [ FormId . unparse ( aFormId ) ] } }
93
- expect ( fromDb ( dbObj , ModelWithCollections ) ) . toEqual ( { setOfFormIdToSet : new Set ( [ aFormId ] ) } )
117
+ it ( 'toDb throws if not an array is given' , ( ) => {
118
+ expect ( ( ) => wrappedMapper . toDb ( < any > new Set ( [ { value : 5 } ] ) ) ) . toThrow ( )
94
119
} )
120
+ it ( 'maps correctly fromDb' , ( ) => {
121
+ const jsVal = wrappedMapper . fromDb ( { L : [ { S : '5' } , { S : '1' } ] } )
122
+ expect ( jsVal ) . toEqual ( [ { value : 5 } , { value : 1 } ] )
123
+ } )
124
+ it ( 'fromDb throws if not a List was given' , ( ) => {
125
+ expect ( ( ) => wrappedMapper . fromDb ( < any > { SS : [ '5' ] } ) ) . toThrow ( )
126
+ expect ( ( ) => wrappedMapper . fromDb ( < any > { NS : [ '5' ] } ) ) . toThrow ( )
127
+ expect ( ( ) => wrappedMapper . fromDb ( < any > { M : { S : '5' } } ) ) . toThrow ( )
128
+ } )
129
+ } )
95
130
96
- it ( 'should throw when not a (S)et attribute' , ( ) => {
97
- const dbObj : Attributes < FailModel > = { myFail : { M : { id : { S : '42' } } } }
98
- expect ( ( ) => fromDb ( dbObj , FailModel ) ) . toThrow ( )
131
+ describe ( 'wrapMapperForDynamoListJsSet' , ( ) => {
132
+ const wrappedMapper = wrapMapperForDynamoListJsSet ( myNumberToStringAttrMapper )
133
+ it ( 'maps correctly toDb' , ( ) => {
134
+ const dbVal = wrappedMapper . toDb ( new Set ( [ { value : 5 } ] ) )
135
+ expect ( dbVal ) . toEqual ( { L : [ { S : '5' } ] } )
136
+ } )
137
+ it ( 'toDb throws if not a set is given' , ( ) => {
138
+ expect ( ( ) => wrappedMapper . toDb ( < any > [ { value : 5 } ] ) ) . toThrow ( )
139
+ } )
140
+ it ( 'maps correctly fromDb' , ( ) => {
141
+ const jsVal = wrappedMapper . fromDb ( { L : [ { S : '5' } , { S : '1' } ] } )
142
+ expect ( jsVal ) . toEqual ( new Set ( [ { value : 5 } , { value : 1 } ] ) )
143
+ } )
144
+ it ( 'fromDb throws if not a List was given' , ( ) => {
145
+ expect ( ( ) => wrappedMapper . fromDb ( < any > { SS : [ '5' ] } ) ) . toThrow ( )
146
+ expect ( ( ) => wrappedMapper . fromDb ( < any > { NS : [ '5' ] } ) ) . toThrow ( )
147
+ expect ( ( ) => wrappedMapper . fromDb ( < any > { M : { S : '5' } } ) ) . toThrow ( )
99
148
} )
100
149
} )
101
150
102
- describe ( 'toDb' , ( ) => {
103
- let aFormId : FormId
151
+ describe ( 'for collection wrapped mappers' , ( ) => {
152
+ describe ( 'fromDb' , ( ) => {
153
+ let aFormId : FormId
154
+ beforeEach ( ( ) => {
155
+ aFormId = new FormId ( FormType . REQUEST , 55 , 2020 )
156
+ } )
104
157
105
- beforeEach ( ( ) => {
106
- aFormId = new FormId ( FormType . REQUEST , 55 , 2020 )
107
- } )
158
+ it ( 'array to (L)ist (itemMapper, sorted)' , ( ) => {
159
+ const dbObj : Attributes < ModelWithCollections > = {
160
+ arrayOfFormIdToListWithStrings : { L : [ formIdMapper . toDb ( aFormId ) ] } ,
161
+ }
162
+ expect ( fromDb ( dbObj , ModelWithCollections ) ) . toEqual ( { arrayOfFormIdToListWithStrings : [ aFormId ] } )
163
+ } )
164
+ it ( 'set to (L)ist (itemMapper, sorted)' , ( ) => {
165
+ const dbObj : Attributes < ModelWithCollections > = {
166
+ setOfFormIdToListWithStrings : { L : [ formIdMapper . toDb ( aFormId ) ] } ,
167
+ }
168
+ expect ( fromDb ( dbObj , ModelWithCollections ) ) . toEqual ( { setOfFormIdToListWithStrings : new Set ( [ aFormId ] ) } )
169
+ } )
108
170
109
- it ( 'array to (L)ist (itemMapper, sorted )' , ( ) => {
110
- expect ( toDb ( { arrayOfFormIdToListWithStrings : [ aFormId ] } , ModelWithCollections ) ) . toEqual ( {
111
- arrayOfFormIdToListWithStrings : { L : [ formIdMapper . toDb ( aFormId ) ] } ,
171
+ it ( 'array to (S)et (itemMapper)' , ( ) => {
172
+ const dbObj : Attributes < ModelWithCollections > = { arrayOfFormIdToSet : { SS : [ FormId . unparse ( aFormId ) ] } }
173
+ expect ( fromDb ( dbObj , ModelWithCollections ) ) . toEqual ( { arrayOfFormIdToSet : [ aFormId ] } )
112
174
} )
113
- } )
114
- it ( 'set to (L)ist (itemMapper, sorted)' , ( ) => {
115
- expect ( toDb ( { setOfFormIdToListWithStrings : new Set ( [ aFormId ] ) } , ModelWithCollections ) ) . toEqual ( {
116
- setOfFormIdToListWithStrings : { L : [ formIdMapper . toDb ( aFormId ) ] } ,
175
+ it ( 'set to (S)et (itemMapper)' , ( ) => {
176
+ const dbObj : Attributes < ModelWithCollections > = { setOfFormIdToSet : { SS : [ FormId . unparse ( aFormId ) ] } }
177
+ expect ( fromDb ( dbObj , ModelWithCollections ) ) . toEqual ( { setOfFormIdToSet : new Set ( [ aFormId ] ) } )
117
178
} )
118
- } )
119
179
120
- it ( 'array to (S)et (itemMapper) ' , ( ) => {
121
- expect ( toDb ( { arrayOfFormIdToSet : [ aFormId ] } , ModelWithCollections ) ) . toEqual ( {
122
- arrayOfFormIdToSet : { SS : [ FormId . unparse ( aFormId ) ] } ,
180
+ it ( 'should throw when not a (S)et attribute ' , ( ) => {
181
+ const dbObj : Attributes < FailModel > = { myFail : { M : { id : { S : '42' } } } }
182
+ expect ( ( ) => fromDb ( dbObj , FailModel ) ) . toThrow ( )
123
183
} )
124
184
} )
125
- it ( 'set to (S)et (itemMapper)' , ( ) => {
126
- expect ( toDb ( { setOfFormIdToSet : new Set ( [ aFormId ] ) } , ModelWithCollections ) ) . toEqual ( {
127
- setOfFormIdToSet : { SS : [ FormId . unparse ( aFormId ) ] } ,
185
+
186
+ describe ( 'toDb' , ( ) => {
187
+ let aFormId : FormId
188
+
189
+ beforeEach ( ( ) => {
190
+ aFormId = new FormId ( FormType . REQUEST , 55 , 2020 )
128
191
} )
129
- } )
130
192
131
- it ( 'should throw when wrong mapper' , ( ) => {
132
- expect ( ( ) => toDb ( { myFail : [ { id : 42 } ] } , FailModel ) ) . toThrow ( )
193
+ it ( 'array to (L)ist (itemMapper, sorted)' , ( ) => {
194
+ expect ( toDb ( { arrayOfFormIdToListWithStrings : [ aFormId ] } , ModelWithCollections ) ) . toEqual ( {
195
+ arrayOfFormIdToListWithStrings : { L : [ formIdMapper . toDb ( aFormId ) ] } ,
196
+ } )
197
+ } )
198
+ it ( 'set to (L)ist (itemMapper, sorted)' , ( ) => {
199
+ expect ( toDb ( { setOfFormIdToListWithStrings : new Set ( [ aFormId ] ) } , ModelWithCollections ) ) . toEqual ( {
200
+ setOfFormIdToListWithStrings : { L : [ formIdMapper . toDb ( aFormId ) ] } ,
201
+ } )
202
+ } )
203
+
204
+ it ( 'array to (S)et (itemMapper)' , ( ) => {
205
+ expect ( toDb ( { arrayOfFormIdToSet : [ aFormId ] } , ModelWithCollections ) ) . toEqual ( {
206
+ arrayOfFormIdToSet : { SS : [ FormId . unparse ( aFormId ) ] } ,
207
+ } )
208
+ } )
209
+ it ( 'set to (S)et (itemMapper)' , ( ) => {
210
+ expect ( toDb ( { setOfFormIdToSet : new Set ( [ aFormId ] ) } , ModelWithCollections ) ) . toEqual ( {
211
+ setOfFormIdToSet : { SS : [ FormId . unparse ( aFormId ) ] } ,
212
+ } )
213
+ } )
214
+
215
+ it ( 'should throw when wrong mapper' , ( ) => {
216
+ expect ( ( ) => toDb ( { myFail : [ { id : 42 } ] } , FailModel ) ) . toThrow ( )
217
+ } )
133
218
} )
134
219
} )
135
220
} )
0 commit comments