github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/types/attributevalue/attributevalue_test.go (about) 1 package attributevalue 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 ) 8 9 // Round trip some data. Note the sets have repeated elements...make sure they 10 // are eliminated 11 func TestAttributeValueMarshal(t *testing.T) { 12 s := []string{ 13 `{"S":"a string"}`, 14 `{"B":"aGkgdGhlcmUK"}`, 15 `{"N":"5"}`, 16 `{"BOOL":true}`, 17 `{"NULL":false}`, 18 `{"BS":["d2VsbCBoZWxsbyB0aGVyZQ==","aGkgdGhlcmUK","aG93ZHk="]}`, 19 `{"L":[{"S":"a string"},{"L":[{"S":"another string"}]}]}`, 20 } 21 for _, v := range s { 22 var a AttributeValue 23 um_err := json.Unmarshal([]byte(v), &a) 24 if um_err != nil { 25 t.Errorf("cannot unmarshal\n") 26 } 27 28 json, jerr := json.Marshal(a) 29 if jerr != nil { 30 t.Errorf("cannot marshal\n") 31 } 32 o := string(json) 33 if (len(v) != len(o)) { 34 e := fmt.Sprintf("%s\n%s\nshould be equal through round-trip",v,o) 35 t.Errorf(e) 36 } 37 } 38 } 39 40 func TestAttributeValueMarshalDeDuplicate(t *testing.T) { 41 s := []string{ 42 `{"SS":["a","b","c","c","c"]}`, 43 `{"NS":["42","1","0","0","1","1","1","42"]}`, 44 `{"M":{"key1":{"S":"a string"},"key2":{"L":[{"NS":["42","42","1"]},{"S":"a string"},{"L":[{"S":"another string"}]}]}}}`, 45 } 46 c := []string{ 47 `{"SS":["a","b","c"]}`, 48 `{"NS":["42","1","0"]}`, 49 `{"M":{"key1":{"S":"a string"},"key2":{"L":[{"NS":["42","1"]},{"S":"a string"},{"L":[{"S":"another string"}]}]}}}`, 50 } 51 for i, v := range s { 52 var a AttributeValue 53 um_err := json.Unmarshal([]byte(v), &a) 54 if um_err != nil { 55 t.Errorf("cannot unmarshal\n") 56 } 57 58 json, jerr := json.Marshal(a) 59 if jerr != nil { 60 t.Errorf("cannot marshal\n") 61 } 62 o := string(json) 63 if (len(c[i]) != len(o)) { 64 e := fmt.Sprintf("%s\n%s\nshould be equal through round-trip",c[i],o) 65 t.Errorf(e) 66 } 67 } 68 } 69 70 // Demonstrate the use of the Valid function 71 func TestAttributeValueInvalid(t *testing.T) { 72 a := NewAttributeValue() 73 a.N = "1" 74 a.S = "a" 75 if a.Valid() { 76 _, jerr := json.Marshal(a) 77 if jerr == nil { 78 t.Errorf("should not have been able to marshal\n") 79 } else { 80 _ = fmt.Sprintf("%v\n", jerr) 81 } 82 } 83 a = NewAttributeValue() 84 a.N = "1" 85 a.B = "fsdfa" 86 if a.Valid() { 87 _, jerr := json.Marshal(a) 88 if jerr == nil { 89 t.Errorf("should not have been able to marshal\n") 90 } 91 } 92 93 a = NewAttributeValue() 94 a.N = "1" 95 a.InsertSS("a") 96 if a.Valid() { 97 _, jerr := json.Marshal(a) 98 if jerr == nil { 99 t.Errorf("should not have been able to marshal\n") 100 } 101 } 102 103 a = NewAttributeValue() 104 if a.Valid() { 105 t.Errorf("should not pass valid\n") 106 } 107 } 108 109 // Empty AttributeValues should emit null 110 func TestAttributeValueEmpty(t *testing.T) { 111 a := NewAttributeValue() 112 _, jerr := json.Marshal(a) 113 if jerr != nil { 114 t.Errorf("cannot marshal\n") 115 } 116 117 var a2 AttributeValue 118 _, jerr = json.Marshal(a2) 119 if jerr != nil { 120 t.Errorf("cannot marshal\n") 121 } 122 } 123 124 // Test the Insert funtions 125 func TestAttributeValueInserts(t *testing.T) { 126 a1 := NewAttributeValue() 127 a1.InsertSS("hi") 128 a1.InsertSS("hi") // duplicate, should be removed 129 a1.InsertSS("bye") 130 json, jerr := json.Marshal(a1) 131 if jerr != nil { 132 t.Errorf("cannot marshal\n") 133 } 134 c := `{"SS":["hi","bye"]}` 135 if len(c) != len(string(json)) { 136 e := fmt.Sprintf("%s\n%s\nshould be same",c,string(json)) 137 t.Errorf(e) 138 } 139 140 av1 := NewAttributeValueMap() 141 k := "myItem" 142 av1[k] = a1 143 av2 := NewAttributeValueMap() 144 cp_err := av1.Copy(av2) 145 if cp_err != nil { 146 t.Errorf(cp_err.Error()) 147 } 148 av1 = nil 149 if _,ok := av2[k]; !ok { 150 t.Errorf("key is not set in copy") 151 } 152 } 153 154 // Test the Insert functions 155 func TestAttributeValueInserts2(t *testing.T) { 156 a1 := NewAttributeValue() 157 a1.InsertSS("hi") 158 a1.InsertSS("hi") // duplicate, should be removed 159 a1.InsertSS("bye") 160 161 a2 := NewAttributeValue() 162 _ = a2.InsertL(a1) 163 a1 = nil // should be fine, above line should make a new copy 164 json, jerr := json.Marshal(a2) 165 if jerr != nil { 166 t.Errorf("cannot marshal\n") 167 } 168 c := `{"L":[{"SS":["hi","bye"]}]}` 169 if len(c) != len(string(json)) { 170 e := fmt.Sprintf("%s\n%s\nshould be same",c,string(json)) 171 t.Errorf(e) 172 } 173 174 a3 := NewAttributeValue() 175 nerr := a3.InsertN("fred") 176 if nerr == nil { 177 t.Errorf("should have returned error from InsertN\n") 178 } 179 berr := a3.InsertB("1") 180 if berr == nil { 181 t.Errorf("should have returned error from InsertB\n") 182 } 183 } 184 185 func TestCopy(t *testing.T) { 186 a1 := NewAttributeValue() 187 _ = a1.InsertSS("hi") 188 _ = a1.InsertSS("bye") 189 190 var a2 = new(AttributeValue) 191 192 cp_err := a1.Copy(a2) 193 if cp_err != nil { 194 t.Errorf("Copy error\n") 195 } 196 197 a2 = nil 198 cp_err = a1.Copy(a2) 199 if cp_err == nil { 200 t.Errorf("should have returned error from Copy\n") 201 } 202 203 a1 = nil 204 a2 = new(AttributeValue) 205 cp_err = a1.Copy(a2) 206 if cp_err == nil { 207 t.Errorf("should have returned error from Copy\n") 208 } 209 210 a2 = nil 211 cp_err = a1.Copy(a2) 212 if cp_err == nil { 213 t.Errorf("should have returned error from Copy\n") 214 } 215 } 216 217 // Make sure Valid emits as null 218 func TestAttributeValueUpdate(t *testing.T) { 219 a := NewAttributeValueUpdate() 220 a.Action = "DELETE" 221 _, jerr := json.Marshal(a) 222 if jerr != nil { 223 t.Errorf("cannot marshal\n") 224 } 225 } 226 227 func TestCoerceAttributeValueBasicJSON(t *testing.T) { 228 js := []string{`{"a":{"b":"c"},"d":[{"e":"f"},"g","h"],"i":[1.0,2.0,3.0],"j":["x","y"]}`, 229 `"a"`, `true`, 230 `[1,2,3,2,3]`} 231 c := []string{`{"a":{"b":"c"},"d":[{"e":"f"},"g","h"],"i":[1,2,3],"j":["x","y"]}`, 232 `"a"`, `true`, 233 `[1,2,3]`} 234 for i, v := range js { 235 j := []byte(v) 236 av, av_err := BasicJSONToAttributeValue(j) 237 if av_err != nil { 238 t.Errorf("cannot coerce") 239 } 240 _, av_json_err := json.Marshal(av) 241 if av_json_err != nil { 242 _ = fmt.Sprintf("%v\n", av_json_err) 243 t.Errorf("cannot marshal") 244 } 245 b, cerr := av.ToBasicJSON() 246 if cerr != nil { 247 t.Errorf("cannot coerce") 248 } 249 if len(c[i]) != len(string(b)) { 250 e := fmt.Sprintf("%s\n%s\nshould be same",c[i],string(b)) 251 t.Errorf(e) 252 } 253 } 254 } 255 256 func TestCoerceAttributeValueMapBasicJSON(t *testing.T) { 257 js := []string{`{"AS":"1234string","AN":3,"ANS":[1,2,1,2,3],"ASS":["a","a","b"],"ABOOL":true,"AL":["1234string",3,[1,2,3],["a","b"]],"AM":{"AMS":"1234string","AMN":3,"AMNS":[1,2,3],"AMSS":["a","b"],"AMBOOL":true,"AL":["1234string",3,[1,2,3],["a","b"]]}}`} 258 c := []string{`{"AS":"1234string","AN":3,"ANS":[1,2,3],"ASS":["a","b"],"ABOOL":true,"AL":["1234string",3,[1,2,3],["a","b"]],"AM":{"AMS":"1234string","AMN":3,"AMNS":[1,2,3],"AMSS":["a","b"],"AMBOOL":true,"AL":["1234string",3,[1,2,3],["a","b"]]}}`} 259 for i, v := range js { 260 j := []byte(v) 261 av, av_err := BasicJSONToAttributeValueMap(j) 262 if av_err != nil { 263 t.Errorf("cannot coerce") 264 } 265 _, av_json_err := json.Marshal(av) 266 if av_json_err != nil { 267 t.Errorf("cannot marshal") 268 } 269 b, cerr := av.ToBasicJSON() 270 if cerr != nil { 271 t.Errorf("cannot coerce") 272 } 273 if len(c[i]) != len(string(b)) { 274 e := fmt.Sprintf("%s\n%s\nshould be same",c[i],string(b)) 275 t.Errorf(e) 276 } 277 } 278 }