github.com/Jeffail/benthos/v3@v3.65.0/lib/bloblang/package_test.go (about) 1 package bloblang 2 3 import ( 4 "testing" 5 6 "github.com/Jeffail/benthos/v3/lib/message" 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestMappings(t *testing.T) { 12 tests := map[string]struct { 13 mapping string 14 input string 15 output string 16 }{ 17 "basic query": { 18 mapping: `root = this.foo 19 let bar = $baz | this.bar.baz`, 20 input: `{"foo":"bar"}`, 21 output: "bar", 22 }, 23 "complex query": { 24 mapping: `root = match this.foo { 25 this.bar == "bruh" => this.baz.buz, 26 _ => $foo 27 }`, 28 input: `{"foo":{"bar":"bruh","baz":{"buz":"the result"}}}`, 29 output: "the result", 30 }, 31 "long assignment": { 32 mapping: `root.foo.bar = "this" 33 root.foo = "that" 34 root.baz.buz.0.bev = "then this"`, 35 output: `{"baz":{"buz":{"0":{"bev":"then this"}}},"foo":"that"}`, 36 }, 37 } 38 39 for name, test := range tests { 40 test := test 41 t.Run(name, func(t *testing.T) { 42 m, err := NewMapping(test.mapping) 43 require.NoError(t, err) 44 45 res, err := m.MapPart(0, message.New([][]byte{[]byte(test.input)})) 46 require.NoError(t, err) 47 assert.Equal(t, test.output, string(res.Get())) 48 }) 49 } 50 } 51 52 func TestFields(t *testing.T) { 53 tests := map[string]struct { 54 field string 55 input string 56 output string 57 }{ 58 "basic query": { 59 field: `foo ${! json("foo") }`, 60 input: `{"foo":"bar"}`, 61 output: "foo bar", 62 }, 63 } 64 65 for name, test := range tests { 66 test := test 67 t.Run(name, func(t *testing.T) { 68 f, err := NewField(test.field) 69 require.NoError(t, err) 70 71 res := f.String(0, message.New([][]byte{[]byte(test.input)})) 72 assert.Equal(t, test.output, res) 73 }) 74 } 75 }