github.com/eris-ltd/erisdb@v0.25.0/event/query/reflect_tagged_test.go (about) 1 package query 2 3 import ( 4 "testing" 5 6 "github.com/hyperledger/burrow/binary" 7 "github.com/hyperledger/burrow/crypto" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 type testTaggable struct { 13 Foo string 14 Bar string 15 Baz binary.HexBytes 16 Address crypto.Address 17 Indices []int 18 unexported int 19 } 20 21 func TestReflectTagged_Keys(t *testing.T) { 22 rt, err := ReflectTags(&testTaggable{}) 23 require.NoError(t, err) 24 assert.Equal(t, []string{"Foo", "Bar", "Baz", "Address", "Indices"}, rt.Keys()) 25 } 26 27 func TestReflectTagged_Get(t *testing.T) { 28 tt := testTaggable{ 29 Foo: "Thumbs", 30 Bar: "Numbed", 31 Baz: []byte{255, 255, 255}, 32 Address: crypto.Address{1, 2, 3}, 33 Indices: []int{5, 7, 9}, 34 } 35 rt, err := ReflectTags(&tt) 36 require.NoError(t, err) 37 38 value, ok := rt.Get("Foo") 39 assert.True(t, ok) 40 assert.Equal(t, tt.Foo, value) 41 42 value, ok = rt.Get("Bar") 43 assert.True(t, ok) 44 assert.Equal(t, tt.Bar, value) 45 46 value, ok = rt.Get("Baz") 47 assert.True(t, ok) 48 assert.Equal(t, "FFFFFF", value) 49 50 value, ok = rt.Get("Indices") 51 assert.True(t, ok) 52 assert.Equal(t, "5;7;9", value) 53 54 value, ok = rt.Get("Address") 55 assert.True(t, ok) 56 assert.Equal(t, "0102030000000000000000000000000000000000", value) 57 58 // Make sure we see updates through pointer 59 tt.Foo = "Plums" 60 value, ok = rt.Get("Foo") 61 assert.True(t, ok) 62 assert.Equal(t, tt.Foo, value) 63 } 64 65 func TestReflectTagged_Len(t *testing.T) { 66 rt, err := ReflectTags(&testTaggable{}) 67 require.NoError(t, err) 68 assert.Equal(t, 5, rt.Len()) 69 } 70 71 func TestExplicitFields(t *testing.T) { 72 tt := testTaggable{ 73 Foo: "Thumbs", 74 Bar: "Numbed", 75 Baz: []byte{255, 255, 255}, 76 Address: crypto.Address{1, 2, 3}, 77 } 78 rt, err := ReflectTags(&tt, "Foo", "Address") 79 require.NoError(t, err) 80 81 value, ok := rt.Get("Foo") 82 assert.True(t, ok) 83 assert.Equal(t, tt.Foo, value) 84 85 value, ok = rt.Get("Address") 86 assert.True(t, ok) 87 assert.Equal(t, "0102030000000000000000000000000000000000", value) 88 89 _, ok = rt.Get("Bar") 90 assert.False(t, ok) 91 92 _, ok = rt.Get("Barsss") 93 assert.False(t, ok) 94 95 _, err = ReflectTags(&tt, "Foo", "Address", "Balloons") 96 require.Error(t, err) 97 } 98 99 func TestReflectTagged_nil(t *testing.T) { 100 type testStruct struct { 101 Foo string 102 } 103 104 var ts *testStruct 105 106 rf, err := ReflectTags(ts) 107 require.NoError(t, err) 108 value, ok := rf.Get("Foo") 109 assert.False(t, ok) 110 assert.Equal(t, "", value) 111 }