github.com/yandex/pandora@v0.5.32/components/providers/scenario/config/decode_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/yandex/pandora/components/providers/scenario/vs"
     8  )
     9  
    10  type mockVS struct {
    11  	name     string
    12  	vars     map[string]string
    13  	initErr  error
    14  	initCall int
    15  }
    16  
    17  func (m *mockVS) GetName() string {
    18  	return m.name
    19  }
    20  
    21  func (m *mockVS) GetVariables() any {
    22  	return m.vars
    23  }
    24  
    25  func (m *mockVS) Init() error {
    26  	m.initCall--
    27  	return m.initErr
    28  }
    29  
    30  func TestExtractVariableStorage(t *testing.T) {
    31  	tests := []struct {
    32  		name    string
    33  		cfg     *AmmoConfig
    34  		want    map[string]any
    35  		wantErr assert.ErrorAssertionFunc
    36  	}{
    37  		{
    38  			name: "default",
    39  			cfg: &AmmoConfig{
    40  				VariableSources: []vs.VariableSource{
    41  					&mockVS{initCall: 1, name: "users", vars: map[string]string{"user_id": "1"}},
    42  					&mockVS{initCall: 1, name: "filter_src", vars: map[string]string{"filter": "filter"}},
    43  				},
    44  			},
    45  			want: map[string]any{
    46  				"users":      map[string]string{"user_id": "1"},
    47  				"filter_src": map[string]string{"filter": "filter"},
    48  			},
    49  			wantErr: assert.NoError,
    50  		},
    51  		{
    52  			name: "init error",
    53  			cfg: &AmmoConfig{
    54  				VariableSources: []vs.VariableSource{
    55  					&mockVS{initCall: 1, name: "users", vars: map[string]string{"user_id": "1"}},
    56  					&mockVS{initCall: 1, name: "filter_src", vars: map[string]string{"filter": "filter"}, initErr: assert.AnError},
    57  				},
    58  			},
    59  			wantErr: assert.Error,
    60  			want:    map[string]any{"users": map[string]string{"user_id": "1"}},
    61  		},
    62  	}
    63  	for _, tt := range tests {
    64  		t.Run(tt.name, func(t *testing.T) {
    65  			got, err := ExtractVariableStorage(tt.cfg)
    66  			if !tt.wantErr(t, err) {
    67  				return
    68  			}
    69  
    70  			vars := got.Variables()
    71  			assert.Equal(t, tt.want, vars)
    72  			for _, source := range tt.cfg.VariableSources {
    73  				assert.Equal(t, 0, source.(*mockVS).initCall)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func Test_SpreadNames(t *testing.T) {
    80  	tests := []struct {
    81  		name      string
    82  		input     []ScenarioConfig
    83  		want      map[string]int
    84  		wantTotal int
    85  	}{
    86  		{
    87  			name:      "",
    88  			input:     []ScenarioConfig{{Name: "a", Weight: 20}, {Name: "b", Weight: 30}, {Name: "c", Weight: 60}},
    89  			want:      map[string]int{"a": 2, "b": 3, "c": 6},
    90  			wantTotal: 11,
    91  		},
    92  		{
    93  			name:      "",
    94  			input:     []ScenarioConfig{{Name: "a", Weight: 100}, {Name: "b", Weight: 100}, {Name: "c", Weight: 100}},
    95  			want:      map[string]int{"a": 1, "b": 1, "c": 1},
    96  			wantTotal: 3,
    97  		},
    98  		{
    99  			name:      "",
   100  			input:     []ScenarioConfig{{Name: "a", Weight: 100}},
   101  			want:      map[string]int{"a": 1},
   102  			wantTotal: 1,
   103  		},
   104  		{
   105  			name:      "",
   106  			input:     []ScenarioConfig{{Name: "a", Weight: 0}},
   107  			want:      map[string]int{"a": 1},
   108  			wantTotal: 1,
   109  		},
   110  		{
   111  			name:      "",
   112  			input:     []ScenarioConfig{{Name: "a", Weight: 0}, {Name: "b", Weight: 1}},
   113  			want:      map[string]int{"a": 1, "b": 1},
   114  			wantTotal: 2,
   115  		},
   116  	}
   117  	for _, tt := range tests {
   118  		t.Run(tt.name, func(t *testing.T) {
   119  			got, total := SpreadNames(tt.input)
   120  			assert.Equalf(t, tt.want, got, "spreadNames(%v)", tt.input)
   121  			assert.Equalf(t, tt.wantTotal, total, "spreadNames(%v)", tt.input)
   122  		})
   123  	}
   124  }
   125  
   126  func Test_ParseShootName(t *testing.T) {
   127  	testCases := []struct {
   128  		input     string
   129  		wantName  string
   130  		wantCnt   int
   131  		wantSleep int
   132  		wantErr   bool
   133  	}{
   134  		{"shoot", "shoot", 1, 0, false},
   135  		{"shoot(5)", "shoot", 5, 0, false},
   136  		{"shoot(3,4,5)", "shoot", 3, 4, false},
   137  		{"shoot(5,6)", "shoot", 5, 6, false},
   138  		{"space test(7)", "space test", 7, 0, false},
   139  		{"symbol#(3)", "symbol#", 3, 0, false},
   140  		{"shoot(  9  )", "shoot", 9, 0, false},
   141  		{"shoot (6)", "shoot", 6, 0, false},
   142  		{"shoot()", "shoot", 1, 0, false},
   143  		{"shoot(abc)", "", 0, 0, true},
   144  		{"shoot(6", "", 0, 0, true},
   145  		{"shoot(6),", "", 0, 0, true},
   146  	}
   147  
   148  	for _, tc := range testCases {
   149  		t.Run(tc.input, func(t *testing.T) {
   150  			name, cnt, sleep, err := ParseShootName(tc.input)
   151  			if tc.wantErr {
   152  				assert.Error(t, err)
   153  				return
   154  			}
   155  			assert.NoError(t, err)
   156  			assert.Equal(t, tc.wantName, name, "Name does not match for input: %s", tc.input)
   157  			assert.Equal(t, tc.wantSleep, sleep, "Name does not match for input: %s", tc.input)
   158  			assert.Equal(t, tc.wantCnt, cnt, "Count does not match for input: %s", tc.input)
   159  		})
   160  	}
   161  }