Golang - Unit test for parsing yaml file and checking objects

I want to check the yaml syntax and test it with unit test. I create structures that are with the appropriate types, but the statement is always falid, I try to execute the following code, which failed constantly

This is the content of the barley, which is valid (possibly with a copy that it changed, but I was able to parse it correctly)

ID: demo version: 0.0.5 dep: - name: db path: mtb requires: - name: vi_db - name: srv path: srv1 properties: LOG_LEVEL: "info" parameters: mem: 12G requires: - name: db properties: 

This is the test I created.

  func Test_parseFile(t *testing.T) { yamlfile, err := ioutil.ReadFile("./testdata/file.yaml") type Properties map[string]string type Parameters map[string]interface{} type Modules struct { Name string Path string `yaml:"path,omitempty"` Requires []Requires `yaml:"requires,omitempty"` Properties Properties `yaml:"properties,omitempty"` } type Requires struct { Name string `yaml:"name,omitempty"` Properties Properties `yaml:"properties,omitempty"` } type args struct { contentFile []byte } tests := []struct {        name string        args args        wantOut Properties        wantNoTests bool        wantErr bool    }{        {            name: "test",            args: args{                contentFile: yamlfile,            },            wantOut: Modules{                Name: "srv",                Path: "srv1",                Properties{                    "LOG_LEVEL": "info",                    "DEBUG_LOG_LEVEL": "ALL",                }, Parameters:{ "mem":"12G", },                Requires: {                    name: "db",                    Properties{                        "CONFIG": '[tomcontext.xml:                        {"service_nameDB" : "~{con-name}"}]'                    },                },            },            wantNoTests: true,            wantErr: true,        },    } 

This is a verification code.

 for _, tt := range tests {        t.Run(tt.name, func(t *testing.T) {            gotOut := ParseFile(tt.args.contentFile)            if !reflect.DeepEqual(gotOut.Modules[1], tt.wantOut) {                t.Errorf("parseFile() = %v, want %v", gotOut.Modules[2], tt.wantOut)            } 

Error:

 parseFile() = map[], want map[LOG_LEVEL:info DEBUG_LOG_LEVEL:ALL] 

How can I overcome it to check the properties of the module?

The ParseFile method is just err := yaml.Unmarshal([]byte(yamlFile), &yamlconent)

+3
source share
1 answer

I'm not quite sure about this, but I managed to get your test to work as follows:

 package sandbox import ( "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gopkg.in/yaml.v2" ) type Properties map[string]string type Parameters map[string]interface{} type Requires struct { Name string `yaml:"name,omitempty"` Properties Properties `yaml:"properties,omitempty"` } type Module struct { Name string Path string `yaml:"path,omitempty"` Requires []Requires `yaml:"requires,omitempty"` Properties Properties `yaml:"properties,omitempty"` Parameters Parameters `yaml:"parameters,omitempty"` } type File struct { Modules []Module } func Test_ParseFile(t *testing.T) { input := []byte(`ID: demo version: 0.0.5 modules: - name: db path: mtb requires: - name: vi_db - name: srv path: srv1 properties: LOG_LEVEL: "info" DEBUG_LOG_LEVEL : ALL parameters: mem: 12G requires: - name: db properties: CONFIG: '[tomcontext.xml: {"service_nameDB" : "~{con-name}"}]'`) tests := []struct { name string wantOut Module }{ { name: "test", wantOut: Module{ Name: "srv", Path: "srv1", Properties: Properties{ "LOG_LEVEL": "info", "DEBUG_LOG_LEVEL": "ALL", }, Parameters: Parameters{ "mem": "12G", }, Requires: []Requires{ { Name: "db", Properties: Properties{ "CONFIG": `[tomcontext.xml: {"service_nameDB" : "~{con-name}"}]`, }, }, }, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { actual, err := ParseFile(input) require.NoError(t, err) require.NotNil(t, actual) require.Len(t, actual.Modules, 2) assert.Equal(t, tt.wantOut, actual.Modules[1]) }) } } func ParseFile(yamlFile []byte) (File, error) { var f File err := yaml.Unmarshal(yamlFile, &f) return f, err } 

Please note that I imported https://github.com/stretchr/testify to make the tests a little easier. Of course, you can replace this with your original reflect.DeepEquals check. Testify is useful here because it prints a useful diff in case the wait fails.

+3
source

Source: https://habr.com/ru/post/1275794/


All Articles