github.com/yandex/pandora@v0.5.32/core/provider/json_test.go (about) 1 package provider 2 3 import ( 4 "context" 5 "io" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 "github.com/yandex/pandora/core" 12 "github.com/yandex/pandora/core/datasource" 13 "github.com/yandex/pandora/lib/testutil" 14 ) 15 16 type testJSONAmmo struct { 17 ID string 18 Data string 19 } 20 21 // TODO(skipor): test this in decode provider, not json 22 func TestDecodeProviderPasses(t *testing.T) { 23 input := strings.NewReader(` {"data":"first"} `) 24 conf := DefaultJSONProviderConfig() 25 conf.Decode.Source = datasource.NewReader(input) 26 conf.Decode.Passes = 3 27 newAmmo := func() core.Ammo { 28 return &testJSONAmmo{} 29 } 30 provider := NewJSONProvider(newAmmo, conf) 31 err := provider.Run(context.Background(), testDeps()) 32 require.NoError(t, err) 33 34 expected := func(data string) *testJSONAmmo { 35 return &testJSONAmmo{Data: data} 36 } 37 ammo, ok := provider.Acquire() 38 require.True(t, ok) 39 assert.Equal(t, expected("first"), ammo) 40 41 ammo, ok = provider.Acquire() 42 require.True(t, ok) 43 assert.Equal(t, expected("first"), ammo) 44 45 ammo, ok = provider.Acquire() 46 require.True(t, ok) 47 assert.Equal(t, expected("first"), ammo) 48 49 _, ok = provider.Acquire() 50 assert.False(t, ok) 51 } 52 53 func TestCustomJSONProvider(t *testing.T) { 54 input := strings.NewReader(` {"data":"first"}`) 55 conf := DefaultJSONProviderConfig() 56 conf.Decode.Source = datasource.NewReader(input) 57 conf.Decode.Limit = 1 58 newAmmo := func() core.Ammo { 59 return &testJSONAmmo{} 60 } 61 wrapDecoder := func(_ core.ProviderDeps, decoder AmmoDecoder) AmmoDecoder { 62 return AmmoDecoderFunc(func(ammo core.Ammo) error { 63 err := decoder.Decode(ammo) 64 if err != nil { 65 return err 66 } 67 ammo.(*testJSONAmmo).Data += " transformed" 68 return nil 69 }) 70 } 71 provider := NewCustomJSONProvider(wrapDecoder, newAmmo, conf) 72 err := provider.Run(context.Background(), testDeps()) 73 require.NoError(t, err) 74 expected := func(data string) *testJSONAmmo { 75 return &testJSONAmmo{Data: data} 76 } 77 ammo, ok := provider.Acquire() 78 require.True(t, ok) 79 assert.Equal(t, expected("first transformed"), ammo) 80 81 _, ok = provider.Acquire() 82 assert.False(t, ok) 83 } 84 85 func TestJSONProvider(t *testing.T) { 86 input := strings.NewReader(` {"data":"first"} 87 {"data":"second"} `) 88 conf := DefaultJSONProviderConfig() 89 conf.Decode.Source = datasource.NewReader(input) 90 conf.Decode.Limit = 3 91 newAmmo := func() core.Ammo { 92 return &testJSONAmmo{} 93 } 94 provider := NewJSONProvider(newAmmo, conf) 95 err := provider.Run(context.Background(), testDeps()) 96 require.NoError(t, err) 97 98 expected := func(data string) *testJSONAmmo { 99 return &testJSONAmmo{Data: data} 100 } 101 ammo, ok := provider.Acquire() 102 require.True(t, ok) 103 assert.Equal(t, expected("first"), ammo) 104 105 ammo, ok = provider.Acquire() 106 require.True(t, ok) 107 assert.Equal(t, expected("second"), ammo) 108 109 ammo, ok = provider.Acquire() 110 require.True(t, ok) 111 assert.Equal(t, expected("first"), ammo) 112 113 _, ok = provider.Acquire() 114 assert.False(t, ok) 115 } 116 117 func TestDecoderSimple(t *testing.T) { 118 var val struct { 119 Data string 120 } 121 input := strings.NewReader(`{"data":"first"}`) 122 decoder := NewJSONAmmoDecoder(input, 512) 123 err := decoder.Decode(&val) 124 require.NoError(t, err) 125 assert.Equal(t, "first", val.Data) 126 127 err = decoder.Decode(&val) 128 require.Equal(t, io.EOF, err) 129 } 130 131 func TestDecoderWhitespaces(t *testing.T) { 132 var val struct { 133 Data string 134 } 135 input := strings.NewReader(` {"data":"first"} 136 {"data":"second"} {"data":"third"} `) 137 decoder := NewJSONAmmoDecoder(input, 512) 138 err := decoder.Decode(&val) 139 require.NoError(t, err) 140 assert.Equal(t, "first", val.Data) 141 142 err = decoder.Decode(&val) 143 require.NoError(t, err) 144 assert.Equal(t, "second", val.Data) 145 146 err = decoder.Decode(&val) 147 require.NoError(t, err) 148 assert.Equal(t, "third", val.Data) 149 150 err = decoder.Decode(&val) 151 require.Equal(t, io.EOF, err) 152 } 153 154 func TestDecoderReset(t *testing.T) { 155 val := testJSONAmmo{ 156 ID: "id", 157 } 158 input := strings.NewReader(`{"data":"first"}`) 159 decoder := NewJSONAmmoDecoder(input, 512) 160 err := decoder.Decode(&val) 161 require.NoError(t, err) 162 assert.Equal(t, "first", val.Data) 163 assert.Zero(t, val.ID) 164 } 165 166 func testDeps() core.ProviderDeps { 167 return core.ProviderDeps{Log: testutil.NewLogger()} 168 }