github.com/yandex/pandora@v0.5.32/components/providers/scenario/vs/vs_json_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 TestVariableSourceJson_Init(t *testing.T) { 12 initFs := func(t *testing.T) afero.Fs { 13 fs := afero.NewMemMapFs() 14 file, err := fs.Create("users.json") 15 require.NoError(t, err) 16 _, err = file.WriteString(`{"error": "timeout", "timeout": "3s", "isResult": true, "number": 1}`) 17 require.NoError(t, err) 18 return fs 19 } 20 deferFs := func(t *testing.T, fs afero.Fs) { 21 err := fs.Remove("users.json") 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 *VariableSourceJSON 30 wantErr bool 31 wantStore any 32 }{ 33 { 34 name: "default", 35 initFs: initFs, 36 deferFs: deferFs, 37 vs: &VariableSourceJSON{ 38 Name: "users", 39 File: "users.json", 40 }, 41 wantErr: false, 42 wantStore: map[string]any{"error": "timeout", "timeout": "3s", "isResult": true, "number": float64(1)}, 43 }, 44 { 45 name: "slice", 46 initFs: func(t *testing.T) afero.Fs { 47 fs := afero.NewMemMapFs() 48 file, err := fs.Create("users.json") 49 require.NoError(t, err) 50 _, err = file.WriteString(`[{"error": "timeout", "timeout": "3s", "isResult": true, "number": 1}]`) 51 require.NoError(t, err) 52 return fs 53 }, 54 deferFs: func(t *testing.T, fs afero.Fs) { 55 err := fs.Remove("users.json") 56 require.NoError(t, err) 57 }, 58 vs: &VariableSourceJSON{ 59 Name: "users", 60 File: "users.json", 61 }, 62 wantErr: false, 63 wantStore: []any{map[string]any{"error": "timeout", "timeout": "3s", "isResult": true, "number": float64(1)}}, 64 }, 65 } 66 for _, tt := range tests { 67 t.Run(tt.name, func(t *testing.T) { 68 tt.vs.fs = tt.initFs(t) 69 defer tt.deferFs(t, tt.vs.fs) 70 71 err := tt.vs.Init() 72 if tt.wantErr { 73 assert.Error(t, err) 74 return 75 } 76 assert.NoError(t, err) 77 assert.Equal(t, tt.wantStore, tt.vs.store) 78 79 }) 80 } 81 }