github.com/moleculer-go/moleculer@v0.3.3/payload/payload_test.go (about) 1 package payload_test 2 3 import ( 4 "errors" 5 "os" 6 "time" 7 8 "go.mongodb.org/mongo-driver/bson" 9 10 "github.com/moleculer-go/cupaloy/v2" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 14 "github.com/moleculer-go/moleculer" 15 . "github.com/moleculer-go/moleculer/payload" 16 ) 17 18 var snap = cupaloy.New(cupaloy.FailOnUpdate(os.Getenv("UPDATE_SNAPSHOTS") == "true")) 19 20 var _ = Describe("Payload", func() { 21 22 It("Remove should remove fields from the payload and return a new copy", func() { 23 24 p := New(map[string]string{ 25 "name": "John", 26 "lastname": "Snow", 27 "faction": "Stark", 28 "Winter": "is coming!", 29 }) 30 31 Expect(snap.SnapshotMulti("Remove()", p.Remove("Winter", "name"))).ShouldNot(HaveOccurred()) 32 }) 33 34 It("Bson should return a bson map", func() { 35 36 p := New(map[string]string{ 37 "name": "John", 38 "lastname": "Snow", 39 "faction": "Stark", 40 "Winter": "is coming!", 41 }) 42 43 bs := p.Bson() 44 45 Expect(snap.SnapshotMulti("Bson()", bs)).ShouldNot(HaveOccurred()) 46 }) 47 48 It("Add should add fields to payload", func() { 49 50 p := New(map[string]string{ 51 "name": "John", 52 "lastname": "Snow", 53 "faction": "Stark", 54 "Winter": "is coming!", 55 }) 56 57 m := p.AddMany(map[string]interface{}{ 58 "page": 1, 59 "pageSize": 15, 60 }) 61 62 Expect(snap.SnapshotMulti("Add()", m)).ShouldNot(HaveOccurred()) 63 }) 64 65 type customMap map[string]interface{} 66 type customArray []map[string]interface{} 67 68 It("should deal with custom map types", func() { 69 p := New(customMap{ 70 "name": "John", 71 "lastname": "Snow", 72 "sub": customMap{ 73 "level": 1, 74 "sub": customMap{ 75 "level": 2, 76 }, 77 "subList": customArray{ 78 customMap{ 79 "level": 3, 80 "name": "sub item inside custom array", 81 }, 82 }, 83 }, 84 }) 85 Expect(snap.SnapshotMulti("CustomMap()-RawMap()", p.RawMap())).ShouldNot(HaveOccurred()) 86 Expect(snap.SnapshotMulti("CustomMap()-Bson()", p.Bson())).ShouldNot(HaveOccurred()) 87 }) 88 89 It("should deal with bson.M map types", func() { 90 p := New(bson.M{ 91 "name": "John", 92 "lastname": "Snow", 93 "sub": bson.M{ 94 "level": 1, 95 "sub": bson.M{ 96 "level": 2, 97 }, 98 "subList": []bson.M{ 99 bson.M{ 100 "level": 3, 101 "name": "sub item inside custom array", 102 }, 103 }, 104 }, 105 }) 106 Expect(snap.SnapshotMulti("Bson-values", p.Bson())).ShouldNot(HaveOccurred()) 107 108 }) 109 110 It("Should convert numbers correctly", func() { 111 112 Expect(New(true).Bool()).Should(BeTrue()) 113 Expect(New("true").Bool()).Should(BeTrue()) 114 115 Expect(New(false).Bool()).Should(BeFalse()) 116 Expect(New("false").Bool()).Should(BeFalse()) 117 Expect(New("anything else").Bool()).Should(BeFalse()) 118 Expect(New("").Bool()).Should(BeFalse()) 119 120 Expect(New("10").Int()).Should(Equal(int(10))) 121 Expect(New("10").Int64()).Should(Equal(int64(10))) 122 Expect(New("10").Float32()).Should(Equal(float32(10))) 123 Expect(New("10").Float()).Should(Equal(float64(10))) 124 Expect(New("10").Uint()).Should(Equal(uint64(10))) 125 126 Expect(New(10).Int64()).Should(Equal(int64(10))) 127 Expect(New(10).Float32()).Should(Equal(float32(10))) 128 Expect(New(10).Float()).Should(Equal(float64(10))) 129 Expect(New(10).Uint()).Should(Equal(uint64(10))) 130 131 Expect(New(int64(30)).Int()).Should(Equal(30)) 132 Expect(New(int64(30)).Float32()).Should(Equal(float32(30))) 133 Expect(New(int64(30)).Float()).Should(Equal(float64(30))) 134 Expect(New(int64(30)).Uint()).Should(Equal(uint64(30))) 135 136 Expect(New(float64(60)).Int()).Should(Equal(60)) 137 Expect(New(float64(60)).Float32()).Should(Equal(float32(60))) 138 Expect(New(float64(60)).Int64()).Should(Equal(int64(60))) 139 Expect(New(float64(60)).Uint()).Should(Equal(uint64(60))) 140 141 Expect(New(float32(120)).Int()).Should(Equal(120)) 142 Expect(New(float32(120)).Float()).Should(Equal(float64(120))) 143 Expect(New(float32(120)).Int64()).Should(Equal(int64(120))) 144 Expect(New(float32(120)).Uint()).Should(Equal(uint64(120))) 145 146 Expect(New(uint64(240)).Int()).Should(Equal(240)) 147 Expect(New(uint64(240)).Float()).Should(Equal(float64(240))) 148 Expect(New(uint64(240)).Int64()).Should(Equal(int64(240))) 149 Expect(New(uint64(240)).Float32()).Should(Equal(float32(240))) 150 151 }) 152 153 It("Should create Payload with map and return values correctly", func() { 154 155 var source interface{} = map[string]int{ 156 "height": 150, 157 "width": 230, 158 } 159 params := New(source) 160 Expect(params.Get("height").Int()).Should(Equal(150)) 161 Expect(params.Get("width").Int()).Should(Equal(230)) 162 163 source = map[string]string{ 164 "name": "John", 165 "word": "Snow", 166 } 167 params = New(source) 168 Expect(params.Get("name").Value()).Should(Equal("John")) 169 Expect(params.Get("word").String()).Should(Equal("Snow")) 170 171 var lHeight int64 = 345356436 172 var lWidth int64 = 5623453254123 173 source = map[string]int64{ 174 "height": lHeight, 175 "width": lWidth, 176 } 177 params = New(source) 178 Expect(params.Get("height").Int64()).Should(Equal(lHeight)) 179 Expect(params.Get("width").Int64()).Should(Equal(lWidth)) 180 181 var f32Height float32 = 345356436.5623453254123 182 var f32Width float32 = 5623453254123.345356436 183 source = map[string]float32{ 184 "height": f32Height, 185 "width": f32Width, 186 } 187 params = New(source) 188 Expect(params.Get("height").Float32()).Should(Equal(f32Height)) 189 Expect(params.Get("width").Float32()).Should(Equal(f32Width)) 190 191 var f64Height float64 = 345356436.5623453254123 192 var f64Width float64 = 5623453254123.345356436 193 source = map[string]float64{ 194 "height": f64Height, 195 "width": f64Width, 196 } 197 params = New(source) 198 Expect(params.Get("height").Float()).Should(Equal(f64Height)) 199 Expect(params.Get("width").Float()).Should(Equal(f64Width)) 200 201 timeArray := []time.Time{time.Now(), time.Now().Local(), time.Now().UTC()} 202 source = map[string]interface{}{ 203 "string": "Hellow Night!", 204 "int": 12345678910, 205 "int64": lHeight, 206 "float32": f32Height, 207 "float64": f64Height, 208 "map": map[string]string{ 209 "sub1": "value-sub1", 210 "sub2": "value-sub2", 211 }, 212 "stringArray": []string{"value1", "value2", "value3"}, 213 "intArray": []int{10, 20, 30}, 214 "int64Array": []int64{100, 200, 300}, 215 "float32Array": []float32{100.45, 200.56, 300.67}, 216 "float64Array": []float64{100.45, 200.56, 300.67}, 217 "uintArray": []uint64{1000, 2000, 3000}, 218 "valueArray": []interface{}{"value1", 20, 25.5}, 219 "timeArray": timeArray, 220 "boolArray": []bool{true, false, true}, 221 } 222 params = New(source) 223 Expect(params.Get("notFound").Value()).Should(BeNil()) 224 Expect(params.Get("string").String()).Should(Equal("Hellow Night!")) 225 226 rawMap := make(map[string]interface{}) 227 for key, value := range params.RawMap() { 228 if key == "timeArray" { 229 continue 230 } 231 rawMap[key] = value 232 } 233 Expect(snap.SnapshotMulti("RawMap()", rawMap)).ShouldNot(HaveOccurred()) 234 235 moreOfTheSame := New(params) 236 Expect(moreOfTheSame.Get("notFound").Value()).Should(BeNil()) 237 Expect(moreOfTheSame.Get("string").String()).Should(Equal("Hellow Night!")) 238 239 Expect(params.Get("stringArray").StringArray()).Should(Equal([]string{"value1", "value2", "value3"})) 240 Expect(New([]string{"value1", "value2", "value3"}).StringArray()).Should(Equal([]string{"value1", "value2", "value3"})) 241 Expect(New(map[string]string{"key1": "value1", "key2": "value2"}).RawMap()).Should(BeEquivalentTo(map[string]interface{}{"key1": "value1", "key2": "value2"})) 242 243 Expect(params.Get("intArray").IntArray()).Should(BeEquivalentTo([]int{10, 20, 30})) 244 Expect(New([]int{10, 20, 30}).IntArray()).Should(Equal([]int{10, 20, 30})) 245 Expect(New(map[string]int{"key1": 1, "key2": 2}).RawMap()).Should(BeEquivalentTo(map[string]interface{}{"key1": 1, "key2": 2})) 246 247 Expect(params.Get("boolArray").BoolArray()).Should(BeEquivalentTo([]bool{true, false, true})) 248 Expect(New([]int{10, 20, 30}).IntArray()).Should(Equal([]int{10, 20, 30})) 249 Expect(New([]int{10, 20, 30}).IsArray()).Should(Equal(true)) 250 251 Expect(params.Get("int64Array").Int64Array()).Should(BeEquivalentTo([]int64{100, 200, 300})) 252 Expect(New([]int64{100, 200, 300}).Int64Array()).Should(Equal([]int64{100, 200, 300})) 253 Expect(New(map[string]int64{"key1": 1, "key2": 2}).RawMap()).Should(BeEquivalentTo(map[string]interface{}{"key1": int64(1), "key2": int64(2)})) 254 255 Expect(params.Get("float64Array").FloatArray()).Should(BeEquivalentTo([]float64{100.45, 200.56, 300.67})) 256 Expect(New([]float64{100.45, 200.56, 300.67}).FloatArray()).Should(Equal([]float64{100.45, 200.56, 300.67})) 257 Expect(New(map[string]float64{"key1": 100.45, "key2": 200.56}).RawMap()).Should(BeEquivalentTo(map[string]interface{}{"key1": float64(100.45), "key2": float64(200.56)})) 258 259 Expect(params.Get("float32Array").Float32Array()).Should(BeEquivalentTo([]float32{100.45, 200.56, 300.67})) 260 Expect(New([]float32{100.45, 200.56, 300.67}).Float32Array()).Should(Equal([]float32{100.45, 200.56, 300.67})) 261 Expect(New(map[string]float32{"key1": 100.45, "key2": 200.56}).RawMap()).Should(BeEquivalentTo(map[string]interface{}{"key1": float32(100.45), "key2": float32(200.56)})) 262 263 Expect(params.Get("uintArray").UintArray()).Should(BeEquivalentTo([]uint64{1000, 2000, 3000})) 264 Expect(New([]uint64{1000, 2000, 3000}).UintArray()).Should(Equal([]uint64{1000, 2000, 3000})) 265 Expect(New(map[string]uint64{"key1": 1, "key2": 2}).RawMap()).Should(BeEquivalentTo(map[string]interface{}{"key1": uint64(1), "key2": uint64(2)})) 266 267 Expect(params.Get("valueArray").ValueArray()).Should(BeEquivalentTo([]interface{}{"value1", 20, 25.5})) 268 Expect(params.Get("timeArray").TimeArray()).Should(BeEquivalentTo(timeArray)) 269 now := time.Now() 270 Expect(New(map[string]time.Time{"key1": now, "key2": now}).RawMap()).Should(BeEquivalentTo(map[string]interface{}{"key1": now, "key2": now})) 271 272 Expect(params.Get("int").Int()).Should(Equal(12345678910)) 273 Expect(params.Get("int64").Int64()).Should(Equal(lHeight)) 274 Expect(params.Get("float32").Float32()).Should(Equal(f32Height)) 275 Expect(params.Get("float64").Float()).Should(Equal(f64Height)) 276 Expect(params.Get("map").Map()["sub1"].String()).Should(Equal("value-sub1")) 277 Expect(params.Get("map").Map()["sub2"].String()).Should(Equal("value-sub2")) 278 279 var items []string 280 params.Get("stringArray").ForEach(func(key interface{}, payload moleculer.Payload) bool { 281 items = append(items, payload.String()) 282 return true 283 }) 284 Expect(items).Should(Equal([]string{"value1", "value2", "value3"})) 285 286 items = make([]string, 0) 287 params.Get("stringArray").ForEach(func(key interface{}, payload moleculer.Payload) bool { 288 items = append(items, payload.String()) 289 return false 290 }) 291 Expect(items).Should(Equal([]string{"value1"})) 292 293 mapItems := make(map[string]string) 294 params.Get("map").ForEach(func(key interface{}, payload moleculer.Payload) bool { 295 mapItems[key.(string)] = payload.String() 296 return true 297 }) 298 Expect(mapItems).Should(Equal(map[string]string{ 299 "sub1": "value-sub1", 300 "sub2": "value-sub2", 301 })) 302 303 mapItems = make(map[string]string) 304 params.Get("map").ForEach(func(key interface{}, payload moleculer.Payload) bool { 305 mapItems[key.(string)] = payload.String() 306 return false 307 }) 308 Expect(len(mapItems)).Should(Equal(1)) 309 310 Expect(params.Error()).Should(BeNil()) 311 312 Expect(params.Get("string").StringArray()).Should(BeNil()) 313 Expect(params.Get("string").IntArray()).Should(BeNil()) 314 Expect(params.Get("string").Int64Array()).Should(BeNil()) 315 Expect(params.Get("string").FloatArray()).Should(BeNil()) 316 Expect(params.Get("string").Float32Array()).Should(BeNil()) 317 Expect(params.Get("string").ValueArray()).Should(BeNil()) 318 Expect(params.Get("string").UintArray()).Should(BeNil()) 319 Expect(params.Get("string").BoolArray()).Should(BeNil()) 320 321 Expect(params.Exists()).Should(Equal(true)) 322 Expect(New(nil).Exists()).Should(Equal(false)) 323 324 someErrror := errors.New("some error") 325 params = New(someErrror) 326 Expect(params.IsError()).Should(Equal(true)) 327 Expect(params.Error()).Should(Equal(someErrror)) 328 }) 329 330 It("Only should return a payload containg only the field specified", func() { 331 332 p := New(map[string]string{ 333 "name": "John", 334 "lastname": "Snow", 335 "faction": "Stark", 336 "Winter": "is coming!", 337 }) 338 339 Expect(snap.SnapshotMulti("Only()", p.Only("Winter"))).ShouldNot(HaveOccurred()) 340 }) 341 342 It("PayloadError should create an error with payload", func() { 343 p := PayloadError("Custom error message", New(map[string]string{ 344 "root_Cause": "root cause description", 345 "code": "12321321", 346 })) 347 Expect(snap.SnapshotMulti("PayloadError() .Error()", p.Error())).ShouldNot(HaveOccurred()) 348 Expect(snap.SnapshotMulti("PayloadError() .ErrorPayload()", p.ErrorPayload())).ShouldNot(HaveOccurred()) 349 }) 350 351 type M map[string]interface{} 352 It("should deal field paths name.subname...", func() { 353 p := New(M{ 354 "name": "John", 355 "lastname": "Snow", 356 "address": M{ 357 "street": "jonny ave", 358 "country": M{ 359 "code": "NZ", 360 "name": "New Zealand", 361 }, 362 "options": []M{ 363 M{ 364 "label": "item 1", 365 }, 366 M{ 367 "label": "item 2", 368 }, 369 }, 370 }, 371 }) 372 Expect(p.Get("name").String()).Should(Equal("John")) 373 Expect(p.Get("address.street").String()).Should(Equal("jonny ave")) 374 Expect(p.Get("address.country.code").String()).Should(Equal("NZ")) 375 Expect(p.Get("address.options[0].label").String()).Should(Equal("item 1")) 376 Expect(p.Get("address.options[1].label").String()).Should(Equal("item 2")) 377 }) 378 It("should deal field paths name.subname...", func() { 379 p := New(M{ 380 "address": M{ 381 "street": "jonny ave", 382 "options": []M{ 383 M{ 384 "label": "item 1", 385 }, 386 }, 387 }, 388 }) 389 Expect(p.Get("address.street").String()).Should(Equal("jonny ave")) 390 391 Expect(p.Get("wrong.path").Exists()).Should(BeFalse()) 392 Expect(p.Get("wrong.path").String()).Should(Equal("<nil>")) 393 394 Expect(p.Get("address.wrong").Exists()).Should(BeFalse()) 395 396 Expect(p.Get("address.options[10].label").Exists()).Should(BeFalse()) 397 }) 398 399 It("should deal field paths name.subname...", func() { 400 p := New(M{ 401 "address.street": "jonny ave", 402 }) 403 Expect(p.Get("address.street").String()).Should(Equal("jonny ave")) 404 }) 405 })