Golang - 解析yaml文件和检查对象的单元测试

问题描述 投票:4回答:1

我想测试yaml的解析并通过单元测试来测试它我创建了具有适当类型的结构但是断言总是很糟糕的,我尝试使用以下代码,它经常失败

这是有效的yaml内容(可能是它更改的副本但我能够正确解析它)

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:

这是我创建的测试

    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,
            },
        }

这是断言代码

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)
            }

错误是:

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

我应该如何克服它来检查模块属性?

ParseFile方法只是err := yaml.Unmarshal([]byte(yamlFile), &yamlconent)

go yaml
1个回答
5
投票

我不完全确定问题是什么,但我设法让你的测试像这样工作:

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
}

请注意,我导入了https://github.com/stretchr/testify以使测试更容易一些。当然你可以用原来的reflect.DeepEquals支票替换它。 Testify在这里很有用,因为它会在不满足期望的情况下打印出有用的差异。

© www.soinside.com 2019 - 2024. All rights reserved.