gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:41:18 24 4
gpt4 key购买 nike

我想测试 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)

最佳答案

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

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

关于Golang - 解析 yaml 文件和检查对象的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49181976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com