@@ -10,6 +10,10 @@ import {
1010 arrayToSetAttribute ,
1111 listAttributeToArray ,
1212 setAttributeToArray ,
13+ wrapMapperForDynamoListJsArray ,
14+ wrapMapperForDynamoListJsSet ,
15+ wrapMapperForDynamoSetJsArray ,
16+ wrapMapperForDynamoSetJsSet ,
1317} from './wrap-mapper-for-collection.function'
1418
1519class MyNumber {
@@ -28,108 +32,189 @@ const myCharToNumberAttrMapper: MapperForType<MyChar, NumberAttribute> = {
2832 toDb : propertyValue => ( { N : `${ propertyValue . value . charCodeAt ( 0 ) } ` } ) ,
2933 fromDb : attributeValue => ( { value : String . fromCharCode ( parseInt ( attributeValue . N , 10 ) ) } ) ,
3034}
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+ } )
3843 } )
39- } )
4044
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+ } )
4752 } )
48- } )
4953
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+ } )
5762 } )
58- } )
5963
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+ } )
6469 } )
65- } )
6670
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 } ] )
7283 } )
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+ } )
7390
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 ( )
7999 } )
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 } ] ) )
85103 } )
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+ } )
86110
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' } ] } )
90116 } )
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 ( )
94119 } )
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+ } )
95130
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 ( )
99148 } )
100149 } )
101150
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+ } )
104157
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+ } )
108170
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 ] } )
112174 } )
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 ] ) } )
117178 } )
118- } )
119179
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 ( )
123183 } )
124184 } )
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 )
128191 } )
129- } )
130192
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+ } )
133218 } )
134219 } )
135220} )
0 commit comments