github.com/Jeffail/benthos/v3@v3.65.0/internal/component/metrics/mapping_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  
     7  	"github.com/Jeffail/benthos/v3/lib/log"
     8  	"github.com/Jeffail/benthos/v3/lib/types"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestPathMapping(t *testing.T) {
    14  	type testCase struct {
    15  		input    string
    16  		inLabels []string
    17  		inValues []string
    18  		output   string
    19  		labels   []string
    20  		values   []string
    21  	}
    22  	type test struct {
    23  		name    string
    24  		mapping string
    25  		cases   []testCase
    26  	}
    27  	tests := []test{
    28  		{
    29  			name:    "delete some",
    30  			mapping: `if this.contains("foo") { deleted() }`,
    31  			cases: []testCase{
    32  				{
    33  					input:  "foo",
    34  					output: "",
    35  				},
    36  				{
    37  					input:  "foo",
    38  					output: "",
    39  				},
    40  				{
    41  					input:  "hello foo world",
    42  					output: "",
    43  				},
    44  				{
    45  					input:  "hello world",
    46  					output: "hello world",
    47  				},
    48  				{
    49  					input:    "hello world",
    50  					inLabels: []string{"foo", "bar"},
    51  					inValues: []string{"foo1", "bar1"},
    52  					output:   "hello world",
    53  					labels:   []string{"bar", "foo"},
    54  					values:   []string{"bar1", "foo1"},
    55  				},
    56  			},
    57  		},
    58  		{
    59  			name:    "throw an error",
    60  			mapping: `root = throw("nope")`,
    61  			cases:   []testCase{{input: "foo", output: "foo"}},
    62  		},
    63  		{
    64  			name: "set a static label",
    65  			mapping: `root = this
    66  			 meta foo = "bar"`,
    67  			cases: []testCase{
    68  				{
    69  					input: "foo", output: "foo",
    70  					labels: []string{"foo"},
    71  					values: []string{"bar"},
    72  				},
    73  				{
    74  					input: "foo", output: "foo",
    75  					inLabels: []string{"a", "b"},
    76  					inValues: []string{"a1", "b1"},
    77  					labels:   []string{"a", "b", "foo"},
    78  					values:   []string{"a1", "b1", "bar"},
    79  				},
    80  			},
    81  		},
    82  		{
    83  			name: "set two static labels",
    84  			mapping: `root = this
    85  			 meta foo = "bar"
    86  			 meta bar = "baz"`,
    87  			cases: []testCase{
    88  				{
    89  					input: "foo", output: "foo",
    90  					labels: []string{"bar", "foo"},
    91  					values: []string{"baz", "bar"},
    92  				},
    93  			},
    94  		},
    95  		{
    96  			name:    "replace foo with bar",
    97  			mapping: `this.replace("foo","bar")`,
    98  			cases: []testCase{
    99  				{input: "foo", output: "bar"},
   100  				{input: "hello foo world", output: "hello bar world"},
   101  				{input: "hello world", output: "hello world"},
   102  			},
   103  		},
   104  		{
   105  			name:    "empty mapping",
   106  			mapping: ``,
   107  			cases: []testCase{
   108  				{input: "foo", output: "foo"},
   109  				{input: "hello world", output: "hello world"},
   110  			},
   111  		},
   112  		{
   113  			name:    "wrong value mapping",
   114  			mapping: `root = 10`,
   115  			cases: []testCase{
   116  				{input: "foo", output: "foo"},
   117  				{input: "hello world", output: "hello world"},
   118  			},
   119  		},
   120  	}
   121  
   122  	for _, test := range tests {
   123  		t.Run(test.name, func(t *testing.T) {
   124  			m, err := NewMapping(types.NoopMgr(), test.mapping, log.Noop())
   125  			require.NoError(t, err)
   126  			for i, def := range test.cases {
   127  				out, labels, values := m.mapPath(def.input, def.inLabels, def.inValues)
   128  				assert.Equal(t, def.output, out, strconv.Itoa(i))
   129  				assert.Equal(t, def.labels, labels, strconv.Itoa(i))
   130  				assert.Equal(t, def.values, values, strconv.Itoa(i))
   131  			}
   132  		})
   133  	}
   134  }