forked from TelefonicaTC2Tech/golium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_test.go
261 lines (234 loc) · 6.27 KB
/
table_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package golium
import (
"context"
"errors"
"testing"
"github.com/cucumber/godog"
"github.com/stretchr/testify/require"
)
var (
withoutHeaders = [][]string{{"John", "182"}}
withoutHeadersValues = [][]string{{"John", "182"}, {"Doe", "190"}}
withName = [][]string{{"name"}, {"John"}}
withNameAndHeight = [][]string{{"name", "height"}, {"John", "182"}}
withNameHeightAndAge = [][]string{{"name", "height", "age"}, {"John", "182", "32"}}
withParamAndValue = [][]string{{"param", "value"}, {"Name", "182"}, {"Height", "162"}}
withNameAndValue = [][]string{{"Name", "Value"}, {"example 1", "1"}, {"example 2", "10"}}
)
type Headers struct {
Name string
Height string
}
func TestRemoveHeaders(t *testing.T) {
tcs := []struct {
name string
table *godog.Table
expectedErr error
}{
{
name: "Remove header expected error",
table: NewTable(withoutHeaders),
expectedErr: errors.New(
"cannot remove header: table must have at least one header and one useful row"),
},
{
name: "Remove header table ok",
table: NewTable(withNameAndHeight),
expectedErr: nil,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
// Call the tested function
err := RemoveHeaders(tc.table)
// Check expected behavior
if err != nil && err.Error() != tc.expectedErr.Error() {
t.Errorf("Expected error: %s, Error received: %s", tc.expectedErr, err)
}
if tc.expectedErr == nil && err != nil {
t.Errorf("Expected error: %s, Error received: %s", tc.expectedErr, err)
}
})
}
}
func TestColumnsChecker(t *testing.T) {
tcs := []struct {
name string
table *godog.Table
n int
expectedErr error
}{
{
name: "Check columns less than n",
table: NewTable(withName),
n: 2,
expectedErr: errors.New(
"table must have 2 columns"),
},
{
name: "Check columns more than n",
table: NewTable(withNameHeightAndAge),
n: 2,
expectedErr: errors.New(
"table must have 2 columns"),
},
{
name: "Check columns ok",
table: NewTable(withNameAndHeight),
n: 2,
expectedErr: nil,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
// Call the tested function
err := ColumnsChecker(tc.table.Rows[0].Cells, tc.n)
// Check expected behavior
if err != nil && err.Error() != tc.expectedErr.Error() {
t.Errorf("Expected error: %s, Error received: %s", tc.expectedErr, err)
}
if tc.expectedErr == nil && err != nil {
t.Errorf("Expected error: %s, Error received: %s", tc.expectedErr, err)
}
})
}
}
func TestConvertTableToMap(t *testing.T) {
expectedResult := make(map[string]interface{})
expectedResult["John"] = "182"
tcs := []struct {
name string
table *godog.Table
expectedResult interface{}
expectedErr error
}{
{
name: "Convert table to map ok",
table: NewTable(withNameAndHeight),
expectedResult: expectedResult,
expectedErr: nil,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
// Call the tested function
convertedTable, err := ConvertTableToMap(ctx, tc.table)
require.Equal(t, tc.expectedErr, err)
require.Equal(t, tc.expectedResult, convertedTable)
})
}
}
func TestConvertTableColumnToArray(t *testing.T) {
expectedResult := []string{}
expectedResult = append(expectedResult, "John")
tcs := []struct {
name string
table *godog.Table
expectedResult interface{}
expectedErr error
}{
{
name: "Convert table column to array ok",
table: NewTable(withName),
expectedResult: expectedResult,
expectedErr: nil,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
// Call the tested function
convertedTable, err := ConvertTableColumnToArray(ctx, tc.table)
require.Equal(t, tc.expectedErr, err)
require.Equal(t, tc.expectedResult, convertedTable)
})
}
}
func TestConvertTableToMultiMap(t *testing.T) {
expectedResult := make(map[string][]string)
expectedResult["John"] = []string{"182"}
tcs := []struct {
name string
table *godog.Table
expectedResult interface{}
expectedErr error
}{
{
name: "Convert table to multi map ok",
table: NewTable(withNameAndHeight),
expectedResult: expectedResult,
expectedErr: nil,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
// Call the tested function
convertedTable, err := ConvertTableToMultiMap(ctx, tc.table)
require.Equal(t, tc.expectedErr, err)
require.Equal(t, tc.expectedResult, convertedTable)
})
}
}
func TestConvertTableWithoutHeaderToStruct(t *testing.T) {
tcs := []struct {
name string
table *godog.Table
expectedErr error
}{
{
name: "Convert table wo header to struct ok",
table: NewTable(withParamAndValue),
expectedErr: nil,
},
{
name: "Convert table wo header to struct fail",
table: NewTable(withNameAndHeight),
expectedErr: errors.New("failed setting element 'John' in struct of type " +
"'golium.Headers': field 'John' is not valid"),
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
var props Headers
// Call the tested function
err := ConvertTableWithoutHeaderToStruct(ctx, tc.table, &props)
require.Equal(t, tc.expectedErr, err)
})
}
}
func TestConvertTableWithHeaderToStructSlice(t *testing.T) {
type TestElement struct {
Name string
Value int
}
testSlice := []TestElement{}
tcs := []struct {
name string
table *godog.Table
wantErr bool
}{
{
name: "Convert table with header to struct ok",
table: NewTable(withNameAndValue),
wantErr: false,
},
{
name: "Convert table without header to struct fail",
table: NewTable(withoutHeadersValues),
wantErr: true,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
if err := ConvertTableWithHeaderToStructSlice(
ctx, tc.table, &testSlice); (err != nil) != tc.wantErr {
t.Errorf(
"ConvertTableWithHeaderToStructSlice() error = %v, wantErr %v", err, tc.wantErr)
}
})
}
}