加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 09:25:00 所属栏目:大数据 来源:网络整理
导读:我想测试yaml的解析并通过单元测试进行测试 我创建了具有适当类型的结构,但断言总是如此 falid,我尝试使用以下代码,这些代码经常失败 这是有效的yaml内容(可能是它更改的副本但我能够正确解析它) ID: demoversion: 0.0.5dep: - name: db path: mtb requires:
我想测试yaml的解析并通过单元测试进行测试
我创建了具有适当类型的结构,但断言总是如此
falid,我尝试使用以下代码,这些代码经常失败

这是有效的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方法只是错误:= yaml.Unmarshal([] byte(yamlFile),& yamlconent)

解决方法

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

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"`
    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在这里很有用,因为它会在不满足期望的情况下打印出有用的差异.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读