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

     1  package test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	_import "github.com/yandex/pandora/components/providers/scenario/import"
     8  	"github.com/yandex/pandora/components/providers/scenario/vs"
     9  	"github.com/yandex/pandora/core/config"
    10  	"github.com/yandex/pandora/core/plugin/pluginconfig"
    11  	"gopkg.in/yaml.v2"
    12  )
    13  
    14  func Test_decode_parseVariableSourceCsv(t *testing.T) {
    15  	const exampleVariableSourceYAML = `
    16  src:
    17   type: "file/csv"
    18   name: "users_src"
    19   file: "_files/users.csv"
    20   ignore_first_line: true
    21   delimiter: ";"
    22   fields: [ "user_id", "name" ]
    23  `
    24  
    25  	_import.Import(nil)
    26  	testOnce.Do(func() {
    27  		pluginconfig.AddHooks()
    28  	})
    29  
    30  	data := make(map[string]any)
    31  	err := yaml.Unmarshal([]byte(exampleVariableSourceYAML), &data)
    32  	require.NoError(t, err)
    33  
    34  	out := struct {
    35  		Src vs.VariableSource `yaml:"src"`
    36  	}{}
    37  
    38  	err = config.DecodeAndValidate(data, &out)
    39  	require.NoError(t, err)
    40  
    41  	csvVS, ok := out.Src.(*vs.VariableSourceCsv)
    42  	require.True(t, ok)
    43  	require.True(t, csvVS.IgnoreFirstLine)
    44  	require.Equal(t, "users_src", csvVS.GetName())
    45  	require.Equal(t, "_files/users.csv", csvVS.File)
    46  	require.Equal(t, []string{"user_id", "name"}, csvVS.Fields)
    47  }
    48  
    49  func Test_decode_parseVariableSourceJson(t *testing.T) {
    50  	const exampleVariableSourceJSON = `
    51  src:
    52   type: "file/json"
    53   name: "json_src"
    54   file: "_files/users.json"
    55  `
    56  
    57  	_import.Import(nil)
    58  	testOnce.Do(func() {
    59  		pluginconfig.AddHooks()
    60  	})
    61  
    62  	data := make(map[string]any)
    63  	err := yaml.Unmarshal([]byte(exampleVariableSourceJSON), &data)
    64  	require.NoError(t, err)
    65  
    66  	out := struct {
    67  		Src vs.VariableSource `yaml:"src"`
    68  	}{}
    69  
    70  	err = config.DecodeAndValidate(data, &out)
    71  	require.NoError(t, err)
    72  
    73  	jsonVS, ok := out.Src.(*vs.VariableSourceJSON)
    74  	require.True(t, ok)
    75  	require.Equal(t, "json_src", jsonVS.GetName())
    76  }