github.com/Jeffail/benthos/v3@v3.65.0/lib/metrics/path_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/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestPathMapping(t *testing.T) {
    13  	type test struct {
    14  		input       string
    15  		output      string
    16  		allowLabels bool
    17  		labels      []string
    18  		values      []string
    19  	}
    20  	tests := map[string][]test{
    21  		`if this.contains("foo") { deleted() }`: {
    22  			{
    23  				input:  "foo",
    24  				output: "",
    25  			},
    26  			{
    27  				input:  "foo",
    28  				output: "",
    29  			},
    30  			{
    31  				input:  "hello foo world",
    32  				output: "",
    33  			},
    34  			{
    35  				input:  "hello world",
    36  				output: "hello world",
    37  			},
    38  		},
    39  		`root = this.foo.bar.not_null()`: {
    40  			{input: "foo", output: "foo"},
    41  		},
    42  		`root = this
    43  		 meta foo = "bar"`: {
    44  			{input: "foo", output: "foo"},
    45  			{
    46  				input: "foo", output: "foo",
    47  				allowLabels: true,
    48  				labels:      []string{"foo"},
    49  				values:      []string{"bar"},
    50  			},
    51  		},
    52  		`root = this
    53  		 meta foo = "bar"
    54  		 meta bar = "baz"`: {
    55  			{input: "foo", output: "foo"},
    56  			{
    57  				input: "foo", output: "foo",
    58  				allowLabels: true,
    59  				labels:      []string{"bar", "foo"},
    60  				values:      []string{"baz", "bar"},
    61  			},
    62  		},
    63  		`this.replace("foo","bar")`: {
    64  			{input: "foo", output: "bar"},
    65  			{input: "hello foo world", output: "hello bar world"},
    66  			{input: "hello world", output: "hello world"},
    67  		},
    68  		`10`: {
    69  			{input: "foo", output: "foo"},
    70  			{input: "hello foo world", output: "hello foo world"},
    71  			{input: "hello world", output: "hello world"},
    72  		},
    73  		``: {
    74  			{input: "foo", output: "foo"},
    75  			{input: "hello foo world", output: "hello foo world"},
    76  			{input: "hello world", output: "hello world"},
    77  		},
    78  	}
    79  
    80  	for mapping, defs := range tests {
    81  		mapping := mapping
    82  		defs := defs
    83  		t.Run(mapping, func(t *testing.T) {
    84  			m, err := newPathMapping(mapping, log.Noop())
    85  			require.NoError(t, err)
    86  			for i, def := range defs {
    87  				out, labels, values := m.mapPath(def.input, def.allowLabels)
    88  				assert.Equal(t, def.output, out, strconv.Itoa(i))
    89  				assert.Equal(t, def.labels, labels, strconv.Itoa(i))
    90  				assert.Equal(t, def.values, values, strconv.Itoa(i))
    91  			}
    92  		})
    93  	}
    94  }