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

     1  package vs
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/spf13/afero"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestVariableSourceCsv_Init(t *testing.T) {
    12  	initFs := func(t *testing.T) afero.Fs {
    13  		fs := afero.NewMemMapFs()
    14  		file, err := fs.Create("users.csv")
    15  		require.NoError(t, err)
    16  		_, err = file.WriteString("USER_ID,NAME\n1,John\n2,Jack\n3,Jim\n")
    17  		require.NoError(t, err)
    18  		return fs
    19  	}
    20  	deferFs := func(t *testing.T, fs afero.Fs) {
    21  		err := fs.Remove("users.csv")
    22  		require.NoError(t, err)
    23  	}
    24  
    25  	tests := []struct {
    26  		name      string
    27  		initFs    func(t *testing.T) afero.Fs
    28  		deferFs   func(t *testing.T, fs afero.Fs)
    29  		vs        *VariableSourceCsv
    30  		wantErr   bool
    31  		wantStore []map[string]string
    32  	}{
    33  		{
    34  			name:    "default",
    35  			initFs:  initFs,
    36  			deferFs: deferFs,
    37  			vs: &VariableSourceCsv{
    38  				Name:            "users",
    39  				File:            "users.csv",
    40  				Fields:          []string{"user_id", "name"},
    41  				IgnoreFirstLine: false,
    42  				Delimiter:       ",",
    43  			},
    44  			wantErr:   false,
    45  			wantStore: []map[string]string{{"name": "NAME", "user_id": "USER_ID"}, {"name": "John", "user_id": "1"}, {"name": "Jack", "user_id": "2"}, {"name": "Jim", "user_id": "3"}},
    46  		},
    47  		{
    48  			name:    "replace spaces in field names",
    49  			initFs:  initFs,
    50  			deferFs: deferFs,
    51  			vs: &VariableSourceCsv{
    52  				Name:            "users",
    53  				File:            "users.csv",
    54  				Fields:          []string{"user id", "name name"},
    55  				IgnoreFirstLine: false,
    56  				Delimiter:       ",",
    57  			},
    58  			wantErr:   false,
    59  			wantStore: []map[string]string{{"name_name": "NAME", "user_id": "USER_ID"}, {"name_name": "John", "user_id": "1"}, {"name_name": "Jack", "user_id": "2"}, {"name_name": "Jim", "user_id": "3"}},
    60  		},
    61  		{
    62  			name:    "skip header",
    63  			initFs:  initFs,
    64  			deferFs: deferFs,
    65  			vs: &VariableSourceCsv{
    66  				Name:            "users",
    67  				File:            "users.csv",
    68  				Fields:          []string{"user_id", "name"},
    69  				IgnoreFirstLine: true,
    70  				Delimiter:       ",",
    71  			},
    72  			wantErr:   false,
    73  			wantStore: []map[string]string{{"name": "John", "user_id": "1"}, {"name": "Jack", "user_id": "2"}, {"name": "Jim", "user_id": "3"}},
    74  		},
    75  		{
    76  			name:    "empty fields and not skip header and not header as fields",
    77  			initFs:  initFs,
    78  			deferFs: deferFs,
    79  			vs: &VariableSourceCsv{
    80  				Name:            "users",
    81  				File:            "users.csv",
    82  				Fields:          nil,
    83  				IgnoreFirstLine: false,
    84  				Delimiter:       ",",
    85  			},
    86  			wantErr:   false,
    87  			wantStore: []map[string]string{{"NAME": "NAME", "USER_ID": "USER_ID"}, {"NAME": "John", "USER_ID": "1"}, {"NAME": "Jack", "USER_ID": "2"}, {"NAME": "Jim", "USER_ID": "3"}},
    88  		},
    89  		{
    90  			name: "replace spaces in field names in first line",
    91  			initFs: func(t *testing.T) afero.Fs {
    92  				fs := afero.NewMemMapFs()
    93  				file, err := fs.Create("users.csv")
    94  				require.NoError(t, err)
    95  				_, err = file.WriteString("USER ID,NAME NAME\n1,John\n2,Jack\n3,Jim\n")
    96  				require.NoError(t, err)
    97  				return fs
    98  			},
    99  			deferFs: deferFs,
   100  			vs: &VariableSourceCsv{
   101  				Name:            "users",
   102  				File:            "users.csv",
   103  				Fields:          nil,
   104  				IgnoreFirstLine: false,
   105  				Delimiter:       ",",
   106  			},
   107  			wantErr:   false,
   108  			wantStore: []map[string]string{{"NAME_NAME": "NAME NAME", "USER_ID": "USER ID"}, {"NAME_NAME": "John", "USER_ID": "1"}, {"NAME_NAME": "Jack", "USER_ID": "2"}, {"NAME_NAME": "Jim", "USER_ID": "3"}},
   109  		},
   110  		{
   111  			name:    "empty fields and skip header",
   112  			initFs:  initFs,
   113  			deferFs: deferFs,
   114  			vs: &VariableSourceCsv{
   115  				Name:            "users",
   116  				File:            "users.csv",
   117  				Fields:          nil,
   118  				IgnoreFirstLine: true,
   119  				Delimiter:       ",",
   120  			},
   121  			wantErr:   false,
   122  			wantStore: []map[string]string{{"NAME": "John", "USER_ID": "1"}, {"NAME": "Jack", "USER_ID": "2"}, {"NAME": "Jim", "USER_ID": "3"}},
   123  		},
   124  		{
   125  			name: "skipped header field",
   126  			initFs: func(t *testing.T) afero.Fs {
   127  				fs := afero.NewMemMapFs()
   128  				file, err := fs.Create("users.csv")
   129  				require.NoError(t, err)
   130  				_, err = file.WriteString(",NAME\n1,John\n2,Jack\n3,Jim\n")
   131  				require.NoError(t, err)
   132  				return fs
   133  			},
   134  			deferFs: deferFs,
   135  			vs: &VariableSourceCsv{
   136  				Name:            "users",
   137  				File:            "users.csv",
   138  				Fields:          nil,
   139  				IgnoreFirstLine: true,
   140  				Delimiter:       ",",
   141  			},
   142  			wantErr:   false,
   143  			wantStore: []map[string]string{{"NAME": "John", "0": "1"}, {"NAME": "Jack", "0": "2"}, {"NAME": "Jim", "0": "3"}},
   144  		},
   145  		{
   146  			name:    "skipped header field",
   147  			initFs:  initFs,
   148  			deferFs: deferFs,
   149  			vs: &VariableSourceCsv{
   150  				Name:            "users",
   151  				File:            "users.csv",
   152  				Fields:          []string{"", "name"},
   153  				IgnoreFirstLine: true,
   154  				Delimiter:       ",",
   155  			},
   156  			wantErr:   false,
   157  			wantStore: []map[string]string{{"name": "John", "0": "1"}, {"name": "Jack", "0": "2"}, {"name": "Jim", "0": "3"}},
   158  		},
   159  		{
   160  			name: "delimiter ;",
   161  			initFs: func(t *testing.T) afero.Fs {
   162  				fs := afero.NewMemMapFs()
   163  				file, err := fs.Create("users.csv")
   164  				require.NoError(t, err)
   165  				_, err = file.WriteString("USER_ID;NAME\n1;John\n2;Jack\n3;Jim\n")
   166  				require.NoError(t, err)
   167  				return fs
   168  			},
   169  			deferFs: deferFs,
   170  			vs: &VariableSourceCsv{
   171  				Name:            "users",
   172  				File:            "users.csv",
   173  				Fields:          []string{"", "name"},
   174  				IgnoreFirstLine: true,
   175  				Delimiter:       ";",
   176  			},
   177  			wantErr:   false,
   178  			wantStore: []map[string]string{{"name": "John", "0": "1"}, {"name": "Jack", "0": "2"}, {"name": "Jim", "0": "3"}},
   179  		},
   180  		{
   181  			name: "error when values more than fields",
   182  			initFs: func(t *testing.T) afero.Fs {
   183  				fs := afero.NewMemMapFs()
   184  				file, err := fs.Create("users2.csv")
   185  				require.NoError(t, err)
   186  				_, err = file.WriteString("USER_ID,NAME\n1,John\n2,Jack,skipthisvalue\n3\n")
   187  				require.NoError(t, err)
   188  				return fs
   189  			},
   190  			deferFs: func(t *testing.T, fs afero.Fs) {
   191  				err := fs.Remove("users2.csv")
   192  				require.NoError(t, err)
   193  			},
   194  			vs: &VariableSourceCsv{
   195  				Name:            "users",
   196  				File:            "users2.csv",
   197  				Fields:          nil,
   198  				IgnoreFirstLine: true,
   199  				Delimiter:       ",",
   200  			},
   201  			wantErr:   true,
   202  			wantStore: nil,
   203  		},
   204  		{
   205  			name: "error when values less than fields",
   206  			initFs: func(t *testing.T) afero.Fs {
   207  				fs := afero.NewMemMapFs()
   208  				file, err := fs.Create("users2.csv")
   209  				require.NoError(t, err)
   210  				_, err = file.WriteString("USER_ID,NAME\n1,John\n2,Jack\n3\n")
   211  				require.NoError(t, err)
   212  				return fs
   213  			},
   214  			deferFs: func(t *testing.T, fs afero.Fs) {
   215  				err := fs.Remove("users2.csv")
   216  				require.NoError(t, err)
   217  			},
   218  			vs: &VariableSourceCsv{
   219  				Name:            "users",
   220  				File:            "users2.csv",
   221  				Fields:          nil,
   222  				IgnoreFirstLine: true,
   223  				Delimiter:       ",",
   224  			},
   225  			wantErr:   true,
   226  			wantStore: nil,
   227  		},
   228  	}
   229  	for _, tt := range tests {
   230  		t.Run(tt.name, func(t *testing.T) {
   231  			tt.vs.fs = tt.initFs(t)
   232  			defer tt.deferFs(t, tt.vs.fs)
   233  
   234  			err := tt.vs.Init()
   235  			if tt.wantErr {
   236  				assert.Error(t, err)
   237  				return
   238  			}
   239  			assert.NoError(t, err)
   240  			assert.Equal(t, tt.wantStore, tt.vs.store)
   241  
   242  		})
   243  	}
   244  }