forked from fioepq9/helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviper_test.go
executable file
·72 lines (66 loc) · 1.91 KB
/
viper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package helper_test
import (
"github.com/google/uuid"
"github.com/onsi/gomega/gmeasure"
"os"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/fioepq9/helper"
)
var _ = Describe("viper", Label("viper"), func() {
type Config struct {
Foo string `yaml:"foo"`
File struct {
Hello string `yaml:"hello"`
Question []string `yaml:"question"`
} `yaml:"file"`
Timeout time.Duration `yaml:"timeout"`
People []string `yaml:"people"`
Date time.Time `yaml:"date"`
When time.Time `yaml:"when"`
File2 map[string]any `yaml:"file2"`
File3 []struct {
Name string `yaml:"name"`
Age int `yaml:"age"`
}
}
var c Config
for i := 0; i < 20000; i++ {
os.Setenv(uuid.New().String(), uuid.NewString())
}
It("unmarshal success", func() {
err := helper.Viper().Unmarshal(&c)
Expect(err).To(BeNil())
Expect(c.Foo).To(Equal("bar"))
Expect(c.File.Hello).To(Equal("world"))
Expect(c.File.Question).To(Equal([]string{"what", "is", "the", "answer"}))
Expect(c.Timeout).To(Equal(10 * time.Second))
Expect(c.People).To(Equal([]string{"alice", "bob", "carol"}))
Expect(c.Date.Unix()).To(Equal(int64(1546272000)))
Expect(c.When.Unix()).To(Equal(int64(1675371906)))
Expect(c.File2).To(Equal(map[string]any{
"hello": "world",
"question": []any{"what", "is", "the", "answer"},
}))
Expect(c.File3).To(Equal([]struct {
Name string `yaml:"name"`
Age int `yaml:"age"`
}{
{"alice", 18},
{"bob", 19},
{"carol", 20},
}))
})
It("bench", Serial, func() {
experiment := gmeasure.NewExperiment("viper - Benchmark")
AddReportEntry(experiment.Name, experiment)
experiment.Sample(func(idx int) {
experiment.MeasureDuration("env", func() {
_ = helper.Viper(func(viperHelper *helper.ViperHelper) {
viperHelper.ConfigFile = ""
}).Unmarshal(&c)
})
}, gmeasure.SamplingConfig{N: 200, Duration: time.Minute})
})
})