github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/graphite/pickle/pickle_writer_test.go (about) 1 // Copyright (c) 2019 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package pickle 22 23 import ( 24 "bytes" 25 "math" 26 "testing" 27 28 "github.com/hydrogen18/stalecucumber" 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 ) 32 33 func TestWriteEmptyDict(t *testing.T) { 34 var buf bytes.Buffer 35 w := NewWriter(&buf) 36 w.BeginDict() 37 w.EndDict() 38 require.NoError(t, w.Close()) 39 40 var m map[interface{}]interface{} 41 require.NoError(t, unpickle(buf.Bytes(), &m)) 42 assert.Equal(t, map[interface{}]interface{}{}, m) 43 } 44 45 func TestWriteEmptyList(t *testing.T) { 46 var buf bytes.Buffer 47 w := NewWriter(&buf) 48 w.BeginList() 49 w.EndList() 50 require.NoError(t, w.Close()) 51 52 var m []string 53 require.NoError(t, unpickle(buf.Bytes(), &m)) 54 assert.Equal(t, []string{}, m) 55 } 56 57 func TestWriteComplex(t *testing.T) { 58 var buf bytes.Buffer 59 w := NewWriter(&buf) 60 w.BeginDict() 61 w.WriteDictKey("step") 62 w.WriteInt(3494945) 63 w.WriteDictKey("pi") 64 w.WriteFloat64(3.45E10) 65 w.WriteDictKey("none") 66 w.WriteNone() 67 w.WriteDictKey("noNumber") 68 w.WriteFloat64(math.NaN()) 69 w.WriteDictKey("skey") 70 w.WriteString("hello world") 71 w.WriteDictKey("nested") 72 w.BeginDict() 73 w.WriteDictKey("fooBar") 74 w.BeginList() 75 w.WriteFloat64(349439.3494) 76 w.WriteInt(-9459450) 77 w.WriteString("A Nested String") 78 w.EndList() 79 w.EndDict() 80 w.EndDict() 81 require.NoError(t, w.Close()) 82 83 s := struct { 84 Step int 85 Pi float64 86 NoNumber *float64 87 Skey string 88 Nested struct { 89 FooBar []interface{} 90 } 91 }{} 92 93 require.NoError(t, unpickle(buf.Bytes(), &s)) 94 assert.Equal(t, 3494945, s.Step) 95 assert.Equal(t, 3.45E10, s.Pi) 96 assert.Nil(t, s.NoNumber) 97 assert.Equal(t, "hello world", s.Skey) 98 assert.Equal(t, []interface{}{ 99 349439.3494, int64(-9459450), "A Nested String", 100 }, s.Nested.FooBar) 101 } 102 103 func unpickle(b []byte, data interface{}) error { 104 r := bytes.NewReader(b) 105 return stalecucumber.UnpackInto(data).From(stalecucumber.Unpickle(r)) 106 }