github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/constructor_test.go (about)

     1  package processor_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Jeffail/benthos/v3/lib/log"
     7  	"github.com/Jeffail/benthos/v3/lib/metrics"
     8  	"github.com/Jeffail/benthos/v3/lib/processor"
     9  	yaml "gopkg.in/yaml.v3"
    10  
    11  	_ "github.com/Jeffail/benthos/v3/public/components/all"
    12  )
    13  
    14  func TestConstructorDescription(t *testing.T) {
    15  	if processor.Descriptions() == "" {
    16  		t.Error("package descriptions were empty")
    17  	}
    18  }
    19  
    20  func TestConstructorBadType(t *testing.T) {
    21  	conf := processor.NewConfig()
    22  	conf.Type = "not_exist"
    23  
    24  	if _, err := processor.New(conf, nil, log.Noop(), metrics.Noop()); err == nil {
    25  		t.Error("Expected error, received nil for invalid type")
    26  	}
    27  }
    28  
    29  func TestConstructorConfigYAMLInference(t *testing.T) {
    30  	conf := []processor.Config{}
    31  
    32  	if err := yaml.Unmarshal([]byte(`[
    33  		{
    34  			"text": {
    35  				"value": "foo"
    36  			},
    37  			"jmespath": {
    38  				"query": "foo"
    39  			}
    40  		}
    41  	]`), &conf); err == nil {
    42  		t.Error("Expected error from multi candidates")
    43  	}
    44  
    45  	if err := yaml.Unmarshal([]byte(`[
    46  		{
    47  			"text": {
    48  				"value": "foo"
    49  			}
    50  		}
    51  	]`), &conf); err != nil {
    52  		t.Error(err)
    53  	}
    54  
    55  	if exp, act := 1, len(conf); exp != act {
    56  		t.Errorf("Wrong number of config parts: %v != %v", act, exp)
    57  		return
    58  	}
    59  	if exp, act := processor.TypeText, conf[0].Type; exp != act {
    60  		t.Errorf("Wrong inferred type: %v != %v", act, exp)
    61  	}
    62  	if exp, act := "trim_space", conf[0].Text.Operator; exp != act {
    63  		t.Errorf("Wrong default operator: %v != %v", act, exp)
    64  	}
    65  	if exp, act := "foo", conf[0].Text.Value; exp != act {
    66  		t.Errorf("Wrong value: %v != %v", act, exp)
    67  	}
    68  }
    69  
    70  func TestConstructorConfigDefaultsYAML(t *testing.T) {
    71  	conf := []processor.Config{}
    72  
    73  	if err := yaml.Unmarshal([]byte(`[
    74  		{
    75  			"type": "bounds_check",
    76  			"bounds_check": {
    77  				"max_part_size": 50
    78  			}
    79  		}
    80  	]`), &conf); err != nil {
    81  		t.Error(err)
    82  	}
    83  
    84  	if exp, act := 1, len(conf); exp != act {
    85  		t.Errorf("Wrong number of config parts: %v != %v", act, exp)
    86  		return
    87  	}
    88  	if exp, act := 100, conf[0].BoundsCheck.MaxParts; exp != act {
    89  		t.Errorf("Wrong default parts: %v != %v", act, exp)
    90  	}
    91  	if exp, act := 50, conf[0].BoundsCheck.MaxPartSize; exp != act {
    92  		t.Errorf("Wrong overridden part size: %v != %v", act, exp)
    93  	}
    94  }