-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreaders_test.go
176 lines (161 loc) · 4.92 KB
/
readers_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package alligotor
import (
"bytes"
"reflect"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("files", func() {
Describe("unmarshal", func() {
expectedMap := map[string]interface{}{
"test": map[string]interface{}{"sub": "lel"},
}
Context("yaml", func() {
It("should succeed with valid input", func() {
yamlBytes := []byte(`---
test:
sub: lel
`)
yamlMap, err := unmarshal(bytes.NewReader(yamlBytes))
Expect(err).ShouldNot(HaveOccurred())
Expect(yamlMap.m).To(Equal(expectedMap))
})
})
Context("json", func() {
It("should succeed with valid input", func() {
jsonBytes := []byte(`{"test": {"sub": "lel"}}`)
jsonMap, err := unmarshal(bytes.NewReader(jsonBytes))
Expect(err).ShouldNot(HaveOccurred())
Expect(jsonMap.m).To(Equal(expectedMap))
})
})
Context("not supported", func() {
It("should fail with random input", func() {
randomBytes := []byte("i don't know what I'm doing here")
_, err := unmarshal(bytes.NewReader(randomBytes))
Expect(err).Should(HaveOccurred())
Expect(err).To(Equal(ErrFileFormatNotSupported))
})
})
})
Describe("readFileMap", func() {
var (
m *ciMap
field *Field
name = "someInt"
)
BeforeEach(func() {
m = newCiMap()
field = &Field{
name: name,
value: reflect.ValueOf(0),
}
})
It("returns nil if not set", func() {
val, err := readFileMap(field, m)
Expect(err).ToNot(HaveOccurred())
Expect(val).To(BeNil())
})
It("returns empty string if set to empty string", func() {
m.m = map[string]interface{}{name: ""}
val, err := readFileMap(field, m)
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal([]byte("")))
})
It("return []byte if type mismatch but value is string", func() {
m.m = map[string]interface{}{name: "1234"}
val, err := readFileMap(field, m)
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal([]byte("1234")))
})
It("returns error if type mismatch but value is not a string", func() {
m.m = map[string]interface{}{name: []string{"1234"}}
_, err := readFileMap(field, m)
Expect(err).To(HaveOccurred())
})
It("uses configured overwrite long name", func() {
field.configs = map[string]string{fileKey: "overwrite"}
m.m = map[string]interface{}{"overwrite": 3000}
val, err := readFileMap(field, m)
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal(3000))
})
It("supports arrays", func() {
field.value = reflect.ValueOf([]int{})
m.m = map[string]interface{}{name: []int{1, 2, 3, 4, 5}}
val, err := readFileMap(field, m)
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal([]int{1, 2, 3, 4, 5}))
})
Context("nested", func() {
var base = "base"
BeforeEach(func() {
field.base = []Field{{name: base}}
})
It("works", func() {
m.m = map[string]interface{}{base: map[string]interface{}{name: 1234}}
val, err := readFileMap(field, m)
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal(1234))
})
It("name can be overwritten, base remains", func() {
field.configs = map[string]string{fileKey: "default"}
m.m = map[string]interface{}{base: map[string]interface{}{"default": 1234}}
val, err := readFileMap(field, m)
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal(1234))
})
It("uses overwritten name even if normal one is set", func() {
field.configs = map[string]string{fileKey: "default"}
m.m = map[string]interface{}{base: map[string]interface{}{name: 1235, "default": 1234}}
val, err := readFileMap(field, m)
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal(1234))
})
It("supports overwriting base", func() {
field.base = []Field{{name: base, configs: map[string]string{fileKey: "overwrittenbase"}}}
m.m = map[string]interface{}{"overwrittenbase": map[string]interface{}{name: 1234}}
val, err := readFileMap(field, m)
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal(1234))
})
})
})
Describe("ReadersSource", func() {
var s *ReadersSource
Describe("Init", func() {
BeforeEach(func() {
jsonContent := []byte(`{"test":"1234"}`)
ymlContent := []byte(`test: "1235"`)
s = NewReadersSource(bytes.NewReader(jsonContent), bytes.NewReader(ymlContent))
})
It("initializes fileMaps", func() {
Expect(s.Init(nil)).To(Succeed())
Expect(s.fileMaps).To(Equal([]*ciMap{
{m: map[string]interface{}{"test": "1234"}},
{m: map[string]interface{}{"test": "1235"}},
}))
})
})
Describe("Read", func() {
var field *Field
BeforeEach(func() {
s = &ReadersSource{
fileMaps: []*ciMap{
{m: map[string]interface{}{"test": "1234"}},
{m: map[string]interface{}{"test": "1235"}},
},
}
field = &Field{
name: "test",
value: reflect.ValueOf(""),
}
})
It("fileMaps override each other", func() {
val, err := s.Read(field)
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal("1235"))
})
})
})
})